2014-07-25 12:09:17 +02:00
|
|
|
(function(
|
|
|
|
Engine,
|
|
|
|
Vector
|
|
|
|
){
|
|
|
|
|
2014-07-26 05:28:47 +02:00
|
|
|
Engine.Point.Puller = function(id, x, y, shapeSize){
|
2014-07-25 12:09:17 +02:00
|
|
|
this.id = id;
|
|
|
|
|
2014-07-26 05:28:47 +02:00
|
|
|
this.shapeSize = shapeSize;
|
2014-07-26 22:52:03 +02:00
|
|
|
this.ref = new Vector(x, y);
|
2014-07-26 05:28:47 +02:00
|
|
|
|
2014-07-26 22:52:03 +02:00
|
|
|
this.pos = new Vector(
|
|
|
|
x * shapeSize.x,
|
|
|
|
y * shapeSize.y
|
|
|
|
);
|
2014-07-26 05:28:47 +02:00
|
|
|
|
|
|
|
this.home = this.pos.clone();
|
2014-07-25 12:09:17 +02:00
|
|
|
this.accel = Vector.coerce(this.accel);
|
2014-07-26 05:28:47 +02:00
|
|
|
this.vel = Vector.coerce(this.vel);
|
2014-07-25 12:09:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Engine.Point.Puller.prototype = {
|
|
|
|
|
|
|
|
fillStyle: null,
|
|
|
|
defaultFillstyle: '#b976ff',
|
|
|
|
chasingFillstyle: '#ff6b6b',
|
|
|
|
|
|
|
|
radius: 1,
|
|
|
|
|
|
|
|
maxSpeed: 160,
|
|
|
|
maxForce: 50,
|
|
|
|
|
|
|
|
pos: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
accel: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
vel: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
aRad: 200,
|
|
|
|
|
|
|
|
safety: 0.25,
|
|
|
|
|
2014-07-26 05:28:47 +02:00
|
|
|
resize: function(){
|
|
|
|
this.home.x = this.pos.x = this.ref.x * this.shapeSize.x;
|
|
|
|
this.home.y = this.pos.y = this.ref.y * this.shapeSize.y;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2014-07-25 12:09:17 +02:00
|
|
|
update: function(engine){
|
|
|
|
var target = Vector.coerce(engine.mouse),
|
2014-07-26 05:28:47 +02:00
|
|
|
distanceToMouse, toHome, mag, safety;
|
|
|
|
|
|
|
|
target.x += (this.shapeSize.x - engine.width) / 2;
|
|
|
|
target.y += (this.shapeSize.y - engine.height) / 2;
|
|
|
|
|
|
|
|
distanceToMouse = this.distanceTo(target);
|
2014-07-25 12:09:17 +02:00
|
|
|
|
|
|
|
this.accel.mult(0);
|
|
|
|
|
|
|
|
if (distanceToMouse < this.aRad) {
|
|
|
|
this._chasing = true;
|
|
|
|
this.toChase(target);
|
|
|
|
this.fillStyle = this.chasingFillstyle;
|
|
|
|
} else {
|
|
|
|
this._chasing = false;
|
|
|
|
this.fillStyle = this.defaultFillstyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.toChase(this.home, this.maxForce / 2);
|
|
|
|
|
|
|
|
this.vel.add(this.accel);
|
|
|
|
this.pos.add(
|
|
|
|
Vector.mult(this.vel, engine.tick)
|
|
|
|
);
|
|
|
|
|
|
|
|
toHome = Vector.sub(this.home, this.pos);
|
|
|
|
mag = toHome.mag();
|
|
|
|
safety = this.aRad * (this.safety * 3);
|
|
|
|
if (mag > this.aRad - safety) {
|
|
|
|
toHome.normalize();
|
|
|
|
toHome.mult(this.aRad - safety);
|
|
|
|
this.pos = Vector.sub(this.home, toHome);
|
|
|
|
}
|
2014-07-26 05:28:47 +02:00
|
|
|
|
2014-07-26 08:09:04 +02:00
|
|
|
target = null;
|
|
|
|
toHome = null;
|
2014-07-26 05:28:47 +02:00
|
|
|
return this;
|
2014-07-25 12:09:17 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
toChase: function(target, maxForce){
|
|
|
|
var desired, steer, distance, mult, safety;
|
|
|
|
|
|
|
|
maxForce = maxForce || this.maxForce;
|
|
|
|
|
|
|
|
target = Vector.coerce(target);
|
|
|
|
desired = Vector.sub(target, this.pos);
|
|
|
|
distance = desired.mag();
|
|
|
|
desired.normalize();
|
|
|
|
|
|
|
|
safety = this.aRad * this.safety;
|
|
|
|
|
|
|
|
if (distance < safety) {
|
|
|
|
mult = Engine.map(distance, 0, safety, 0, this.maxSpeed);
|
|
|
|
} else if (distance > this.aRad - safety){
|
|
|
|
mult = Engine.map(this.aRad - distance, 0, safety, 0, this.maxSpeed);
|
|
|
|
} else {
|
|
|
|
mult = this.maxSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
desired.mult(mult);
|
|
|
|
|
|
|
|
steer = Vector.sub(desired, this.vel);
|
|
|
|
steer.limit(maxForce);
|
|
|
|
this.accel.add(steer);
|
2014-07-26 08:09:04 +02:00
|
|
|
|
|
|
|
target = null;
|
|
|
|
desired = null;
|
|
|
|
steer = null;
|
2014-07-25 12:09:17 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
draw: function(ctx, scale){
|
|
|
|
ctx.fillStyle = this.fillStyle;
|
|
|
|
ctx.fillRect(
|
|
|
|
(this.pos.x - this.radius / 2) * scale >> 0,
|
|
|
|
(this.pos.y - this.radius / 2) * scale >> 0,
|
|
|
|
this.radius * scale,
|
|
|
|
this.radius * scale
|
|
|
|
);
|
2014-07-26 05:28:47 +02:00
|
|
|
|
|
|
|
return this;
|
2014-07-25 12:09:17 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
distanceTo: function(target) {
|
|
|
|
var xd = this.home.x - target.x;
|
|
|
|
var yd = this.home.y - target.y;
|
|
|
|
return Math.sqrt(xd * xd + yd * yd );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})(
|
|
|
|
window.Engine,
|
|
|
|
window.Vector
|
|
|
|
);
|