ColorRunner/core/src/de/samdev/colorrunner/game/world/CRGameWorld.java

116 lines
3.2 KiB
Java
Raw Normal View History

2014-08-10 17:19:40 +02:00
package de.samdev.colorrunner.game.world;
import java.util.ArrayList;
import java.util.List;
2014-08-11 02:32:28 +02:00
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import de.samdev.colorrunner.game.renderer.AbstractGameRenderer;
2014-08-10 17:19:40 +02:00
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
2014-08-10 20:59:50 +02:00
import de.samdev.colorrunner.game.world.entities.gameentities.PlayerEntity;
2014-08-11 00:24:55 +02:00
import de.samdev.colorrunner.game.world.entities.gameentities.floor.DownStateFloorTileEntity;
2014-08-11 02:32:28 +02:00
import de.samdev.colorrunner.game.world.entities.gameentities.floor.FloorTileEntity;
2014-08-11 00:24:55 +02:00
import de.samdev.colorrunner.game.world.entities.gameentities.floor.LeftStateFloorTileEntity;
2014-08-11 12:19:51 +02:00
import de.samdev.colorrunner.game.world.entities.gameentities.floor.NoStateFloorTileEntity;
2014-08-11 00:24:55 +02:00
import de.samdev.colorrunner.game.world.entities.gameentities.floor.RightStateFloorTileEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.UpStateFloorTileEntity;
2014-08-11 02:32:28 +02:00
import de.samdev.colorrunner.game.world.map.CRMapStorage;
import de.samdev.colorrunner.game.world.map.CRTiledMap;
2014-08-10 19:41:06 +02:00
import de.samdev.colorrunner.input.GameInputListener;
2014-08-10 17:19:40 +02:00
2014-08-10 19:41:06 +02:00
public class CRGameWorld implements GameInputListener {
public PlayerEntity player;
2014-08-11 00:24:55 +02:00
2014-08-10 17:19:40 +02:00
public List<CRGameEntity> entities = new ArrayList<CRGameEntity>();
2014-08-11 00:24:55 +02:00
2014-08-10 17:19:40 +02:00
public FPSCounter fps = new FPSCounter();
2014-08-11 00:24:55 +02:00
2014-08-11 02:32:28 +02:00
private float mapRightBoundary = 0;
2014-08-10 17:19:40 +02:00
public CRGameWorld() {
2014-08-10 19:41:06 +02:00
addEntity(player = new PlayerEntity(this, 40, 290));
2014-08-11 00:24:55 +02:00
2014-08-11 12:19:51 +02:00
mapRightBoundary = appendMap(CRMapStorage.map_start, new Vector2(mapRightBoundary, 0));
2014-08-11 02:32:28 +02:00
expandMap();
2014-08-10 17:19:40 +02:00
}
public void update(float delta) {
fps.Inc();
2014-08-11 00:24:55 +02:00
2014-08-10 17:19:40 +02:00
for (CRGameEntity ent : entities) {
ent.update(delta);
}
2014-08-11 02:32:28 +02:00
expandMap();
}
private void expandMap() {
while (player.bounds.x + AbstractGameRenderer.MAX_GAME_WIDTH*2 > mapRightBoundary) {
float width = appendMap(CRMapStorage.getMap(), new Vector2(mapRightBoundary, 0));
mapRightBoundary += width;
}
2014-08-10 17:19:40 +02:00
}
2014-08-10 19:41:06 +02:00
public CRGameEntity addEntity(CRGameEntity ent) {
2014-08-10 17:19:40 +02:00
entities.add(ent);
2014-08-11 00:24:55 +02:00
2014-08-10 19:41:06 +02:00
return ent;
}
2014-08-11 02:32:28 +02:00
private float appendMap(CRTiledMap map, Vector2 pos) {
int height = map.getHeight();
int width = map.getWidth();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
float px = pos.x + x * FloorTileEntity.FLOORTILE_WIDTH;
float py = pos.y + (height - y) * FloorTileEntity.FLOORTILE_WIDTH;
switch (map.getGID(x, y)) {
2014-08-11 12:19:51 +02:00
case CRTiledMap.GID_EMPTY_1: break;
case CRTiledMap.GID_EMPTY_2: break;
2014-08-11 02:32:28 +02:00
case CRTiledMap.GID_UP:
addEntity(new UpStateFloorTileEntity(this, px, py));
break;
case CRTiledMap.GID_RIGHT:
addEntity(new RightStateFloorTileEntity(this, px, py));
break;
case CRTiledMap.GID_DOWN:
addEntity(new DownStateFloorTileEntity(this, px, py));
break;
case CRTiledMap.GID_LEFT:
addEntity(new LeftStateFloorTileEntity(this, px, py));
break;
2014-08-11 12:19:51 +02:00
case CRTiledMap.GID_MID:
addEntity(new NoStateFloorTileEntity(this, px, py));
break;
2014-08-11 02:32:28 +02:00
default:
Gdx.app.error("MapLoad", "Unknown GID: " + map.getGID(x, y));
break;
}
}
}
return width * FloorTileEntity.FLOORTILE_WIDTH;
}
2014-08-10 19:41:06 +02:00
@Override
public void doJump() {
player.jump();
}
@Override
public void switchColor(SwipeDirection sd) {
player.switchPhase(sd);
}
@Override
public void doFly() {
player.fly();
2014-08-10 17:19:40 +02:00
}
}