more scripting work

This commit is contained in:
austin 2024-08-25 11:51:15 -04:00
parent 3682497060
commit 719999b6aa
8 changed files with 107 additions and 16 deletions

View File

@ -1,10 +0,0 @@
/**
* An entity
*/
export interface Entity {
id: number,
}

View File

@ -0,0 +1,16 @@
/**
* An entity
*/
export interface Entity {
/**
* Gets the id of the entity
* @returns The id
*/
getId: () => number,
}

View File

@ -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<TutorialUtils>,
/**
* Utilities for performing actions on the server
*/
readonly serverUtils?: Class<ServerUtils>,
}
@ -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
}

View File

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

View File

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

View File

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

View File

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

View File

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