104 lines
3.4 KiB
Java
104 lines
3.4 KiB
Java
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 java.util.Random;
|
|
|
|
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 {
|
|
private final Random random;
|
|
|
|
public List<MapSection> sections;
|
|
private int mapRightBoundary = 0;
|
|
|
|
public EndlessMapProvider(long seed) {
|
|
random = new Random(seed);
|
|
}
|
|
|
|
@Override
|
|
public void init(CRGameWorld world) {
|
|
sections = new ArrayList<MapSection>();
|
|
mapRightBoundary = appendMap(world, CRMapStorage.map_start, 0);
|
|
}
|
|
|
|
@Override
|
|
public boolean isNextEndlessMenu(){
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public TriggerType getTrigger(int x, int y) {
|
|
|
|
for(int i = sections.size()-1; i >= 0; i--)
|
|
{
|
|
int start = sections.get(i).start;
|
|
int end = sections.get(i).end;
|
|
|
|
if(start <= x && end > x){
|
|
if( sections.get(i).crTiledMap.interaktionen[x - start][y] == CRTiledMap.GID_INTER_UP)
|
|
return TriggerType.RUNTOP;
|
|
else if( sections.get(i).crTiledMap.interaktionen[x - start][y] == CRTiledMap.GID_INTER_DOWN)
|
|
return TriggerType.RUNTOP;
|
|
else if( sections.get(i).crTiledMap.interaktionen[x - start][y] == CRTiledMap.GID_INTER_RIGHT)
|
|
return TriggerType.FLY;
|
|
else
|
|
return TriggerType.NOTHING;
|
|
}
|
|
}
|
|
return TriggerType.NOTHING;
|
|
}
|
|
|
|
@Override
|
|
public MapProvider createNew() {
|
|
EndlessMapProvider endlessMapProvider = new EndlessMapProvider(System.currentTimeMillis());
|
|
return endlessMapProvider;
|
|
}
|
|
|
|
@Override
|
|
public void update(CRGameWorld world, Rectangle player)
|
|
{
|
|
while (player.x + AbstractGameRenderer.MAX_GAME_WIDTH*2 > mapRightBoundary * FloorTileEntity.FLOORTILE_WIDTH) {
|
|
float width = appendMap(world, CRMapStorage.getMap(random), mapRightBoundary);
|
|
mapRightBoundary += width;
|
|
}
|
|
}
|
|
|
|
private int appendMap(CRGameWorld world, CRTiledMap map, int posX) {
|
|
int height = map.getHeight();
|
|
int width = map.getWidth();
|
|
|
|
for (int x = 0; x < width; x++) {
|
|
for (int y = 0; y < height; y++) {
|
|
float px = posX * FloorTileEntity.FLOORTILE_WIDTH + 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);
|
|
}
|
|
}
|
|
|
|
sections.add(new MapSection(posX, width, map.name, map));
|
|
|
|
return 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", null);
|
|
}
|
|
}
|