AI 2D V1

by Sam Fereday

JavaScript

// AI 2D v1. (will totally need work doing)
// treat each function as its own unit.
// ensure unit testing works (http://unitjs.com/)
// do it clean, simply and properly for minimal overhead
// change 'state' to 'mode'
var majorFactor = false;

// Getting current target is a case of seeing what you're looking at and if the thing is targetable. So if a player wanders in to the collision / raycast, then that'll be your current target
var currentTarget = {
	name: "player"
}

// Preset fuzzy modes (same as below)
var checkForObstacle = function() {};

// Preset major modes (contain sub modes, integrate these first off before
// putting in preset patterns, we can feed that lot in after.
/* There's a lot more to all the below such as sub logic. Also, switching between modes. So you might be attacking but then choose to be cautious or flee.
Patterns will be set in the data for the entity that will govern what it's going to do habit wise, etc. Same for bosses. Patterns can get quite advanced with specifying scripted movements and moves to either pick randomly or base on various tactics. But we'll get to that later.
Don't forget that each mode will need access to various variables from the entity, surrounding data as well so they know how to act with the given entity patterns. */
// Note: See the AI book for notes on flee and seek. Some good algorithms in there.
// The functions below just have function names to placehold where you might put your code. For instance 'roamPattern' doesn't need to be another function, you can just stick it in where it's called. But at this point it helps with clarity at least.
var attack = function(){

	/*
  1) Get target
  2) Move towards target
  3) If in range, use attack move (with random pause for variation)
  */
   
  if(distance(this, target.position) < desiredRange) {
  	 attackPattern(target, patternID);
  } else {
  	 moveTowards(target);
  }


};

var cautious = function(){

	/*
  1) Get target
  2) Compare stats
  3) Use own...