2014-07-24 05:35:57 +02:00
|
|
|
(function(
|
|
|
|
Engine,
|
|
|
|
Vector
|
2014-07-26 08:09:04 +02:00
|
|
|
){ 'use strict';
|
2014-07-24 05:35:57 +02:00
|
|
|
|
2014-07-26 22:52:03 +02:00
|
|
|
Engine.Point = function(id, x, y, shapeSize){
|
2014-07-24 05:35:57 +02:00
|
|
|
this.id = id;
|
2014-07-26 22:52:03 +02:00
|
|
|
|
|
|
|
this.shapeSize = shapeSize;
|
|
|
|
this.ref = new Vector(x, y);
|
|
|
|
|
|
|
|
this.pos = new Vector(
|
|
|
|
x * shapeSize.x,
|
|
|
|
y * shapeSize.y
|
|
|
|
);
|
|
|
|
|
2014-07-24 05:35:57 +02:00
|
|
|
this.target = this.pos.clone();
|
2014-07-26 22:52:03 +02:00
|
|
|
this.pos.x = shapeSize.x / 2;
|
|
|
|
this.pos.y = shapeSize.y / 2;
|
|
|
|
this.accel = Vector.coerce(this.accel);
|
|
|
|
this.vel = Vector.coerce(this.vel);
|
2014-07-24 05:35:57 +02:00
|
|
|
|
2014-07-28 04:33:15 +02:00
|
|
|
this.stiffness = Engine.getRandomFloat(150, 600);
|
|
|
|
this.friction = Engine.getRandomFloat(12, 18);
|
2014-07-24 05:35:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Engine.Point.prototype = {
|
|
|
|
|
|
|
|
radius: 1,
|
|
|
|
|
2014-07-28 04:33:15 +02:00
|
|
|
stiffness : 200,
|
|
|
|
friction : 13,
|
2014-07-24 05:35:57 +02:00
|
|
|
threshold : 0.03,
|
|
|
|
|
|
|
|
pos: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
accel: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
vel : {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
target: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
|
2014-07-28 03:41:44 +02:00
|
|
|
resize: function(){
|
|
|
|
this.target.x = this.pos.x = this.ref.x * this.shapeSize.x;
|
|
|
|
this.target.y = this.pos.y = this.ref.y * this.shapeSize.y;
|
|
|
|
},
|
|
|
|
|
2014-07-26 22:52:03 +02:00
|
|
|
updateBreathingPhysics: function(){
|
2014-07-28 04:33:15 +02:00
|
|
|
this.stiffness = Engine.getRandomFloat(2, 4);
|
|
|
|
this.friction = Engine.getRandomFloat(1, 2);
|
2014-07-26 22:52:03 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
updateTarget: function(newSize){
|
|
|
|
var diff;
|
|
|
|
|
|
|
|
this.target.x = this.ref.x * newSize.x;
|
|
|
|
this.target.y = this.ref.y * newSize.y;
|
|
|
|
|
|
|
|
diff = Vector.sub(newSize, this.shapeSize).div(2);
|
|
|
|
|
|
|
|
this.target.sub(diff);
|
|
|
|
|
|
|
|
this.target.add({
|
2014-07-28 04:33:15 +02:00
|
|
|
x: Engine.getRandomFloat(-3, 3),
|
|
|
|
y: Engine.getRandomFloat(-3, 3)
|
2014-07-26 22:52:03 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-07-24 05:35:57 +02:00
|
|
|
update: function(engine){
|
|
|
|
var newAccel;
|
|
|
|
|
|
|
|
newAccel = Vector.sub(this.target, this.pos)
|
|
|
|
.mult(this.stiffness)
|
|
|
|
.sub(Vector.mult(this.vel, this.friction));
|
|
|
|
|
|
|
|
this.accel.set(newAccel);
|
|
|
|
|
2014-07-28 04:33:15 +02:00
|
|
|
this.vel.add(Vector.mult(this.accel, engine.tick));
|
2014-07-24 05:35:57 +02:00
|
|
|
|
|
|
|
this.pos.add(
|
|
|
|
Vector.mult(this.vel, engine.tick)
|
|
|
|
);
|
2014-07-26 05:28:47 +02:00
|
|
|
|
2014-07-26 08:09:04 +02:00
|
|
|
newAccel = null;
|
|
|
|
|
2014-07-26 05:28:47 +02:00
|
|
|
return this;
|
2014-07-24 05:35:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
draw: function(ctx, scale){
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.arc(
|
2014-07-26 08:09:04 +02:00
|
|
|
this.pos.x * scale,
|
|
|
|
this.pos.y * scale,
|
2014-07-24 05:35:57 +02:00
|
|
|
this.radius * scale,
|
|
|
|
0,
|
|
|
|
Math.PI * 2,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
ctx.fillStyle = '#ffffff';
|
|
|
|
ctx.fill();
|
2014-07-26 05:28:47 +02:00
|
|
|
return this;
|
2014-07-24 05:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
})(window.Engine, window.Vector);
|