As you will see yourself, quite a few things look similar to the city
class definition and its wrapper functions. However, the launchers are different objects, and so we need different methods and functions for them. Not every step of code is explained now, as some is the same as for the city
class.
launcherClass.monkey
. Strict
mode and import the gameClasses.monkey
file.Strict Import gameClasses
launcher
class.Global launchers := New List<launcher>
Launchers will have to be updated for each frame, so we will have a new wrapper function to call the corresponding method for all existing launchers.
UpdateLaunchers
. In it, we will loop through the launcher's list and call their Update
method.Function UpdateLaunchers:Int() 'Loop through the list of launchers For Local launcher := Eachin launchers 'Call the Update method of each launcher launcher.Update() Next Return True End
The same goes here with drawing the launchers to the canvas.
RenderLaunchers
. In it, we will loop through the launcher's list to call their Render
method.Function RenderLaunchers:Int() For Local launcher := Eachin launchers launcher.Render() Next Return True End
GetTotalRocketcount
. In this function, we will add up all the rockets from each launcher found.Function GetTotalRocketcount:Int() Local rc:Int = 0 'Var to store the total amount of rockets 'Loop through the list of launchers For Local launcher := Eachin launchers 'Add the rocket count to the global var rc rc += launcher.cRockets Next Return rc 'Return back the number of rockets found. End
To add rockets to to each launcher, we will build another function that does just that.
AddLauncherRockets
. As a parameter, it will take the amount, which then will be added to a launcher inside the loop.Function AddLauncherRockets:Int(ramount:Int) For Local launcher := Eachin launchers launcher.cRockets += ramount 'Add rockets to the launcher Next Return True End
Now, we will add a function to actually create a rocket launcher.
CreateLauncher
. As parameters, it will have the x
position of the launcher, the keyboard key that triggers a shot, and the number of rockets it has initially.Function CreateLauncher:Int(xpos:Float, triggerkey:Int, rcount:Int) Local l:launcher = New launcher 'Create a new launcher l.Init(xpos, triggerkey, rcount) 'Initialize it launchers.AddLast(l) 'Add the launcher to the launchers list Return True End
The last wrapper function we will add is to remove all existing launchers.
RemoveLaunchers
. In it, we will call the Clear
method of the launchers list.Function RemoveLaunchers:Int() launchers.Clear() Return True End
Phew! Again, quite a few wrapper functions! But now it is time for the actual launcher
class.
launcher
.Class launcher
Field x:Float = 0.0 'x pos of a launcher Field y:Float = 0.0 'y pos of a launcher Field cRockets:Int = 0 'Rocket count Field triggerKey:Int = 0 'Trigger key to shoot
To initialize a launcher, the class has an Init
method.
Init
, parameters for the x
position, the trigger key, and the count of rockets.Method Init:Int(xpos:Float, trigkey:Int, rcount:Int) x = xpos 'Set the x position y = game.cHeight - 40.0 'Calculate the y position cRockets = rcount 'Set the rocket count triggerKey = trigkey 'Set the trigger key Return True End
Updating a launcher means checking if the trigger key was pressed and then launching a rocket at the target.
Update
method.Method Update:Int()
If KeyHit(triggerKey) And cRockets > 0 Then
CreateRocket
will be featured in the next class that we add. Still, add it as a comment.'CreateRocket(MouseX(), MouseY(), Self.x )
cRockets -= 1 Endif Return True End
Again, as with the city
class, we have a render
method here too. After all, we need to render the launchers, right?
Render
method.Method Render:Int()
SetColor(0, 255, 255) DrawCircle(x, y - 15, 5) DrawRect(x-2, y - 15, 4, 15)
SetColor(255, 255, 255)
xdiff
variable that will be an offset, to draw the rocket markers.Local xdiff:Int = cRockets*3 / 2
If cRockets > 0 Then
For Local i:Int = 1 To cRockets DrawLine(x + i*3 - xdiff, y+20, x + i*3 - xdiff, y + 30) Next Endif
Return True End End
We're done! The launcher class is ready to be implemented.
Now we have the device to fire our rockets. Sadly there aren't any rockets to fire, but we will get there. But what have we done? We built a class for a launcher with its own Init, Update
, and Render
methods. Also, we created some wrapper functions that will help us to act on several launchers at once. The class is ready to be implemented; the only thing we have to change later on is a small line in the Update
method, so that it becomes active:
CreateRocket(MouseX(), MouseY(), Self.x )