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

110 lines
2.9 KiB
Java

package de.samdev.colorrunner.game.world;
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import de.samdev.colorrunner.game.renderer.AbstractGameRenderer;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.PlayerEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.DownStateFloorTileEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.FloorTileEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.LeftStateFloorTileEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.RightStateFloorTileEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.UpStateFloorTileEntity;
import de.samdev.colorrunner.game.world.map.CRMapStorage;
import de.samdev.colorrunner.game.world.map.CRTiledMap;
import de.samdev.colorrunner.input.GameInputListener;
public class CRGameWorld implements GameInputListener {
public PlayerEntity player;
public List<CRGameEntity> entities = new ArrayList<CRGameEntity>();
public FPSCounter fps = new FPSCounter();
private float mapRightBoundary = 0;
public CRGameWorld() {
addEntity(player = new PlayerEntity(this, 40, 290));
expandMap();
}
public void update(float delta) {
fps.Inc();
for (CRGameEntity ent : entities) {
ent.update(delta);
}
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;
}
}
public CRGameEntity addEntity(CRGameEntity ent) {
entities.add(ent);
return ent;
}
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)) {
case CRTiledMap.GID_EMPTY: break;
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;
default:
Gdx.app.error("MapLoad", "Unknown GID: " + map.getGID(x, y));
break;
}
}
}
return width * FloorTileEntity.FLOORTILE_WIDTH;
}
@Override
public void doJump() {
player.jump();
}
@Override
public void switchColor(SwipeDirection sd) {
player.switchPhase(sd);
}
@Override
public void doFly() {
player.fly();
}
}