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:
-
game
Game- Game instance this
ObjectManager
is attached to.
- Game instance this
-
ObjectConstructor
Object- 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:
-
x
Number- The tile map coord x.
-
y
Number- The tile map coord y.
-
obj
Object | 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:
-
x
Number- The tile map coord x.
-
y
Number- The tile map coord y.
Returns:
loadEntitiesFromArrayString
-
mapData
-
charToType
-
[defaultType]
-
[replaceCurrentObjects=false]
Loads object data from an array of strings.
Parameters:
-
mapData
Array- The array of strings to load.
-
charToType
Object- 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
mapData
not found incharToType
will 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:
-
type
String- The type to make the object
Returns:
move
-
x
-
y
-
object
Changes the position of an object already added to this objectManager.
Parameters:
-
x
Number- The destination tile map coord x.
-
y
Number- The destination tile map coord y.
-
object
Obj- The objectity to be removed.
remove
-
object
Removes an object from the manager.
Parameters:
-
object
Object- The objectity to be removed.
reset
()
Resets this entityManager.
setSize
-
width
-
height
Sets the size of the map to manage objects within.
Parameters:
-
width
Number- Width of current map in tiles.
-
height
Number- 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