ig.

Creature

new Creature()

Base creature entity, that adds some simple AI to ig.Character such as aggro and fleeing. IMPORTANT: this is an abstract entity that should be extended.

Author:
  • Collin Hover - collinhover.com
  • Jakub Siemiatkowski - jsiemiatkowski@gmail.com
Source:
Example
// a creature can automatically find, move to, and attack prey
// you just need to supply it with one of 4 prey finding options
// find by name
creaturePrey.name = "nameOfPrey";
creaturePredator.preyName = "nameOfPrey";
// find by class
creaturePrey = ig.game.spawnEntity(ig.EntityPreyClass);
creaturePredator.preyClass = ig.EntityPreyClass;
// find by type (for more info on types, check out ig.utils.addType)
ig.utils.addType(ig.EntityExtended, creaturePrey, 'type', "TYPE_OF_PREY");
creaturePredator.preyType = ig.utils.getType(ig.EntityExtended, "TYPE_OF_PREY");
// find by group (for more info on types, check out ig.utils.addType)
ig.utils.addType(ig.EntityExtended, creaturePrey, 'group', "GROUP_OF_PREY", "GROUP");
creaturePredator.preyGroup = ig.utils.getType(ig.EntityExtended, "GROUP_OF_PREY", "GROUP");
// these same 4 finding options apply to finding a predator
// but unlike prey, a creature will only try to flee or run away from a predator
// normally, a creature won't react to prey or predator until they are within range
creaturePredator.reactionDistance = 100;
// you can also force the creature to have a line of sight to the prey or predator
creaturePredator.needsLineOfSightPrey = true;
creaturePrey.needsLineOfSightPredator = true;
// once a prey or predator is found, pathfinding begins using ig.Character.moveTo
// this same method is used for wander and tethering
// to change the way in which pathfinding for each of these works
// use their individual settings objects
creaturePredator.moveToPreySettings = {...};
creaturePrey.moveToPredatorSettings = {...};
creaturePredator.moveToTetherSettings = {...};
creaturePrey.moveToWanderSettings = {...};
// one notable setting is the pathfinding search distance
// which, for best results, should match the reaction distance
creaturePredator.moveToPreySettings = {
     searchDistance: creaturePredator.reactionDistance,
     ...
};
// creatures can wander back and forth
creaturePredator.canWanderX = true;
// and for creatures that don't have gravity
creaturePredator.canWanderY = true;
// creatures can also tether to another entity, i.e. they won't move too far from a specific location
creaturePredator.entityTether = creatureTether;
// tether distance is flexible
creaturePredator.tetherDistance = 100;
// creatures can selectively break their tether when chasing prey or fleeing from predators
creaturePredator.canBreakTether = true;
// when managing creatures, note that they can find predators and prey
// but they will do nothing automatically with them
// and they will not wander or tether either

canBreakTether :Boolean

Whether creature can break tether range to follow a prey or flee from a predator.

Source:
See:

canFlee :Boolean

Whether creature can flee.

Source:
See:

canLearnPredators :Boolean

Whether creature can learn about new predators based on what it takes damage from.

Source:
See:

<readonly> canWander :Boolean

Whether creature can wander, set automatically during update based on current status.

Source:

canWanderX :Boolean

Whether creature can wander in x direction.

Source:
See:

canWanderY :Boolean

Whether creature can wander in y direction.

Source:
See:

collides

Creature collides lite.

Default Value:
  • lite
Source:

detectHiddenPredator :Boolean

Whether creature can detect hidden predators

Source:
See:

detectHiddenPrey :Boolean

Whether creature can detect hidden prey.

Source:
See:

<readonly> entityTether :ig.EntityExtended

Tether entity.

Default Value:
  • null
Source:

fleeHealthPct :Number

Percentage of health, between 0 and 1, when creature begins to flee.

Source:
See:

<readonly> fleeing :Boolean

Whether creature is currently fleeing. Tip: creature can start with flee mode on.

Default Value:
  • false
Source:

<readonly> fleeingLowHealth :Boolean

Whether creature is currently fleeing due to low health.

Default Value:
  • false
Source:

moveToPredatorSettings :Object

Settings for moving to predator. For options, see ig.Character#moveTo.

Source:

moveToPreySettings :Object

Settings for moving to prey. For options, see ig.Character#moveTo.

Source:

moveToTetherSettings :Object

Settings for moving to tether. For options, see ig.Character#moveTo.

Source:

moveToWanderSettings :Object

Settings for random move. For options, see ig.Character#moveTo.

Source:

needsLineOfSightPredator :Boolean

Whether needs line of sight for predator.

Source:
See:

needsLineOfSightPrey :Boolean

Whether needs line of sight for prey.

Source:
See:

performance

Creature is dynamic.

Default Value:
  • dynamic
Source:

predator :ig.EntityExtended

Target predator entity.

Default Value:
  • null
Source:

predatorClass :ig.EntityExtended

Entity class that this creature is prey for, i.e. creature should run away from them.
- priority 2 in finding predator, overrides group and type, overriden by name.

Default Value:
  • null
Source:

predatorFromDamage :Boolean

Whether creature sets predator to anything that damages it below flee threshold.

Source:
See:

predatorGroup :Bitflag

Groups that this creature is prey for, i.e. creature should run away from them.
- priority 4 in finding predator, overrides none.

Source:
See:

predatorName :String

Name of unique entity that this creature is prey for, i.e. creature should run away from them.
- priority 1 in finding predator, overrides group, type, and class.

Source:

predatorType :ig.EntityExtended

Entity type that this creature is prey for, i.e. creature should run away from them.
- priority 3 in finding predator, overrides group, overriden by name and class.

Source:

prey :ig.EntityExtended

Target prey entity.

Default Value:
  • null
Source:

preyClass :String|ig.EntityExtended

Entity class that this creature will prey on, i.e. creature will attack them.
- priority 2 in finding prey, overrides type and group but overriden by name.

Default Value:
  • null
Source:

preyGroup :Bitflag

Groups that this creature will prey on, i.e. creature will attack them.
- priority 4 in finding prey, overrides none.

Source:
See:

preyName :String

Name of unique entity that this creature will prey on, i.e. creature will attack them.
- priority 1 in finding prey, overrides type, group, and class.

Source:

preyType :ig.EntityExtended

Entity type that this creature will prey on, i.e. creature will attack them.
- priority 3 in finding predator, overrides group, overriden by name and class.

Source:

reactionDelay :Number

Distance at which creature reacts to other creatures in ig.Creature#preyGroup and ig.Creature#predatorGroup.

Source:
See:

reactionDistance :Number

Distance at which creature reacts to other creatures in ig.Creature#preyGroup and ig.Creature#predatorGroup. Tip: for best results, reaction distance should be <= pathfinding distance!

Source:
See:

<readonly> reactionTimer :ig.Timer

Timer for reaction ticks.
- created on init

Source:

tetherDistance :Number

Distance creature can move around its tether. Tip: a spawned creature will use its spawner as a tether, unless ig.Creature#tetherName is set and matches a valid entity in the game.

Source:
See:
Example
// a creature can have a tether by:
// 1. it was spawned by a spawner
// 2. it has a tether name that matches a entity
// otherwise, tethering will be skipped
// when tether distance is <= 0
// a creature can go as far as it wants
myCreature.tetherDistance = 0;
// when tether distance is > 0
// a creature will only go up to that distance
myCreature.tetherDistance = 100;
// unless it can break its tether
// to follow prey or flee from a predator
myCreature.canBreakTether = true;

<readonly> tethering :Boolean

Whether creature is tethering, i.e. returning back to its tether.

Default Value:
  • false
Source:

tetherName :String

Name of tether entity.

Source:

tetherToSpawner :Boolean

Try to use spawner as tether.

Source:
See:

<readonly> wanderDirection :Vector2|Object

Direction of wander movement.

Source:

<readonly> wandering :Boolean

Whether creature is currently wandering.

Default Value:
  • false
Source:

<readonly> wanderPos :Vector2|Object

Target position of random movement.

Source:

wanderSwitchChance :Number

Chance as a percent between 0 and 1 that direction will switch while wandering. Tip: setting this above 0.005 will cause creature to switch direction often and this can look rather stupid!

Source:
See:

wanderSwitchChanceStopped :Number

Chance as a percent between 0 and 1 that direction will switch while not wandering. Tip: setting this above 0.015 will cause creature to never really stop moving.

Source:
See:

attack(entity) → {Boolean}

Attacks a target (does nothing except a simple distance check by default). IMPORTANT: override this with a class specific method!

Parameters:
Name Type Description
entity ig.EntityExtended target entity to attack
Source:
Returns:
{ Boolean } whether creature is close enough to attack target

cleanup()

Source:

clearPredator()

Removes predator as target and clears all predator targeting methods, effectively disabling the creature from finding predators.

Source:

clearPrey()

Removes prey as target and clears all prey targeting methods, effectively disabling the creature from finding prey.

Source:

findPredatorPrey(force)

Finds and sets the closest predator and prey.

Parameters:
Name Type Argument Default Description
force Boolean <optional>
false whether to get new
Source:

flee(target, settings) → {Boolean}

Flees from a target.

Parameters:
Name Type Description
target ig.EntityExtended target entity to flee from
settings Object settings for move
Source:
Returns:
{ Boolean } whether creature started fleeing (not if is fleeing)

initProperties()

Source:

initTypes()

Source:

isSafeToWander() → {Boolean}

Source:
Returns:
{ Boolean } whether creature can wander.

pause()

Source:

receiveDamage()

Damages creature.
- if ig.Creature#canLearnPredators, adds the from entity's group to creature's predator group.
- if ig.Creature#health is below ig.Creature#fleeHealthPct, will force damaging entity as predator and flee.

Source:

receiveHealing()

Heals creature.
- if ig.Creature#health is above ig.Creature#fleeHealthPct, will drop predator and stop fleeing.

Source:

removePredator()

Removes predator as target.

Source:

removePrey()

Removes prey as target.

Source:

resetCore()

Source:

resetExtras()

Source:

spawn()

Source:

unpause()

Source:

updateChanges()

Checks closest targets and reacts accordingly.

Source:

updatePredator()

Checks predator and reacts to it.

Source:

updatePrey()

Checks prey and reacts to it by moving closer and attacking.

Source:

updateTether()

Checks tether and moves back to it as necessary.

Source:

updateWander()

Moves around randomly.

Source: