To create the bomb
class, we will follow the ensuing steps:
bombClass.monkey
. Strict
mode, import gameClasses
, and create a global list that holds all instances of the bomb
class.Strict Import gameClasses Global bombs := New List<bomb>
For one last time, we will add a few wrapper functions, which we then can add to the mainClass.monkey
file, later on.
Function UpdateBombs:Int() For Local bomb := Eachin bombs bomb.Update() Next Return True End Function RenderBombs:Int() For Local bomb := Eachin bombs bomb.Render() Next Return True End Function CreateBomb:Int() Local b:bomb = New bomb b.Init() bombs.AddLast(b) Return True End Function RemoveBombs:Int() bombs.Clear() Return True End Function GetBombsCount:Int() Return bombs.Count() End
Now, define the actual bomb
class.
bomb
.Class bomb
Add the data fields for a bomb.
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
x
and y
, and the speed factor.Field dx:Float = 0.0 'x difference Field dy:Float = 0.0 'y difference Field speed:Float = 0.0 'speed factor
Now, let's add the usual methods to the class.
Init
.Method Init:Int()
x
value is determined randomly.sx = Rnd(5.0, (game.cWidth - 5.0)) sy = 0.0
X
position of the target. Depending on whether there are no rockets left, the bomb will automatically target the first city that is remaining.If GetTotalRocketcount() > 0 Then 'Get random target x position tx = Rnd(5.0, (game.cWidth - 5.0)) Else 'Get target x position of first city Local c:city = game.cities.First() tx = c.x Endif ty = game.cHeight - 40.0
cx = sx cy = sy
x
and y
positions.dx = tx - sx dy = ty - sy
speed = Rnd(500, 700) Return True End
Update
method.Method Update:Int()
x
and y
positions.cx += dx / time cy += dy / time
CheckCollisions
method, which we will implement later on.CheckCollisions()
y
position is reached, remove the bomb from the bombs' list. Then, close the method.If cy > ty Then bombs.Remove(Self) Return True End
Render
, to draw the bomb on the canvas.Method Render:Int()
SetColor(255, 0, 0) DrawLine(sx, sy, cx, cy)
SetColor(255, 55, 0) DrawCircle(cx, cy, 2) Return True End
Now comes the interesting part. The collision check for the bomb. Without it, bombs won't be destroyed, and cities will live forever.
CheckCollisions
.Method CheckCollisions:Int()
dist
, which will store a distance factor.Local dist:Float 'stores a distance
For Local explosion := Eachin explosions
dist = GetDistance(cx, cy, explosion.x, explosion.y)
If dist < explosion Then
bombs.Remove(Self)
game.score += explosion.score
100
.game.totalBombsDestroyed += 1 explosion.score += 100 Endif Next
30
.For Local city := Eachin cities 'Get distance to the bomb dist = GetDistance(cx, cy, city.x, city.y) 'Check against city radius (30) If dist < 30 Then bombs.Remove(Self) cities.Remove(city) Endif Next
15
pixels.'Check if launcher got hit by the bomb For Local launcher := Eachin launchers dist = GetDistance(cx, cy, launcher.x, launcher.y) If dist < 15 Then bombs.Remove(Self) launchers.Remove(launcher) Endif Next
Return True End End
Voila! The last class is defined.
3.12.123.189