Creature

by @Pattentrick

Oh you heard about the creature abstract, which provides some basic AI that you can use for your character entities, but you don't know how you should use it? Don't look any further, you have come to the right place! In this tutorial we well explain the basics of the creature abstract like attacking, wandering and fleeing, so you can make your very own creatures in no time!

Attack!

One of the core concepts behind the creature abstract is the idea to have prey and predators. A creature can automatically find, move to, and attack prey. You just need to supply it with one of the 4 prey finding options.

A creature can find prey by name:

creaturePrey.name = "nameOfPrey";
creaturePredator.preyName = "nameOfPrey";

By class (this can also be the name of the class as string):

creaturePrey = ig.game.spawnEntity(ig.EntityPreyClass);
creaturePredator.preyClass = ig.EntityPreyClass;

By a specific type:

ig.utils.addType(ig.EntityExtended, creaturePrey, 'type', "TYPE_OF_PREY");
creaturePredator.preyType = ig.utils.getType(ig.EntityExtended, "TYPE_OF_PREY");

Or even by a group:

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 also apply to finding a predator. But keep in mind that a creature will only try to flee, or run away, from a predator. Not from another prey. Normally a creature won't react to prey or predator until they are within a certain range. But you can easily modify that range:

creaturePredator.reactionDistance = 100;

On top of that 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, the creature will use pathfinding via the moveTo method of the character abstract to move around your level. You can define your own settings for that pathfinding like this:

creaturePredator.moveToPreySettings = {...};
creaturePrey.moveToPredatorSettings = {...};

One notable setting is the pathfinding search distance which, for best results, should match the reaction distance:

creaturePredator.moveToPreySettings = {
     searchDistance: creaturePredator.reactionDistance
};

For more pathfinding options see the moveTo method of the character abstract:

(ig.Character.html#moveTo

While a predator is approaching its prey, it will call its attack method. By default this method will just do a distance check and returns true if it's near enough and stops the creature movement. Feel free to overwrite that method with your own attack game mechanics.

Fly, you fools!

A creature will automatically flee if it notices a predator in its reaction range. For fleeing the creature will also use pathfinding as mentioned above. You can use the moveToPredatorSettings if you want to change the pathfinding behaviour on fleeing.

Don't like cowardly prey creatures? You can disable fleeing:

canFlee: false;

And don't forget that your prey needs to know about its predator for proper fleeing:

predatorClass: "EntityWolf";

Let's take a Walk

Creatures can wander around your level. Just set the wander properties to true:

// wandering on the x axis
canWanderX: true;
// wandering on the y axis (in top down mode)
canWanderY: true;

Wandering also uses pathfinding. You can change the default settings for that via the moveToWanderSettings. Also you can define in percentage how often the creature should switch directions while wandering:

wanderSwitchChance: 0.004;
wanderSwitchChanceStopped: 0.010;

A creature can also wander around its tether. A spawned creature will use its spawner as tether, unless you set the name of another entity as its tether:

tetherName: "carpet";

When tether distance is <= 0, a creature can go as far as it wants:

tetherDistance = 0;

When tether distance is > 0, a creature will only go up to that distance:

tetherDistance = 100;

Unless it can break its tether to follow prey or flee from a predator:

canBreakTether = true;

Learn More!

Creature Class
Character Class
Config file