
上QQ阅读APP看书,第一时间看更新
Time for action — creating the bomb class
To create the bomb
class, we will follow the ensuing steps:
- Open an empty script and save it under the name
bombClass.monkey
. - As always, switch into
Strict
mode, importgameClasses
, and create a global list that holds all instances of thebomb
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. - Add functions to create, update, render, remove, and count bombs.
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. - Create a class called
bomb
.Class bomb
Add the data fields for a bomb.
- Add fields to store the start, target, and current positions.
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
- The last fields will be needed to store the difference between
x
andy
, 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.
- Implement a method to initialize a bomb, called
Init
.Method Init:Int()
- Set its starting position. The
x
value is determined randomly.sx = Rnd(5.0, (game.cWidth - 5.0)) sy = 0.0
- Determine the
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
- Set the current position to the starting one.
cx = sx cy = sy
- Calculate the difference between the starting and target
x
andy
positions.dx = tx - sx dy = ty - sy
- Get a random value for the speed factor and then close the method.
speed = Rnd(500, 700) Return True End
- Add the
Update
method.Method Update:Int()
- Calculate the current
x
andy
positions.cx += dx / time cy += dy / time
- Check for collisions with cities and explosions via a call to the
CheckCollisions
method, which we will implement later on.CheckCollisions()
- If the target
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
- Create a method called
Render
, to draw the bomb on the canvas.Method Render:Int()
- Draw the bomb's tail.
SetColor(255, 0, 0) DrawLine(sx, sy, cx, cy)
- Draw the bomb head and close the method.
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.
- Add a new method called
CheckCollisions
.Method CheckCollisions:Int()
- Add a local variable called
dist
, which will store a distance factor.Local dist:Float 'stores a distance
- Loop through all explosions.
For Local explosion := Eachin explosions
- Determine the distance between the bomb and the explosion.
dist = GetDistance(cx, cy, explosion.x, explosion.y)
- Check if the distance is smaller than the radius of the explosion.
If dist < explosion Then
- Remove the bomb.
bombs.Remove(Self)
- Add the explosion score value to the game score.
game.score += explosion.score
- Increase the number of bombs that were destroyed, and increase the explosion score value by
100
.game.totalBombsDestroyed += 1 explosion.score += 100 Endif Next
- Loop through all cities. Repeat the same distance check with a city radius of
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
- Loop through all launchers and repeat the distance check with a radius of
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
- Close off the method and the class.
Return True End End
Voila! The last class is defined.
What just happened?
We have built a class that reassembles our bombs. Inside this class, we have defined the usual wrapper functions and methods. What is special about this class is the CheckCollisions
method, which does all the collision checks, for a bomb, against cities, launchers, and explosions.