Refactor chasing code into a generic target chaser

This commit is contained in:
2014-06-19 17:30:52 -07:00
parent b17493942b
commit f782d54ff9
2 changed files with 28 additions and 15 deletions

View File

@@ -8,6 +8,10 @@ Development notes
This stuff is just for me to keep track, since I'm afraid using GitHub's issue tracker or Wiki would bloat my process, and since I only have 4 days, I can't afford that. This stuff is just for me to keep track, since I'm afraid using GitHub's issue tracker or Wiki would bloat my process, and since I only have 4 days, I can't afford that.
## TODO : Import SAT and use polygonal cones for vision testing
http://jriecken.github.io/sat-js/ - I had tried using Phaser's bits for this interally, but it was janky, so I put it off and used rectangles instead. Should really get around to using this.
## TODO : AI should wander around the map ## TODO : AI should wander around the map
The AI should, in their default states, wander around the map aimlessly. They should restrict themselves to normal pedestrian traffic lanes, which will likely require the marking of such lanes in TilED. The AI should, in their default states, wander around the map aimlessly. They should restrict themselves to normal pedestrian traffic lanes, which will likely require the marking of such lanes in TilED.

View File

@@ -942,8 +942,9 @@ var AISprite = function(game, x, y, key, frame) {
return true; return true;
} }
this.path_tween_start = function() this.path_tween_start = function(movingstate)
{ {
movingState = (typeof movementState == undefined ? movementState : (STATE_MOVING | STATE_RUNNING);
this.path_tweens = []; this.path_tweens = [];
prevpos = [this.x, this.y] prevpos = [this.x, this.y]
for ( var i = 0; for ( var i = 0;
@@ -1017,40 +1018,47 @@ var AISprite = function(game, x, y, key, frame) {
} }
} }
this.action_chaseplayer = function() this.chasetarget = function(target, alertedState, movingstate, visual)
{ {
var movingstate = STATE_NONE; alertedState = (typeof alertedState == undefined ? alertedState : STATE_ALERTED);
if ( game.physics.arcade.collide(this, player) ) visual = (typeof visual == undefined ? visual : false);
if ( game.physics.arcade.collide(this, target) )
return; return;
if ( this.path_index >= this.path.length ) { if ( this.path_index >= this.path.length ) {
this.path_tween_stop(); this.path_tween_stop();
console.log("I am at the end of my path"); console.log("I am at the end of my path");
if ( this.canSeeSprite(player, false) == true ) { if ( (visual == false) || (this.canSeeSprite(target, false) == true )) {
console.log("I can see the player"); console.log("I can see the target");
this.setAwarenessEffect(STATE_ALERTED); this.setAwarenessEffect(alertedState);
this.path_set(player, true); this.path_set(target, true);
this.path_tween_start(); this.path_tween_start(movingstate);
} else { } else {
if ( this.rotation_timer == null ) { if ( this.rotation_timer == null ) {
console.log("I can't see the player - turning so I can"); console.log("I can't see the target - turning so I can");
this.rotation_timer = game.time.create(false); this.rotation_timer = game.time.create(false);
timerev = this.rotation_timer.add(250, this.turnUnseenDirection, this); timerev = this.rotation_timer.add(250, this.turnUnseenDirection, this);
this.rotation_timer.start() this.rotation_timer.start()
} }
} }
} else { } else {
if ( this.path_set(player, this.blocked(true)) == true ) { if ( this.path_set(target, this.blocked(true)) == true ) {
console.log("I just got a new path"); console.log("I just got a new path");
if ( this.canSeeSprite(player, false) == false ) { if ( (visual == false) || (this.canSeeSprite(target, false) == false )) {
this.path_purge(); this.path_purge();
this.path_tween_stop(); this.path_tween_stop();
} else { } else {
this.setAwarenessEffect(STATE_ALERTED); this.setAwarenessEffect(alertedState);
this.path_tween_start(); this.path_tween_start(movingstate);
} }
} }
} }
}
this.action_chaseplayer = function()
{
var movingstate = STATE_NONE;
this.action_chasetarget(player);
return; return;
} }
@@ -1075,7 +1083,8 @@ var AISprite = function(game, x, y, key, frame) {
if ( game.rnd.integerInRange(0, 100) < 95 ) if ( game.rnd.integerInRange(0, 100) < 95 )
return; return;
this.turnUnseenDirection(); this.turnUnseenDirection();
setMovingState(this, this.state | STATE_MOVING); addState(this, STATE_MOVING);
setSpriteMovement(this);
} }
this.update = function() this.update = function()