added StaticMapProvider

This commit is contained in:
Mike Schwörer 2017-04-14 01:06:53 +02:00
parent c9909f0317
commit 71b69ec0a9
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
9 changed files with 161 additions and 89 deletions

View File

@ -1,8 +1,8 @@
package de.samdev.colorrunner;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;
import de.samdev.colorrunner.screens.menu.SplashScreen;
public class CRGame extends Game {
@ -15,4 +15,8 @@ public class CRGame extends Game {
//else
setScreen(new SplashScreen());
}
public static CRGame Inst() {
return ((CRGame) Gdx.app.getApplicationListener());
}
}

View File

@ -11,6 +11,7 @@ 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;
import de.samdev.colorrunner.game.world.map.provider.StaticMapProvider;
public class CRGameRenderer extends AbstractGameRenderer {
private CRGameWorld gameworld;
@ -26,12 +27,12 @@ public class CRGameRenderer extends AbstractGameRenderer {
public void doRender() {
updateCameraOffset(gameworld.player.bounds.x + gameworld.player.bounds.width/2);
gameworld.camViewBoundaries = getCamViewRectangle();
if (CRGame.DEBUG) renderDebugInfo();
renderMain();
if (CRGame.DEBUG) renderDebugBoxes();
if (CRGame.DEBUG) renderDebugInfo();
}
private void renderDebugBoxes() {
@ -81,7 +82,10 @@ 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);
if (gameworld.mapprovider instanceof EndlessMapProvider)renderDebug("Procedural Piece: \"" + ((EndlessMapProvider)gameworld.mapprovider).getCurrentSection(gameworld.player.bounds).piece_name + "\"");
if (gameworld.mapprovider instanceof EndlessMapProvider)
renderDebug("Procedural Piece: \"" + ((EndlessMapProvider)gameworld.mapprovider).getCurrentSection(gameworld.player.bounds).piece_name + "\"");
else if (gameworld.mapprovider instanceof StaticMapProvider)
renderDebug("Level Progress: " + ((StaticMapProvider)gameworld.mapprovider).mapPlayerPos + "/" + ((StaticMapProvider)gameworld.mapprovider).mapMaxWidth);
endDebug();
}
}

View File

