2014-07-24 05:35:57 +02:00
|
|
|
(function(
|
|
|
|
Engine,
|
|
|
|
Vector
|
|
|
|
){
|
|
|
|
|
|
|
|
Engine.Particle = function(width, height){
|
|
|
|
var side, targetX, targetY;
|
|
|
|
this.accel = Vector.coerce(this.accel);
|
|
|
|
this.vel = Vector.coerce(this.vel);
|
2014-07-25 07:44:25 +02:00
|
|
|
this.pos = new Vector(0, 0);
|
2014-07-24 05:35:57 +02:00
|
|
|
|
|
|
|
this.maxRadius = Engine.getRandomFloat(0.1, 2.5);
|
2014-07-25 12:09:17 +02:00
|
|
|
// this.maxSpeed = Engine.getRandomFloat(0.01, 1000);
|
|
|
|
this.maxSpeed = Engine.getRandomFloat(20, 1000);
|
2014-07-24 05:35:57 +02:00
|
|
|
|
|
|
|
// Pick a random target
|
|
|
|
side = Engine.getRandomInt(0, 3);
|
|
|
|
if (side === 0 || side === 2) {
|
2014-07-25 07:44:25 +02:00
|
|
|
targetY = (side === 0) ? -(height / 2) : (height / 2);
|
|
|
|
targetX = Engine.getRandomInt(-(width / 2), width / 2);
|
2014-07-24 05:35:57 +02:00
|
|
|
} else {
|
2014-07-25 07:44:25 +02:00
|
|
|
targetY = Engine.getRandomInt(-(height / 2), height / 2);
|
|
|
|
targetX = (side === 3) ? -(width / 2) : (width / 2);
|
2014-07-24 05:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.target = new Vector(targetX, targetY);
|
2014-07-25 07:44:25 +02:00
|
|
|
this.getAccelVector();
|
|
|
|
|
2014-07-24 05:35:57 +02:00
|
|
|
this.maxDistance = this.distanceTo(this.target);
|
|
|
|
|
|
|
|
this.fillA = '#8750c2';
|
|
|
|
this.fillB = '#b976ff';
|
|
|
|
this.frameMax = Engine.getRandomInt(1, 5);
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.Particle.prototype = {
|
|
|
|
|
|
|
|
radius: 1,
|
|
|
|
|
|
|
|
frame: 0,
|
|
|
|
showA: false,
|
|
|
|
|
|
|
|
accel: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
vel: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
pos: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
opacity: 1,
|
|
|
|
|
|
|
|
maxSpeed: 1500,
|
|
|
|
maxForce: 1500,
|
|
|
|
|
2014-07-25 07:44:25 +02:00
|
|
|
getAccelVector: function(){
|
|
|
|
this.accel = Vector.sub(this.target, this.pos)
|
|
|
|
.normalize()
|
|
|
|
.mult(this.maxSpeed);
|
|
|
|
},
|
2014-07-24 05:35:57 +02:00
|
|
|
|
2014-07-25 07:44:25 +02:00
|
|
|
update: function(engine){
|
|
|
|
var distancePercent, halfWidth, halfHeight;
|
2014-07-24 05:35:57 +02:00
|
|
|
|
|
|
|
this.vel
|
|
|
|
.add(this.accel)
|
|
|
|
.limit(this.maxSpeed);
|
|
|
|
|
|
|
|
this.pos.add(Vector.mult(this.vel, engine.tick));
|
|
|
|
|
2014-07-25 07:44:25 +02:00
|
|
|
halfWidth = engine.width / 2 + this.maxRadius;
|
|
|
|
halfHeight = engine.height / 2 + this.maxRadius;
|
|
|
|
|
2014-07-24 05:35:57 +02:00
|
|
|
if (
|
2014-07-25 07:44:25 +02:00
|
|
|
this.pos.x < -(halfWidth) ||
|
|
|
|
this.pos.x > halfWidth ||
|
|
|
|
this.pos.y < -(halfHeight) ||
|
|
|
|
this.pos.y > halfHeight
|
2014-07-24 05:35:57 +02:00
|
|
|
) {
|
|
|
|
this.kill(engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
distancePercent = (this.maxDistance - this.distanceTo(this.target)) / this.maxDistance;
|
|
|
|
this.radius = Math.max(0.1, this.maxRadius * distancePercent);
|
|
|
|
|
|
|
|
this.frame++;
|
|
|
|
if (this.frame > this.frameMax) {
|
|
|
|
this.frame = 0;
|
|
|
|
this.showA = !this.showA;
|
|
|
|
}
|
|
|
|
|
2014-07-26 08:09:04 +02:00
|
|
|
if (this.showA) {
|
|
|
|
engine.particlesA[engine.particlesA.length] = this;
|
|
|
|
} else {
|
|
|
|
engine.particlesB[engine.particlesB.length] = this;
|
|
|
|
}
|
|
|
|
|
2014-07-24 05:35:57 +02:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
draw: function(ctx, scale){
|
|
|
|
if (this.radius < 0.25) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.showA) {
|
|
|
|
ctx.fillStyle = this.fillA;
|
|
|
|
} else {
|
|
|
|
ctx.fillStyle = this.fillB;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw a square - very performant
|
|
|
|
ctx.fillRect(
|
2014-07-25 06:37:48 +02:00
|
|
|
this.pos.x * scale >> 0,
|
|
|
|
this.pos.y * scale >> 0,
|
2014-07-24 05:35:57 +02:00
|
|
|
this.radius * scale,
|
|
|
|
this.radius * scale
|
|
|
|
);
|
|
|
|
|
|
|
|
// Draw a circle - far less performant
|
|
|
|
// ctx.beginPath();
|
|
|
|
// ctx.arc(
|
|
|
|
// this.pos.x * scale,
|
|
|
|
// this.pos.y * scale,
|
|
|
|
// this.radius * scale,
|
|
|
|
// 0,
|
|
|
|
// Math.PI * 2,
|
|
|
|
// false
|
|
|
|
// );
|
|
|
|
// ctx.fill();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
kill: function(engine){
|
2014-07-24 09:46:14 +02:00
|
|
|
engine._deferredParticles.push(this);
|
2014-07-24 05:35:57 +02:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
distanceTo: function(target) {
|
|
|
|
var xd = this.pos.x - target.x;
|
|
|
|
var yd = this.pos.y - target.y;
|
|
|
|
return Math.sqrt(xd * xd + yd * yd );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})(window.Engine, window.Vector);
|