package de.samdev.colorrunner.game.world; import java.util.ArrayList; import java.util.List; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.math.Rectangle; 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.NoStateFloorTileEntity; 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.game.world.map.MapSection; import de.samdev.colorrunner.input.GameInputListener; public class CRGameWorld implements GameInputListener { public PlayerEntity player; public List entities; public List sections; public FPSCounter fps = new FPSCounter(); private float mapRightBoundary = 0; public Rectangle camViewBoundaries = new Rectangle(); public CRGameWorld() { reinitialize(); } private void reinitialize() { entities = new ArrayList(); sections = new ArrayList(); addEntity(player = new PlayerEntity(this, 40, 290)); mapRightBoundary = appendMap(CRMapStorage.map_start, new Vector2(0, 0)); expandMap(); } public void update(float delta) { fps.Inc(); for (int i = entities.size()-1; i >= 0; i--) { CRGameEntity ent = entities.get(i); if (ent.active) ent.update(delta); else entities.remove(i); } 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_1: break; case CRTiledMap.GID_EMPTY_2: 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; case CRTiledMap.GID_MID: addEntity(new NoStateFloorTileEntity(this, px, py)); break; default: Gdx.app.error("MapLoad", "Unknown GID: " + map.getGID(x, y)); break; } } } sections.add(new MapSection(pos.x, width * FloorTileEntity.FLOORTILE_WIDTH, map.name)); return width * FloorTileEntity.FLOORTILE_WIDTH; } public MapSection getCurrentSection() { for (MapSection sec : sections) { if (sec.start <= player.bounds.x && sec.end >= player.bounds.x) return sec; } return new MapSection(0, 0, "NULL"); } @Override public void doJump() { player.jump(); } @Override public void switchColor(SwipeDirection sd) { player.switchPhase(sd); } @Override public void doFly() { player.fly(); } @Override public void reset() { reinitialize(); } }