From 99dbf13dd18a96a184f863b8de28bef46d7fe9ee Mon Sep 17 00:00:00 2001 From: austin Date: Wed, 14 May 2025 19:57:30 -0400 Subject: [PATCH] grid-aligned entity work --- docs/src/progress/renderertodo.md | 1 + .../client/interact/ItemActions.java | 30 +++++++++++++++---- .../controls/cursor/CursorState.java | 25 +++++++++++++++- .../types/common/CommonEntityUtils.java | 5 +++- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 104e8eca..6c5cdf02 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1779,6 +1779,7 @@ Fix engine not closing when X button is hit on main menu Sorting imgui debug windows Grid alignment cursor Furniture spawner items triggers grid alignment cursor +Grid aligned entity work diff --git a/src/main/java/electrosphere/client/interact/ItemActions.java b/src/main/java/electrosphere/client/interact/ItemActions.java index 8500a8f0..fd646203 100644 --- a/src/main/java/electrosphere/client/interact/ItemActions.java +++ b/src/main/java/electrosphere/client/interact/ItemActions.java @@ -53,7 +53,10 @@ public class ItemActions { public static void attemptPrimaryItemAction(){ Vector3d eyePos = new Vector3d(CameraEntityUtils.getCameraEye(Globals.playerCamera)); Vector3d centerPos = new Vector3d(CameraEntityUtils.getCameraCenter(Globals.playerCamera)); - Vector3d cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + Vector3d cursorPos = Globals.cursorState.getCursorPosition(); + if(cursorPos == null){ + cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + } if(cursorPos == null){ cursorPos = new Vector3d(centerPos).add(new Vector3d(eyePos).normalize().mul(-CollisionEngine.DEFAULT_INTERACT_DISTANCE)); } @@ -110,7 +113,10 @@ public class ItemActions { public static void repeatPrimaryItemAction(){ Vector3d eyePos = new Vector3d(CameraEntityUtils.getCameraEye(Globals.playerCamera)); Vector3d centerPos = new Vector3d(CameraEntityUtils.getCameraCenter(Globals.playerCamera)); - Vector3d cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + Vector3d cursorPos = Globals.cursorState.getCursorPosition(); + if(cursorPos == null){ + cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + } if(cursorPos == null){ cursorPos = new Vector3d(centerPos).add(new Vector3d(eyePos).normalize().mul(-CollisionEngine.DEFAULT_INTERACT_DISTANCE)); } @@ -132,7 +138,10 @@ public class ItemActions { public static void releasePrimaryItemAction(){ Vector3d eyePos = new Vector3d(CameraEntityUtils.getCameraEye(Globals.playerCamera)); Vector3d centerPos = new Vector3d(CameraEntityUtils.getCameraCenter(Globals.playerCamera)); - Vector3d cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + Vector3d cursorPos = Globals.cursorState.getCursorPosition(); + if(cursorPos == null){ + cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + } if(cursorPos == null){ cursorPos = new Vector3d(centerPos).add(new Vector3d(eyePos).normalize().mul(-CollisionEngine.DEFAULT_INTERACT_DISTANCE)); } @@ -162,7 +171,10 @@ public class ItemActions { public static void attemptSecondaryItemAction(){ Vector3d eyePos = new Vector3d(CameraEntityUtils.getCameraEye(Globals.playerCamera)); Vector3d centerPos = new Vector3d(CameraEntityUtils.getCameraCenter(Globals.playerCamera)); - Vector3d cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + Vector3d cursorPos = Globals.cursorState.getCursorPosition(); + if(cursorPos == null){ + cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + } if(cursorPos == null){ cursorPos = new Vector3d(centerPos).add(new Vector3d(eyePos).normalize().mul(-CollisionEngine.DEFAULT_INTERACT_DISTANCE)); } @@ -211,7 +223,10 @@ public class ItemActions { public static void repeatSecondaryItemAction(){ Vector3d eyePos = new Vector3d(CameraEntityUtils.getCameraEye(Globals.playerCamera)); Vector3d centerPos = new Vector3d(CameraEntityUtils.getCameraCenter(Globals.playerCamera)); - Vector3d cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + Vector3d cursorPos = Globals.cursorState.getCursorPosition(); + if(cursorPos == null){ + cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + } if(cursorPos == null){ cursorPos = new Vector3d(centerPos).add(new Vector3d(eyePos).normalize().mul(-CollisionEngine.DEFAULT_INTERACT_DISTANCE)); } @@ -260,7 +275,10 @@ public class ItemActions { public static void releaseSecondaryItemAction(){ Vector3d eyePos = new Vector3d(CameraEntityUtils.getCameraEye(Globals.playerCamera)); Vector3d centerPos = new Vector3d(CameraEntityUtils.getCameraCenter(Globals.playerCamera)); - Vector3d cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + Vector3d cursorPos = Globals.cursorState.getCursorPosition(); + if(cursorPos == null){ + cursorPos = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), CollisionEngine.DEFAULT_INTERACT_DISTANCE); + } if(cursorPos == null){ cursorPos = new Vector3d(centerPos).add(new Vector3d(eyePos).normalize().mul(-CollisionEngine.DEFAULT_INTERACT_DISTANCE)); } diff --git a/src/main/java/electrosphere/controls/cursor/CursorState.java b/src/main/java/electrosphere/controls/cursor/CursorState.java index f1affe7d..65f667f6 100644 --- a/src/main/java/electrosphere/controls/cursor/CursorState.java +++ b/src/main/java/electrosphere/controls/cursor/CursorState.java @@ -346,7 +346,7 @@ public class CursorState { * @param position The position * @param data The data */ - private static void nudgeGridAlignment(Vector3d position, GridAlignedData data){ + public static void nudgeGridAlignment(Vector3d position, GridAlignedData data){ if(data.getWidth() / 2 == 1){ position.x = position.x + BlockChunkData.BLOCK_SIZE_MULTIPLIER / 2.0f; } @@ -602,4 +602,27 @@ public class CursorState { this.gridAlignmentData = gridAlignedData; } + /** + * Gets the position of the currently visible cursor + * @return The position if a cursor is visible, null otherwise + */ + public Vector3d getCursorPosition(){ + if(Globals.clientSceneWrapper.getScene().getEntitiesWithTag(EntityTags.DRAWABLE).contains(Globals.playerCursor)){ + return EntityUtils.getPosition(Globals.playerCursor); + } + if(Globals.clientSceneWrapper.getScene().getEntitiesWithTag(EntityTags.DRAWABLE).contains(Globals.playerBlockCursor)){ + return EntityUtils.getPosition(Globals.playerBlockCursor); + } + if(Globals.clientSceneWrapper.getScene().getEntitiesWithTag(EntityTags.DRAWABLE).contains(Globals.playerAreaCursor)){ + return EntityUtils.getPosition(Globals.playerAreaCursor); + } + if(Globals.clientSceneWrapper.getScene().getEntitiesWithTag(EntityTags.DRAWABLE).contains(CursorState.playerFabCursor)){ + return EntityUtils.getPosition(CursorState.playerFabCursor); + } + if(Globals.clientSceneWrapper.getScene().getEntitiesWithTag(EntityTags.DRAWABLE).contains(CursorState.playerGridAlignedCursor)){ + return EntityUtils.getPosition(CursorState.playerGridAlignedCursor); + } + return null; + } + } diff --git a/src/main/java/electrosphere/entity/types/common/CommonEntityUtils.java b/src/main/java/electrosphere/entity/types/common/CommonEntityUtils.java index a59ace86..b91a8839 100644 --- a/src/main/java/electrosphere/entity/types/common/CommonEntityUtils.java +++ b/src/main/java/electrosphere/entity/types/common/CommonEntityUtils.java @@ -12,6 +12,7 @@ import org.ode4j.ode.DBody; import electrosphere.client.interact.ClientInteractionEngine; import electrosphere.collision.PhysicsEntityUtils; import electrosphere.collision.collidable.Collidable; +import electrosphere.controls.cursor.CursorState; import electrosphere.data.collidable.CollidableTemplate; import electrosphere.data.common.CommonEntityType; import electrosphere.data.creature.CreatureData; @@ -744,7 +745,9 @@ public class CommonEntityUtils { //grid alignment if(rawType.getGridAlignedData() != null){ - position.set(realm.getServerWorldData().clampRealToBlock(position)); + Vector3d placementPos = realm.getServerWorldData().clampRealToBlock(position); + CursorState.nudgeGridAlignment(placementPos,rawType.getGridAlignedData()); + position.set(placementPos); Globals.clientScene.registerEntityToTag(entity, EntityTags.BLOCK_OCCUPANT); //TODO: must register with all nearby scenes as well because it could possibly occupy other chunks }