Time for action — creating the enemy class

We will create this class in one batch, including data section, update, and constructor methods:

  1. Create a new class called Enemy. It won't be extended from anything.
    Class Enemy
    
  2. As we need a reference to the actual fantomEngine object, we will need a field to store it inside the class:
    Field enemyObj:ftObject = Null
    
  3. Now, add a stack for the path. The path is made of single 2D vectors, which are from a new object type of the fantomEngine called ftVec2D:
    Field pathStack:= New Stack<ftVec2D>
    
  4. Next, add a New constructor method, with the initial enemy position and the number of path nodes to be created as parameters:
    Method New(x:Float, y:Float, pathCount:Int)
    
  5. Create an image object and assign it to the previous enemyObj field that we have added before:
    enemyObj = g.eng.CreateImage(g.atlas,0,0,64,64, x , y)
    
  6. Our enemies are bigger, so set the radius to 32, and then assign it to the game layer:
    enemyObj.SetRadius(32)
    enemyObj.SetLayer(g.layerGame)
    
  7. For collision detection, set its collision group to the game's grpEnemy constant:

    enemyObj. SetColGroup(g.grpEnemy) Add a local FOR loop with a variable i ranging from 1 to the pathCount parameter:

    For Local i:Int = 1 To pathCount
    
  8. Check whether i is equal to 1. If it is, add a new path node at the top of the screen and a random x position:
    If i = 1 Then
    pathStack.Push(New ftVec2D(Rnd(g.cw), 0.0))
    

    This should make sure that the enemy moves back before it gets deleted after he has traveled along its path. We will read items from the stack, from the top. So, the first item will be the last one we will use.

  9. Now, if i is not equal 0, add a random position into the path stack. By subtracting 100 from the canvas height, we will give the player a little safety zone on the play field whenever an enemy appears:
    Else
    pathStack.Push(New ftVec2D(Rnd(g.cw), Rnd(g.ch-100)))
    Endif
    
  10. Now, close the FOR loop and the method:
    Next
    End
    

    Later in the game, we need to remove enemies and their underlying fantom objects. Let's add a method to the Enemy class to do this:

  11. Create a new method called GetObj, which returns the ftObject:
    Method GetObj:ftObject()
    Return enemyObj
    End
    
  12. The last method we will add to the enemy class is the Update method:
  13. Insert the Update method into the Enemy class. It will return a value from the type of Bool:
    Method Update:Bool()
    
  14. Check whether there are items left on the path stack:
    If pathStack.Length() >= 1 Then
    
  15. Now, check whether the object has any transitions attached to it. If not, it would mean it just started its life or finished a previous transition:
    If enemyObj.GetTransitionCount()=0 Then
    
  16. Retrieve a position vector form the path stack and store it inside the local vec variable from the type of ftVec2D:
    Local vec:ftVec2D = pathStack.Pop()
    
  17. Determine a local time factor, which takes the distance from the enemy to the destination path node into its calculation. This makes sure that shorter distances are traveled in a shorter period of time:
    Local time:Float = 7500.0 / (1000.0/enemyObj.GetVectorDist(vec.x, vec.y))
    
  18. Now, create a new position transition with the path node's position values, the time we have calculated before. We don't set the transition ID, as we only need to check whether all nodes were traveled on; we don't need to check on a single transition:
    enemyObj.CreateTransPos(vec.x, vec.y, time , False, 0)
    Endif
    Else
    
  19. If no path stack entries are left and no transitions are assigned anymore, this means the enemy has reached its final destination. Return FALSE, then:
    If enemyObj.GetTransitionCount()=0 Then Return False
    Endif
    
  20. Return TRUE for everything else. It means that this enemy is still alive. Then, close this method and the class:
    Return True
    End
    End
    

What just happened?

We have created a new class for our enemies. This class will create one at will, calculate its path, and also update it when one node of the path is reached.

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

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