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
: AMap
storing world entities, keyed by theirEntityTypeID
._walls
: AMap
storing wall entities, keyed by theirID
._bridges
: AMap
storing bridge entities, keyed by theirID
._roofs
: AMap
storing roof entities, keyed by theirEntityTypeID
._planets
: An array storing planet entities._entitiesAwaitingReplenishment
: AMap
tracking 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
handleMainPlayerMovedUnderOrOutFromRoof
method. init()
: Sets up the manager if not already initialized (_didInit
isfalse
):- Creates data structures (
_worldEntities
,_walls
,_bridges
,_roofs
,_planets
,_entitiesAwaitingReplenishment
). - Initializes
_upVector
for 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
_worldEntities
usinge.EntityTypeID
as 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
_walls
usinge.ID
as the key. -
Similar duplicate check and logging as
addWorldEntity
. -
addBridge(e)
: - Adds a bridge to
_bridges
usinge.ID
as the key. -
Follows the same duplicate check pattern.
-
addRoof(e)
: - Adds a roof to
_roofs
usinge.EntityTypeID
as 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
_planets
array without duplicate checks.
Retrieving Entities¶
These methods fetch entities by their IDs:
getWorldEntityById(e)
: Returns the entity from_worldEntities
with keye
.getWallById(e)
: Returns the wall from_walls
with keye
.getBridgeById(e)
: Returns the bridge from_bridges
with keye
.getRoofById(e)
: Returns the roof from_roofs
with keye
.getPlanetByEntityId(e)
: Iterates_planets
to find a planet with matchingEntityID
.
Checking Entity Existence¶
These methods verify if an entity exists in its respective collection:
hasWorldEntity(e)
: Checks if_worldEntities
has keye
.hasWall(e)
: Checks if_walls
has keye
.hasBridge(e)
: Checks if_bridges
has keye
.hasRoof(e)
: Checks if_roofs
has 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.destroyWorldEntity
and 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 < east
andsouth < north
. - Uses entity positions (
CurrentGamePosition
orPosition
) 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
entityIds
as exhausted (exhaustedResources()
) and adds them to_entitiesAwaitingReplenishment
. - If
flag
istrue
, checks nearby entities in_entitiesAwaitingReplenishment
and replenishes those not inentityIds
if they share the same grid tile (based onIF
constant). setEntityReplenishedResources(e)
:- Calls
replenishedResources()
on the entity with IDe
and removes it from_entitiesAwaitingReplenishment
. markEntityAsLooted(e, isLooted)
:- Marks an entity as exhausted or replenished based on the
isLooted
boolean.
Smelting Check¶
isTargetForSmelting(e)
:- Checks if the entity with ID
e
has asmelt
action in itsActions
array. - Returns
true
if 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.,S
for south,W
for 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
_didInit
and_isMainPlayerUnderRoof
tofalse
and clearing all entities and planets.