Skip to content

Instantly share code, notes, and snippets.

@JoseRivas1998
Created January 2, 2015 23:08
Show Gist options
  • Select an option

  • Save JoseRivas1998/71adba285daa67064cb1 to your computer and use it in GitHub Desktop.

Select an option

Save JoseRivas1998/71adba285daa67064cb1 to your computer and use it in GitHub Desktop.
This is the Ground class from Terry the Cave Explorer
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.EllipseMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Ellipse;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.tcg.caveexplorer.Game;
import com.tcg.caveexplorer.MyCamera;
import com.tcg.caveexplorer.managers.GameStateManager;
public class Ground {
private Array<Rectangle> bounds;
private TiledMap tileMap;
private float tileSize;
private Rectangle checkpoint;
private Vector2 cp;
private Rectangle clue;
private OrthogonalTiledMapRenderer tmr;
private float width, height;
private int widthInTiles, heightInTiles;
private long totalGhosts;
GameStateManager gsm;
private Array<Ghost> ghosts;
private Array<OneUp> oneUp;
private Array<Health> health;
public Ground(int numMap, GameStateManager gsm) {
this.gsm = gsm;
bounds = new Array<Rectangle>();
ghosts = new Array<Ghost>();
checkpoint = new Rectangle();
cp = new Vector2();
clue = new Rectangle();
oneUp = new Array<OneUp>();
health = new Array<Health>();
createTiles(numMap);
}
public float getWidth() {
return width;
}
public int getWidthInTiles() {
return widthInTiles;
}
public float getHeight() {
return height;
}
public int getHeightInTiles() {
return heightInTiles;
}
boolean passed;
private void createTiles(int numMap) {
// load tile map
try {
tileMap = new TmxMapLoader().load("maps/map" + numMap + ".tmx");
passed = true;
}
catch(Exception e) {
passed = false;
System.out.println("Cannot find file: maps/map" + numMap + ".tmx");
gsm.setState(gsm.TITLE, 0);
}
if(passed) {
tmr = new OrthogonalTiledMapRenderer(tileMap);
tileSize = tileMap.getProperties().get("tilewidth", Integer.class);
TiledMapTileLayer ground;
ground = (TiledMapTileLayer) tileMap.getLayers().get("ground");
createLayer(ground, bounds);
Game.log("Ground layer created");
try {
createHealth();
Game.log(health.size + " Health ups created");
} catch(Exception e) {
Game.log(e.toString());
}
try {
createOneUp();
Game.log(oneUp.size + " 1ups created");
} catch(Exception e) {
Game.log(e.toString());
}
createCheckpoint();
Game.log("Checkpoint created at: (" + checkpoint.x + ", " + checkpoint.y + ")" );
createClue();
Game.log("Clue point created at: (" + clue.x + ", " + clue.y + ")" );
try {
createWeakGhosts();
totalGhosts = ghosts.size;
Game.log("Weak Ghosts Created");
} catch(Exception e) {
Game.log(e.toString());
}
try {
createStrongGhosts();
totalGhosts = ghosts.size;
Game.log("Strong Ghosts Created");
} catch(Exception e) {
Game.log(e.toString());
}
try {
createBossGhost();
totalGhosts = ghosts.size;
Game.log("Boss Ghost Created");
} catch(Exception e) {
Game.log(e.toString());
}
try {
createFinal();
totalGhosts = ghosts.size;
Game.log("Final Boss Created");
} catch(Exception e) {
Game.log(e.toString());
}
widthInTiles = ground.getWidth();
width = widthInTiles * tileSize;
heightInTiles = ground.getHeight();
height = heightInTiles * tileSize;
Game.log("Level " + numMap + " created with a tile size of:" + new Vector2(widthInTiles, heightInTiles).toString());
Game.log("There are " + ghosts.size + " ghosts in level " + numMap);
}
}
private void createLayer(TiledMapTileLayer layer, Array<Rectangle> rect) {
for(int row = 0; row < layer.getHeight(); row++) {
for(int col = 0; col < layer.getWidth(); col++) {
Cell cell = layer.getCell(col, row);
// check if cell exists
if(cell == null) continue;
if(cell.getTile() == null) continue;
rect.add(new Rectangle(col * tileSize, row * tileSize, tileSize, tileSize));
}
}
}
private void createCheckpoint() {
TiledMapTileLayer checkpointL;
checkpointL = (TiledMapTileLayer) tileMap.getLayers().get("checkpoint");
for(int row = 0; row < checkpointL.getHeight(); row++) {
for(int col = 0; col < checkpointL.getWidth(); col++) {
Cell cell = checkpointL.getCell(col, row);
// check if cell exists
if(cell == null) continue;
if(cell.getTile() == null) continue;
checkpoint.set(col * tileSize, row * tileSize, tileSize, tileSize);
cp.set(checkpoint.x + (checkpoint.width * .5f), checkpoint.y + checkpoint.height);
}
}
}
private void createClue() {
TiledMapTileLayer checkpointL;
checkpointL = (TiledMapTileLayer) tileMap.getLayers().get("clue");
for(int row = 0; row < checkpointL.getHeight(); row++) {
for(int col = 0; col < checkpointL.getWidth(); col++) {
Cell cell = checkpointL.getCell(col, row);
// check if cell exists
if(cell == null) continue;
if(cell.getTile() == null) continue;
clue.set(col * tileSize, row * tileSize, tileSize, tileSize);
}
}
}
private void createWeakGhosts() {
MapLayer layer = tileMap.getLayers().get("wghost");
for(MapObject mo : layer.getObjects()) {
Ellipse e = ((EllipseMapObject) mo).getEllipse();
float x = e.x;
float y = e.y;
String p;
if(MathUtils.randomBoolean(.75f)) {
p = "entities/ghost/";
} else {
p = "entities/ghost1/";
}
Ghost g = new WeakGhost(x, y, p);
addGhost(g);
}
}
private void createStrongGhosts() {
MapLayer layer = tileMap.getLayers().get("sghost");
for(MapObject mo : layer.getObjects()) {
Ellipse e = ((EllipseMapObject) mo).getEllipse();
float x = e.x;
float y = e.y;
String p;
if(MathUtils.randomBoolean(.75f)) {
p = "entities/ghost/";
} else {
p = "entities/ghost1/";
}
Ghost g = new StrongGhost(x, y, p);
addGhost(g);
}
}
private void createBossGhost() {
MapLayer layer = tileMap.getLayers().get("bghost");
for(MapObject mo : layer.getObjects()) {
Ellipse e = ((EllipseMapObject) mo).getEllipse();
float x = e.x;
float y = e.y;
Ghost g = new BossGhost(x, y);
addGhost(g);
}
}
private void createFinal() {
MapLayer layer = tileMap.getLayers().get("boss");
for(MapObject mo : layer.getObjects()) {
Ellipse e = ((EllipseMapObject) mo).getEllipse();
float x = e.x;
float y = e.y;
Ghost g = new FinalBoss(x, y);
addGhost(g);
}
}
private void createOneUp() {
MapLayer layer = tileMap.getLayers().get("1up");
for(MapObject mo : layer.getObjects()) {
Ellipse e = ((EllipseMapObject) mo).getEllipse();
float x = e.x;
float y = e.y;
OneUp up = new OneUp(x, y);
oneUp.add(up);
}
}
private void createHealth() {
MapLayer layer = tileMap.getLayers().get("health");
for(MapObject mo : layer.getObjects()) {
Ellipse e = ((EllipseMapObject) mo).getEllipse();
float x = e.x;
float y = e.y;
Health h = new Health(x, y);
health.add(h);
}
}
public void addGhost(Ghost g) {
ghosts.add(g);
}
public void reset() {
do {
ghosts.clear();
} while(ghosts.size > 0);
createWeakGhosts();
createBossGhost();
createStrongGhosts();
}
public void newMap(int numMap) {
do {
ghosts.clear();
} while(ghosts.size > 0);
do {
health.clear();
} while(oneUp.size > 0);
do {
health.clear();
} while(health.size > 0);
bounds.clear();
createTiles(numMap);
}
public long getGhostsKilled() {
return (totalGhosts - ghosts.size);
}
public void update() {
}
public void draw(SpriteBatch sb, float dt, MyCamera cam) {
tmr.setView(cam);
tmr.render();
sb.begin();
sb.setProjectionMatrix(cam.combined);
for(OneUp up : oneUp) {
up.draw(sb);
}
for(Health h : health) {
h.draw(sb);
}
sb.end();
}
public void debug(float dt, ShapeRenderer sr) {
for(Rectangle rect: bounds) {
sr.rect(rect.x, rect.y, rect.width, rect.height);
}
sr.rect(checkpoint.x, checkpoint.y, checkpoint.width, checkpoint.height);
sr.rect(clue.x, clue.y, clue.width, clue.height);
for(OneUp up : oneUp) {
up.debug(sr);
}
for(Health h : health) {
h.debug(sr);
}
}
public Array<Rectangle> getBounds() {
return bounds;
}
public Array<Ghost> getGhosts() {
return ghosts;
}
public Rectangle getCheckpoint() {
return checkpoint;
}
public Vector2 getCp() {
return cp;
}
public Rectangle getClue() {
return clue;
}
public Array<OneUp> getOneUp() {
return oneUp;
}
public Array<Health> getHealth() {
return health;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment