Time for action — creating the data structure

As always, we will place our data storage in the game class. You could also create these fields as global variables, but it is a better practice to encapsulate them inside a class, if you are mainly using an object-oriented coding style.

  1. Let's add the field for our layers first. We don't need one for the background, as this is the default layer anyway.
    Class game Extends App
    Field eng:engine
    Field isSuspended:Bool = False
    Field layerGame:ftLayer
    Field layerTitle:ftLayer
    
  2. Next, we will add an object that will store the info text about how many crates are left to be placed on targets.
    Field txtCrates:ftObject
    
  3. To store the bitmap font, we need another field.
    Field font1:ftFont
    
  4. The sprite sheet (atlas) will be stored inside its own field.
    Field atlas:Image
    
  5. Now, we add two fields to store the number of total crates and how many are on a target.
    Field onTarget:Int = 0
    Field crateNum:Int=0
    
  6. During collision checks, we need to check if we are close to a wall and store that information. As we have two points in front of the machine to check, we have two Boolean fields.
    Field hitWall:Bool=False
    Field hitWall2:Bool=False
    
  7. The same goes with crates. We need to store two crate objects.
    Field hitCrate:ftObject=Null
    Field hitCrate2:ftObject=Null
    
  8. The player machine has to be stored inside an object too, of course.
    Field player:ftObject
    
  9. Now, add a field for the level number and one for the game mode.
    Field levelNum:Int = 0
    Field gameMode:Int = gmMenu
    
  10. During the loading of a map, we need to store the initial player position. For this, we need a FLOAT array with two entries.
    Field plStPos:Float[] = [0.0, 0.0]
    

    These were the fields to store dynamic data. Now we need some constants.

  11. First, add constants for the game modes: Menu, Play, and NextLevel.
    Const gmMenu:Int = 1
    Const gmPlay:Int = 2
    Const gmNextLevel:Int = 3
    
  12. We need a few constants for the collision groups as well as the object tags. The two player constants are needed because we will have two collision zones in front of the machine.
    Const grpPlayer2:Int = 6
    Const grpPlayer:Int = 5
    Const grpCrate:Int = 2
    Const grpWall:Int = 1
    Const grpFloor:Int = 3
    Const grpTarget:Int = 4
    
  13. The last constant we will add is one for a transition ID. We need this when we want to switch to a new level.
    Const tidNextLevel:Int=1
    Method OnCreate:Int()
    

    That's all!

What just happened?

We have added various fields for the game objects and also constants to control the game itself. Constants make reading your source code much easier.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.182.221