new abstraction, scenegraph. moved to engine. refactor game code.
parent
064326a023
commit
22c4518675
|
|
@ -2,12 +2,10 @@ package xyz.valnet.engine.scenegraph;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import xyz.valnet.hadean.scenes.GameScene;
|
||||
|
||||
public class GameObject implements IRenderable, ITickable {
|
||||
private GameScene scene;
|
||||
private SceneGraph scene;
|
||||
|
||||
public void link(GameScene scene) {
|
||||
public void link(SceneGraph scene) {
|
||||
this.scene = scene;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package xyz.valnet.hadean.input;
|
||||
package xyz.valnet.engine.scenegraph;
|
||||
|
||||
import xyz.valnet.engine.math.Vector4f;
|
||||
|
||||
public interface IMouseListener {
|
||||
public interface IMouseCaptureArea {
|
||||
public void mouseEnter();
|
||||
public void mouseLeave();
|
||||
public boolean mouseDown(int button);
|
||||
|
|
@ -0,0 +1,149 @@
|
|||
package xyz.valnet.engine.scenegraph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import xyz.valnet.engine.App;
|
||||
|
||||
public abstract class SceneGraph implements IScene {
|
||||
protected final List<GameObject> objects = new ArrayList<GameObject>();
|
||||
private final List<GameObject> newObjects = new ArrayList<GameObject>();
|
||||
private final List<GameObject> removeObjects = new ArrayList<GameObject>();
|
||||
|
||||
private IMouseCaptureArea hoveredMouseListener = null;
|
||||
|
||||
public <T> T get(Class<T> clazz) {
|
||||
for(GameObject obj : objects) {
|
||||
if(clazz.isInstance(obj)) {
|
||||
return clazz.cast(obj);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> List<T> getAll(Class<T> clazz) {
|
||||
List<T> stuff = new ArrayList<T>();
|
||||
for(GameObject obj : objects) {
|
||||
if(clazz.isInstance(obj)) {
|
||||
stuff.add(clazz.cast(obj));
|
||||
}
|
||||
}
|
||||
return stuff;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dTime) {
|
||||
// ADD OBJECTS
|
||||
if(!newObjects.isEmpty()) {
|
||||
List<GameObject> added = new ArrayList<GameObject>();
|
||||
|
||||
for(GameObject obj : newObjects) {
|
||||
objects.add(obj);
|
||||
added.add(obj);
|
||||
}
|
||||
newObjects.clear();
|
||||
|
||||
for(GameObject obj : added) {
|
||||
obj.start();
|
||||
}
|
||||
}
|
||||
|
||||
// REMOVE OBJECTS
|
||||
if(!removeObjects.isEmpty()) {
|
||||
for(GameObject obj : removeObjects) {
|
||||
objects.remove(obj);
|
||||
}
|
||||
removeObjects.clear();
|
||||
}
|
||||
|
||||
// TICK OBJECTS
|
||||
for(GameObject obj : objects) {
|
||||
obj.update(dTime);
|
||||
}
|
||||
|
||||
// DO MOUSE UPDATES!
|
||||
List<IMouseCaptureArea> mouseListeners = getAll(IMouseCaptureArea.class);
|
||||
mouseListeners.sort(new Comparator<IMouseCaptureArea>() {
|
||||
@Override
|
||||
public int compare(IMouseCaptureArea a, IMouseCaptureArea b) {
|
||||
int al = a.getLayer();
|
||||
int bl = b.getLayer();
|
||||
return al < bl ? 1 : bl < al ? -1 : 0;
|
||||
}
|
||||
});
|
||||
for(IMouseCaptureArea listener : mouseListeners) {
|
||||
boolean currentlyEntered = listener.getBox().contains(App.mouseX, App.mouseY);
|
||||
if(currentlyEntered) {
|
||||
if(listener != hoveredMouseListener) {
|
||||
if(hoveredMouseListener != null) {
|
||||
hoveredMouseListener.mouseLeave();
|
||||
}
|
||||
hoveredMouseListener = listener;
|
||||
listener.mouseEnter();
|
||||
}
|
||||
break;
|
||||
} else if(listener == hoveredMouseListener) {
|
||||
// this is the one that is currently hovered, but it isnt!
|
||||
// turn that shit OFF
|
||||
hoveredMouseListener.mouseLeave();
|
||||
hoveredMouseListener = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
this.construct();
|
||||
|
||||
for(GameObject obj : objects) {
|
||||
obj.link(this);
|
||||
}
|
||||
|
||||
for(GameObject obj : objects) {
|
||||
obj.start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
for(GameObject obj : objects) {
|
||||
obj.render();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void construct();
|
||||
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
objects.clear();
|
||||
}
|
||||
|
||||
public void add(GameObject obj) {
|
||||
newObjects.add(obj);
|
||||
obj.link(this);
|
||||
}
|
||||
|
||||
public void remove(GameObject obj) {
|
||||
removeObjects.add(obj);
|
||||
}
|
||||
|
||||
public boolean inScene(GameObject gameObject) {
|
||||
return objects.contains(gameObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDown(int button) {
|
||||
for(IMouseCaptureArea iml : getAll(IMouseCaptureArea.class)) {
|
||||
iml.mouseDown(button);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseUp(int button) {
|
||||
for(IMouseCaptureArea iml : getAll(IMouseCaptureArea.class)) {
|
||||
iml.mouseUp(button);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,13 +8,13 @@ import xyz.valnet.engine.graphics.Drawing;
|
|||
import xyz.valnet.engine.math.Vector2f;
|
||||
import xyz.valnet.engine.math.Vector4f;
|
||||
import xyz.valnet.engine.scenegraph.GameObject;
|
||||
import xyz.valnet.engine.scenegraph.IMouseCaptureArea;
|
||||
import xyz.valnet.hadean.Layers;
|
||||
import xyz.valnet.hadean.input.IMouseListener;
|
||||
import xyz.valnet.hadean.util.Assets;
|
||||
|
||||
import static xyz.valnet.engine.util.Math.lerp;
|
||||
|
||||
public class Selection extends GameObject implements IMouseListener {
|
||||
public class Selection extends GameObject implements IMouseCaptureArea {
|
||||
|
||||
public Vector2f initialCoords;
|
||||
private Camera camera;
|
||||
|
|
|
|||
|
|
@ -6,15 +6,15 @@ import java.util.List;
|
|||
|
||||
import xyz.valnet.engine.math.Vector4f;
|
||||
import xyz.valnet.engine.scenegraph.GameObject;
|
||||
import xyz.valnet.engine.scenegraph.IMouseCaptureArea;
|
||||
import xyz.valnet.hadean.Layers;
|
||||
import xyz.valnet.hadean.input.Button;
|
||||
import xyz.valnet.hadean.input.IButtonListener;
|
||||
import xyz.valnet.hadean.input.IMouseListener;
|
||||
import xyz.valnet.hadean.input.SimpleButton;
|
||||
import xyz.valnet.hadean.util.Action;
|
||||
import xyz.valnet.hadean.util.Assets;
|
||||
|
||||
public class SelectionUI extends GameObject implements ISelectionChangeListener, IButtonListener, IMouseListener {
|
||||
public class SelectionUI extends GameObject implements ISelectionChangeListener, IButtonListener, IMouseCaptureArea {
|
||||
|
||||
private String name = "";
|
||||
private int count = 0;
|
||||
|
|
|
|||
|
|
@ -7,9 +7,10 @@ import xyz.valnet.engine.graphics.Tile9;
|
|||
import xyz.valnet.engine.math.Vector4f;
|
||||
import xyz.valnet.engine.math.Vector4i;
|
||||
import xyz.valnet.engine.scenegraph.GameObject;
|
||||
import xyz.valnet.engine.scenegraph.IMouseCaptureArea;
|
||||
import xyz.valnet.hadean.util.Assets;
|
||||
|
||||
public class Button extends GameObject implements IMouseListener {
|
||||
public class Button extends GameObject implements IMouseCaptureArea {
|
||||
|
||||
private int x, y, width, height;
|
||||
private String text;
|
||||
|
|
|
|||
|
|
@ -3,9 +3,10 @@ package xyz.valnet.hadean.input;
|
|||
import xyz.valnet.engine.graphics.Drawing;
|
||||
import xyz.valnet.engine.math.Vector4f;
|
||||
import xyz.valnet.engine.scenegraph.GameObject;
|
||||
import xyz.valnet.engine.scenegraph.IMouseCaptureArea;
|
||||
import xyz.valnet.hadean.util.Assets;
|
||||
|
||||
public class GOButton extends GameObject implements IMouseListener {
|
||||
public class GOButton extends GameObject implements IMouseCaptureArea {
|
||||
|
||||
private boolean hovered = false;
|
||||
public int layer = 1;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ import java.util.List;
|
|||
|
||||
import xyz.valnet.engine.App;
|
||||
import xyz.valnet.engine.scenegraph.GameObject;
|
||||
import xyz.valnet.engine.scenegraph.IMouseCaptureArea;
|
||||
import xyz.valnet.engine.scenegraph.IScene;
|
||||
import xyz.valnet.engine.scenegraph.SceneGraph;
|
||||
import xyz.valnet.hadean.gameobjects.BottomBar;
|
||||
import xyz.valnet.hadean.gameobjects.Camera;
|
||||
import xyz.valnet.hadean.gameobjects.Pawn;
|
||||
|
|
@ -15,7 +17,6 @@ import xyz.valnet.hadean.gameobjects.SelectionUI;
|
|||
import xyz.valnet.hadean.gameobjects.Terrain;
|
||||
import xyz.valnet.hadean.gameobjects.tabs.ArchitectTab;
|
||||
import xyz.valnet.hadean.gameobjects.tabs.MenuTab;
|
||||
import xyz.valnet.hadean.input.IMouseListener;
|
||||
|
||||
// TODO BIG IDEAS
|
||||
// have caches of types that ill need (Like IMouseListener)
|
||||
|
|
@ -26,106 +27,18 @@ import xyz.valnet.hadean.input.IMouseListener;
|
|||
// IE: all objects will be linked and able to be `get`ed
|
||||
// at their start call! :D
|
||||
|
||||
public class GameScene implements IScene {
|
||||
public class GameScene extends SceneGraph {
|
||||
|
||||
// generic
|
||||
private List<GameObject> objects = new ArrayList<GameObject>();
|
||||
private List<GameObject> newObjects = new ArrayList<GameObject>();
|
||||
private List<GameObject> removeObjects = new ArrayList<GameObject>();
|
||||
// private List<IRenderable> renderables = new ArrayList<IRenderable>();
|
||||
|
||||
private IMouseListener hoveredMouseListener = null;
|
||||
|
||||
// specific
|
||||
|
||||
public <T> T get(Class<T> clazz) {
|
||||
for(GameObject obj : objects) {
|
||||
if(clazz.isInstance(obj)) {
|
||||
return clazz.cast(obj);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> List<T> getAll(Class<T> clazz) {
|
||||
List<T> stuff = new ArrayList<T>();
|
||||
for(GameObject obj : objects) {
|
||||
if(clazz.isInstance(obj)) {
|
||||
stuff.add(clazz.cast(obj));
|
||||
}
|
||||
}
|
||||
return stuff;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
for(GameObject obj : objects) {
|
||||
obj.render();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dTime) {
|
||||
// ADD OBJECTS
|
||||
if(!newObjects.isEmpty()) {
|
||||
List<GameObject> added = new ArrayList<GameObject>();
|
||||
|
||||
for(GameObject obj : newObjects) {
|
||||
objects.add(obj);
|
||||
added.add(obj);
|
||||
}
|
||||
newObjects.clear();
|
||||
|
||||
for(GameObject obj : added) {
|
||||
obj.start();
|
||||
}
|
||||
}
|
||||
|
||||
// REMOVE OBJECTS
|
||||
if(!removeObjects.isEmpty()) {
|
||||
for(GameObject obj : removeObjects) {
|
||||
objects.remove(obj);
|
||||
}
|
||||
removeObjects.clear();
|
||||
}
|
||||
|
||||
// TICK OBJECTS
|
||||
for(GameObject obj : objects) {
|
||||
obj.update(dTime);
|
||||
}
|
||||
|
||||
// DO MOUSE UPDATES!
|
||||
List<IMouseListener> mouseListeners = getAll(IMouseListener.class);
|
||||
mouseListeners.sort(new Comparator<IMouseListener>() {
|
||||
@Override
|
||||
public int compare(IMouseListener a, IMouseListener b) {
|
||||
int al = a.getLayer();
|
||||
int bl = b.getLayer();
|
||||
return al < bl ? 1 : bl < al ? -1 : 0;
|
||||
}
|
||||
});
|
||||
for(IMouseListener listener : mouseListeners) {
|
||||
boolean currentlyEntered = listener.getBox().contains(App.mouseX, App.mouseY);
|
||||
if(currentlyEntered) {
|
||||
if(listener != hoveredMouseListener) {
|
||||
if(hoveredMouseListener != null) {
|
||||
hoveredMouseListener.mouseLeave();
|
||||
}
|
||||
hoveredMouseListener = listener;
|
||||
listener.mouseEnter();
|
||||
}
|
||||
break;
|
||||
} else if(listener == hoveredMouseListener) {
|
||||
// this is the one that is currently hovered, but it isnt!
|
||||
// turn that shit OFF
|
||||
hoveredMouseListener.mouseLeave();
|
||||
hoveredMouseListener = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
protected void construct() {
|
||||
objects.add(new Terrain());
|
||||
for(int i = 0; i < 5; i ++) {
|
||||
objects.add(new Pawn());
|
||||
|
|
@ -138,45 +51,7 @@ public class GameScene implements IScene {
|
|||
objects.add(new ArchitectTab());
|
||||
objects.add(new MenuTab());
|
||||
|
||||
for(GameObject obj : objects) {
|
||||
obj.link(this);
|
||||
}
|
||||
|
||||
for(GameObject obj : objects) {
|
||||
obj.start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
objects.clear();
|
||||
}
|
||||
|
||||
public void add(GameObject obj) {
|
||||
newObjects.add(obj);
|
||||
obj.link(this);
|
||||
}
|
||||
|
||||
public void remove(GameObject obj) {
|
||||
removeObjects.add(obj);
|
||||
}
|
||||
|
||||
public boolean inScene(GameObject gameObject) {
|
||||
return objects.contains(gameObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDown(int button) {
|
||||
for(IMouseListener iml : getAll(IMouseListener.class)) {
|
||||
iml.mouseDown(button);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseUp(int button) {
|
||||
for(IMouseListener iml : getAll(IMouseListener.class)) {
|
||||
iml.mouseUp(button);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue