Compare commits

...

2 Commits

Author SHA1 Message Date
Ivory dd775f6d9c dynamic window size, not resizable. 2023-07-01 06:57:43 -04:00
Ivory 634f48d941 pathfinding bug fixed 2023-07-01 06:10:16 -04:00
21 changed files with 92 additions and 51 deletions

View File

@ -25,7 +25,8 @@ public class App {
// The window handle // The window handle
private long window; private long window;
private int width = 1024, height = 576; // private int width = 1024, height = 576;
private int width = 1600, height = 900;
private Matrix4f matrix = Matrix4f.orthographic(0, width, height, 0, 0, 100); private Matrix4f matrix = Matrix4f.orthographic(0, width, height, 0, 0, 100);
public static int mouseX, mouseY; public static int mouseX, mouseY;
@ -69,7 +70,7 @@ public class App {
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // the window will be resizable glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // the window will be resizable
// Create the window // Create the window
window = glfwCreateWindow(width, height, "Hello World!", NULL, NULL); window = glfwCreateWindow(width, height, "Val Engine", NULL, NULL);
if ( window == NULL ) if ( window == NULL )
throw new RuntimeException("Failed to create the GLFW window"); throw new RuntimeException("Failed to create the GLFW window");
@ -94,11 +95,6 @@ public class App {
game.scrollUp(); game.scrollUp();
else if(yOffset < 0) else if(yOffset < 0)
game.scrollDown(); game.scrollDown();
// if(yOffset > 0)
// game.scrollLeft();
// else if(yOffset < 0)
// game.scrollRight();
}); });
glfwSetMouseButtonCallback(window, (long window, int button, int action, int mods) -> { glfwSetMouseButtonCallback(window, (long window, int button, int action, int mods) -> {
@ -179,7 +175,7 @@ public class App {
glDepthMask(true); glDepthMask(true);
glfwSwapInterval(1); glfwSwapInterval(1);
game.resize(width, height);
game.start(); game.start();
} }

View File

@ -3,6 +3,7 @@ package xyz.valnet.engine;
import static xyz.valnet.engine.util.Math.lerp; import static xyz.valnet.engine.util.Math.lerp;
import xyz.valnet.engine.math.Matrix4f; import xyz.valnet.engine.math.Matrix4f;
import xyz.valnet.engine.math.Vector2i;
import xyz.valnet.engine.scenegraph.IScene; import xyz.valnet.engine.scenegraph.IScene;
public abstract class Game { public abstract class Game {
@ -14,6 +15,8 @@ public abstract class Game {
private int framesSinceKeyframe = 0; private int framesSinceKeyframe = 0;
private long lastFrame = System.nanoTime(); private long lastFrame = System.nanoTime();
private long lastKeyframe = System.nanoTime(); private long lastKeyframe = System.nanoTime();
private Vector2i bufferDimensions = new Vector2i(0, 0);
public abstract void start(); public abstract void start();
@ -88,4 +91,12 @@ public abstract class Game {
public final void keyRepeat(int key) { public final void keyRepeat(int key) {
scene.keyRepeat(key); scene.keyRepeat(key);
} }
public Vector2i getBufferDimensions() {
return bufferDimensions;
}
public void resize(int width, int height) {
bufferDimensions = new Vector2i(width, height);
}
} }

View File

@ -25,8 +25,9 @@ public abstract class ImmediateUI extends GameObject implements IMouseCaptureAre
private boolean mouseDown; private boolean mouseDown;
public void render() { public void render() {
Vector2i screenSize = getBufferDimensions();
begin(); begin();
gui(); gui(screenSize.x, screenSize.y);
end(); end();
} }
@ -55,7 +56,7 @@ public abstract class ImmediateUI extends GameObject implements IMouseCaptureAre
return Layers.GENERAL_UI_INTERACTABLE; return Layers.GENERAL_UI_INTERACTABLE;
} }
protected abstract void gui(); protected abstract void gui(int screenWidth, int screenHeight);
private record StackingContext( private record StackingContext(
boolean fixedSize, boolean fixedSize,

View File

@ -4,7 +4,7 @@ import java.io.Serializable;
public class Vector2i implements Serializable { public class Vector2i implements Serializable {
public int x, y; public final int x, y;
public static Vector2i one = new Vector2i(1, 1); public static Vector2i one = new Vector2i(1, 1);
public static Vector2i zero = new Vector2i(0, 0); public static Vector2i zero = new Vector2i(0, 0);

View File

@ -3,6 +3,7 @@ package xyz.valnet.engine.scenegraph;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import xyz.valnet.engine.math.Vector2i;
import xyz.valnet.engine.scenegraph.SceneGraph.GameObjectCallback; import xyz.valnet.engine.scenegraph.SceneGraph.GameObjectCallback;
import xyz.valnet.hadean.util.Pair; import xyz.valnet.hadean.util.Pair;
@ -101,6 +102,10 @@ public class GameObject implements IRenderable, ITickable, Serializable {
scene.registerRemoveListener(listener); scene.registerRemoveListener(listener);
} }
protected Vector2i getBufferDimensions() {
return scene.getBufferDimensions();
}
protected void beforeRemoved() {} protected void beforeRemoved() {}
protected void afterRemoved() {} protected void afterRemoved() {}

View File

@ -18,6 +18,7 @@ import java.util.stream.Collectors;
import xyz.valnet.engine.App; import xyz.valnet.engine.App;
import xyz.valnet.engine.Game; import xyz.valnet.engine.Game;
import xyz.valnet.engine.math.Box; import xyz.valnet.engine.math.Box;
import xyz.valnet.engine.math.Vector2i;
import xyz.valnet.hadean.gameobjects.ui.tabs.DebugTab; import xyz.valnet.hadean.gameobjects.ui.tabs.DebugTab;
import xyz.valnet.hadean.util.Pair; import xyz.valnet.hadean.util.Pair;
@ -374,4 +375,8 @@ public abstract class SceneGraph implements IScene {
public Pair<Float, Integer> getFPS() { public Pair<Float, Integer> getFPS() {
return new Pair<Float, Integer>(game.getAverageFPS(), game.getMeasuredFPS()); return new Pair<Float, Integer>(game.getAverageFPS(), game.getMeasuredFPS());
} }
protected Vector2i getBufferDimensions() {
return game.getBufferDimensions();
}
} }

View File

@ -25,8 +25,6 @@ import static xyz.valnet.engine.util.Math.lerp;
public class Camera extends GameObject implements ITransient, IMouseCaptureArea { public class Camera extends GameObject implements ITransient, IMouseCaptureArea {
private int tileWidth = 16; private int tileWidth = 16;
// TODO link these in some way to the real resolution. lot of work here.
private int screenWidth = 1024, screenHeight = 576;
private Vector2f focus = new Vector2f(0, 0); private Vector2f focus = new Vector2f(0, 0);
@ -90,7 +88,8 @@ public class Camera extends GameObject implements ITransient, IMouseCaptureArea
} }
public final Vector2i world2screen(float x, float y) { public final Vector2i world2screen(float x, float y) {
return new Vector2i((int)(x * tileWidth + screenWidth / 2 - focus.x * tileWidth), (int)(y * tileWidth + screenHeight / 2 - focus.y * tileWidth)); Vector2i bufferDim = getBufferDimensions();
return new Vector2i((int)(x * tileWidth + bufferDim.x / 2 - focus.x * tileWidth), (int)(y * tileWidth + bufferDim.y / 2 - focus.y * tileWidth));
} }
public final Vector2i world2screen(Vector2f pos) { public final Vector2i world2screen(Vector2f pos) {
@ -102,7 +101,8 @@ public class Camera extends GameObject implements ITransient, IMouseCaptureArea
} }
public final Vector2f screen2world(float x, float y) { public final Vector2f screen2world(float x, float y) {
return new Vector2f((x - screenWidth / 2 + focus.x * tileWidth) / tileWidth, (y - screenHeight / 2 + focus.y * tileWidth) / tileWidth); Vector2i bufferDim = getBufferDimensions();
return new Vector2f((x - bufferDim.x / 2 + focus.x * tileWidth) / tileWidth, (y - bufferDim.y / 2 + focus.y * tileWidth) / tileWidth);
} }
public final Vector2f screen2world(Vector2f pos) { public final Vector2f screen2world(Vector2f pos) {
@ -111,11 +111,12 @@ public class Camera extends GameObject implements ITransient, IMouseCaptureArea
// !! this takes an AABB and returns and AABB // !! this takes an AABB and returns and AABB
public final Vector4f world2screen(Vector4f input) { public final Vector4f world2screen(Vector4f input) {
Vector2i bufferDim = getBufferDimensions();
return new Vector4f( return new Vector4f(
input.x * tileWidth + screenWidth / 2 - focus.x * tileWidth, input.x * tileWidth + bufferDim.x / 2 - focus.x * tileWidth,
input.y * tileWidth + screenHeight / 2 - focus.y * tileWidth, input.y * tileWidth + bufferDim.y / 2 - focus.y * tileWidth,
input.z * tileWidth + screenWidth / 2 - focus.x * tileWidth, input.z * tileWidth + bufferDim.x / 2 - focus.x * tileWidth,
input.w * tileWidth + screenHeight / 2 - focus.y * tileWidth input.w * tileWidth + bufferDim.y / 2 - focus.y * tileWidth
); );
} }
@ -216,7 +217,7 @@ public class Camera extends GameObject implements ITransient, IMouseCaptureArea
@Override @Override
public void scrollUp() { public void scrollUp() {
tileWidth *= 2; tileWidth *= 2;
tileWidth = Math.min(tileWidth, 32); tileWidth = Math.min(tileWidth, 64);
} }
} }

View File

@ -30,8 +30,8 @@ public class Clock extends GameObject {
public void render() { public void render() {
Drawing.setLayer(Layers.GENERAL_UI); Drawing.setLayer(Layers.GENERAL_UI);
String str = toString(); String str = toString();
int left = 950; int left = getBufferDimensions().x - (1024 - 950);
Assets.font.drawStringOutlined(str, left, 520); Assets.font.drawStringOutlined(str, left, getBufferDimensions().y - 56);
} }
public float getSunlight() { public float getSunlight() {

View File

@ -122,7 +122,7 @@ public class BuildLayer extends GameObject implements IMouseCaptureArea, ITransi
@Override @Override
public List<Box> getGuiBoxes() { public List<Box> getGuiBoxes() {
return List.of(active ? new Box(0, 0, 1024, 576) : Box.none); return List.of(active ? new Box(0, 0, getBufferDimensions()) : Box.none);
} }
@Override @Override

View File

@ -5,6 +5,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import xyz.valnet.engine.math.Vector2i;
import xyz.valnet.engine.scenegraph.GameObject; import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.engine.scenegraph.ITransient; import xyz.valnet.engine.scenegraph.ITransient;
import xyz.valnet.hadean.input.Button; import xyz.valnet.hadean.input.Button;
@ -28,17 +29,18 @@ public class BottomBar extends GameObject implements IButtonListener, ITransient
} }
public void registerButton(IBottomBarItem newItem) { public void registerButton(IBottomBarItem newItem) {
Vector2i screen = getBufferDimensions();
clearButtons(); clearButtons();
items.add(newItem); items.add(newItem);
int n = items.size(); int n = items.size();
int i = 0; int i = 0;
for(IBottomBarItem item : items) { for(IBottomBarItem item : items) {
int l = (int)((i / (float) n) * screenWidth); int l = (int)((i / (float) n) * screen.x);
int r = (int)(((i + 1) / (float) n) * screenWidth); int r = (int)(((i + 1) / (float) n) * screen.x);
int w = r - l; int w = r - l;
Button btn = new SimpleButton(item.getTabName(), l, 576 - bottomBarHeight, w, bottomBarHeight, Layers.BOTTOM_BAR); Button btn = new SimpleButton(item.getTabName(), l, screen.y - bottomBarHeight, w, bottomBarHeight, Layers.BOTTOM_BAR);
if(item.isButtonClickSilent()) btn = btn.setClickSound(false); if(item.isButtonClickSilent()) btn = btn.setClickSound(false);
btn.registerClickListener(this); btn.registerClickListener(this);
add(btn); add(btn);

View File

@ -58,7 +58,7 @@ public class HoverQuery extends GameObject implements ITransient {
public void render() { public void render() {
if(!visible) return; if(!visible) return;
Drawing.setLayer(Layers.LOW_PRIORITY_UI); Drawing.setLayer(Layers.LOW_PRIORITY_UI);
int i = 576 - BottomBar.bottomBarHeight - 24; int i = getBufferDimensions().y - BottomBar.bottomBarHeight - 24;
for(String thingString : thingStrings) { for(String thingString : thingStrings) {
for(String str : thingString.split("\n")) { for(String str : thingString.split("\n")) {
Assets.font.drawStringOutlined(str, 8, i); Assets.font.drawStringOutlined(str, 8, i);

View File

@ -6,9 +6,17 @@ import xyz.valnet.hadean.gameobjects.ui.tabs.DebugTab;
public class Popup extends ImmediateUI { public class Popup extends ImmediateUI {
@Override @Override
protected void gui() { protected void gui(int screenWidth, int screenHeight) {
window(10, 100, 1004, 200, () -> { int desireWidth = 500;
int desiredHeight = 300;
window(
screenWidth / 2 - desireWidth / 2,
screenHeight / 2 - desiredHeight / 2,
desireWidth,
desiredHeight,
() -> {
header(" Popup Test"); header(" Popup Test");
horizontal(() -> { horizontal(() -> {

View File

@ -117,12 +117,12 @@ public class SelectionUI extends ImmediateUI implements ISelectionChangeListener
} }
@Override @Override
protected void gui() { protected void gui(int screenWidth, int screenHeight) {
// if(selected.isEmpty()) return; // if(selected.isEmpty()) return;
if(!opened && openness <= 0.0001f) return; if(!opened && openness <= 0.0001f) return;
// main window // main window
window(animate(-width - 50, 0), 576 - height - BottomBar.bottomBarHeight + 1, width, height, () -> { window(animate(-width - 50, 0), screenHeight - height - BottomBar.bottomBarHeight + 1, width, height, () -> {
if(selectedByType.size() == 1) { if(selectedByType.size() == 1) {
if(selectedCount == 1) { if(selectedCount == 1) {
text(properName); text(properName);
@ -132,7 +132,7 @@ public class SelectionUI extends ImmediateUI implements ISelectionChangeListener
if(details.length == 0) { if(details.length == 0) {
text("No details available."); text("No details available.");
} else for(Detail detail : details) { } else for(Detail detail : details) {
text(detail.toString(30)); text(detail.toString(20));
} }
}); });
} else { } else {
@ -156,7 +156,7 @@ public class SelectionUI extends ImmediateUI implements ISelectionChangeListener
}); });
// actions // actions
window(width - 1, animate(576 + 50, 576 - 48 - BottomBar.bottomBarHeight + 1), 1024 - width + 1, 48, () -> { window(width - 1, animate(screenHeight + 50, screenHeight - 48 - BottomBar.bottomBarHeight + 1), screenWidth - width + 1, 48, () -> {
if(selectedByType.size() == 1) { if(selectedByType.size() == 1) {
horizontal(() -> { horizontal(() -> {
for(Action action : actions) { for(Action action : actions) {

View File

@ -23,7 +23,7 @@ public class WorkshopOrdersUI extends ImmediateUI implements ITransient {
} }
@Override @Override
protected void gui() { protected void gui(int screenWidth, int screenHeight) {
if(shop == null) return; if(shop == null) return;
window(0, 0, 200, 300, () -> { window(0, 0, 200, 300, () -> {

View File

@ -186,12 +186,12 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IBuildLay
return "Build"; return "Build";
} }
public void gui() { public void gui(int screenWidth, int screenHeight) {
if(!shouldRender()) return; if(!shouldRender()) return;
int height = 8 + 16 + 8 + buildables.size() * (32 + 8); int height = 8 + 16 + 8 + buildables.size() * (32 + 8);
window(animate(-180, 0), 576 - BottomBar.bottomBarHeight - height + 1, 150, height, () -> { window(animate(-180, 0), screenHeight - BottomBar.bottomBarHeight - height + 1, 150, height, () -> {
text("Build"); text("Build");
for(String category : buildables.keySet()) { for(String category : buildables.keySet()) {
@ -206,7 +206,10 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IBuildLay
} }
}); });
window(149, animate(576 + 50, 576 - BottomBar.bottomBarHeight - 16 - 32 + 1 - 24), 875, 48 + 24, () -> { window(149, animate(screenHeight + 50, screenHeight - BottomBar.bottomBarHeight - 16 - 32 + 1 - 24),
screenWidth - (1024 - 875),
48 + 24,
() -> {
if(selectedCategory == null) { if(selectedCategory == null) {
space(20); space(20);
text(" Select a Category..."); text(" Select a Category...");

View File

@ -24,10 +24,10 @@ public class DebugTab extends Tab implements IKeyboardListener {
protected void onOpen() {} protected void onOpen() {}
@Override @Override
protected void gui() { protected void gui(int screenWidth, int screenHeight) {
if(!shouldRender()) return; if(!shouldRender()) return;
window(0, animate(-200, 0), 1024 - width + 1, 176, () -> { window(0, animate(-200, 0), screenWidth - width + 1, 176, () -> {
for(int i = 10; i > logs.size(); i --) { for(int i = 10; i > logs.size(); i --) {
text(" "); text(" ");
} }
@ -36,7 +36,7 @@ public class DebugTab extends Tab implements IKeyboardListener {
} }
}); });
window(animate(1050, 1024 - width), 0, width, 576 - BottomBar.bottomBarHeight + 1, () -> { window(animate(screenWidth + 30, screenWidth - width), 0, width, screenHeight - BottomBar.bottomBarHeight + 1, () -> {
text("Debug"); text("Debug");
space(8); space(8);

View File

@ -10,10 +10,10 @@ public class JobBoardTab extends Tab {
private int height = 200; private int height = 200;
@Override @Override
protected void gui() { protected void gui(int screenWidth, int screenHeight) {
if(!shouldRender()) return; if(!shouldRender()) return;
window(0, animate(576 + 50, 576 - BottomBar.bottomBarHeight - height + 1), 1024, height, () -> { window(0, animate(screenHeight + 50, screenHeight - BottomBar.bottomBarHeight - height + 1), screenWidth, height, () -> {
horizontal(() -> { horizontal(() -> {
vertical(() -> { vertical(() -> {
text("Valid"); text("Valid");

View File

@ -18,10 +18,10 @@ public class MenuTab extends Tab implements IPauser {
} }
@Override @Override
protected void gui() { protected void gui(int screenWidth, int screenHeight) {
if(!shouldRender()) return; if(!shouldRender()) return;
window(1024 / 2 - width / 2, animate(-height - 50, 576 / 2 - height / 2), width, height, () -> { window(screenWidth / 2 - width / 2, animate(-height - 50, screenHeight / 2 - height / 2), width, height, () -> {
text(" === Paused ==="); text(" === Paused ===");
space(8); space(8);
if(button("Resume")) { if(button("Resume")) {

View File

@ -111,6 +111,7 @@ public class Tree extends WorldObject implements ISelectable, IWorkable {
@Override @Override
protected void beforeRemoved() { protected void beforeRemoved() {
super.beforeRemoved();
Vector2i pos = getWorldPosition().xy(); Vector2i pos = getWorldPosition().xy();
add(new Log(pos.x, pos.y)); add(new Log(pos.x, pos.y));
} }

View File

@ -68,13 +68,14 @@ public abstract class WorldObject extends GameObject implements IWorldObject {
tile.placeThing(this); tile.placeThing(this);
} }
if(linkedTiles.size() == 0 && inScene()) { // ?? this shouldnt bew pivotal to anything?
remove(this); // if(linkedTiles.size() == 0 && inScene()) {
} // remove(this);
// }
if(linkedTiles.size() != 0 && !inScene()) { // if(linkedTiles.size() != 0 && !inScene()) {
add(this); // add(this);
} // }
} }
@ -133,4 +134,11 @@ public abstract class WorldObject extends GameObject implements IWorldObject {
return new Box(x, y, w, h); return new Box(x, y, w, h);
} }
@Override
protected void beforeRemoved() {
for(Tile tile : this.getTiles()) {
tile.removeThing(this);
}
}
} }

View File

@ -240,6 +240,6 @@ public class Pawn extends Agent {
@Override @Override
public boolean isWalkable() { public boolean isWalkable() {
// TODO thiss could be an interesting mechanic, but it may be bad // TODO thiss could be an interesting mechanic, but it may be bad
return false; return true;
} }
} }