From 9709a87fb0b4a54d8063af85a8b48bde4710506b Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Fri, 27 Jun 2014 18:04:34 -0700 Subject: [PATCH] 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. --- moonlight/gfx/map.json | 6 +++--- moonlight/src/AISprite.js | 14 +++++++++----- moonlight/src/GameStates.js | 4 ++-- moonlight/src/Util.js | 16 ++++++++++++---- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/moonlight/gfx/map.json b/moonlight/gfx/map.json index e293fb0..4ffab24 100644 --- a/moonlight/gfx/map.json +++ b/moonlight/gfx/map.json @@ -277,7 +277,7 @@ "visible":true, "width":0, "x":320, - "y":521 + "y":512 }, { "gid":3544, @@ -323,8 +323,8 @@ "type":"AI", "visible":true, "width":0, - "x":158, - "y":830 + "x":160, + "y":832 }], "opacity":1, "type":"objectgroup", diff --git a/moonlight/src/AISprite.js b/moonlight/src/AISprite.js index 15dbf94..da45a9e 100644 --- a/moonlight/src/AISprite.js +++ b/moonlight/src/AISprite.js @@ -514,15 +514,18 @@ var AISprite = function(game, x, y, key, frame) { this.action_wander = function() { - var newstate = STATE_NONE; if ( this.sprite_canmove == false) { if ( this.x !== this.origin.x || this.y !== this.origin.y ) { this.chasetarget(this.origin, STATE_NONE, STATE_MOVING, - false, - 1000); + false); + } else { + setMovingState(this, faceStateFromString(this.sprite_facing)); + this.animations.stop(); + this.animations.play(getMovingAnimationName(this)); + this.animations.stop(); } return; } @@ -597,7 +600,7 @@ var AISprite = function(game, x, y, key, frame) { addAnimation(this, 'bipedrunright'); addAnimation(this, 'bipedrunup'); addAnimation(this, 'bipedrundown'); - setMovingState(this, STATE_FACE_DOWN); + setMovingState(this, faceStateFromString(this.sprite_facing)); setSpriteMovement(this); this.ready_to_update = true; } @@ -620,6 +623,7 @@ var AISprite = function(game, x, y, key, frame) { game.physics.arcade.enable(this); this.body.immovable = true; pathfinder_grid = []; + this.sprite_facing = "down"; this.walkables = []; this.path = []; 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.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_text = null; this.enable_word_bubble = false; diff --git a/moonlight/src/GameStates.js b/moonlight/src/GameStates.js index 2d14df9..4612a83 100644 --- a/moonlight/src/GameStates.js +++ b/moonlight/src/GameStates.js @@ -23,7 +23,7 @@ GameState.prototype.create = function() ); if ( lp['inject_sprites'] == true ) { 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.aiSprites.forEach(function(spr) { spr.update_new_values(); @@ -285,7 +285,7 @@ GameState.prototype.update = function() this.aiSprites.forEach(_inner_collide, this); this.updateShadowTexture(); - if ( this.aiSprites.debug == true ) { + if ( this.aiSprites.debug == false ) { function _draw_viewrect(x) { var r = x.viewRectangle(); if ( isSet(r) == false ) diff --git a/moonlight/src/Util.js b/moonlight/src/Util.js index 5f2b3d6..d159d1c 100644 --- a/moonlight/src/Util.js +++ b/moonlight/src/Util.js @@ -101,6 +101,9 @@ function nearestWalkableTile(spr) 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++ ) { var rv = _walkable_inner(i); if ( isSet(rv) ) { @@ -109,11 +112,7 @@ function nearestWalkableTile(spr) 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 nearestWalkableTile(spr, multiplier + 1); } function addAnimation(obj, anim) @@ -134,6 +133,15 @@ function getAwarenessState(spr) 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) { if ( hasState(spr, STATE_FACE_LEFT) )