abstract MapProvider away

This commit is contained in:
Mike Schwörer 2017-04-14 00:13:20 +02:00
parent 0d984ecd84
commit dbdc2a670e
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 124 additions and 68 deletions

View File

@ -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();
}
}

View File

@ -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<CRGameEntity> entities;
public List<MapSection> 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<CRGameEntity>();
sections = new ArrayList<MapSection>();
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() {

View File

@ -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;
}
}
}

View File

@ -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<MapSection> sections;
private float mapRightBoundary = 0;
@Override
public void init(CRGameWorld world) {
sections = new ArrayList<MapSection>();
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");
}
}

View File

@ -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);
}