Time for action — building some cities

For each of the objects in RocketCommander, we will build an individual class, in its own file. So, let's just do that.

  1. Create a new empty script and save it under the name cityClass.monkey.
  2. Add the usual Strict statement to it and import our gameClasses.monkey file.
    Strict
    Import gameClasses
    
  3. Create a global list that stores all instances of the city class.
    Global cities := New List<city>
    

    Besides the methods of an object, we will create some functions that we can use from within the mainClass file; for example, a function to render all the cities in one batch. These helper functions are wrappers for code, which we could have stored inside the mainClass.monkey file. But, we want to keep things nice and neat. So let's create them here.

    The first wrapper function will be the one that renders all cities in one call on the canvas.

  4. Create a new function called RenderCities.
    Function RenderCities:Int()
    
  5. Now, loop through our list of cities.
    For Local city := Eachin cities
    
  6. Inside the loop, we will call a method of the city class, which we will implement soon. This Render method will render that particular city.
    city.Render()
    
  7. Now, close the FOR loop, and also close the function in the usual way.
    Next
    Return True
    End
    

    Next will be a function to create a new city. As a function parameter, it will have its horizontal (x) position in pixels. As all cities sit always at the same height, we don't need to add another parameter for this.

  8. Create a new function header called CreateCity.
    Function CreateCity:Int(xpos:Float)
    
  9. Create a local instance of the city class.
    Local c:city = New city
    
  10. To initialize the properties of a new city, we will call its Init method, which we will add soon. As a parameter, we will give the x position.
    c.Init(xpos)
    
  11. Now, add this city to the list of cities and close the function.
    cities.AddLast(c)
    Return True
    End
    

    Even if a function basically wraps only one line of code, it makes sense from a higher level of view. The next function clears the list of cities. A list in Monkey is a one-line statement. Let's assume that we want to use a different way of storing cities, such as in an array, for example. Then, we would only need to change the wrapper function, but the call in the main class could stay the same.

  12. Add a function called RemoveCities.
    Function RemoveCities:Int()
    
  13. Call the Clear method of the cities list to empty it. Then close off the function.
    cities.Clear()
    Return True
    End
    

    The same goes for the next function, used to determine the number of cities stored inside the cities list.

  14. Create a new function called GetCityCount.
    Function GetCityCount:Int()
    
  15. Instead of returning TRUE, we will return the result of the Count method of the cities list. Then close the function.
    Return cities.Count()
    End
    

    Enough of these wrapper functions! It is time to build our actual city class.

  16. Create a new class called city, which is not extended from any other class.
    Class city
    
  17. Add two fields to store the x and y positions of the city.
    Field x:Float = 0.0 'x pos of a city
    Field y:Float = 0.0 'y pos of a city
    
  18. Add the previously mentioned method called Init, to initialize a new city.
    Method Init:Int(xpos:Float)
    
  19. Set its fields, giving an x position and a height, which is calculated as the canvas height minus 40. Then close this method.
    x = xpos
    y = game.cHeight - 40.0
    Return True
    End
    

    Now, we will create a method that draws a city on the canvas. You will notice that all the objects in the game have mostly the same methods. The difference will be in the details of each method.

  20. Add a method called Render.
    Method Render:Int()
    
  21. Set the drawing color to a bright yellow.
    SetColor(255,255,0)
    
  22. Draw the city as three rectangles and their x and y positions on the canvas.
    DrawRect(x - 30, y - 20, 20, 20)
    DrawRect(x - 10, y - 30, 20, 30)
    DrawRect(x + 10, y - 10, 20, 10)
    
  23. Close this method, and the city class.
    Return True
    End
    End
    

That's it for the city class. No other functions and/or methods are needed. Just make sure that you have saved the file.

What just happened?

Here we are. We didn't build Rome, but we can now build small little cities for our game. Each city has the methods Render and Init. As cities are not dynamic objects, and we don't need to check for collisions, you won't need an Update or CollisionCheck method for them. We have also built wrapper functions that will do the dirty work for us when it comes to rendering all the cities at once, or deleting all the cities that are remaining.

Implementing the city class into the game

Now that we have a class that deals with our cities, we should implement it into the game and finally see something on the screen when we run 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.145.75.217