API Docs for:
Show:

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.
  • ObjectConstructor Object
    • Object constructor used to create new objects with this.add().
  • [width] Number optional
    • Width of current map in tiles.
  • [height] Number optional
    • Height of current map in tiles.

Methods

add

(
  • x
  • y
  • obj
)
Object

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).

Returns:

Object:

The added object.

get

(
  • x
  • y
)
Object | False

Retrieves an entity by map tile coords.

Parameters:

  • x Number
    • The tile map coord x.
  • y Number
    • The tile map coord y.

Returns:

Object | False:

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.
  • [defaultType] String optional
    • If set, all characters in mapData not found in charToType will be replaced by an object with defaultType.
  • [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
)
Object

Creates a new object instance of given type.

Parameters:

  • type String
    • The type to make the object

Returns:

Object:

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.

Properties

game

Game

Game instance this obj is attached to.

map

Array2d

Array2d containing all objects by their current map tile coord

objects

Array

Array containing all objects.