
Lesson 7 Source Files
Resource Files
Prior to this lesson everything was just randomly placed. Now we’ve added a “levels” directory with numbered sub-directories for each level. In those directories are the files which define the graphics we’ll be using for walls and objects. We also define the level itself. A maximum of 256 graphics can be used for objects and 256 graphics can be used for wall, floors and ceilings. Each level can define different graphics to use so we’re not too restricted.
- tile.txt - this file defines our wall, floor and ceiling textures as well as what character is used in the map file to reference them. The format is texture number, character code, texture, mask. The character codes must be a single character. The mask is optional.
- obj.txt - this file defines our object textures, floor and ceiling textures. You only need to load the textures for the objects you’ll be using in the level but the texture number must always be the same. Those are hard coded in the object’s class file. The format is texture number, texture, mask. The mask is optional but objects should always have a mask.
- obj.map.txt - this file defines our object character codes. There is a hash map in the object handler that associates an object type number with an object type string. This file associates a character code with the object type string. Currently we have “bunny,” “player,” and “tree.” When the map is loaded the player object is returned to the wolf5k class which needs to know what object is the player. There should only be one player character code per map. If there are multiple player codes in the map the last one will be used as the real player object. All others will do nothing. The format for this file is character code, object type string.
- map.*.txt - We have four files which define the map; wall, floor, ceil and obj. The wall, floor and ceil files all use the tile.txt codes. The obj file uses the obj.map.txt codes. The wall file defines the size of the map. The dimensions of the map are determined by the size of the last row and the size of the total map string. Line returns are ignored. “.”’s indicate that the default texture or object is used which is usually no texture or object at all. Only the floor is set to have a default texture which is grass. You files should all have the same dimensions and every line should be the same length. If not, things won’t render correctly.
I’ve also fixed the hopping code. It’s much simpler now. Also I determined that the distance calculation was off by a multiple of 64 when using it to render sprites. 64 pixels is now 64 pixels at 64 pixels from the player and 32 pixels when 128 pixels from the player like it should be. The tree has been replaced by a bouncing beach ball.
One of the problems that shows up when doing time based movement is that things don’t always collide like they should because objects move too fast. I’ve improved the collision detection to track the movement from the previous location to the new location using time slices equal to 1/50th of the time difference from the previous frame. The collision detection is now essentially running at 50 times the frame rate of the game itself. Nothing is moving particularly fast in this game so it’s accurate enough.
This isn’t perfect. The real way to do it is to first figure out if the two object collide using simple alegbra and the line formula. Then you calculate the time it takes for both objects to get to that point (distance to the point divided by the render time) and if they both get there at the same time, there’s a collision. That only checks for them being at the exact same point however. It’s a little more complex when you take into account a collision radius around the objects. We’ll stick to the simple way for now. Maybe in lesson 8 I’ll improve it. The current way does hurt the frame rate noticably.
We’re also only checking the x and y position. Bunnies can’t hop over your beach balls at this time.
The main thing in the next lesson will be to finally render title screens and status bars.