We will create this class in one batch, including data section, update, and constructor methods:
Enemy
. It won't be extended from anything.Class Enemy
Field enemyObj:ftObject = Null
ftVec2D:
Field pathStack:= New Stack<ftVec2D>
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)
enemyObj
field that we have added before:enemyObj = g.eng.CreateImage(g.atlas,0,0,64,64, x , y)
32
, and then assign it to the game layer:enemyObj.SetRadius(32) enemyObj.SetLayer(g.layerGame)
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
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.
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
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:
GetObj
, which returns the ftObject:
Method GetObj:ftObject() Return enemyObj End
Update
method: Update
method into the Enemy
class. It will return a value from the type of Bool:
Method Update:Bool()
If pathStack.Length() >= 1 Then
If enemyObj.GetTransitionCount()=0 Then
vec
variable from the type of ftVec2D:
Local vec:ftVec2D = pathStack.Pop()
Local time:Float = 7500.0 / (1000.0/enemyObj.GetVectorDist(vec.x, vec.y))
enemyObj.CreateTransPos(vec.x, vec.y, time , False, 0) Endif Else
FALSE
, then:If enemyObj.GetTransitionCount()=0 Then Return False Endif
TRUE
for everything else. It means that this enemy is still alive. Then, close this method and the class:Return True End End