📜 ⬆️ ⬇️

We write a game for Android using AndEngine. Part 3

Hello!
Here is a long-term continuation of a series of articles on how to create a not-so- simple game for android using AndEngine .

Already read the previous articles?
Part 1
Part 2
Then we continue.

Today we will deal with a map of game objects. What is she like? A two-dimensional array of game objects with dimensions 13x7. The size is a bit strange, but with a cell size of 64x64, it starts to seem quite logical.
Step 1. Set the necessary constants and fields.
public class GameObjectsMap {
public static final int HEIGHT = 7;
public static final int WIDTH = 13;
public static final float CELL_SIZE_X = 64f;
public static final float CELL_SIZE_Y = 64f;

public static final int LASER_LAYER = 0;
public static final int GAME_OBJECTS_LAYER = LASER_LAYER + 1;

private GameObject[][] mMap = new GameObject[WIDTH][HEIGHT];
private Textures mTextures;
GameObjectsMap(final Textures textures) {
mTextures = textures;
}
}


* This source code was highlighted with Source Code Highlighter .

Step 2. Adding objects.
For more convenience, we need a method for adding objects to the field:
public void add(Type type, final int posH, final int posW, final int angle) {
GameObject object = null ;
switch (type) {
case lasergun:
object = new LaserGun(posH, posW, angle, mTextures
.getLaserGun());
break ;
case mirror:
break ;
case target:
break ;
default :
break ;
}
mMap[posH][posW] = object ;
}


* This source code was highlighted with Source Code Highlighter .

Step 3. Displaying objects.
An attentive reader could see Enum Type in a previous post in the description of the GameObject class. In the add method we pass the type of the object, its position and angle of rotation, after which it appears in our array, but still is not displayed. To display game objects we need the following method:
public void addToScene(Scene scene) {
for ( int i = 0; i < mMap.length; i++) {
for ( int j = 0; j < mMap[i].length; j++) {
GameObject o = mMap[i][j];
if (o != null ) {
o.attachTo(scene);
}
}
}
}


* This source code was highlighted with Source Code Highlighter .

Pretty simple. Well, just in case:
public void clear() {
for ( int i = 0; i < mMap.length; i++) {
Arrays.fill(mMap[i], null );
}
}


* This source code was highlighted with Source Code Highlighter .

Now we can use a simple and clear type construction to add objects to the scene:

mGameObjectsMap.add (Type.lasergun, 0, 0, 3);
')
Step 4. Handling touches.
All the same, we are doing the game without us interacting with the user. Unfortunately, there are no standard handlers for the Entity class, so we will act in the old manner.

scene.setOnSceneTouchListener (mGameObjectsController);

Where mGameObjectsController implements the IOnSceneTouchListener interface. In fact, not much different from the usual OnTouchListener. And in my case, it looks like this:
public boolean onSceneTouchEvent(final Scene pScene, TouchEvent pSceneTouchEvent) {
if (pSceneTouchEvent.isActionDown()) {
float x = pSceneTouchEvent.getX();
float y = pSceneTouchEvent.getY();
int cellNumX = ( int ) ((x - x % GameObjectsMap.CELL_SIZE_X)
/ GameObjectsMap.CELL_SIZE_X);
int cellNumY = ( int ) ((y - y % GameObjectsMap.CELL_SIZE_X)
/ GameObjectsMap.CELL_SIZE_X);
DynamicGameObject object = (DynamicGameObject)mMap.getMap()[cellNumX][cellNumY];
if ( object != null ) {
//
}
}
return false ;
}


* This source code was highlighted with Source Code Highlighter .


Yes, I agree, you can calculate the position of the cell differently, but this option works and I like it.

That's all for today. Some of my projects you can find here . Questions, suggestions and constructive criticism, as always, are welcome.
PS The source code is still there .
PPS Continuation.

Source: https://habr.com/ru/post/123367/


All Articles