79 lines
2.1 KiB
Java
Raw Permalink Normal View History

2018-06-18 15:17:56 -07:00
package com.jalenwinslow.game.gameobjects;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;
import com.jalenwinslow.game.Handler;
/**
* Created by jalen on 2/16/2018.
*/
public class GameObjectHandler {
private Handler handler;
private Array<GameObject> gameObjects;
private Player player;
private Maze maze;
public GameObjectHandler(Handler handler) {
this.handler = handler;
this.gameObjects = new Array<GameObject>();
}
public void update(float dt) {
for (int i = 0; i < gameObjects.size; i++) {
gameObjects.get(i).update(dt);
}
handler.debugger.msg = "gameobjects " + gameObjects.size;
}
public void render(SpriteBatch batch) {
sortGameObjects();
for (int i = 0; i < gameObjects.size; i++) {
gameObjects.get(i).render(batch);
batch.setColor(1, 1, 1, 1);
}
}
public void dispose() {
for (GameObject go: gameObjects) go.dispose();;
gameObjects.clear();
player = null;
}
private void sortGameObjects() {
for (int i = 0; i < gameObjects.size; i++) {
if (i+1 < gameObjects.size && gameObjects.get(i+1) != null) {
if (gameObjects.get(i).getDepth() < gameObjects.get(i+1).getDepth()) {
gameObjects.swap(i, i+1);
}
}
}
}
public void addGameObject(GameObject gameObject) {
gameObjects.add(gameObject);
}
public void addPlayer(Player player) {
this.player = player;
gameObjects.add(player);
}
public void removePlayer(Player player) {
gameObjects.removeValue(player, false);
this.player = null;
}
public void removeGameObject(GameObject gameObject) {
gameObjects.removeValue(gameObject, false);
}
//Getters and Setters
public Array<GameObject> getGameObjects() {return gameObjects;}
public Player getPlayer() {return player;}
public Maze getMaze() {return maze;}
public void setMaze(Maze maze) {this.maze = maze;}
}