The Level
class is a place to design the level. If you want more enemies of a certain type or less lasers to make the fire rate less rapid, then this is where to do it. In a game you were planning to release you would probably extend Level
and design multiple instances with different enemies, quantities and backgrounds. For this project we will stick with just one rigid level but in the next project we will take the level design idea further.
Create a class called Level
and add all the following members and import
statements.
import android.content.Context; import android.graphics.PointF; import java.util.ArrayList; class Level { // Keep track of specific types public static final int BACKGROUND_INDEX = 0; public static final int PLAYER_INDEX = 1; public static final int FIRST_PLAYER_LASER = 2; public static final int LAST_PLAYER_LASER = 4; public static int mNextPlayerLaser; public static final int FIRST_ALIEN = 5; public static final int SECOND_ALIEN = 6; public static final int THIRD_ALIEN = 7; public static final int FOURTH_ALIEN = 8; public static final int FIFTH_ALIEN = 9; public static final int SIXTH_ALIEN = 10; public static final int LAST_ALIEN = 10; public static final int FIRST_ALIEN_LASER = 11; public static final int LAST_ALIEN_LASER = 15; public static int mNextAlienLaser; // This will hold all the instances of GameObject private ArrayList<GameObject> objects; }
Most of the variables we just coded are public
, static
and final
. They will be used to keep track of how many aliens and lasers we spawn as well as keep track of where in our GameObject ArrayList
certain objects are stored. As they are public
, static
and final
they can be easily referred to but not altered, from any class.
There are two variables which aren't final
these are mNextPlayerLaser
and mNextAlienLaser
which will be used to loop through the lasers that we spawn to determine which one to shoot next.
The final declaration in the previous code is an ArrayList
of GameObject
instances called objects
. This is where we will stash all the instances that the GameObjectFactory
assembles for us.
Next add the constructor method.
public Level(Context context, PointF mScreenSize, GameEngine ge){ objects = new ArrayList<>(); GameObjectFactory factory = new GameObjectFactory( context, mScreenSize, ge); buildGameObjects(factory); }
In the constructor the ArrayList
is initialized. Next, a new instance of GameObjectFactory
is created. The final line of code calls the buildGameObjects
method and passes in this new GameObjectFactory
instance called factory
.
Now code the buildGameObjects
method.
ArrayList<GameObject> buildGameObjects( GameObjectFactory factory){ objects.clear(); objects.add(BACKGROUND_INDEX, factory .create(new BackgroundSpec())); objects.add(PLAYER_INDEX, factory .create(new PlayerSpec())); // Spawn the player's lasers for (int i = FIRST_PLAYER_LASER; i != LAST_PLAYER_LASER + 1; i++) { objects.add(i, factory .create(new PlayerLaserSpec())); } mNextPlayerLaser = FIRST_PLAYER_LASER; // Create some aliens // Create some alien lasers return objects; } ArrayList<GameObject> getGameObjects(){ return objects; }
To start with the ArrayList
is cleared in case this isn't the first time the method has been called. We wouldn't want two or more levels worth of objects being updated and drawn.
Let's look at the next line of code again because if we can understand it we can understand how all the GameObject
instances are created.
objects.add(BACKGROUND_INDEX, factory .create(new BackgroundSpec()));
The code starts with objects.add
which is to add a new reference into the ArrayList
. The first argument is BACKGROUND_INDEX
and indicates the position in the ArrayList
where we want the background to go.
The second argument passed to the add
method needs to be a reference to a GameObject
. It is just that, but it is a little convoluted. This is the code:
factory.create(new BackgroundSpec())
This calls the create
method on the GameObjectFactory
instance which you might remember returns a GameObject
of the required type. So, at this stage there is a properly built background in the form of a GameObject
just waiting to be updated and drawn.
Next, we add a player in the same way and then we loop through a for
loop from FIRST_PLAYER_LASER
to LAST_PLAYER_LASER
and add a bunch of lasers for the player to shoot.
The variable mNextPlayerLaser
is initialized to the value of FIRST_PLAYER_LASER
ready for the GameEngine
class to spawn when it is needed.
The last method is the getGameObjects
method. It returns a reference to objects
. This is how we will share objects
with other classes that need it.
3.137.151.61