ObjectManager Class
Manages a list of all objects and their tile positions. Handles adding, removing, moving objects within the game.
Constructor
ObjectManager
-
game -
ObjectConstructor -
[width] -
[height]
Parameters:
-
gameGame- Game instance this
ObjectManageris attached to.
- Game instance this
-
ObjectConstructorObject- Object constructor used to create new objects with
this.add().
- Object constructor used to create new objects with
-
[width]Number optional- Width of current map in tiles.
-
[height]Number optional- Height of current map in tiles.
Item Index
Methods
add
-
x -
y -
obj
Adds an object to the manager at given map tile coord. If an object is already at this map tile coord it is removed from the manager completely.
Parameters:
-
xNumber- The tile map coord x.
-
yNumber- The tile map coord y.
-
objObject | String- The Object being set at given coords. If obj is a string a new Object will be created using
this.makeNewObjectFromType(obj).
- The Object being set at given coords. If obj is a string a new Object will be created using
Returns:
The added object.
get
-
x -
y
Retrieves an entity by map tile coords.
Parameters:
-
xNumber- The tile map coord x.
-
yNumber- The tile map coord y.
Returns:
loadEntitiesFromArrayString
-
mapData -
charToType -
[defaultType] -
[replaceCurrentObjects=false]
Loads object data from an array of strings.
Parameters:
-
mapDataArray- The array of strings to load.
-
charToTypeObject- An object mapping string characters to Object types see
this.makeNewObjectFromType(). Characters in mapData not in charToType are ignored.
- An object mapping string characters to Object types see
-
[defaultType]String optional- If set, all characters in
mapDatanot found incharToTypewill be replaced by an object withdefaultType.
- If set, all characters in
-
[replaceCurrentObjects=false]Bool optional- If true current objects at positons of objects being added will be removed. Otherwise new objects at occupied positions will not be added.
Example:
// 'P' will be ignored and a floor tile will be placed at that position
var mapData = [
'####',
'#..#',
'#.Z#',
'####',
],
charToType = {
'Z': 'zombie'
};
entityManager.loadTilesFromArrayString(mapData, charToType);
makeNewObjectFromType
-
type
Creates a new object instance of given type.
Parameters:
-
typeString- The type to make the object
Returns:
move
-
x -
y -
object
Changes the position of an object already added to this objectManager.
Parameters:
-
xNumber- The destination tile map coord x.
-
yNumber- The destination tile map coord y.
-
objectObj- The objectity to be removed.
remove
-
object
Removes an object from the manager.
Parameters:
-
objectObject- The objectity to be removed.
reset
()
Resets this entityManager.
setSize
-
width -
height
Sets the size of the map to manage objects within.
Parameters:
-
widthNumber- Width of current map in tiles.
-
heightNumber- Height of current map in tiles.
update
-
[excludeObject]
Calls the object.update() method on all objects. Removes object.dead == true objects.
Typically called after a player has resolved their actions.
Not all object managers need to upade the objects they manage.
Parameters:
-
[excludeObject]Object optional- excludeObject will be skipped if found in
this.objects. Typically used to skip the player object.
- excludeObject will be skipped if found in
