better dragging build thing

pull/1/head
valyrie97 2022-12-23 17:06:25 -05:00
parent fe95f750c2
commit f576907ec1
8 changed files with 75 additions and 24 deletions

View File

@ -28,7 +28,7 @@ public class HadeanGame extends Game {
Drawing.setLayer(0); Drawing.setLayer(0);
super.render(); super.render();
Drawing.setLayer(99); Drawing.setLayer(99);
renderDebugInfo(); // renderDebugInfo();
} }
private Runtime runtime = Runtime.getRuntime(); private Runtime runtime = Runtime.getRuntime();

View File

@ -3,6 +3,7 @@ package xyz.valnet.hadean.gameobjects;
import xyz.valnet.engine.graphics.Drawing; import xyz.valnet.engine.graphics.Drawing;
import xyz.valnet.engine.graphics.Sprite; import xyz.valnet.engine.graphics.Sprite;
import xyz.valnet.engine.math.Vector2f; import xyz.valnet.engine.math.Vector2f;
import xyz.valnet.engine.math.Vector2i;
import xyz.valnet.engine.math.Vector4f; import xyz.valnet.engine.math.Vector4f;
import xyz.valnet.engine.scenegraph.GameObject; import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.hadean.interfaces.IWorldBoundsAdapter; import xyz.valnet.hadean.interfaces.IWorldBoundsAdapter;
@ -38,6 +39,10 @@ public class Camera extends GameObject {
return new Vector2f((x - screenWidth / 2 + focus.x * tileWidth) / tileWidth, (y - screenHeight / 2 + focus.y * tileWidth) / tileWidth); return new Vector2f((x - screenWidth / 2 + focus.x * tileWidth) / tileWidth, (y - screenHeight / 2 + focus.y * tileWidth) / tileWidth);
} }
public Vector2i screen2worldI(float x, float y) {
return new Vector2i((int)Math.floor((x - screenWidth / 2 + focus.x * tileWidth) / tileWidth), (int)Math.floor((y - screenHeight / 2 + focus.y * tileWidth) / tileWidth));
}
@Deprecated @Deprecated
public void draw(Sprite sprite, float x, float y) { public void draw(Sprite sprite, float x, float y) {
Vector2f screenPos = world2screen(x, y); Vector2f screenPos = world2screen(x, y);

View File

@ -2,6 +2,7 @@ package xyz.valnet.hadean.gameobjects.inputlayer;
import xyz.valnet.engine.App; import xyz.valnet.engine.App;
import xyz.valnet.engine.math.Vector2f; import xyz.valnet.engine.math.Vector2f;
import xyz.valnet.engine.math.Vector2i;
import xyz.valnet.engine.math.Vector4f; import xyz.valnet.engine.math.Vector4f;
import xyz.valnet.engine.scenegraph.GameObject; import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.engine.scenegraph.IMouseCaptureArea; import xyz.valnet.engine.scenegraph.IMouseCaptureArea;
@ -46,7 +47,12 @@ public class BuildLayer extends GameObject implements IMouseCaptureArea {
} }
private void broadcastWorldCoords() { private void broadcastWorldCoords() {
Vector2f worldcoords = camera.screen2world(App.mouseX, App.mouseY); Vector2i worldcoords = camera.screen2worldI(App.mouseX, App.mouseY);
if(mouseDown) {
Vector2i[] ords = orderCoords(new Vector2i(x, y), worldcoords);
listener.update(ords[0].x, ords[0].y, ords[2].x + 1, ords[2].y + 1);
return;
}
listener.update(worldcoords.x, worldcoords.y, 1, 1); listener.update(worldcoords.x, worldcoords.y, 1, 1);
} }
@ -65,7 +71,7 @@ public class BuildLayer extends GameObject implements IMouseCaptureArea {
hovered = false; hovered = false;
} }
private float x, y; private int x, y;
private int screenX, screenY; private int screenX, screenY;
private boolean mouseDown = false; private boolean mouseDown = false;
@ -75,7 +81,7 @@ public class BuildLayer extends GameObject implements IMouseCaptureArea {
listener.cancel(); listener.cancel();
deactiveate(); deactiveate();
} else if(button == 0 && active && hovered) { } else if(button == 0 && active && hovered) {
Vector2f worldcoords = camera.screen2world(App.mouseX, App.mouseY); Vector2i worldcoords = camera.screen2worldI(App.mouseX, App.mouseY);
mouseDown = true; mouseDown = true;
screenX = App.mouseX; screenX = App.mouseX;
screenY = App.mouseY; screenY = App.mouseY;
@ -87,20 +93,30 @@ public class BuildLayer extends GameObject implements IMouseCaptureArea {
@Override @Override
public void mouseUp(int button) { public void mouseUp(int button) {
if(button == 0 && active && mouseDown) { if(button == 0 && active && mouseDown) {
Vector2f worldcoords = camera.screen2world(App.mouseX, App.mouseY); Vector2i worldcoords = camera.screen2worldI(App.mouseX, App.mouseY);
mouseDown = false; mouseDown = false;
float x1 = x; int x1 = x;
float y1 = y; int y1 = y;
float x2 = worldcoords.x; int x2 = worldcoords.x;
float y2 = worldcoords.y; int y2 = worldcoords.y;
float minX = Math.min(x1, x2); int minX = Math.min(x1, x2);
float minY = Math.min(y1, y2); int minY = Math.min(y1, y2);
float maxX = Math.max(x1, x2); int maxX = Math.max(x1, x2);
float maxY = Math.max(y1, y2); int maxY = Math.max(y1, y2);
listener.select(minX, minY, maxX, maxY); listener.select(minX, minY, maxX, maxY);
} }
} }
private Vector2i[] orderCoords(Vector2i a, Vector2i b) {
return new Vector2i[] {
new Vector2i(Math.min(a.x, b.x), Math.min(a.y, b.y)),
new Vector2i(Math.max(a.x, b.x), Math.max(a.y, b.y)),
new Vector2i(Math.max(a.x, b.x) - Math.min(a.x, b.x), Math.max(a.y, b.y) - Math.min(a.y, b.y))
};
}
@Override @Override
public Vector4f getBox() { public Vector4f getBox() {
return active ? new Vector4f(0, 0, 1024, 576) : Vector4f.zero; return active ? new Vector4f(0, 0, 1024, 576) : Vector4f.zero;

View File

@ -36,6 +36,7 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
private int padding = 10; private int padding = 10;
private int x, y; private int x, y;
private int w, h;
@Override @Override
public void render() { public void render() {
@ -46,7 +47,9 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
if(opened.value()) { if(opened.value()) {
// draw the currently selected build item // draw the currently selected build item
Assets.flat.pushColor(new Vector4f(1f, 1f, 1f, 0.8f)); Assets.flat.pushColor(new Vector4f(1f, 1f, 1f, 0.8f));
camera.draw(Layers.BUILD_INTERACTABLE, Assets.stockpile, x, y); for(int i = 0; i < w; i ++) for(int j = 0; j < h; j ++) {{
camera.draw(Layers.BUILD_INTERACTABLE, Assets.stockpile, x + i, y + j);
}}
Assets.flat.popColor(); Assets.flat.popColor();
} }
} }
@ -61,17 +64,19 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
IBuildLayerListener buildListener = new IBuildLayerListener() { IBuildLayerListener buildListener = new IBuildLayerListener() {
@Override @Override
public void update(float nx, float ny, float nw, float nh) { public void update(int nx, int ny, int nw, int nh) {
x = (int)Math.floor(nx); x = nx;
y = (int)Math.floor(ny); y = ny;
w = nw;
h = nh;
} }
@Override @Override
public void select(float x1, float y1, float x2, float y2) { public void select(int x1, int y1, int x2, int y2) {
int ix1 = (int)Math.floor(x1); int ix1 = x1;
int iy1 = (int)Math.floor(y1); int iy1 = y1;
int ix2 = (int)Math.floor(x2); int ix2 = x2;
int iy2 = (int)Math.floor(y2); int iy2 = y2;
for(int x = ix1; x <= ix2; x ++) { for(int x = ix1; x <= ix2; x ++) {
for(int y = iy1; y <= iy2; y ++) { for(int y = iy1; y <= iy2; y ++) {
ITileThing thing = new FarmPlot(x, y); ITileThing thing = new FarmPlot(x, y);

View File

@ -0,0 +1,5 @@
package xyz.valnet.hadean.gameobjects.worldobjects;
public class Item {
}

View File

@ -1,5 +1,6 @@
package xyz.valnet.hadean.gameobjects.worldobjects; package xyz.valnet.hadean.gameobjects.worldobjects;
import xyz.valnet.engine.math.Vector2f;
import xyz.valnet.engine.math.Vector4f; import xyz.valnet.engine.math.Vector4f;
import xyz.valnet.hadean.gameobjects.JobBoard; import xyz.valnet.hadean.gameobjects.JobBoard;
import xyz.valnet.hadean.interfaces.ISelectable; import xyz.valnet.hadean.interfaces.ISelectable;
@ -41,6 +42,11 @@ public class Rice extends WorldObject implements ITileThing, ISelectable {
@Override @Override
public void render() { public void render() {
camera.draw(Layers.AIR, Assets.riceBag, x, y); camera.draw(Layers.AIR, Assets.riceBag, x, y);
Assets.flat.pushColor(Vector4f.black);
Vector2f screeCoords = camera.world2screen(x, y);
Assets.miniFont.drawString("123", (int)screeCoords.x, (int)screeCoords.y);
Assets.flat.popColor();
} }
@Override @Override

View File

@ -1,7 +1,7 @@
package xyz.valnet.hadean.interfaces; package xyz.valnet.hadean.interfaces;
public interface IBuildLayerListener { public interface IBuildLayerListener {
public void update(float x, float y, float w, float h); public void update(int x, int y, int w, int h);
public void select(float x, float y, float w, float h); public void select(int x, int y, int w, int h);
public void cancel(); public void cancel();
} }

View File

@ -14,6 +14,7 @@ public class Assets {
public static final Texture atlas; public static final Texture atlas;
public static final Font font; public static final Font font;
public static final Font miniFont;
public static final Tile9 redFrame; public static final Tile9 redFrame;
public static final Tile9 frame; public static final Tile9 frame;
public static final Tile9 fireFrame; public static final Tile9 fireFrame;
@ -165,6 +166,19 @@ public class Assets {
charset.put('|', new Sprite(atlas, new Vector4i(224, 32, 8, 16))); charset.put('|', new Sprite(atlas, new Vector4i(224, 32, 8, 16)));
font = new Font(charset, 8, 16); font = new Font(charset, 8, 16);
Map<Character, Sprite> miniCharset = new HashMap<Character, Sprite>();
miniCharset.put('1', new Sprite(atlas, new Vector4i(0, 112, 4, 5)));
miniCharset.put('2', new Sprite(atlas, new Vector4i(4, 112, 4, 5)));
miniCharset.put('3', new Sprite(atlas, new Vector4i(8, 112, 4, 5)));
miniCharset.put('4', new Sprite(atlas, new Vector4i(12, 112, 4, 5)));
miniCharset.put('5', new Sprite(atlas, new Vector4i(16, 112, 4, 5)));
miniCharset.put('6', new Sprite(atlas, new Vector4i(20, 112, 4, 5)));
miniCharset.put('7', new Sprite(atlas, new Vector4i(24, 112, 4, 5)));
miniCharset.put('8', new Sprite(atlas, new Vector4i(28, 112, 4, 5)));
miniCharset.put('9', new Sprite(atlas, new Vector4i(32, 112, 4, 5)));
miniCharset.put('0', new Sprite(atlas, new Vector4i(36, 112, 4, 5)));
miniFont = new Font(miniCharset, 4, 5);
frame = new Tile9( frame = new Tile9(
new Sprite(atlas, new Vector4i(24, 88, 8, 8)), new Sprite(atlas, new Vector4i(24, 88, 8, 8)),
new Sprite(atlas, new Vector4i(32, 88, 8, 8)), new Sprite(atlas, new Vector4i(32, 88, 8, 8)),