@ -1,29 +1,16 @@
package de.samdev.colorrunner.game.world;
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.Game;
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 java.util.ArrayList;
import java.util.List;
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.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 {
@ -35,14 +22,15 @@ public class CRGameWorld implements GameInputListener {
public MapProvider mapprovider;
public CRGameWorld() {
public CRGameWorld(MapProvider prov) {
mapprovider = prov;
reinitialize();
}
private void reinitialize() {
entities = new ArrayList<CRGameEntity>();
mapprovider = new EndlessMapProvider();
addEntity(player = new PlayerEntity(this, 40, 290));
mapprovider.init(this);

View File

@ -1,50 +1,55 @@
package de.samdev.colorrunner.game.world.map;
import com.badlogic.gdx.Gdx;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.badlogic.gdx.Gdx;
public class CRMapStorage {
public static final Random random = new Random();
public static final List<CRTiledMap> maps = new ArrayList<CRTiledMap>();
public static final CRTiledMap map_start = register("p00_start.tmx", false);
public static final CRTiledMap map_start = load("map_pieces", "p00_start.tmx");
public static final CRTiledMap map_01 = register("p01.tmx");
public static final CRTiledMap map_02 = register("p02.tmx");
public static final CRTiledMap map_03 = register("p03.tmx");
public static final CRTiledMap map_04 = register("p04.tmx");
public static final CRTiledMap map_05 = register("p05.tmx");
public static final CRTiledMap map_06 = register("p06.tmx");
public static final CRTiledMap map_07 = register("p07.tmx");
public static final CRTiledMap map_08 = register("p08.tmx");
public static final CRTiledMap map_09 = register("p09.tmx");
public static final CRTiledMap map_10 = register("p10.tmx");
public static final CRTiledMap map_11 = register("p11.tmx");
public static final CRTiledMap map_12 = register("p12.tmx");
public static final CRTiledMap map_13 = register("p13.tmx");
public static final CRTiledMap map_14 = register("p14.tmx");
public static final CRTiledMap map_15 = register("p15.tmx");
public static final CRTiledMap map_16 = register("p16.tmx");
public static final CRTiledMap map_17 = register("p17.tmx");
public static final CRTiledMap map_18 = register("p18.tmx");
public static final CRTiledMap map_19 = register("p19.tmx");
public static final CRTiledMap map_20 = register("p20.tmx");
public static final CRTiledMap map_21 = register("p21.tmx");
public static final CRTiledMap map_22 = register("p22.tmx");
public static final CRTiledMap map_23 = register("p23.tmx");
public static final CRTiledMap map_24 = register("p24.tmx");
public static final CRTiledMap map_25 = register("p25.tmx");
public static final CRTiledMap map_26 = register("p26.tmx");
public static CRTiledMap register(String path) {
return register(path, true);
public static final CRTiledMap map_01 = register("map_pieces", "p01.tmx");
public static final CRTiledMap map_02 = register("map_pieces", "p02.tmx");
public static final CRTiledMap map_03 = register("map_pieces", "p03.tmx");
public static final CRTiledMap map_04 = register("map_pieces", "p04.tmx");
public static final CRTiledMap map_05 = register("map_pieces", "p05.tmx");
public static final CRTiledMap map_06 = register("map_pieces", "p06.tmx");
public static final CRTiledMap map_07 = register("map_pieces", "p07.tmx");
public static final CRTiledMap map_08 = register("map_pieces", "p08.tmx");
public static final CRTiledMap map_09 = register("map_pieces", "p09.tmx");
public static final CRTiledMap map_10 = register("map_pieces", "p10.tmx");
public static final CRTiledMap map_11 = register("map_pieces", "p11.tmx");
public static final CRTiledMap map_12 = register("map_pieces", "p12.tmx");
public static final CRTiledMap map_13 = register("map_pieces", "p13.tmx");
public static final CRTiledMap map_14 = register("map_pieces", "p14.tmx");
public static final CRTiledMap map_15 = register("map_pieces", "p15.tmx");
public static final CRTiledMap map_16 = register("map_pieces", "p16.tmx");
public static final CRTiledMap map_17 = register("map_pieces", "p17.tmx");
public static final CRTiledMap map_18 = register("map_pieces", "p18.tmx");
public static final CRTiledMap map_19 = register("map_pieces", "p19.tmx");
public static final CRTiledMap map_20 = register("map_pieces", "p20.tmx");
public static final CRTiledMap map_21 = register("map_pieces", "p21.tmx");
public static final CRTiledMap map_22 = register("map_pieces", "p22.tmx");
public static final CRTiledMap map_23 = register("map_pieces", "p23.tmx");
public static final CRTiledMap map_24 = register("map_pieces", "p24.tmx");
public static final CRTiledMap map_25 = register("map_pieces", "p25.tmx");
public static final CRTiledMap map_26 = register("map_pieces", "p26.tmx");
public static final CRTiledMap lvl_01 = load("levels", "plevel001.tmx");
private static CRTiledMap load(String folder, String path) {
return internalload(folder, path, false);
}
public static CRTiledMap register(String path, boolean doRegister) {
CRTiledMap m = CRTiledMap.load(path, Gdx.files.internal("map_pieces/" + path).readString());
private static CRTiledMap register(String folder, String path) {
return internalload(folder, path, true);
}
private static CRTiledMap internalload(String folder, String path, boolean doRegister) {
CRTiledMap m = CRTiledMap.load(path, Gdx.files.internal(folder + "/" + path).readString());
if (doRegister)
maps.add(m);
@ -52,7 +57,7 @@ public class CRMapStorage {
return m;
}
public static CRTiledMap getMap() {
return maps.get(random.nextInt(maps.size()));
public static CRTiledMap getMap(Random r) {
return maps.get(r.nextInt(maps.size()));
}
}

View File

@ -5,6 +5,7 @@ import com.badlogic.gdx.math.Vector2;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import de.samdev.colorrunner.game.renderer.AbstractGameRenderer;
import de.samdev.colorrunner.game.world.CRGameWorld;
@ -15,10 +16,15 @@ import de.samdev.colorrunner.game.world.map.CRTiledMap;
import de.samdev.colorrunner.game.world.map.MapSection;
public class EndlessMapProvider extends MapProvider {
private final Random random;
public List<MapSection> sections;
private float mapRightBoundary = 0;
public EndlessMapProvider(long seed) {
random = new Random(seed);
}
@Override
public void init(CRGameWorld world) {
sections = new ArrayList<MapSection>();
@ -29,7 +35,7 @@ public class EndlessMapProvider extends MapProvider {
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));
float width = appendMap(world, CRMapStorage.getMap(random), new Vector2(mapRightBoundary, 0));
mapRightBoundary += width;
}
}

View File

@ -0,0 +1,62 @@
package de.samdev.colorrunner.game.world.map.provider;
import com.badlogic.gdx.math.Rectangle;
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.CRTiledMap;
public class StaticMapProvider extends MapProvider {
private static final int CHUNK_SIZE = 16;
private CRTiledMap map;
private int mapRightBoundary = 0;
public int mapPlayerPos = 0;
public int mapMaxWidth = 0;
public StaticMapProvider(CRTiledMap m) {
map = m;
mapMaxWidth = map.getWidth();
}
@Override
public void init(CRGameWorld world) {
mapRightBoundary = appendMap(world);
}
private int appendMap(CRGameWorld world) {
int height = map.getHeight();
int width = map.getWidth();
int xstart = mapRightBoundary;
int xend = mapRightBoundary + CHUNK_SIZE;
if (xend > width) xend = width;
for (int x = xstart; x < xend; x++) {
for (int y = 0; y < height; y++) {
float px = x * FloorTileEntity.FLOORTILE_WIDTH;
float py = (height - y) * FloorTileEntity.FLOORTILE_WIDTH;
CRGameEntity e = CRTiledMap.CreateTile(world, map.getGID(x, y), px, py);
if (e != null) world.addEntity(e);
}
}
return xend;
}
@Override
public void update(CRGameWorld world, Rectangle player) {
mapPlayerPos = (int)(player.x / FloorTileEntity.FLOORTILE_WIDTH);
while (player.x + AbstractGameRenderer.MAX_GAME_WIDTH*2 > mapRightBoundary * FloorTileEntity.FLOORTILE_WIDTH && mapRightBoundary < mapMaxWidth) {
mapRightBoundary = appendMap(world);
}
}
}

View File

@ -4,12 +4,11 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import de.samdev.colorrunner.game.renderer.CRGameRenderer;
import de.samdev.colorrunner.game.world.AverageExecutionLogger;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.map.provider.MapProvider;
import de.samdev.colorrunner.input.CRGameInputProcessor;
public class GameScreen implements Screen {
@ -19,8 +18,8 @@ public class GameScreen implements Screen {
private AverageExecutionLogger execTime = new AverageExecutionLogger();
public GameScreen() {
world = new CRGameWorld(); // initialize world
public GameScreen(MapProvider prov) {
world = new CRGameWorld(prov); // initialize world
renderer = new CRGameRenderer(world, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); // initialize renderer
input = new CRGameInputProcessor(world);

View File

@ -1,6 +1,5 @@
package de.samdev.colorrunner.screens.menu;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
@ -13,6 +12,10 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import de.samdev.colorrunner.CRGame;
import de.samdev.colorrunner.game.world.map.CRMapStorage;
import de.samdev.colorrunner.game.world.map.provider.EndlessMapProvider;
import de.samdev.colorrunner.game.world.map.provider.StaticMapProvider;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;
public class MainMenu implements Screen {
@ -23,13 +26,13 @@ public class MainMenu implements Screen {
private Skin skin = new Skin(Gdx.files.internal("skins/menuSkin.json"),
new TextureAtlas(Gdx.files.internal("skins/menuSkin.pack")));
private TextButton buttonPlay = new TextButton("Play", skin),
buttonExit = new TextButton("Exit", skin),
buttonOption = new TextButton("Option", skin);
private TextButton buttonPlay1 = new TextButton("Play Level 1", skin);
private TextButton buttonPlay2 = new TextButton("Play Endless", skin);
private TextButton buttonExit = new TextButton("Exit", skin);
private TextButton buttonOption = new TextButton("Option", skin);
private Label title = new Label("Color Runner", skin);
@Override
public void render(float delta) {
@ -43,23 +46,29 @@ public class MainMenu implements Screen {
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
// NOP
}
@Override
public void show() {
buttonPlay.addListener(new ClickListener(){
buttonPlay1.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
((Game)Gdx.app.getApplicationListener()).setScreen(new GameScreen());
CRGame.Inst().setScreen(new GameScreen(new StaticMapProvider(CRMapStorage.lvl_01)));
}
});
buttonPlay2.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
CRGame.Inst().setScreen(new GameScreen(new EndlessMapProvider(System.currentTimeMillis())));
}
});
buttonOption.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
((Game)Gdx.app.getApplicationListener()).setScreen(new OptionMenuScreen());
CRGame.Inst().setScreen(new OptionMenuScreen());
}
});
@ -67,12 +76,12 @@ public class MainMenu implements Screen {
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
// or System.exit(0);
}
});
table.add(title).padBottom(40).row();
table.add(buttonPlay).size((int)((double)Gdx.graphics.getWidth() / 2.0D),60).padBottom(20).row();
table.add(buttonPlay1).size((int)((double)Gdx.graphics.getWidth() / 2.0D),60).padBottom(20).row();
table.add(buttonPlay2).size((int)((double)Gdx.graphics.getWidth() / 2.0D),60).padBottom(20).row();
table.add(buttonOption).size((int)((double)Gdx.graphics.getWidth() / 2.0D),60).padBottom(20).row();
table.add(buttonExit).size((int)((double)Gdx.graphics.getWidth() / 2.0D),60).padBottom(20).row();
@ -80,25 +89,21 @@ public class MainMenu implements Screen {
stage.addActor(table);
Gdx.input.setInputProcessor(stage);
}
@Override
public void hide() {
dispose();
}
@Override
public void pause() {
// TODO Auto-generated method stub
// NOP
}
@Override
public void resume() {
// TODO Auto-generated method stub
// NOP
}
@Override

View File

@ -1,6 +1,5 @@
package de.samdev.colorrunner.screens.menu;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
@ -13,7 +12,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;
import de.samdev.colorrunner.CRGame;
public class OptionMenuScreen implements Screen{
@ -59,7 +58,7 @@ public class OptionMenuScreen implements Screen{
buttonBack.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
((Game)Gdx.app.getApplicationListener()).setScreen(new MainMenu());
CRGame.Inst().setScreen(new MainMenu());
}
});