Last time we nailed down the X-Coordinate of our Platform Nodes, and this time we’ll dig into the much more difficult task of picking a Y-Coordinate. In our last post, we just left the Coordinate as a value of 4, which will make a single unbroken platform. (Pretty boring right?)
But now it’s time to add a little challenge!
The primary aspect of platform games is the ability of the player to jump from one platform to another, essentially making an obstacles course. Sometimes there can be monsters, or treasures, or traps, but at their heart, platform games really come down to that simple premise.
So to procedurally generate this feeling, our first step is to start programmatically carrying the height of our platforms as they scroll across our screen.
To do this, we’re going to take some time to do a bit of design effort that will save us some time down the road.
What we’re going to do is add a new class to our project that encapsulates our height calculation algorithm. This design principle is called the Strategy Pattern, and is the best pattern to encapsulate complicated algorithm that might change over time. For now, we’ll be keeping our algorithm simple, but we may want to change it over time, and this design pattern will help make that easier.
So we’ll make a new class in our project and call it EEPlatformGenerator.
Now that we have our strategy class, we can get to work.
But, there’s no sense getting ourselves wound around the axel immediately, so we’ll implement the simplest possible algorithm that we can. The simplest way to vary the height of our platforms is to just make it random. To do this, we’ll spin a quick helper function to generate a random number between two values, a maximum and minimum value:
Now all we have to decide what those maximum and minimum values are going to be. So we’re going add some data to our new class:
What this data does is:
- currentBlockHeight – This datum will track the Block Height of the last platform.
- jumpHeight – This datum is the height our player can achieve when he jumps.
- dropHeight – This datum is the height form which a player can fall without taking damage.
- platformCielingHeight – This datum will provide the maximum height of a platform. We’ll use this to prevent our platforms from climbing to ridiculous heights, we wouldn’t want a platform with a height of 145,057.
Now we’ll use our new data to build a method to determine the height of our next platform: