• ig.Game

ig.

GameExtended

new GameExtended()

Impact++ game.
- uses ig.Layer to organize entities, based on Impact Layers plugin by Amadeus https://github.com/amadeus/impact-layers Tip: your game class should extend this to utilize Impact++ properly.

Author:
  • Collin Hover - collinhover.com
Source:

autoSpawnPlayer :Boolean

Whether game should autospawn player when a new level is loaded. IMPORTANT: this only works when ig.GameExtended#playerClass is set or when player is persistent and the player has been spawned once already!

Source:
See:

camera :ig.Camera

Camera used for screen control.
- created on init.

Source:

cameraClass :ig.Camera

Class of camera to use when creating ig.GameExtended#camera.

Source:

cameraSettings :Object

Settings to merge into ig.GameExtended#camera on init.

Default Value:
  • null
Source:

canSearchForPlayerByClass :Boolean

Whether game can search for player by class in ig.GameExtended#getPlayer.

Default Value:
  • true
Source:

canSearchForPlayerByType :Boolean

Whether game can search for player by type in ig.GameExtended#getPlayer.

Default Value:
  • true
Source:

<readonly> changed :Boolean

Whether game has changed since last update.

Source:

<readonly> changedLights :Boolean

Whether game lights have changed since last update.

Source:

clearColor :String

Color to clear screen with.

Source:
See:

<readonly> deferring :Boolean

Whether game is deferring add, remove, level changing, etc. Tip: this property is briefly set to false during game update when all deferred items are processed.

Source:

<readonly> dirtyEntities :Boolean

Whether game has added or removed entities since last update.

Source:

<readonly> dirtyLights :Boolean

Whether game has added or removed lights since last update.

Source:

<readonly> entities :Array

List of entities across all layers, repopulated during update and only when layers have changed.

Source:

gravity :Number

Gravity magnitude in positive y direction.

Source:

<readonly> hasLevel :Boolean

Whether game has a level currently. Tip: this property is briefly set to false when levels are changing.

Source:

layers :Array

List of layers, sorted by zIndex.

Source:

layersMap :Object

Layers mapped by name.

Source:

<readonly> lights :Array

List of lights across all layers, repopulated during update and only when layers have changed.

Source:

<readonly> namedEntities :Object

Map of entities by name across all layers.

Source:
See:
  • ig.EntityExtended#name

onPaused :ig.Signal

Signal dispatched when game is paused.
- created on init.

Source:

onUnpaused :ig.Signal

Signal dispatched when game is unpaused.
- created on init.

Source:

pathfindingMap :ig.PathfindingMap

Map used for pathfinding.

Default Value:
  • null
Source:

<readonly> paused :Boolean

Whether game is paused. IMPORTANT: does not guarantee that all layers are paused or unpaused!

Source:

pauseLocked :Boolean

Whether pause state is locked.
- set directly or use ig.Game#pause and {@link ig.Game#paused} to toggle lock.

Source:

<readonly> persistentEntities :Object

Map of entities by name across all layers that are persistent within the game, i.e. able to change levels.

Source:
See:

playerClass :ig.Player

The player class to spawn when using ig.GameExtended#autoSpawnPlayer. Tip: if you don't want player to auto spawn at the start of the game, but you do want player to spawn on every level after game starts, leave this empty, spawn player manually once, and make sure player is ig.EntityExtended#persistent.

Default Value:
  • null
Source:

playerManager :ig.PlayerManager

Player manager used to control the player character.
- created on init.

Source:

playerManagerClass :ig.Camera

Class of player manager to use when creating ig.GameExtended#playerManager.

Source:

playerManagerSettings :Object

Settings to merge into ig.GameExtended#playerManager on init.

Default Value:
  • null
Source:

shapesLevel :Array

Shape(s) of the level inner boundary, used for ig.GameExtended#insideLevel checks.

Source:

shapesPasses :Array

Passes to extract shapes from collision map.
- shapes are extracted during ig.GameExtended#loadLevel via ig.GameExtended#createShapes Tip: Impact++ can automatically convert collision maps into shapes (vertices, edges, etc), via shapesPasses.

Default Value:
  • gets climbable shapes using options [ { ignoreSolids: true, ignoreOneWays: true } ]
Source:
See:
Example
// will skip extracting shapes from collision map
game.shapesPasses = null;
// after level is loaded, get all edge shapes for lighting
game.shapesPasses = {
     lighting: {
         ignoreClimbables: true,
         discardBoundaryInner: true
     }
};
// after level is loaded, get all climbable shapes
game.shapesPasses = {
     climbables: {
         ignoreSolids: true,
         ignoreOneWays: true
     }
};
// note that by default, Impact++ extracts
// the level boundary inner edge loop
// and all climbable shapes

sortLayersBy :function

Method to sort layers by. Defaults to z index sort.

Source:

addItem(item, settings)

Deferred add of an item to a layer. IMPORTANT: to create an entity, use ig.GameExtended#spawnEntity.

Parameters:
Name Type Argument Description
item * item to add.
settings Object <optional>
settings object.
Source:
Example
// add item to a different layer than it is on
settings.layerName = 'otherLayer';
// add is actually for changing between layers
settings.forChange = true;

addLayer(layer)

Adds a layer to the active layer list.

Parameters:
Name Type Description
layer ig.Layer layer to be added.
Source:

buildLevel()

Builds a level immediately. IMPORTANT: for proper stability, use ig.GameExtended#loadLevelDeferred instead.

Source:

buildLevelDeferred()

Defers build of ig.Game#_levelToBuild. IMPORTANT: for proper stability, use ig.GameExtended#loadLevelDeferred instead.

Source:

changeLayer(layerName, settings)

Deferred layer settings change.

Parameters:
Name Type Description
layerName String name of layer to be modified.
settings Object settings to merge into layer.
Source:

clearPersistentEntities(name)

Clears ALL ig.EntityExtended#persistent entities from game cache. IMPORTANT: this does NOT kill the entity.

Parameters:
Name Type Description
name String
Source:

clearPersistentEntity(name)

Clears a ig.EntityExtended#persistent entity by name from the game cache, allowing it to be fully reset. IMPORTANT: this does NOT kill the entity.

Parameters:
Name Type Description
name String
Source:

createShapes()

Converts collision map to shapes.

Source:

draw()

Draws game.
- clears canvas
- records rounded screen pos
- draws all layers

Source:

drawEntities(layerName)

Draws all entities (items) on a layer. IMPORTANT: normally, you will not need to call this function.

Parameters:
Name Type Argument Description
layerName String <optional>
layer name.
Source:

flagAsKilled()

Flags an entity as killed. Can be used before removal to facilitate death animations.

Parameters:
Name Type Description
entity. Entity
Source:

getCharactersByGroup(group, settings) → {Array}

Gets all characters of a particular group.

Parameters:
Name Type Argument Description
group String | Number | Bitflag entity group.
settings Object <optional>
settings for search.
Source:
See:
Returns:
{ Array } all found entities.

getEntitiesByClass(cls, settings) → {Array}

Gets all entities of a particular class. IMPORTANT: the name is misleading, but this does not search by ig.EntityExtended.TYPE.

Parameters:
Name Type Argument Description
cls ig.Class | String entity class.
settings Object <optional>
settings for search.
Source:
See:
Returns:
{ Array } all found entities.

getEntitiesByGroup(group, settings) → {Array}

Gets all entities of a particular group.

Parameters:
Name Type Argument Description
group String | Number | Bitflag entity group.
settings Object <optional>
settings for search.
Source:
See:
Returns:
{ Array } all found entities.

getEntitiesByType(type, settings) → {Array}

Gets all entities of a particular type. IMPORTANT: this method no longer searches for entities by class, but by their {@ig.EntityExtended#type} property. To search by class, use {@ig.GameExtended#getEntitiesByClass}.

Parameters:
Name Type Argument Description
type String | Number | Bitflag entity type.
settings Object <optional>
settings for search.
Source:
See:
Returns:
{ Array } all found entities.

getEntitiesMatching(matchBy, settings) → {Array}

Gets all matching entities.

Parameters:
Name Type Argument Description
matchBy function function to use to check if entity matches search.
settings Object <optional>
settings for search.
Source:
Returns:
{ Array } all found entities.
Example
// the search takes a match function and plain settings object
// each entity is passed to the match function
// if the function returns true, entity is matched
// and the settings control the search
// search only the entities layer
game.getEntitiesMatching( matchBy, {
     layerName: 'entities'
});
// get only the first
game.getEntitiesMatching( matchBy, {
     first: true
});
// skip sorting search results
game.getEntitiesMatching( matchBy, {
     unsorted: true
});
// sort results by distance from 100, 100 instead of the game's sortBy
// this requires the 'from' property
game.getEntitiesMatching( matchBy, {
     byDistance: true,
     from: { x: 100, y: 100 }
});
// sort results by distance from myCharacter
// this requires the 'from' property
game.getEntitiesMatching( matchBy, {
     byDistance: true,
     from: myCharacter
});
// only get entities that are within distance squared
// this requires the 'from' property
game.getEntitiesMatching( matchBy, {
     distanceSquared: 10000,
     from: myCharacter
});
// only get entities that intersect bounds
game.getEntitiesMatching( matchBy, {
     minX: myCharacter.pos.x,
     minY: myCharacter.pos.x,
     maxX: myCharacter.pos.x + myCharacter.size.x,
     maxY: myCharacter.pos.y + myCharacter.size.y
});
// only get entities in line of sight
game.getEntitiesMatching( matchBy, {
     lineOfSight: true
});
// only get entities that are not hidden
game.getEntitiesMatching( matchBy, {
     nonHidden: true
});

getMapByName(name) → {Map}

Gets a map by matching name.

Parameters:
Name Type Description
name String map name to search.
Source:
Returns:
{ Map } Map if found, else undefined.

getPlayer(settings) → {ig.Player}

Get the player object. This method searches for name, then class if ig.GameExtended#canSearchForPlayerByClass, then type if ig.GameExtended#canSearchForPlayerByType.

Parameters:
Name Type Argument Description
settings Object <optional>
settings for search.
Source:
Returns:
{ ig.Player } player if exists in game.

handleInput()

Handles input just before entities are updated. If a tap targets a UI element, it will intercept that tap and nothing else will receive it.

Source:

init()

Initializes game.
- creates camera
- creates signals
- sets layer sort method
- adds layer: backgroundMaps as a map layer with no update
- adds layer: lights
- adds layer: entities
- adds layer: foregroundMaps as a map layer with no update
- adds layer: overlayas an auto sorted layer
- adds layer: ui as an auto sorted layer that cannot be paused
- starts listening for window resize

Source:

inputEnd()

Ends input by removing all bindings set in ig.GameExtended#inputStart.

Source:

inputStart()

Starts input by setting up some default bindings. IMPORTANT: this method is called automatically on game init!

Source:
Example
// inputStart sets up bindings
// including:
// WASD, ARROWS, Spacebar Jump
// gestures like touch, tap, hold, swipe
// for example touch is bound to both mouse keys
ig.input.bind(ig.KEY.MOUSE1, 'touch');
ig.input.bind(ig.KEY.MOUSE2, 'touch');
// and tap is used in place of click
// to support both desktop and mobile devices
ig.input.bind(ig.KEY.TAP, 'tap');

isInsideLevel(x, y) → {Boolean}

Whether an entity or position is inside the level. Most accurate when game is using pathfinding and has a collision map, and always true if using neither! Tip: if you are having issues with this method, it is most likely because a) your game has pathfinding disabled, and/or b) your level does not have a continuous border of solid tiles in the collision map!

Parameters:
Name Type Description
x Number target x
y Number target y
Source:
Returns:
{ Boolean } whether inside level.

layerChangeItem(item, layerName)

Removes this item from current layer and adds to new layer.

Parameters:
Name Type Argument Description
item * item to swap layers.
layerName String <optional>
name of layer to swap to, defaults to previous change layer to allow easy swap to/from.
Source:

loadLevel(level, playerSpawnerName)

Loads a level immediately. IMPORTANT: for proper stability, use ig.GameExtended#loadLevelDeferred instead.

Parameters:
Name Type Argument Description
level Object | String <optional>
level data or level name.
playerSpawnerName String <optional>
name of entity in new level to set as player spawn location.
Source:

loadLevelDeferred(level, playerSpawnerName)

Deferred load of a level.

Parameters:
Name Type Argument Description
level Object | String level data or level name.
playerSpawnerName String <optional>
name of entity in new level to set as player spawn location.
Source:
Example
// these will all load the 'LevelTest1' level
game.loadLevelDeferred( "LevelTest1" );
game.loadLevelDeferred( "Test1" );
game.loadLevelDeferred( "test1" );

pause(lock)

Pauses game, provided pause is not locked and a level is not being built. IMPORTANT: locking pause will not allow any other pause or unpause until unlocked!

Parameters:
Name Type Argument Default Description
lock Boolean <optional>
false whether to lockdown pause and show no visible sign (i.e. no dimming)
Source:

removeEntity(entity, settings)

Removes an entity.
- this is not the same as ig.EntityExtended#kill Tip: use this to remove an entity for better stability.

Parameters:
Name Type Description
entity Entity
settings Object settings for ig.Game#removeItem
Source:
See:
  • ig.Game#removeItem

removeItem(item, settings)

Deferred removal of item. IMPORTANT: to kill an entity, use ig.EntityExtended#kill, and to remove an entity, use ig.GameExtended#removeEntity.

Parameters:
Name Type Description
item * item to remove.
settings Object settings object.
Source:
Example
// remove item from a different layer than it is on
settings.layerName = 'otherLayer';
// removal is actually for changing between layers
settings.forChange = true;

removeLayer(layer)

Deferred removal of layer.

Parameters:
Name Type Description
layer ig.Layer layer to be removed.
Source:

resetLoadLevel()

Resets level loading properties.

Source:

resize(force)

Resize the canvas based on the window. Tip: use this to resize Impact++ properly.

Parameters:
Name Type Argument Default Description
force Boolean <optional>
false whether to force global resize (only do this when absolutely necessary).
Source:

resizeDeferred()

Resizes game on next update and is much better on performance (max of 1 resize per update).

Source:

sortEntities(layerName)

Sort of entities, sorting all layers if no layer name passed.

Parameters:
Name Type Argument Description
layerName String <optional>
layer name.
Source:

sortEntitiesDeferred(layerName)

Deferred sort of entities, sorting all layers if no layer name passed.

Parameters:
Name Type Argument Description
layerName String <optional>
layer name.
Source:

sortEntitiesOnLayer(layer)

Sort of entities on a specific layer.

Parameters:
Name Type Description
layer ig.Layer layer on which to sort items.
Source:

sortEntitiesOnLayerDeferred(layer)

Deferred sort of entities on a specific layer.

Parameters:
Name Type Description
layer ig.Layer layer on which to sort items.
Source:

spawnEntity(type, x, y, settings) → {Entity}

Spawns an entity of type. IMPORTANT: use this to instantiate an entity for better stability.

Parameters:
Name Type Argument Description
type String | Entity entity type or an entity.
x Number x position.
y Number y position.
settings Object <optional>
settings to be merged into entity.
Source:
Returns:
{ Entity } created entity.

unloadLevel()

Unloads the current level immediately and resets layers, camera, etc. IMPORTANT: for proper stability, use ig.GameExtended#unloadLevelDeferred instead.

Source:

unloadLevelDeferred()

Deferred unload of the current level.

Source:

unpause(unlock)

Unpauses game, provided pause is not locked and a level is not being built.

Parameters:
Name Type Argument Default Description
unlock Boolean <optional>
false whether to unlock pause
Source:

update()

Updates game.
- removes, adds, and changes deferred layers
- removes and adds all deferred items
- loads a new level
- regathers all entities and lights in game if entities have been added or removed
- updates inputs
- updates tweens
- updates layers
- updates camera

Source:

updateEntities(layerName)

Update entities in layer. IMPORTANT: normally, you will not need to call this function.

Parameters:
Name Type Description
layerName String layer name.
Source: