Making AI able to 'see' the player

This commit is contained in:
2014-06-14 15:06:02 -07:00
parent 2ae5caec21
commit df45cbd9e1

View File

@@ -588,7 +588,7 @@ EffectSprite.prototype = Object.create(Phaser.Sprite.prototype);
EffectSprite.prototype.constructor = EffectSprite; EffectSprite.prototype.constructor = EffectSprite;
var AISprite = function(game, x, y, key, frame) { var AISprite = function(game, x, y, key, frame) {
this.canSeeSprite = function(spr) { this.canSeeSprite = function(spr, debug=false) {
var xd = (spr.x - this.x); var xd = (spr.x - this.x);
if ( xd < 0 ) if ( xd < 0 )
xd = -(xd); xd = -(xd);
@@ -598,6 +598,8 @@ var AISprite = function(game, x, y, key, frame) {
var hyp = Math.sqrt(Number(xd * xd) + Number(yd * yd)); var hyp = Math.sqrt(Number(xd * xd) + Number(yd * yd));
if ( hyp > this.view_distance ) { if ( hyp > this.view_distance ) {
if ( debug == true )
console.log(spr + " is too far away");
return false; return false;
} }
// View cone intersection test // View cone intersection test
@@ -658,11 +660,18 @@ var AISprite = function(game, x, y, key, frame) {
var withinView = false; var withinView = false;
rectLines.forEach(function(sl) { rectLines.forEach(function(sl) {
[[p1, p2], [p2, p3], [p3, p4], [p4, p1]].forEach(function(vl) { [[p1, p2], [p2, p3], [p3, p4], [p4, p1]].forEach(function(vl) {
console.log("sl vs tl");
tl = new Phaser.Line(vl[0].x, vl[0].y, tl = new Phaser.Line(vl[0].x, vl[0].y,
vl[1].x, vl[1].y); vl[1].x, vl[1].y);
if ( debug == true ) {
console.log(sl);
console.log(tl);
}
if ( tl.intersects(sl) ) if ( tl.intersects(sl) )
return true; return true;
}, this); }, this);
if ( debug == true )
console.log("contains ? " + sl.start.x + " " + sl.start.y);
if ( viewcone.contains(sl.start.x, sl.start.y) ) if ( viewcone.contains(sl.start.x, sl.start.y) )
return true; return true;
}, this); }, this);