First Commit
This commit is contained in:
52
com/jalenwinslow/game/gameobjects/GameObject.java
Normal file
52
com/jalenwinslow/game/gameobjects/GameObject.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package com.jalenwinslow.game.gameobjects;
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.jalenwinslow.game.Handler;
|
||||
|
||||
/**
|
||||
* Created by jalen on 2/16/2018.
|
||||
*/
|
||||
|
||||
public abstract class GameObject {
|
||||
|
||||
protected Handler handler;
|
||||
protected Vector2 pos;
|
||||
protected float depth;
|
||||
protected TextureRegion image;
|
||||
protected Rectangle bounds;
|
||||
|
||||
public GameObject(Handler handler, float x, float y, Texture image) {
|
||||
this.handler = handler;
|
||||
this.pos = new Vector2(x, y);
|
||||
this.depth = y;
|
||||
if (image != null) this.image = new TextureRegion(image);
|
||||
else this.image = null;
|
||||
this.bounds = new Rectangle();
|
||||
}
|
||||
|
||||
public abstract void init(int initState);
|
||||
public abstract void update(float dt);
|
||||
public abstract void render(SpriteBatch batch);
|
||||
public abstract void dispose();
|
||||
|
||||
public boolean hasCollided(float x, float y) {
|
||||
return (x < bounds.x + bounds.width && x > bounds.x && y < bounds.y + bounds.height && y > bounds.y);
|
||||
}
|
||||
|
||||
//Getters and Setters
|
||||
public Vector2 getPos() {return pos;}
|
||||
public float getDepth() {return depth;}
|
||||
public TextureRegion getImage() {return image;}
|
||||
public Rectangle getBounds() {return bounds;}
|
||||
|
||||
public void setPos(Vector2 pos) {this.pos = pos;}
|
||||
public void setPos(float x, float y) {this.pos.set(x, y);}
|
||||
public void setDepth(float depth) {this.depth = depth;}
|
||||
public void setImage(TextureRegion image) {this.image = image;}
|
||||
public void setBounds(Rectangle bounds) {this.bounds = bounds;}
|
||||
|
||||
}
|
78
com/jalenwinslow/game/gameobjects/GameObjectHandler.java
Normal file
78
com/jalenwinslow/game/gameobjects/GameObjectHandler.java
Normal file
@@ -0,0 +1,78 @@
|
||||
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;}
|
||||
}
|
Reference in New Issue
Block a user