Surely you know what you have to do now. Like all the other main objects we have created so far, each rocket will have its own class, which is located in its own file.
rocketClass.monkey
. Strict
mode, importing gameClasses
, and a global rockets
list definition.Strict Import gameClasses Global rockets := New List<rocket>
Just like with cities and launchers, we will have some wrapper functions. Let's start with the function to update all rockets.
UpdateRockets
function. There we loop through the rockets
list and call the Update
method of each rocket we find.Function UpdateRockets:Int() For Local rocket := Eachin rockets rocket.Update() Next Return True End
The same goes for drawing the rockets on the canvas.
RenderRockets
. Again, it loops through all rockets and calls the Render
method.Function RenderRockets:Int() For Local rocket := Eachin rockets rocket.Render() Next Return True End
To create a rocket, we will have the CreateRocket
function.
CreateRocket
function will be the mouse position and the starting x
position.Function CreateRocket:Int(mx:Int, my:Int, xp:Int) Local r:rocket = New rocket 'Create a new rocket r.Init(mx, my, xp) 'Initialize the rocket rockets.AddLast(r) 'Add the rocket to the rockets list Return True End
And, to remove all existing rockets, we have the RemoveRockets function.
RemoveRockets
. In it, we call the Clear
method of the rockets list.Function RemoveRockets:Int() rockets.Clear() Return True End
The last function we need is one that calculates the distance between two coordinates. We need this to determine speed factors and also for our collision checks later on.
GetDistance
. The parameters are two pairs of X
and Y
position variables.Function GetDistance:Float( x1:Float, y1:Float, x2:Float, y2:Float ) Local diffx:Float, diffy:Float 'to store the difference diffx = x1 - x2 'Calculate the X difference diffy = y1 - y2 'Calculate the Y difference 'Return the square root of the sum of divX^2 plus divY^2 Return Sqrt((diffx * diffx) + (diffy * diffy)) End
That's all for the wrapper functions. Save the file now. Next stop, the rocket class!
rocket
.Class rocket
Now, we will create some data fields to help with animating the rocket.
Field sx:Float = 0.0 'x start pos Field sy:Float = 0.0 'y start pos
Field tx:Float = 0.0 'x target pos Field ty:Float = 0.0 'y target pos
Field cx:Float = 0.0 'x current pos Field cy:Float = 0.0 'y current pos
Field dx:Float = 0.0 'x difference Field dy:Float = 0.0 'y difference Field speed:Float = 0.0 'speed factor
Ok, we have created the data fields for each rocket. Let's implement some methods.
Init
method. The parameters are the target position and the starting x
position.Method Init:Int(rx:Float, ry:Float, xp:Int)
x
is given by the method parameter and y
is calculated.sx = xp sy = game.cHeight 60.0
tx = rx ty = ry
cx = sx cy = sy
x
and y
starting position and target, we calculate these by a simple subtraction formula.dx = tx - sx dy = ty - sy
4
. Then, close the method.speed = GetDistance(sx, sy, tx, ty)/4.0 Return True End
In each frame, we will need to calculate the new position of a rocket. For this, we need an Update
method.
Update
.Method Update:Int()
x
and y
by the speed factor.cx += dx/speed cy += dy/speed
Y
position is less than the target Y
position.If cy < ty Then
rockets
list. After that, close the IF check, and the method.rockets.Remove(Self) Endif Return True End
Just like cities and launchers, we want to render a rocket.
Render
method.Method Render:Int()
DrawLine
statement.SetColor(70, 70, 70) DrawLine(sx, sy, cx, cy)
Local cd:Int = Rnd(0, 50) 'Determine a random color factor SetColor(255-cd, 255-cd, 255-cd) DrawCircle(cx, cy, 3)
Return True End End
That was quick, right? We have a class for our rockets now. We can create them easily and update them through a simple call of a function. Once they reach their target, they destroy themselves.
18.118.198.81