pull/1/head
Valerie 2022-05-21 05:44:33 -04:00
parent bb19f4df72
commit dc033c0d9b
6 changed files with 120 additions and 21 deletions

View File

@ -18,11 +18,7 @@ public class Drawing {
Drawing.layer = layer;
}
public static void drawSprite(Sprite sprite, int x, int y, int w, int h) {
drawSprite(sprite, x, y, w, h, 0);
}
public static void drawSprite(Sprite sprite, int x, int y, int width, int height, float lift) {
public static void drawSprite(Sprite sprite, int x, int y, int width, int height) {
// lazy texture binding
if(bound != sprite.atlas) {
if(bound != null) bound.unbind();
@ -31,10 +27,10 @@ public class Drawing {
glBegin(GL_QUADS);
glVertexAttrib2f(SimpleShader.TEX_COORD, sprite.sourceBoxUV.x, sprite.sourceBoxUV.y);
glVertex3f(x, y, layer + height * lift);
glVertex3f(x, y, layer);
glVertexAttrib2f(SimpleShader.TEX_COORD, sprite.sourceBoxUV.x + sprite.sourceBoxUV.z, sprite.sourceBoxUV.y);
glVertex3f(x + width, y, layer + height * lift);
glVertex3f(x + width, y, layer);
glVertexAttrib2f(SimpleShader.TEX_COORD, sprite.sourceBoxUV.x + sprite.sourceBoxUV.z, sprite.sourceBoxUV.y + sprite.sourceBoxUV.w);
glVertex3f(x + width, y + height, layer);

View File

@ -1,7 +1,10 @@
package xyz.valnet.hadean.gameobjects;
import xyz.valnet.engine.math.Vector4f;
import xyz.valnet.hadean.util.Action;
public interface ISelectable {
public Vector4f getWorldBox();
public Action[] getActions();
public void runAction(Action action);
}

View File

@ -3,19 +3,21 @@ package xyz.valnet.hadean.gameobjects;
import static org.lwjgl.opengl.GL11.GL_LINES;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2f;
import static org.lwjgl.opengl.GL11.glVertex3f;
import static org.lwjgl.opengl.GL20.glVertexAttrib2f;
import static xyz.valnet.engine.util.Math.lerp;
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.shaders.SimpleShader;
import xyz.valnet.hadean.Tile;
import xyz.valnet.hadean.pathfinding.AStarPathfinder;
import xyz.valnet.hadean.pathfinding.IPathfinder;
import xyz.valnet.hadean.pathfinding.Node;
import xyz.valnet.hadean.pathfinding.Path;
import xyz.valnet.hadean.scenes.GameScene;
import xyz.valnet.hadean.util.Action;
import xyz.valnet.hadean.util.Assets;
public class Pawn extends GameObject implements ISelectable {
@ -27,12 +29,14 @@ public class Pawn extends GameObject implements ISelectable {
private Path path;
private final float speed = 70f;
private final float invocationThreshold = 70f;
private Camera camera;
private Terrain terrain;
private IPathfinder pathfinder;
private boolean debug = false;
@Override
public void start() {
camera = get(Camera.class);
@ -47,20 +51,22 @@ public class Pawn extends GameObject implements ISelectable {
if(path != null && !path.isComplete()) {
Node next = path.peek();
float t = counter / speed;
float t = counter / invocationThreshold;
camera.draw(Assets.pawn, lerp(x - 0.5f, next.x, t), lerp(y - 0.5f, next.y, t));
if(path != null) {
if(path != null && debug) {
for(Node node : path) {
glBegin(GL_LINES);
Vector2f u, v;
if(node.from == null) u = camera.world2screen(x, y);
else u = camera.world2screen(node.from.x + 0.5f, node.from.y + 0.5f);
v = camera.world2screen(node.x + 0.5f, node.y + 0.5f);
glVertex2f(u.x, u.y);
glVertex2f(v.x, v.y);
glVertexAttrib2f(SimpleShader.TEX_COORD, 0, 88 / 256f);
glVertex3f(u.x, u.y, 3f);
glVertexAttrib2f(SimpleShader.TEX_COORD, 0, 88 / 255f);
glVertex3f(v.x, v.y, 3f);
glEnd();
}
}
@ -105,7 +111,14 @@ public class Pawn extends GameObject implements ISelectable {
path = pathfinder.getPath(ix, iy, idx, idy);
}
private boolean paused = false;
private void move() {
if(paused) {
counter --;
if(counter < 0) counter = 0;
return;
}
// check if we CAN move.
Node nextNode = path.peek();
Tile nextTile = terrain.getTile(nextNode.x, nextNode.y);
@ -119,7 +132,7 @@ public class Pawn extends GameObject implements ISelectable {
}
counter ++;
if(counter < speed) return;
if(counter < invocationThreshold) return;
path.pop();
x = nextNode.x + 0.5f;
@ -130,7 +143,7 @@ public class Pawn extends GameObject implements ISelectable {
@Override
public Vector4f getWorldBox() {
if(path != null && !path.isComplete()) {
float t = counter / speed;
float t = counter / invocationThreshold;
Node n = path.peek();
float x1 = lerp(x - 0.5f, n.x, t);
float y1 = lerp(y - 0.5f, n.y, t);
@ -139,5 +152,29 @@ public class Pawn extends GameObject implements ISelectable {
return new Vector4f(x - 0.5f, y - 0.5f, x + 0.5f, y + 0.5f);
}
}
private static final Action ACTION_REROUTE = new Action("Re-route");
private static final Action ACTION_TOGGLE_DEBUG = new Action("Toggle\nDebug");
private static final Action ACTION_PAUSE = new Action("Pause");
@Override
public Action[] getActions() {
return new Action[] {
ACTION_REROUTE,
ACTION_TOGGLE_DEBUG,
ACTION_PAUSE
};
}
@Override
public void runAction(Action action) {
if(action == ACTION_PAUSE) {
paused = !paused;
} else if(action == ACTION_REROUTE) {
newPath();
} else if(action == ACTION_TOGGLE_DEBUG) {
debug = !debug;
}
}
}

View File

@ -8,6 +8,7 @@ import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.hadean.input.Button;
import xyz.valnet.hadean.input.IButtonListener;
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 {
@ -19,6 +20,10 @@ public class SelectionUI extends GameObject implements ISelectionChangeListener,
private HashMap<String, Button> narrowButtons = new HashMap<String, Button>();
private HashMap<Button, List<ISelectable>> narrowBuckets = new HashMap<Button, List<ISelectable>>();
private static final Button[] ACTIONS_BUTTONS_NULL = new Button[] {};
private Button[] actionButtons = ACTIONS_BUTTONS_NULL;
private Selection selectionManager;
// this will be null normally, but set if
@ -46,8 +51,14 @@ public class SelectionUI extends GameObject implements ISelectionChangeListener,
// i ++;
// }
if(selectedTypes.size() == 1) {
Assets.font.drawString("" + count + "x " + name, 26, 376);
for(Button btn : actionButtons) {
btn.draw();
}
} else {
for(Button btn : narrowButtons.values()) {
btn.draw();
@ -62,11 +73,20 @@ public class SelectionUI extends GameObject implements ISelectionChangeListener,
selectionManager.updateSelection(newSelection);
newSelection = null;
}
for(Button btn : narrowButtons.values()) {
btn.update();
if(selectedTypes.size() == 1) {
for(Button btn : actionButtons) {
btn.update();
}
} else {
for(Button btn : narrowButtons.values()) {
btn.update();
}
}
}
private HashMap<Button, Action> buttonActionMap = new HashMap<Button, Action>();
@Override
public void selectionChanged(List<ISelectable> selected) {
this.selected = selected;
@ -74,6 +94,8 @@ public class SelectionUI extends GameObject implements ISelectionChangeListener,
selectedTypes.clear();
narrowButtons.clear();
narrowBuckets.clear();
buttonActionMap.clear();
actionButtons = ACTIONS_BUTTONS_NULL;
for(ISelectable selectable : selected) {
String name = selectable.getClass().getName();
String[] splitName = name.split("\\.");
@ -96,13 +118,29 @@ public class SelectionUI extends GameObject implements ISelectionChangeListener,
narrowBuckets.put(btn, list);
count = 1;
this.name = shortName;
if(actionButtons == ACTIONS_BUTTONS_NULL) {
Action[] actions = selectable.getActions();
Button[] actionButtons = new Button[actions.length];
for(int i = 0; i < actions.length; i ++) {
actionButtons[i] = new SimpleButton(actions[i].name, 320 + i * 110, 466, 100, 100);
actionButtons[i].registerClickListener(this);
buttonActionMap.put(actionButtons[i], actions[i]);
}
this.actionButtons = actionButtons;
}
}
}
}
@Override
public void click(Button target) {
if(! narrowBuckets.containsKey(target)) return;
newSelection = narrowBuckets.get(target);
if(narrowBuckets.containsKey(target)) {
newSelection = narrowBuckets.get(target);
} else if(buttonActionMap.containsKey(target)) {
Action action = buttonActionMap.get(target);
for(ISelectable selectable : selected) {
selectable.runAction(action);
}
}
}
}

View File

@ -2,6 +2,7 @@ package xyz.valnet.hadean.gameobjects;
import xyz.valnet.engine.math.Vector4f;
import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.hadean.util.Action;
import xyz.valnet.hadean.util.Assets;
public class Tree extends GameObject implements ITileThing, ISelectable {
@ -32,4 +33,19 @@ public class Tree extends GameObject implements ITileThing, ISelectable {
public Vector4f getWorldBox() {
return new Vector4f(x - 1, y - 2, x + 2, y + 1);
}
public static final Action ACTION_CHOP = new Action("Chop");
@Override
public Action[] getActions() {
return new Action[] {
ACTION_CHOP
};
}
@Override
public void runAction(Action action) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,9 @@
package xyz.valnet.hadean.util;
public class Action {
public String name;
public Action(String name) {
this.name = name;
}
}