From dbdc2a670ea604bb16bba7dbc378a4a4d538d424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 14 Apr 2017 00:13:20 +0200 Subject: [PATCH] abstract MapProvider away --- .../game/renderer/CRGameRenderer.java | 3 +- .../colorrunner/game/world/CRGameWorld.java | 75 ++----------------- .../game/world/map/CRTiledMap.java | 38 ++++++++++ .../map/provider/EndlessMapProvider.java | 64 ++++++++++++++++ .../game/world/map/provider/MapProvider.java | 12 +++ 5 files changed, 124 insertions(+), 68 deletions(-) create mode 100644 core/src/de/samdev/colorrunner/game/world/map/provider/EndlessMapProvider.java create mode 100644 core/src/de/samdev/colorrunner/game/world/map/provider/MapProvider.java diff --git a/core/src/de/samdev/colorrunner/game/renderer/CRGameRenderer.java b/core/src/de/samdev/colorrunner/game/renderer/CRGameRenderer.java index 6d08ab1..38da0ca 100644 --- a/core/src/de/samdev/colorrunner/game/renderer/CRGameRenderer.java +++ b/core/src/de/samdev/colorrunner/game/renderer/CRGameRenderer.java @@ -10,6 +10,7 @@ import de.samdev.colorrunner.CRGame; import de.samdev.colorrunner.game.world.CRGameWorld; import de.samdev.colorrunner.game.world.entities.CRGameEntity; import de.samdev.colorrunner.game.world.entities.MovingEntity; +import de.samdev.colorrunner.game.world.map.provider.EndlessMapProvider; public class CRGameRenderer extends AbstractGameRenderer { private CRGameWorld gameworld; @@ -80,7 +81,7 @@ public class CRGameRenderer extends AbstractGameRenderer { renderDebug("ExecTime: " + (int)(avgExecTime*100) / 100.0+ " / " + (int)(10000/gameworld.fps.getFPS())/10.0 + "ms (" + (int)((avgExecTime/(1000/gameworld.fps.getFPS()))*1000) / 10.0 + "%)"); renderDebug("CameraOffset: (" + (int)cam.position.x + "|" + (int)cam.position.y + ")"); renderDebug("Player(x): " + (int)gameworld.player.bounds.x); - renderDebug("Procedural Piece: \"" + gameworld.getCurrentSection().piece_name + "\""); + if (gameworld.mapprovider instanceof EndlessMapProvider)renderDebug("Procedural Piece: \"" + ((EndlessMapProvider)gameworld.mapprovider).getCurrentSection(gameworld.player.bounds).piece_name + "\""); endDebug(); } } diff --git a/core/src/de/samdev/colorrunner/game/world/CRGameWorld.java b/core/src/de/samdev/colorrunner/game/world/CRGameWorld.java index c577557..ddb801c 100644 --- a/core/src/de/samdev/colorrunner/game/world/CRGameWorld.java +++ b/core/src/de/samdev/colorrunner/game/world/CRGameWorld.java @@ -20,34 +20,32 @@ import de.samdev.colorrunner.game.world.entities.gameentities.floor.UpStateFloor 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.game.world.map.provider.EndlessMapProvider; +import de.samdev.colorrunner.game.world.map.provider.MapProvider; import de.samdev.colorrunner.input.GameInputListener; import de.samdev.colorrunner.screens.gameScreen.GameScreen; import de.samdev.colorrunner.screens.menu.MainMenu; 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 MapProvider mapprovider; + public CRGameWorld() { reinitialize(); } private void reinitialize() { entities = new ArrayList(); - sections = new ArrayList(); + mapprovider = new EndlessMapProvider(); addEntity(player = new PlayerEntity(this, 40, 290)); - mapRightBoundary = appendMap(CRMapStorage.map_start, new Vector2(0, 0)); - expandMap(); + mapprovider.init(this); } public void update(float delta) { @@ -66,14 +64,8 @@ public class CRGameWorld implements GameInputListener { { ((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu()); } - 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; - } + mapprovider.update(this, player.bounds); } public CRGameEntity addEntity(CRGameEntity ent) { @@ -81,57 +73,6 @@ public class CRGameWorld implements GameInputListener { 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() { diff --git a/core/src/de/samdev/colorrunner/game/world/map/CRTiledMap.java b/core/src/de/samdev/colorrunner/game/world/map/CRTiledMap.java index fd07e3b..a3a55d2 100644 --- a/core/src/de/samdev/colorrunner/game/world/map/CRTiledMap.java +++ b/core/src/de/samdev/colorrunner/game/world/map/CRTiledMap.java @@ -1,9 +1,18 @@ package de.samdev.colorrunner.game.world.map; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.XmlReader; import com.badlogic.gdx.utils.XmlReader.Element; +import de.samdev.colorrunner.game.world.CRGameWorld; +import de.samdev.colorrunner.game.world.entities.StaticEntity; +import de.samdev.colorrunner.game.world.entities.gameentities.floor.DownStateFloorTileEntity; +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; + public class CRTiledMap { public final static int GID_EMPTY_1 = 0; public final static int GID_EMPTY_2 = 1; @@ -56,4 +65,33 @@ public class CRTiledMap { public int getGID(int x, int y) { return map[x][y]; } + + public static StaticEntity CreateTile(CRGameWorld world, int id, float px, float py) { + + switch (id) { + + case CRTiledMap.GID_EMPTY_1: + case CRTiledMap.GID_EMPTY_2: + return null; + + case CRTiledMap.GID_UP: + return new UpStateFloorTileEntity(world, px, py); + + case CRTiledMap.GID_RIGHT: + return new RightStateFloorTileEntity(world, px, py); + + case CRTiledMap.GID_DOWN: + return new DownStateFloorTileEntity(world, px, py); + + case CRTiledMap.GID_LEFT: + return new LeftStateFloorTileEntity(world, px, py); + + case CRTiledMap.GID_MID: + return new NoStateFloorTileEntity(world, px, py); + + default: + Gdx.app.error("MapLoad", "Unknown GID: " + id); + return null; + } + } } diff --git a/core/src/de/samdev/colorrunner/game/world/map/provider/EndlessMapProvider.java b/core/src/de/samdev/colorrunner/game/world/map/provider/EndlessMapProvider.java new file mode 100644 index 0000000..5e631e6 --- /dev/null +++ b/core/src/de/samdev/colorrunner/game/world/map/provider/EndlessMapProvider.java @@ -0,0 +1,64 @@ +package de.samdev.colorrunner.game.world.map.provider; + +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector2; + +import java.util.ArrayList; +import java.util.List; + +import de.samdev.colorrunner.game.renderer.AbstractGameRenderer; +import de.samdev.colorrunner.game.world.CRGameWorld; +import de.samdev.colorrunner.game.world.entities.CRGameEntity; +import de.samdev.colorrunner.game.world.entities.gameentities.floor.FloorTileEntity; +import de.samdev.colorrunner.game.world.map.CRMapStorage; +import de.samdev.colorrunner.game.world.map.CRTiledMap; +import de.samdev.colorrunner.game.world.map.MapSection; + +public class EndlessMapProvider extends MapProvider { + + public List sections; + private float mapRightBoundary = 0; + + @Override + public void init(CRGameWorld world) { + sections = new ArrayList(); + mapRightBoundary = appendMap(world, CRMapStorage.map_start, new Vector2(0, 0)); + } + + @Override + public void update(CRGameWorld world, Rectangle player) + { + while (player.x + AbstractGameRenderer.MAX_GAME_WIDTH*2 > mapRightBoundary) { + float width = appendMap(world, CRMapStorage.getMap(), new Vector2(mapRightBoundary, 0)); + mapRightBoundary += width; + } + } + + private float appendMap(CRGameWorld world, 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; + + CRGameEntity e = CRTiledMap.CreateTile(world, map.getGID(x, y), px, py); + if (e != null) world.addEntity(e); + } + } + + sections.add(new MapSection(pos.x, width * FloorTileEntity.FLOORTILE_WIDTH, map.name)); + + return width * FloorTileEntity.FLOORTILE_WIDTH; + } + + public MapSection getCurrentSection(Rectangle player) { + for (MapSection sec : sections) { + if (sec.start <= player.x && sec.end >= player.x) + return sec; + } + + return new MapSection(0, 0, "NULL"); + } +} diff --git a/core/src/de/samdev/colorrunner/game/world/map/provider/MapProvider.java b/core/src/de/samdev/colorrunner/game/world/map/provider/MapProvider.java new file mode 100644 index 0000000..cecb219 --- /dev/null +++ b/core/src/de/samdev/colorrunner/game/world/map/provider/MapProvider.java @@ -0,0 +1,12 @@ +package de.samdev.colorrunner.game.world.map.provider; + + +import com.badlogic.gdx.math.Rectangle; + +import de.samdev.colorrunner.game.world.CRGameWorld; + +public abstract class MapProvider { + public abstract void init(CRGameWorld world); + + public abstract void update(CRGameWorld world, Rectangle player); +}