From f782d54ff9dccf4723c92dfe3b739a8b3f528d97 Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Thu, 19 Jun 2014 17:30:52 -0700 Subject: [PATCH] Refactor chasing code into a generic target chaser --- README.md | 4 ++++ moonlight/js/moonlight-skulk.js | 39 ++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index dfa4292..d91068b 100644 --- a/README.md +++ b/README.md @@ -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. +## 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 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. diff --git a/moonlight/js/moonlight-skulk.js b/moonlight/js/moonlight-skulk.js index 190ee09..9aa9192 100644 --- a/moonlight/js/moonlight-skulk.js +++ b/moonlight/js/moonlight-skulk.js @@ -942,8 +942,9 @@ var AISprite = function(game, x, y, key, frame) { 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 = []; prevpos = [this.x, this.y] 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; - if ( game.physics.arcade.collide(this, player) ) + alertedState = (typeof alertedState == undefined ? alertedState : STATE_ALERTED); + visual = (typeof visual == undefined ? visual : false); + if ( game.physics.arcade.collide(this, target) ) return; if ( this.path_index >= this.path.length ) { this.path_tween_stop(); console.log("I am at the end of my path"); - if ( this.canSeeSprite(player, false) == true ) { - console.log("I can see the player"); - this.setAwarenessEffect(STATE_ALERTED); - this.path_set(player, true); - this.path_tween_start(); + if ( (visual == false) || (this.canSeeSprite(target, false) == true )) { + console.log("I can see the target"); + this.setAwarenessEffect(alertedState); + this.path_set(target, true); + this.path_tween_start(movingstate); } else { 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); timerev = this.rotation_timer.add(250, this.turnUnseenDirection, this); this.rotation_timer.start() } } } 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"); - if ( this.canSeeSprite(player, false) == false ) { + if ( (visual == false) || (this.canSeeSprite(target, false) == false )) { this.path_purge(); this.path_tween_stop(); } else { - this.setAwarenessEffect(STATE_ALERTED); - this.path_tween_start(); + this.setAwarenessEffect(alertedState); + this.path_tween_start(movingstate); } } } + } + + this.action_chaseplayer = function() + { + var movingstate = STATE_NONE; + this.action_chasetarget(player); return; } @@ -1075,7 +1083,8 @@ var AISprite = function(game, x, y, key, frame) { if ( game.rnd.integerInRange(0, 100) < 95 ) return; this.turnUnseenDirection(); - setMovingState(this, this.state | STATE_MOVING); + addState(this, STATE_MOVING); + setSpriteMovement(this); } this.update = function()