From 816b85919771d3846c18dec2561b3d4ac4f2539b Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Sun, 6 Jul 2014 17:18:08 -0700 Subject: [PATCH] Close #1 : AI now follows polyline routes on the Routes layer of the map --- moonlight/gfx/map.json | 68 ++++++++++++++++++++++++++++++++++++++- moonlight/src/AISprite.js | 37 +++++++++++++++++---- moonlight/src/Util.js | 10 ++++++ 3 files changed, 108 insertions(+), 7 deletions(-) diff --git a/moonlight/gfx/map.json b/moonlight/gfx/map.json index 0c36f53..1807c02 100644 --- a/moonlight/gfx/map.json +++ b/moonlight/gfx/map.json @@ -291,7 +291,8 @@ "sprite_facing":"left", "sprite_group":"townsfolk-female", "sprite_has_treasure":"true", - "sprite_name":"townsfolk-female-2" + "sprite_name":"townsfolk-female-2", + "sprite_route":"BigTopRoute1" }, "type":"AI", "visible":true, @@ -600,6 +601,71 @@ "width":50, "x":0, "y":0 + }, + { + "height":50, + "name":"Routes", + "objects":[ + { + "height":0, + "name":"BigTopRoute1", + "polyline":[ + { + "x":0, + "y":0 + }, + { + "x":0, + "y":128 + }, + { + "x":-64, + "y":192 + }, + { + "x":-64, + "y":320 + }, + { + "x":96, + "y":320 + }, + { + "x":96, + "y":128 + }, + { + "x":128, + "y":32 + }, + { + "x":128, + "y":-64 + }, + { + "x":32, + "y":-64 + }, + { + "x":0, + "y":0 + }], + "properties": + { + + }, + "type":"", + "visible":true, + "width":0, + "x":640, + "y":160 + }], + "opacity":1, + "type":"objectgroup", + "visible":true, + "width":50, + "x":0, + "y":0 }], "orientation":"orthogonal", "properties": diff --git a/moonlight/src/AISprite.js b/moonlight/src/AISprite.js index e7889af..b0b7224 100644 --- a/moonlight/src/AISprite.js +++ b/moonlight/src/AISprite.js @@ -605,11 +605,33 @@ var AISprite = function(game, x, y, key, frame) { } return; } - if ( game.rnd.integerInRange(0, 100) < 95 ) - return; - this.turnUnseenDirection(); - addState(this, STATE_MOVING); - setSpriteMovement(this); + if ( isSet(this.sprite_route) == true ) { + if ( this.path.length > 0 && this.path_index >= this.path.length ) { + this.sprite_route_index += 1; + if ( this.sprite_route_index >= this.sprite_route.polyline.length ) + this.sprite_route_index = 0; + this.path_purge(); + } + var dpoint = this.sprite_route.polyline[this.sprite_route_index]; + if ( isSet(this.target) == false ) { + this.target = new Phaser.Sprite(null, + this.sprite_route.x + dpoint[0], + this.sprite_route.y + dpoint[1]); + } else { + this.target.x = this.sprite_route.x + dpoint[0]; + this.target.y = this.sprite_route.y + dpoint[1]; + } + this.chasetarget(this.target, + STATE_NONE, + STATE_MOVING, + false); + } + + // if ( game.rnd.integerInRange(0, 100) < 95 ) + // return; + // this.turnUnseenDirection(); + // addState(this, STATE_MOVING); + // setSpriteMovement(this); } this.update = function() @@ -677,8 +699,9 @@ var AISprite = function(game, x, y, key, frame) { if ( this.sprite_has_treasure ) { this.sprite_treasure = getRandomTreasure(); } - this.path_maximum_steps = parseInt(this.path_maximum_steps); + if ( isSet(this.sprite_route) == true ) + this.sprite_route = getRouteByName(this.sprite_route); this.loadTexture(this.sprite_name, 0); addAnimation(this, 'bipedwalkleft'); addAnimation(this, 'bipedwalkright'); @@ -745,6 +768,8 @@ var AISprite = function(game, x, y, key, frame) { this.body.collideWorldBounds = true; this.sprite_name = "townsfolk-male-1"; this.sprite_group = "townsfolk-male"; + this.sprite_route = null; + this.sprite_route_index = 0; this.update_new_values(); } diff --git a/moonlight/src/Util.js b/moonlight/src/Util.js index f95ee39..d849f30 100644 --- a/moonlight/src/Util.js +++ b/moonlight/src/Util.js @@ -343,3 +343,13 @@ function getRandomTreasure() var treasure = treasures[game.rnd.integerInRange(0, treasures.length-1)]; return treasure; } + +function getRouteByName(name) +{ + var routes = game.state.states.game.map.collision.Routes; + for ( var i = 0 ; i < routes.length ; i++ ) { + if ( routes[i]['name'] == name ) + return routes[i]; + } + throw("Could not locate path " + name); +}