Close #3 : Hunt / Search behavior works as expected
This commit is contained in:
@@ -76,6 +76,9 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
this.setAwarenessEffect = function(state) {
|
this.setAwarenessEffect = function(state) {
|
||||||
var animkey = "";
|
var animkey = "";
|
||||||
|
|
||||||
|
if ( state == STATE_NONE )
|
||||||
|
return;
|
||||||
|
|
||||||
if ( hasState(this, state) == true ) {
|
if ( hasState(this, state) == true ) {
|
||||||
// restart the awareness timer
|
// restart the awareness timer
|
||||||
this.startAwarenessTimer();
|
this.startAwarenessTimer();
|
||||||
@@ -248,7 +251,6 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
gridWithAISprites()
|
gridWithAISprites()
|
||||||
);
|
);
|
||||||
prevpoint = [this.x, this.y];
|
prevpoint = [this.x, this.y];
|
||||||
console.log("New path has at most " + maxsteps + " steps in it");
|
|
||||||
for ( var i = 0 ; i < Math.min(maxsteps, tpath.length) ; i++ ) {
|
for ( var i = 0 ; i < Math.min(maxsteps, tpath.length) ; i++ ) {
|
||||||
if ( (prevpoint[0]+prevpoint[1]) == ((tpath[i][0]*32)+(tpath[i][1]*32)) )
|
if ( (prevpoint[0]+prevpoint[1]) == ((tpath[i][0]*32)+(tpath[i][1]*32)) )
|
||||||
continue;
|
continue;
|
||||||
@@ -335,6 +337,7 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
while ( this.seen_directions.indexOf(newdirection) !== -1 ) {
|
while ( this.seen_directions.indexOf(newdirection) !== -1 ) {
|
||||||
newdirection = directions[game.rnd.integerInRange(0, 3)];
|
newdirection = directions[game.rnd.integerInRange(0, 3)];
|
||||||
}
|
}
|
||||||
|
this.seen_directions.push(newdirection);
|
||||||
setMovingState(this, newdirection);
|
setMovingState(this, newdirection);
|
||||||
this.animations.stop();
|
this.animations.stop();
|
||||||
this.animations.play(getMovingAnimationName(this));
|
this.animations.play(getMovingAnimationName(this));
|
||||||
@@ -350,11 +353,6 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
alertedState = (typeof alertedState == 'undefined' ? STATE_ALERTED : alertedState);
|
alertedState = (typeof alertedState == 'undefined' ? STATE_ALERTED : alertedState);
|
||||||
visual = (typeof visual == 'undefined' ? false : visual);
|
visual = (typeof visual == 'undefined' ? false : visual);
|
||||||
movingstate = (typeof alertedState == 'undefined' ? STATE_NONE : movingstate);
|
movingstate = (typeof alertedState == 'undefined' ? STATE_NONE : movingstate);
|
||||||
var rotation_times = {};
|
|
||||||
rotation_times["" + STATE_UNAWARE] = 5000;
|
|
||||||
rotation_times["" + STATE_CONCERNED] = 1000;
|
|
||||||
rotation_times["" + STATE_ALERTED] = 250;
|
|
||||||
rotation_times["" + STATE_LOSTHIM] = 1000;
|
|
||||||
if ( game.physics.arcade.collide(this, target) )
|
if ( game.physics.arcade.collide(this, target) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -366,14 +364,7 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
this.path_set(target, true, maxsteps, useNearestWalkable);
|
this.path_set(target, true, maxsteps, useNearestWalkable);
|
||||||
this.path_tween_start(movingstate);
|
this.path_tween_start(movingstate);
|
||||||
} else {
|
} else {
|
||||||
if ( this.rotation_timer == null ) {
|
this.startTimedRotation();
|
||||||
this.rotation_timer = game.time.create(false);
|
|
||||||
timerev = this.rotation_timer.add(
|
|
||||||
rotation_times["" + getAwarenessState(this)],
|
|
||||||
this.turnUnseenDirection,
|
|
||||||
this);
|
|
||||||
this.rotation_timer.start()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ( this.path_set(target, this.blocked(true), maxsteps, useNearestWalkable) == true ) {
|
if ( this.path_set(target, this.blocked(true), maxsteps, useNearestWalkable) == true ) {
|
||||||
@@ -431,6 +422,7 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
this.target.path_purge();
|
this.target.path_purge();
|
||||||
this.target.setAwarenessEffect(STATE_ALERTED);
|
this.target.setAwarenessEffect(STATE_ALERTED);
|
||||||
this.target.target = this.lastSawPlayerAt;
|
this.target.target = this.lastSawPlayerAt;
|
||||||
|
this.target.lastSawPlayerAt = this.lastSawPlayerAt;
|
||||||
addState(this.target, STATE_RUNNINGTOREPORT);
|
addState(this.target, STATE_RUNNINGTOREPORT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -443,8 +435,10 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
this.awareness_timer.stop();
|
this.awareness_timer.stop();
|
||||||
this.awareness_change_enabled = true;
|
this.awareness_change_enabled = true;
|
||||||
this.setAwarenessEffect(STATE_LOSTHIM);
|
this.setAwarenessEffect(STATE_LOSTHIM);
|
||||||
|
setMovingState(this, STATE_NONE);
|
||||||
this.turnRandomDirection();
|
this.turnRandomDirection();
|
||||||
this.target = null;
|
this.target = null;
|
||||||
|
this.path_purge();
|
||||||
delState(this, STATE_RUNNINGTOLIGHT);
|
delState(this, STATE_RUNNINGTOLIGHT);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -458,21 +452,56 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.action_huntplayer = function()
|
this.random_huntable_target = function(hunt_radius) {
|
||||||
{
|
hunt_radius = (typeof hunt_radius == 'undefined' ? this.hunt_radius : hunt_radius);
|
||||||
console.log("I AM HUNTING FOR THE PLAYER");
|
|
||||||
if ( this.path.length < 1 ||
|
|
||||||
this.path_index >= this.path.length ) {
|
|
||||||
var curmap = game.state.states.game.map;
|
var curmap = game.state.states.game.map;
|
||||||
var intgridx = parseInt(this.state_changed_at.x/32);
|
var intgridx = parseInt(this.state_changed_at.x/32);
|
||||||
var intgridy = parseInt(this.state_changed_at.y/32);
|
var intgridy = parseInt(this.state_changed_at.y/32);
|
||||||
var boundleft = Math.max(0, (intgridx - this.hunt_radius));
|
var boundleft = Math.max(0, (intgridx - hunt_radius));
|
||||||
var boundtop = Math.max(0, (intgridy - this.hunt_radius));
|
var boundtop = Math.max(0, (intgridy - hunt_radius));
|
||||||
var boundright = Math.min(curmap.width, (intgridx + this.hunt_radius));
|
var boundright = Math.min(curmap.width, (intgridx + hunt_radius));
|
||||||
var boundbottom = Math.min(curmap.height, (intgridy + this.hunt_radius));
|
var boundbottom = Math.min(curmap.height, (intgridy + hunt_radius));
|
||||||
var destx = game.rnd.integerInRange(boundleft, boundright);
|
var destx = game.rnd.integerInRange(boundleft, boundright);
|
||||||
var desty = game.rnd.integerInRange(boundtop, boundbottom);
|
var desty = game.rnd.integerInRange(boundtop, boundbottom);
|
||||||
this.target = new Phaser.Sprite(game, destx*32, desty*32, null);
|
return new Phaser.Sprite(game, destx*32, desty*32, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.startTimedRotation = function() {
|
||||||
|
var rotation_times = {};
|
||||||
|
rotation_times["" + STATE_UNAWARE] = 5000;
|
||||||
|
rotation_times["" + STATE_CONCERNED] = 1000;
|
||||||
|
rotation_times["" + STATE_ALERTED] = 250;
|
||||||
|
rotation_times["" + STATE_LOSTHIM] = 1000;
|
||||||
|
if ( isSet(this.rotation_timer) == false ) {
|
||||||
|
this.rotation_timer = game.time.create(false);
|
||||||
|
timerev = this.rotation_timer.add(
|
||||||
|
rotation_times["" + getAwarenessState(this)],
|
||||||
|
this.turnUnseenDirection,
|
||||||
|
this);
|
||||||
|
this.rotation_timer.start()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.action_huntplayer = function()
|
||||||
|
{
|
||||||
|
if ( hasState(this, STATE_LOOKINGFORTARGET) == false &&
|
||||||
|
this.path.length < 1 ) {
|
||||||
|
this.target = this.random_huntable_target()
|
||||||
|
} else if ( hasState(this, STATE_LOOKINGFORTARGET) ) {
|
||||||
|
if ( this.seen_directions.length < 4 ) {
|
||||||
|
this.startTimedRotation();
|
||||||
|
} else {
|
||||||
|
this.seen_directions = [];
|
||||||
|
delState(this, STATE_LOOKINGFORTARGET);
|
||||||
|
this.target = this.random_huntable_target()
|
||||||
|
}
|
||||||
|
} else if ( this.path.length > 0 &&
|
||||||
|
this.path_index >= this.path.length ) {
|
||||||
|
this.path_tween_stop();
|
||||||
|
setMovingState(this, getFaceState(this));
|
||||||
|
setSpriteMovement(this);
|
||||||
|
this.target = null;
|
||||||
|
addState(this, STATE_LOOKINGFORTARGET);
|
||||||
}
|
}
|
||||||
if ( isSet(this.target) ) {
|
if ( isSet(this.target) ) {
|
||||||
this.chasetarget(this.target,
|
this.chasetarget(this.target,
|
||||||
@@ -487,6 +516,14 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
{
|
{
|
||||||
var newstate = STATE_NONE;
|
var newstate = STATE_NONE;
|
||||||
if ( this.sprite_canmove == false) {
|
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);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( game.rnd.integerInRange(0, 100) < 95 )
|
if ( game.rnd.integerInRange(0, 100) < 95 )
|
||||||
@@ -597,7 +634,7 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
this.awareness_timer = null;
|
this.awareness_timer = null;
|
||||||
this.lastSawPlayerAt = null;
|
this.lastSawPlayerAt = null;
|
||||||
this.seen_directions = [];
|
this.seen_directions = [];
|
||||||
this.sprite_awareness_duration = 60000;
|
this.sprite_awareness_duration = 30000;
|
||||||
this.sprite_canmove = 'true';
|
this.sprite_canmove = 'true';
|
||||||
this.collide_with_player = 'true';
|
this.collide_with_player = 'true';
|
||||||
this.collide_with_map = 'true';
|
this.collide_with_map = 'true';
|
||||||
@@ -605,7 +642,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, y);
|
this.origin = new Phaser.Point(x/32, y/32);
|
||||||
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;
|
||||||
|
|||||||
@@ -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();
|
||||||
@@ -259,7 +259,7 @@ GameState.prototype.update = function()
|
|||||||
if ( x.collide_with_player == false )
|
if ( x.collide_with_player == false )
|
||||||
return;
|
return;
|
||||||
if ( x.canSeeSprite(player, false) == true ) {
|
if ( x.canSeeSprite(player, false) == true ) {
|
||||||
x.lastSawPlayerAt = new Phaser.Point(player.x, player.y);
|
x.lastSawPlayerAt = new Phaser.Sprite(game, player.x, player.y, null);
|
||||||
if ( this.physics.arcade.collide(x, player) ) {
|
if ( this.physics.arcade.collide(x, player) ) {
|
||||||
x.setAwarenessEffect(STATE_ALERTED);
|
x.setAwarenessEffect(STATE_ALERTED);
|
||||||
} else if ( player.lightmeter >= x.sprite_can_see_lightmeter ) {
|
} else if ( player.lightmeter >= x.sprite_can_see_lightmeter ) {
|
||||||
@@ -269,7 +269,9 @@ GameState.prototype.update = function()
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
if ( hasState(x, STATE_LOSTHIM) == false ) {
|
if ( this.physics.arcade.collide(x, player) ) {
|
||||||
|
x.setAwarenessEffect(STATE_CONCERNED);
|
||||||
|
} else if ( hasState(x, STATE_LOSTHIM) == false ) {
|
||||||
x.setAwarenessEffect(STATE_LOSTHIM);
|
x.setAwarenessEffect(STATE_LOSTHIM);
|
||||||
} else {
|
} else {
|
||||||
x.setAwarenessEffect(STATE_UNAWARE);
|
x.setAwarenessEffect(STATE_UNAWARE);
|
||||||
@@ -286,9 +288,9 @@ GameState.prototype.update = function()
|
|||||||
if ( this.aiSprites.debug == true ) {
|
if ( this.aiSprites.debug == true ) {
|
||||||
function _draw_viewrect(x) {
|
function _draw_viewrect(x) {
|
||||||
var r = x.viewRectangle();
|
var r = x.viewRectangle();
|
||||||
if ( isSet(r) )
|
if ( isSet(r) == false )
|
||||||
return;
|
return;
|
||||||
this.shadowTexture.context.fillStyle = 'rgb(128, 128, 128)';
|
this.shadowTexture.context.fillStyle = 'rgb(128, 128, 255)';
|
||||||
this.shadowTexture.context.fillRect(r.left,
|
this.shadowTexture.context.fillRect(r.left,
|
||||||
r.top,
|
r.top,
|
||||||
r.width,
|
r.width,
|
||||||
@@ -297,7 +299,7 @@ GameState.prototype.update = function()
|
|||||||
this.aiSprites.forEach(_draw_viewrect, this);
|
this.aiSprites.forEach(_draw_viewrect, this);
|
||||||
function _draw_aipath(x) {
|
function _draw_aipath(x) {
|
||||||
var p = x.path;
|
var p = x.path;
|
||||||
if ( isSet(p) )
|
if ( isSet(p) == false)
|
||||||
return;
|
return;
|
||||||
this.shadowTexture.context.fillStyle = 'rgb(255, 128, 128)';
|
this.shadowTexture.context.fillStyle = 'rgb(255, 128, 128)';
|
||||||
p.forEach(function(r) {
|
p.forEach(function(r) {
|
||||||
|
|||||||
@@ -275,42 +275,42 @@ var moonlightSettings = {
|
|||||||
'loop': false
|
'loop': false
|
||||||
},
|
},
|
||||||
'bipedwalkdown': {
|
'bipedwalkdown': {
|
||||||
'frames': [1, 2, 0],
|
'frames': [1, 0, 1, 2],
|
||||||
'speed': 4,
|
'speed': 4,
|
||||||
'loop': true
|
'loop': true
|
||||||
},
|
},
|
||||||
'bipedwalkleft': {
|
'bipedwalkleft': {
|
||||||
'frames': [4, 5, 3],
|
'frames': [4, 3, 4, 5],
|
||||||
'speed': 4,
|
'speed': 4,
|
||||||
'loop': true
|
'loop': true
|
||||||
},
|
},
|
||||||
'bipedwalkright': {
|
'bipedwalkright': {
|
||||||
'frames': [7, 8, 6],
|
'frames': [7, 6, 7, 8],
|
||||||
'speed': 4,
|
'speed': 4,
|
||||||
'loop': true
|
'loop': true
|
||||||
},
|
},
|
||||||
'bipedwalkup': {
|
'bipedwalkup': {
|
||||||
'frames': [10, 11, 9],
|
'frames': [10, 9, 10, 11],
|
||||||
'speed': 4,
|
'speed': 4,
|
||||||
'loop': true
|
'loop': true
|
||||||
},
|
},
|
||||||
'bipedrundown': {
|
'bipedrundown': {
|
||||||
'frames': [1, 2, 0],
|
'frames': [1, 0, 1, 2],
|
||||||
'speed': 12,
|
'speed': 12,
|
||||||
'loop': true
|
'loop': true
|
||||||
},
|
},
|
||||||
'bipedrunleft': {
|
'bipedrunleft': {
|
||||||
'frames': [4, 5, 3],
|
'frames': [4, 3, 4, 5],
|
||||||
'speed': 12,
|
'speed': 12,
|
||||||
'loop': true
|
'loop': true
|
||||||
},
|
},
|
||||||
'bipedrunright': {
|
'bipedrunright': {
|
||||||
'frames': [7, 8, 6],
|
'frames': [7, 6, 7, 8],
|
||||||
'speed': 12,
|
'speed': 12,
|
||||||
'loop': true
|
'loop': true
|
||||||
},
|
},
|
||||||
'bipedrunup': {
|
'bipedrunup': {
|
||||||
'frames': [10, 11, 9],
|
'frames': [10, 9, 10, 11],
|
||||||
'speed': 12,
|
'speed': 12,
|
||||||
'loop': true
|
'loop': true
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user