For each of the objects in RocketCommander
, we will build an individual class, in its own file. So, let's just do that.
cityClass.monkey
. Strict
statement to it and import our gameClasses.monkey
file.Strict Import gameClasses
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.
RenderCities
.Function RenderCities:Int()
For Local city := Eachin cities
city
class, which we will implement soon. This Render
method will render that particular city.city.Render()
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.
CreateCity
.Function CreateCity:Int(xpos:Float)
Local c:city = New city
Init
method, which we will add soon. As a parameter, we will give the x
position.c.Init(xpos)
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.
RemoveCities
.Function RemoveCities:Int()
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.
GetCityCount
.Function GetCityCount:Int()
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.
city
, which is not extended from any other class.Class city
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
Init
, to initialize a new city.Method Init:Int(xpos:Float)
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.
Render
.Method Render:Int()
SetColor(255,255,0)
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)
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.
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.
3.145.75.217