AI returns to their origin when they cant move and are in wandering state. AI gets their facing state from the map. Guards can re-encounter you while traveling back to the origin.

This commit is contained in:
2014-06-27 18:04:34 -07:00
parent 6873a1ba53
commit 9709a87fb0
4 changed files with 26 additions and 14 deletions

View File

@@ -277,7 +277,7 @@
"visible":true, "visible":true,
"width":0, "width":0,
"x":320, "x":320,
"y":521 "y":512
}, },
{ {
"gid":3544, "gid":3544,
@@ -323,8 +323,8 @@
"type":"AI", "type":"AI",
"visible":true, "visible":true,
"width":0, "width":0,
"x":158, "x":160,
"y":830 "y":832
}], }],
"opacity":1, "opacity":1,
"type":"objectgroup", "type":"objectgroup",

View File

@@ -514,15 +514,18 @@ var AISprite = function(game, x, y, key, frame) {
this.action_wander = function() this.action_wander = function()
{ {
var newstate = STATE_NONE;
if ( this.sprite_canmove == false) { if ( this.sprite_canmove == false) {
if ( this.x !== this.origin.x || if ( this.x !== this.origin.x ||
this.y !== this.origin.y ) { this.y !== this.origin.y ) {
this.chasetarget(this.origin, this.chasetarget(this.origin,
STATE_NONE, STATE_NONE,
STATE_MOVING, STATE_MOVING,
false, false);
1000); } else {
setMovingState(this, faceStateFromString(this.sprite_facing));
this.animations.stop();
this.animations.play(getMovingAnimationName(this));
this.animations.stop();
} }
return; return;
} }
@@ -597,7 +600,7 @@ var AISprite = function(game, x, y, key, frame) {
addAnimation(this, 'bipedrunright'); addAnimation(this, 'bipedrunright');
addAnimation(this, 'bipedrunup'); addAnimation(this, 'bipedrunup');
addAnimation(this, 'bipedrundown'); addAnimation(this, 'bipedrundown');
setMovingState(this, STATE_FACE_DOWN); setMovingState(this, faceStateFromString(this.sprite_facing));
setSpriteMovement(this); setSpriteMovement(this);
this.ready_to_update = true; this.ready_to_update = true;
} }
@@ -620,6 +623,7 @@ var AISprite = function(game, x, y, key, frame) {
game.physics.arcade.enable(this); game.physics.arcade.enable(this);
this.body.immovable = true; this.body.immovable = true;
pathfinder_grid = []; pathfinder_grid = [];
this.sprite_facing = "down";
this.walkables = []; this.walkables = [];
this.path = []; this.path = [];
this.state_changed_at = new Phaser.Point(this.x, this.y); this.state_changed_at = new Phaser.Point(this.x, this.y);
@@ -642,7 +646,7 @@ var AISprite = function(game, x, y, key, frame) {
this.view_distance = 32 * 5; this.view_distance = 32 * 5;
this.timer = null; this.timer = null;
this.rotation_timer = null; this.rotation_timer = null;
this.origin = new Phaser.Point(x/32, y/32); this.origin = new Phaser.Point(x, y);
this.bubble_immediate = false; this.bubble_immediate = false;
this.bubble_text = null; this.bubble_text = null;
this.enable_word_bubble = false; this.enable_word_bubble = false;

View File

@@ -23,7 +23,7 @@ GameState.prototype.create = function()
); );
if ( lp['inject_sprites'] == true ) { if ( lp['inject_sprites'] == true ) {
this.aiSprites = game.add.group(); this.aiSprites = game.add.group();
this.aiSprites.debug = false; this.aiSprites.debug = true;
this.map.createFromObjects('AI', 3544, 'player', 0, true, false, this.aiSprites, AISprite); this.map.createFromObjects('AI', 3544, 'player', 0, true, false, this.aiSprites, AISprite);
this.aiSprites.forEach(function(spr) { this.aiSprites.forEach(function(spr) {
spr.update_new_values(); spr.update_new_values();
@@ -285,7 +285,7 @@ GameState.prototype.update = function()
this.aiSprites.forEach(_inner_collide, this); this.aiSprites.forEach(_inner_collide, this);
this.updateShadowTexture(); this.updateShadowTexture();
if ( this.aiSprites.debug == true ) { if ( this.aiSprites.debug == false ) {
function _draw_viewrect(x) { function _draw_viewrect(x) {
var r = x.viewRectangle(); var r = x.viewRectangle();
if ( isSet(r) == false ) if ( isSet(r) == false )

View File

@@ -101,6 +101,9 @@ function nearestWalkableTile(spr)
return null; return null;
} }
if ( grid.nodes[spr.y/32][spr.x/32].walkable == true )
return [spr.x/32, spr.y/32];
for ( var i = 1 ; i < 100 ; i++ ) { for ( var i = 1 ; i < 100 ; i++ ) {
var rv = _walkable_inner(i); var rv = _walkable_inner(i);
if ( isSet(rv) ) { if ( isSet(rv) ) {
@@ -109,11 +112,7 @@ function nearestWalkableTile(spr)
return rv return rv
} }
} }
//if ( multiplier >= 10 )
console.log("Couldn't find a near walkable tile");
console.log([spr.x / 32, spr.y / 32]);
return [parseInt(spr.x / 32), parseInt(spr.y / 32)]; return [parseInt(spr.x / 32), parseInt(spr.y / 32)];
//return nearestWalkableTile(spr, multiplier + 1);
} }
function addAnimation(obj, anim) function addAnimation(obj, anim)
@@ -134,6 +133,15 @@ function getAwarenessState(spr)
return STATE_LOSTHIM; return STATE_LOSTHIM;
} }
function faceStateFromString(face)
{
var states = { "up": STATE_FACE_UP,
"down": STATE_FACE_DOWN,
"left": STATE_FACE_LEFT,
"right": STATE_FACE_RIGHT };
return states[face.toLowerCase()];
}
function getFaceState(spr) function getFaceState(spr)
{ {
if ( hasState(spr, STATE_FACE_LEFT) ) if ( hasState(spr, STATE_FACE_LEFT) )