WorldEntityManager Documentation¶
Overview¶
The WorldEntityManager class is a singleton responsible for managing various types of entities within the HighSpell game world. It maintains collections of world entities, walls, bridges, roofs, and planets, providing methods to add, remove, and query these entities. The class also handles specific game mechanics, such as roof visibility based on the player's position and entity resource states.
This documentation aligns with the HighSpell Botting Resources project, focusing on educational analysis and technical understanding of the game client’s structure, specifically its entity management system.
Usefulness for botting¶
The _worldEntities Map is a convenient Map of every visible WorldEntity allowing the user easy access to WorldEntityDef and the EntityTypeId of the object. These WorldEntity objects can often be passed as they are to functions such as TargetActionManager.handleTargetAction(e, t) which takes an WorldEntity's numeric action e, and the WorldEntity, t, itself to trigger said action.
Singleton Pattern¶
The WorldEntityManager uses the singleton pattern to ensure only one instance exists:
- Getter:
static get Instance() - Returns the singleton instance, creating it if it doesn’t exist.
- Ensures global access to the manager without multiple instantiations.
Key Properties¶
_worldEntities: AMapstoring world entities, keyed by theirEntityTypeID._walls: AMapstoring wall entities, keyed by theirID._bridges: AMapstoring bridge entities, keyed by theirID._roofs: AMapstoring roof entities, keyed by theirEntityTypeID._planets: An array storing planet entities._entitiesAwaitingReplenishment: AMaptracking entities that have exhausted resources and are awaiting replenishment._alwaysHideRoofs: A boolean controlling whether roofs are always hidden, synced withGameSettings.getAlwaysHideRoofs()._isMainPlayerUnderRoof: A boolean indicating if the main player is under a roof, affecting roof visibility.
Constructor and Initialization¶
- Constructor: Initializes the manager with default values and binds the
handleMainPlayerMovedUnderOrOutFromRoofmethod. init(): Sets up the manager if not already initialized (_didInitisfalse):- Creates data structures (
_worldEntities,_walls,_bridges,_roofs,_planets,_entitiesAwaitingReplenishment). - Initializes
_upVectorfor raycasting (used in roof detection).
Entity Management Methods¶
Adding Entities¶
These methods add entities to their respective collections, ensuring no duplicates and returning a boolean indicating success.
addWorldEntity(e):- Adds a world entity to
_worldEntitiesusinge.EntityTypeIDas the key. - Checks for duplicates and logs a warning if the entity already exists.
-
Example:
WorldEntityManager.Instance.addWorldEntity(entity). -
addWall(e): - Adds a wall to
_wallsusinge.IDas the key. -
Similar duplicate check and logging as
addWorldEntity. -
addBridge(e): - Adds a bridge to
_bridgesusinge.IDas the key. -
Follows the same duplicate check pattern.
-
addRoof(e): - Adds a roof to
_roofsusinge.EntityTypeIDas the key. - Sets initial visibility of roof meshes based on
_isMainPlayerUnderRoof. -
Logs a warning if the roof already exists.
-
addPlanet(e): - Appends a planet to the
_planetsarray without duplicate checks.
Retrieving Entities¶
These methods fetch entities by their IDs:
getWorldEntityById(e): Returns the entity from_worldEntitieswith keye.getWallById(e): Returns the wall from_wallswith keye.getBridgeById(e): Returns the bridge from_bridgeswith keye.getRoofById(e): Returns the roof from_roofswith keye.getPlanetByEntityId(e): Iterates_planetsto find a planet with matchingEntityID.
Checking Entity Existence¶
These methods verify if an entity exists in its respective collection:
hasWorldEntity(e): Checks if_worldEntitieshas keye.hasWall(e): Checks if_wallshas keye.hasBridge(e): Checks if_bridgeshas keye.hasRoof(e): Checks if_roofshas keye.
Removing Entities¶
These methods remove entities, calling their destroy() method and removing them from their collections:
removeWorldEntityById(e):- Destroys the entity using
EntityFactory.Instance.destroyWorldEntityand removes it from_worldEntities. removeWallById(e):- Calls
destroy()on the wall and removes it from_walls. removeBridgeById(e):- Calls
destroy()on the bridge and removes it from_bridges. removeRoofById(e):- Calls
destroy()on the roof and removes it from_roofs. removePlanetByEntityId(e):- Finds and destroys a planet with matching
EntityID, then removes it from_planets.
Bulk Removal¶
removeEntitiesOutsideOfBorder(west, north, east, south):- Removes entities (world entities, walls, bridges, roofs) outside the specified 2D boundaries.
- Validates borders to ensure
west < eastandsouth < north. - Uses entity positions (
CurrentGamePositionorPosition) to determine if they are outside the bounds.
Clearing Entities¶
clearWorldEntities():- Destroys and clears all entities in
_worldEntities,_walls,_bridges,_roofs, and_entitiesAwaitingReplenishment. clearPlanets():- Destroys and clears all planets in
_planets.
Resource Management¶
The manager tracks entities with exhausted resources, typically for lootable objects.
setEntitiesExhaustedResources(entityIds, flag, coords):- Marks entities in
entityIdsas exhausted (exhaustedResources()) and adds them to_entitiesAwaitingReplenishment. - If
flagistrue, checks nearby entities in_entitiesAwaitingReplenishmentand replenishes those not inentityIdsif they share the same grid tile (based onIFconstant). setEntityReplenishedResources(e):- Calls
replenishedResources()on the entity with IDeand removes it from_entitiesAwaitingReplenishment. markEntityAsLooted(e, isLooted):- Marks an entity as exhausted or replenished based on the
isLootedboolean.
Smelting Check¶
isTargetForSmelting(e):- Checks if the entity with ID
ehas asmeltaction in itsActionsarray. - Returns
trueif the entity can be smelted.
Door Interaction¶
handleWalkThroughDoor(e, t):- Handles door animation when the player walks through a door entity (
e). - Adjusts the door’s rotation and position based on its
Direction(e.g.,Sfor south,Wfor west). - Temporarily disables the door (
IsEnabled = false), moves it, and resets it after a 1.2-second timeout.
Entity Updates and Rendering¶
updateEntities(delta):- Updates all planets and world entities by calling their
update(delta)methods. drawEntities(e, t, i):- Draws planets and animateable world entities by calling their
draw(e, t, i)methods.
Reset¶
reset():- Resets the manager by setting
_didInitand_isMainPlayerUnderRooftofalseand clearing all entities and planets.