From 719999b6aa5bae40e8404c7a0fc693e37c272abd Mon Sep 17 00:00:00 2001 From: austin Date: Sun, 25 Aug 2024 11:51:15 -0400 Subject: [PATCH] more scripting work --- assets/Scripts/engine/entity/entity.ts | 10 ----- assets/Scripts/types/host/entity/entity.ts | 16 +++++++ assets/Scripts/types/host/static-classes.ts | 35 ++++++++++++++++ .../java/electrosphere/entity/Entity.java | 3 ++ .../entity/ServerEntityUtils.java | 6 --- .../electrosphere/script/ScriptEngine.java | 2 + .../script/translation/JSServerUtils.java | 42 +++++++++++++++++++ .../script/utils/AccessTransforms.java | 9 ++++ 8 files changed, 107 insertions(+), 16 deletions(-) delete mode 100644 assets/Scripts/engine/entity/entity.ts create mode 100644 assets/Scripts/types/host/entity/entity.ts create mode 100644 src/main/java/electrosphere/script/translation/JSServerUtils.java diff --git a/assets/Scripts/engine/entity/entity.ts b/assets/Scripts/engine/entity/entity.ts deleted file mode 100644 index 81bf3d94..00000000 --- a/assets/Scripts/engine/entity/entity.ts +++ /dev/null @@ -1,10 +0,0 @@ - - -/** - * An entity - */ -export interface Entity { - id: number, -} - - diff --git a/assets/Scripts/types/host/entity/entity.ts b/assets/Scripts/types/host/entity/entity.ts new file mode 100644 index 00000000..037c81bb --- /dev/null +++ b/assets/Scripts/types/host/entity/entity.ts @@ -0,0 +1,16 @@ + + + + +/** + * An entity + */ +export interface Entity { + + /** + * Gets the id of the entity + * @returns The id + */ + getId: () => number, + +} diff --git a/assets/Scripts/types/host/static-classes.ts b/assets/Scripts/types/host/static-classes.ts index 1849525c..e8ece579 100644 --- a/assets/Scripts/types/host/static-classes.ts +++ b/assets/Scripts/types/host/static-classes.ts @@ -1,4 +1,6 @@ +import { Entity } from "/Scripts/types/host/entity/entity"; import { TutorialUtils } from "/Scripts/types/host/renderer/ui/tutorial"; +import { Vector } from "/Scripts/types/spatial"; /** @@ -21,6 +23,11 @@ export interface StaticClasses { */ readonly tutorialUtils?: Class, + /** + * Utilities for performing actions on the server + */ + readonly serverUtils?: Class, + } @@ -50,3 +57,31 @@ export interface SimulationClass { readonly setFramestep: (value: number) => void, } + +/** + * Utilities for core functionality on the server + */ +export interface ServerUtils { + + /** + * Spawns a creature + * @param creatureType The type of creature + * @returns The entity created on the server + */ + readonly spawnCreature: (sceneInstanceId: number, creatureType: string, position: Vector) => Entity + + /** + * Gets the position of an entity + * @param entity The entity + * @returns The position + */ + readonly getPosition: (entity: Entity) => Vector + + /** + * Sets the position of an entity + * @param entity The entity + * @param position The position + */ + readonly setPosition: (entity: Entity, position: Vector) => void + +} diff --git a/src/main/java/electrosphere/entity/Entity.java b/src/main/java/electrosphere/entity/Entity.java index d7a280f3..0c90d9e6 100644 --- a/src/main/java/electrosphere/entity/Entity.java +++ b/src/main/java/electrosphere/entity/Entity.java @@ -2,6 +2,8 @@ package electrosphere.entity; import java.util.HashMap; +import org.graalvm.polyglot.HostAccess.Export; + /** * An entity @@ -29,6 +31,7 @@ public class Entity { * Gets the id of this entity * @return The id */ + @Export public int getId() { return id; } diff --git a/src/main/java/electrosphere/entity/ServerEntityUtils.java b/src/main/java/electrosphere/entity/ServerEntityUtils.java index 97f6bd8e..fbce69c6 100644 --- a/src/main/java/electrosphere/entity/ServerEntityUtils.java +++ b/src/main/java/electrosphere/entity/ServerEntityUtils.java @@ -70,12 +70,6 @@ public class ServerEntityUtils { } } } - // //if the server is also a client, update the drawcell manager to know to pull new chunks - // if(Globals.RUN_CLIENT){ - // Globals.drawCellManager.invalidateAllCells(); - // Globals.drawCellManager.setCellX(Globals.clientPlayerData.getWorldPos().x); - // Globals.drawCellManager.setCellY(Globals.clientPlayerData.getWorldPos().z); - // } //reposition entity CollisionObjUtils.serverPositionCharacter(entity, position); } diff --git a/src/main/java/electrosphere/script/ScriptEngine.java b/src/main/java/electrosphere/script/ScriptEngine.java index 94e13eca..4a138299 100644 --- a/src/main/java/electrosphere/script/ScriptEngine.java +++ b/src/main/java/electrosphere/script/ScriptEngine.java @@ -17,6 +17,7 @@ import electrosphere.engine.Globals; import electrosphere.engine.Main; import electrosphere.logger.LoggerInterface; import electrosphere.menu.tutorial.TutorialMenus; +import electrosphere.script.translation.JSServerUtils; import electrosphere.util.FileUtils; import electrosphere.util.math.MathUtils; @@ -85,6 +86,7 @@ public class ScriptEngine { {"mathUtils",MathUtils.class}, {"simulation",Main.class}, {"tutorialUtils",TutorialMenus.class}, + {"serverUtils",JSServerUtils.class}, }; //singletons from the host that are provided to the javascript context diff --git a/src/main/java/electrosphere/script/translation/JSServerUtils.java b/src/main/java/electrosphere/script/translation/JSServerUtils.java new file mode 100644 index 00000000..78057589 --- /dev/null +++ b/src/main/java/electrosphere/script/translation/JSServerUtils.java @@ -0,0 +1,42 @@ +package electrosphere.script.translation; + +import electrosphere.entity.Entity; +import electrosphere.entity.EntityUtils; +import electrosphere.entity.ServerEntityUtils; +import electrosphere.entity.types.creature.CreatureUtils; +import electrosphere.script.access.Vector; +import electrosphere.script.utils.AccessTransforms; + +/** + * Server utilities provided to the js context + */ +public class JSServerUtils { + + /** + * Spawns a creature + * @param creatureType The creature + */ + public static void spawnCreature(int sceneInstanceId, String creatureType, Vector position){ + //TODO: find realm from scene id + CreatureUtils.serverSpawnBasicCreature(null, AccessTransforms.getVector(position), creatureType, null); + } + + /** + * Gets the position of an entity + * @param entity The entity + * @return The position of the entity + */ + public static Vector getPosition(Entity entity){ + return AccessTransforms.getVector(EntityUtils.getPosition(entity)); + } + + /** + * Sets the position of an entity + * @param entity The entity + * @param vector THe new position of the entity + */ + public static void setPosition(Entity entity, Vector vector){ + ServerEntityUtils.repositionEntity(entity, AccessTransforms.getVector(vector)); + } + +} diff --git a/src/main/java/electrosphere/script/utils/AccessTransforms.java b/src/main/java/electrosphere/script/utils/AccessTransforms.java index f8071694..4207544b 100644 --- a/src/main/java/electrosphere/script/utils/AccessTransforms.java +++ b/src/main/java/electrosphere/script/utils/AccessTransforms.java @@ -14,5 +14,14 @@ public class AccessTransforms { public static electrosphere.script.access.Vector getVector(org.joml.Vector3d source){ return new electrosphere.script.access.Vector(source.x, source.y, source.z); } + + /** + * Converts an access vector into a JOML vector + * @param source The access vectpr + * @return The JOML vector + */ + public static org.joml.Vector3d getVector(electrosphere.script.access.Vector source){ + return new org.joml.Vector3d(source.x,source.y,source.z); + } }