Close #1 : AI now follows polyline routes on the Routes layer of the map

This commit is contained in:
2014-07-06 17:18:08 -07:00
parent 3e27b50a80
commit 816b859197
3 changed files with 108 additions and 7 deletions

View File

@@ -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":

View File

@@ -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();
}

View File

@@ -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);
}