Editing voxels from script engine
All checks were successful
studiorailgun/Renderer/pipeline/head This commit looks good

This commit is contained in:
austin 2024-09-30 16:47:38 -04:00
parent 42bc125897
commit 8e2f357101
9 changed files with 90 additions and 18 deletions

View File

@ -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,

View File

@ -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()
}
}
]

View File

@ -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
}

View File

@ -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<MenuUtils>,
/**
* Utilities for interacting with voxels on the client
*/
readonly voxelUtils?: Class<ClientVoxelUtils>,
}
/**

View File

@ -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

View File

@ -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());
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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

View File

@ -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