From 8e2f3571011640e3772d9d51c8c41bf6fbd20022 Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 30 Sep 2024 16:47:38 -0400 Subject: [PATCH] Editing voxels from script engine --- assets/Data/entity/items.json | 3 +- assets/Scripts/client/clienthooks.ts | 6 +++ .../types/host/client/client-voxel-utils.ts | 11 ++++++ assets/Scripts/types/host/static-classes.ts | 6 +++ docs/src/progress/renderertodo.md | 3 ++ .../client/item/ItemActions.java | 21 ++++------- .../client/script/ScriptClientVoxelUtils.java | 37 +++++++++++++++++++ .../game/data/item/type/Item.java | 19 ++++++++-- .../electrosphere/script/ScriptEngine.java | 2 + 9 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 assets/Scripts/types/host/client/client-voxel-utils.ts create mode 100644 src/main/java/electrosphere/client/script/ScriptClientVoxelUtils.java diff --git a/assets/Data/entity/items.json b/assets/Data/entity/items.json index 8727d5db..85ae59ea 100644 --- a/assets/Data/entity/items.json +++ b/assets/Data/entity/items.json @@ -341,7 +341,8 @@ "path" : "Models/basic/geometry/unitvector.glb" } }, - "clientSidePrimary": "OPEN_VOXEL", + "clientSidePrimary": "ADD_VOXEL", + "clientSideSecondary": "OPEN_VOXEL", "collidable": { "type" : "CUBE", "dimension1" : 0.1, diff --git a/assets/Scripts/client/clienthooks.ts b/assets/Scripts/client/clienthooks.ts index 1f061856..9f29ee61 100644 --- a/assets/Scripts/client/clienthooks.ts +++ b/assets/Scripts/client/clienthooks.ts @@ -10,5 +10,11 @@ export const clientHooks: Hook[] = [ callback: (engine: Engine) => { engine.classes.menuUtils.static.openVoxel() } + }, + { + signal: "ADD_VOXEL", + callback: (engine: Engine) => { + engine.classes.voxelUtils.static.applyEdit() + } } ] \ No newline at end of file diff --git a/assets/Scripts/types/host/client/client-voxel-utils.ts b/assets/Scripts/types/host/client/client-voxel-utils.ts new file mode 100644 index 00000000..d6d9e03c --- /dev/null +++ b/assets/Scripts/types/host/client/client-voxel-utils.ts @@ -0,0 +1,11 @@ +/** + * Utilities for interating with voxels on the client + */ +export interface ClientVoxelUtils { + + /** + * Applies the current voxel palette where the player's cursor is looking + */ + readonly applyEdit: () => void + +} \ No newline at end of file diff --git a/assets/Scripts/types/host/static-classes.ts b/assets/Scripts/types/host/static-classes.ts index 1ee3b9a1..e7ad3098 100644 --- a/assets/Scripts/types/host/static-classes.ts +++ b/assets/Scripts/types/host/static-classes.ts @@ -1,3 +1,4 @@ +import { ClientVoxelUtils } from "/Scripts/types/host/client/client-voxel-utils"; import { Entity } from "/Scripts/types/host/entity/entity"; import { MenuUtils } from "/Scripts/types/host/renderer/ui/menus"; import { TutorialUtils } from "/Scripts/types/host/renderer/ui/tutorial"; @@ -34,6 +35,11 @@ export interface StaticClasses { */ readonly menuUtils?: Class, + /** + * Utilities for interacting with voxels on the client + */ + readonly voxelUtils?: Class, + } /** diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index d59eafb1..8615b3ff 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -858,6 +858,9 @@ Fix attack tree checks Disable client equip tests until can review Toolbar scrolling Items executing script engine hooks on usage +Fix server attack tree regressions +Editing voxels hook and extensions for voxel palette item + # TODO diff --git a/src/main/java/electrosphere/client/item/ItemActions.java b/src/main/java/electrosphere/client/item/ItemActions.java index 56c0a4c6..4321782f 100644 --- a/src/main/java/electrosphere/client/item/ItemActions.java +++ b/src/main/java/electrosphere/client/item/ItemActions.java @@ -1,10 +1,6 @@ package electrosphere.client.item; import electrosphere.client.script.ClientScriptUtils; -import electrosphere.client.ui.menu.WindowStrings; -import electrosphere.client.ui.menu.WindowUtils; -import electrosphere.client.ui.menu.ingame.MenuGeneratorsTerrainEditing; -import electrosphere.controls.ControlHandler.ControlsState; import electrosphere.engine.Globals; import electrosphere.entity.Entity; import electrosphere.entity.state.attack.ClientAttackTree; @@ -93,16 +89,13 @@ public class ItemActions { //tell the server we want the secondary hand item to START doing something Globals.clientConnection.queueOutgoingMessage(InventoryMessage.constructclientRequestPerformItemActionMessage("handRight", ITEM_ACTION_CODE_SECONDARY, ITEM_ACTION_CODE_STATE_ON)); //TODO: do any immediate client side calculations here (ie start playing an animation until we get response from server) - ClientToolbarState clientToolbarState = ClientToolbarState.getClientToolbarState(Globals.playerEntity); - Entity primaryEntity = clientToolbarState.getCurrentPrimaryItem(); - if(primaryEntity != null && Globals.gameConfigCurrent.getItemMap().getItem(primaryEntity) != null){ - Item data = Globals.gameConfigCurrent.getItemMap().getItem(primaryEntity); - if(data.getClientSidePrimary() != null){ - switch(data.getClientSidePrimary()){ - case "OPEN_VOXEL": { - WindowUtils.replaceWindow(WindowStrings.VOXEL_TYPE_SELECTION,MenuGeneratorsTerrainEditing.createVoxelTypeSelectionPanel()); - Globals.controlHandler.hintUpdateControlState(ControlsState.IN_GAME_MAIN_MENU); - } break; + if(Globals.playerEntity != null){ + ClientToolbarState clientToolbarState = ClientToolbarState.getClientToolbarState(Globals.playerEntity); + Entity primaryEntity = clientToolbarState.getCurrentPrimaryItem(); + if(primaryEntity != null && Globals.gameConfigCurrent.getItemMap().getItem(primaryEntity) != null){ + Item data = Globals.gameConfigCurrent.getItemMap().getItem(primaryEntity); + if(data.getClientSideSecondary() != null){ + ClientScriptUtils.fireSignal(data.getClientSideSecondary()); } } } diff --git a/src/main/java/electrosphere/client/script/ScriptClientVoxelUtils.java b/src/main/java/electrosphere/client/script/ScriptClientVoxelUtils.java new file mode 100644 index 00000000..bafab8a8 --- /dev/null +++ b/src/main/java/electrosphere/client/script/ScriptClientVoxelUtils.java @@ -0,0 +1,37 @@ +package electrosphere.client.script; + +import org.graalvm.polyglot.HostAccess.Export; +import org.joml.Vector3d; + +import electrosphere.client.entity.camera.CameraEntityUtils; +import electrosphere.client.terrain.editing.TerrainEditing; +import electrosphere.collision.CollisionEngine; +import electrosphere.engine.Globals; +import electrosphere.entity.Entity; + +/** + * Utilities for interacting with voxels from the client + */ +public class ScriptClientVoxelUtils { + + /** + * Applies the current voxel palette where the player's cursor is looking + */ + @Export + public static void applyEdit(){ + CollisionEngine collisionEngine = Globals.clientSceneWrapper.getCollisionEngine(); + Entity camera = Globals.playerCamera; + if( + collisionEngine != null && + camera != null + ){ + Vector3d eyePos = new Vector3d(CameraEntityUtils.getCameraEye(camera)); + Vector3d centerPos = new Vector3d(CameraEntityUtils.getCameraCenter(camera)); + Vector3d cursorPos = collisionEngine.rayCastPosition(new Vector3d(centerPos), new Vector3d(eyePos).mul(-1.0), 5.0); + if(Globals.clientSelectedVoxelType != null){ + TerrainEditing.editTerrain(cursorPos, 1.1f, Globals.clientSelectedVoxelType.getId(), 0.1f); + } + } + } + +} diff --git a/src/main/java/electrosphere/game/data/item/type/Item.java b/src/main/java/electrosphere/game/data/item/type/Item.java index 22d8d902..6fde2a21 100644 --- a/src/main/java/electrosphere/game/data/item/type/Item.java +++ b/src/main/java/electrosphere/game/data/item/type/Item.java @@ -23,10 +23,15 @@ public class Item extends CommonEntityType { ItemAudio itemAudio; /** - * A function that should be performed client-side when the player uses this as their primary item + * A hook that should fire client-side when the player uses this as their primary item */ String clientSidePrimary; + /** + * A hook that should fire client-side when the player uses this as their primary item + */ + String clientSideSecondary; + /** * The block data for this item */ @@ -73,13 +78,21 @@ public class Item extends CommonEntityType { } /** - * Gets the client side primary action to perform - * @return The action + * Gets the client side primary hook to fire + * @return The hook */ public String getClientSidePrimary(){ return clientSidePrimary; } + /** + * Gets the client side secondary hook to fire + * @return The hook + */ + public String getClientSideSecondary(){ + return clientSideSecondary; + } + /** * Gets the block data for the item * @return THe block data diff --git a/src/main/java/electrosphere/script/ScriptEngine.java b/src/main/java/electrosphere/script/ScriptEngine.java index 9e4c8ffb..ebd784db 100644 --- a/src/main/java/electrosphere/script/ScriptEngine.java +++ b/src/main/java/electrosphere/script/ScriptEngine.java @@ -13,6 +13,7 @@ import org.graalvm.polyglot.Source; import org.graalvm.polyglot.Source.Builder; import org.graalvm.polyglot.Value; +import electrosphere.client.script.ScriptClientVoxelUtils; import electrosphere.client.ui.menu.script.ScriptMenuUtils; import electrosphere.client.ui.menu.tutorial.TutorialMenus; import electrosphere.engine.Globals; @@ -94,6 +95,7 @@ public class ScriptEngine { {"tutorialUtils",TutorialMenus.class}, {"serverUtils",JSServerUtils.class}, {"menuUtils",ScriptMenuUtils.class}, + {"voxelUtils",ScriptClientVoxelUtils.class}, }; //singletons from the host that are provided to the javascript context