Time for action — creating the bomb class

To create the bomb class, we will follow the ensuing steps:

  1. Open an empty script and save it under the name bombClass.monkey.
  2. As always, switch into 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.

  3. 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.

  4. Create a class called bomb.
    Class bomb
    

    Add the data fields for a bomb.

  5. 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
    
  6. The last fields will be needed to store the difference between 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.

  7. Implement a method to initialize a bomb, called Init.
    Method Init:Int()
    
  8. Set its starting position. The x value is determined randomly.
    sx = Rnd(5.0, (game.cWidth - 5.0))
    sy = 0.0
    
  9. 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
    
  10. Set the current position to the starting one.
    cx = sx
    cy = sy
    
  11. Calculate the difference between the starting and target x and y positions.
    dx = tx - sx
    dy = ty - sy
    
  12. Get a random value for the speed factor and then close the method.
    speed = Rnd(500, 700)
    Return True
    End
    
  13. Add the Update method.
    Method Update:Int()
    
  14. Calculate the current x and y positions.
    cx += dx / time
    cy += dy / time
    
  15. Check for collisions with cities and explosions via a call to the CheckCollisions method, which we will implement later on.
    CheckCollisions()
    
  16. 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
    
  17. Create a method called Render, to draw the bomb on the canvas.
    Method Render:Int()
    
  18. Draw the bomb's tail.
    SetColor(255, 0, 0)
    DrawLine(sx, sy, cx, cy)
    
  19. 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.

  20. Add a new method called CheckCollisions.
    Method CheckCollisions:Int()
    
  21. Add a local variable called dist, which will store a distance factor.
    Local dist:Float 'stores a distance
    
  22. Loop through all explosions.
    For Local explosion := Eachin explosions
    
  23. Determine the distance between the bomb and the explosion.
    dist = GetDistance(cx, cy, explosion.x, explosion.y)
    
  24. Check if the distance is smaller than the radius of the explosion.
    If dist < explosion Then
    
  25. Remove the bomb.
    bombs.Remove(Self)
    
  26. Add the explosion score value to the game score.
    game.score += explosion.score
    
  27. Increase the number of bombs that were destroyed, and increase the explosion score value by 100.
    game.totalBombsDestroyed += 1
    explosion.score += 100
    Endif
    Next
    
  28. 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
    
  29. 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
    
  30. 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.

Implementing the bomb class into the game

Implement the bomb class into the game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.123.189