Close #3 : Hunt / Search behavior works as expected

This commit is contained in:
2014-06-26 23:39:32 -07:00
parent 4f8a09581d
commit a1fb550c64
3 changed files with 83 additions and 44 deletions

View File

@@ -76,6 +76,9 @@ var AISprite = function(game, x, y, key, frame) {
this.setAwarenessEffect = function(state) {
var animkey = "";
if ( state == STATE_NONE )
return;
if ( hasState(this, state) == true ) {
// restart the awareness timer
this.startAwarenessTimer();
@@ -248,7 +251,6 @@ var AISprite = function(game, x, y, key, frame) {
gridWithAISprites()
);
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++ ) {
if ( (prevpoint[0]+prevpoint[1]) == ((tpath[i][0]*32)+(tpath[i][1]*32)) )
continue;
@@ -335,6 +337,7 @@ var AISprite = function(game, x, y, key, frame) {
while ( this.seen_directions.indexOf(newdirection) !== -1 ) {
newdirection = directions[game.rnd.integerInRange(0, 3)];
}
this.seen_directions.push(newdirection);
setMovingState(this, newdirection);
this.animations.stop();
this.animations.play(getMovingAnimationName(this));
@@ -350,11 +353,6 @@ var AISprite = function(game, x, y, key, frame) {
alertedState = (typeof alertedState == 'undefined' ? STATE_ALERTED : alertedState);
visual = (typeof visual == 'undefined' ? false : visual);
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) )
return;
@@ -366,14 +364,7 @@ var AISprite = function(game, x, y, key, frame) {
this.path_set(target, true, maxsteps, useNearestWalkable);
this.path_tween_start(movingstate);
} else {
if ( this.rotation_timer == null ) {
this.rotation_timer = game.time.create(false);
timerev = this.rotation_timer.add(
rotation_times["" + getAwarenessState(this)],
this.turnUnseenDirection,
this);
this.rotation_timer.start()
}
this.startTimedRotation();
}
} else {
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.setAwarenessEffect(STATE_ALERTED);
this.target.target = this.lastSawPlayerAt;
this.target.lastSawPlayerAt = this.lastSawPlayerAt;
addState(this.target, STATE_RUNNINGTOREPORT);
}
}
@@ -443,8 +435,10 @@ var AISprite = function(game, x, y, key, frame) {
this.awareness_timer.stop();
this.awareness_change_enabled = true;
this.setAwarenessEffect(STATE_LOSTHIM);
setMovingState(this, STATE_NONE);
this.turnRandomDirection();
this.target = null;
this.path_purge();
delState(this, STATE_RUNNINGTOLIGHT);
return;
}
@@ -458,21 +452,56 @@ var AISprite = function(game, x, y, key, frame) {
}
}
this.action_huntplayer = function()
{
console.log("I AM HUNTING FOR THE PLAYER");
if ( this.path.length < 1 ||
this.path_index >= this.path.length ) {
this.random_huntable_target = function(hunt_radius) {
hunt_radius = (typeof hunt_radius == 'undefined' ? this.hunt_radius : hunt_radius);
var curmap = game.state.states.game.map;
var intgridx = parseInt(this.state_changed_at.x/32);
var intgridy = parseInt(this.state_changed_at.y/32);
var boundleft = Math.max(0, (intgridx - this.hunt_radius));
var boundtop = Math.max(0, (intgridy - this.hunt_radius));
var boundright = Math.min(curmap.width, (intgridx + this.hunt_radius));
var boundbottom = Math.min(curmap.height, (intgridy + this.hunt_radius));
var boundleft = Math.max(0, (intgridx - hunt_radius));
var boundtop = Math.max(0, (intgridy - hunt_radius));
var boundright = Math.min(curmap.width, (intgridx + hunt_radius));
var boundbottom = Math.min(curmap.height, (intgridy + hunt_radius));
var destx = game.rnd.integerInRange(boundleft, boundright);
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) ) {
this.chasetarget(this.target,
@@ -487,6 +516,14 @@ var AISprite = function(game, x, y, key, frame) {
{
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);
}
return;
}
if ( game.rnd.integerInRange(0, 100) < 95 )
@@ -597,7 +634,7 @@ var AISprite = function(game, x, y, key, frame) {
this.awareness_timer = null;
this.lastSawPlayerAt = null;
this.seen_directions = [];
this.sprite_awareness_duration = 60000;
this.sprite_awareness_duration = 30000;
this.sprite_canmove = 'true';
this.collide_with_player = 'true';
this.collide_with_map = 'true';
@@ -605,7 +642,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, y);
this.origin = new Phaser.Point(x/32, y/32);
this.bubble_immediate = false;
this.bubble_text = null;
this.enable_word_bubble = false;

View File

@@ -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();
@@ -259,7 +259,7 @@ GameState.prototype.update = function()
if ( x.collide_with_player == false )
return;
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) ) {
x.setAwarenessEffect(STATE_ALERTED);
} else if ( player.lightmeter >= x.sprite_can_see_lightmeter ) {
@@ -269,7 +269,9 @@ GameState.prototype.update = function()
}
return;
} 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);
} else {
x.setAwarenessEffect(STATE_UNAWARE);
@@ -286,9 +288,9 @@ GameState.prototype.update = function()
if ( this.aiSprites.debug == true ) {
function _draw_viewrect(x) {
var r = x.viewRectangle();
if ( isSet(r) )
if ( isSet(r) == false )
return;
this.shadowTexture.context.fillStyle = 'rgb(128, 128, 128)';
this.shadowTexture.context.fillStyle = 'rgb(128, 128, 255)';
this.shadowTexture.context.fillRect(r.left,
r.top,
r.width,
@@ -297,7 +299,7 @@ GameState.prototype.update = function()
this.aiSprites.forEach(_draw_viewrect, this);
function _draw_aipath(x) {
var p = x.path;
if ( isSet(p) )
if ( isSet(p) == false)
return;
this.shadowTexture.context.fillStyle = 'rgb(255, 128, 128)';
p.forEach(function(r) {

View File

@@ -275,42 +275,42 @@ var moonlightSettings = {
'loop': false
},
'bipedwalkdown': {
'frames': [1, 2, 0],
'frames': [1, 0, 1, 2],
'speed': 4,
'loop': true
},
'bipedwalkleft': {
'frames': [4, 5, 3],
'frames': [4, 3, 4, 5],
'speed': 4,
'loop': true
},
'bipedwalkright': {
'frames': [7, 8, 6],
'frames': [7, 6, 7, 8],
'speed': 4,
'loop': true
},
'bipedwalkup': {
'frames': [10, 11, 9],
'frames': [10, 9, 10, 11],
'speed': 4,
'loop': true
},
'bipedrundown': {
'frames': [1, 2, 0],
'frames': [1, 0, 1, 2],
'speed': 12,
'loop': true
},
'bipedrunleft': {
'frames': [4, 5, 3],
'frames': [4, 3, 4, 5],
'speed': 12,
'loop': true
},
'bipedrunright': {
'frames': [7, 8, 6],
'frames': [7, 6, 7, 8],
'speed': 12,
'loop': true
},
'bipedrunup': {
'frames': [10, 11, 9],
'frames': [10, 9, 10, 11],
'speed': 12,
'loop': true
},