2014-07-24 09:37:27 +02:00
|
|
|
(function(
|
|
|
|
Engine,
|
|
|
|
Point,
|
|
|
|
Polygon,
|
|
|
|
Vector
|
|
|
|
){
|
|
|
|
|
2014-07-26 22:52:03 +02:00
|
|
|
Engine.Shape = function(x, y, width, height, points, polygons){
|
2014-07-24 09:37:27 +02:00
|
|
|
var i, ref, point, poly;
|
|
|
|
|
2014-07-26 22:52:03 +02:00
|
|
|
this.pos = new Vector(x, y);
|
2014-07-24 09:37:27 +02:00
|
|
|
this.size = new Vector(width, height);
|
2014-07-26 22:52:03 +02:00
|
|
|
this.sizeRef = this.size.clone();
|
2014-07-24 09:37:27 +02:00
|
|
|
|
|
|
|
ref = {};
|
|
|
|
this.points = [];
|
|
|
|
this.polygons = [];
|
|
|
|
|
|
|
|
for (i = 0; i < points.length; i++) {
|
|
|
|
point = new Point(
|
|
|
|
points[i].id,
|
2014-07-26 22:52:03 +02:00
|
|
|
points[i].x,
|
|
|
|
points[i].y,
|
|
|
|
this.size
|
2014-07-24 09:37:27 +02:00
|
|
|
);
|
|
|
|
ref[point.id] = point;
|
|
|
|
this.points.push(point);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < polygons.length; i++) {
|
|
|
|
poly = polygons[i];
|
|
|
|
this.polygons.push(new Polygon(
|
|
|
|
ref[poly.points[0]],
|
|
|
|
ref[poly.points[1]],
|
|
|
|
ref[poly.points[2]],
|
2014-07-28 02:19:01 +02:00
|
|
|
poly.color,
|
|
|
|
poly.stroke
|
2014-07-24 09:37:27 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.Shape.prototype = {
|
|
|
|
|
2014-07-26 22:52:03 +02:00
|
|
|
breathing: false,
|
|
|
|
|
|
|
|
breath: 0,
|
|
|
|
breathLength: 1,
|
|
|
|
breatheIn: false,
|
|
|
|
|
2014-07-28 03:41:44 +02:00
|
|
|
resize: function(newSize, offset){
|
|
|
|
var len, p;
|
|
|
|
|
|
|
|
this.size.x = newSize;
|
|
|
|
this.size.y = newSize;
|
2014-07-28 04:51:59 +02:00
|
|
|
this.sizeRef.x = newSize;
|
|
|
|
this.sizeRef.y = newSize;
|
2014-07-28 03:41:44 +02:00
|
|
|
|
|
|
|
this.pos.x = -(newSize / 2);
|
|
|
|
this.pos.y = -(newSize / 2 + offset);
|
|
|
|
|
|
|
|
for (p = 0, len = this.points.length; p < len; p++) {
|
|
|
|
this.points[p].resize();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-07-26 22:52:03 +02:00
|
|
|
startBreathing: function(){
|
|
|
|
var p;
|
|
|
|
|
|
|
|
this.breathing = true;
|
|
|
|
this.breath = this.breathLength;
|
|
|
|
|
|
|
|
for (p = 0; p < this.points.length; p++) {
|
|
|
|
this.points[p].updateBreathingPhysics();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
breathe: function(tick){
|
|
|
|
var p, scale, newSize;
|
|
|
|
|
|
|
|
this.breath += tick;
|
|
|
|
|
|
|
|
if (this.breath < this.breathLength) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-28 04:33:15 +02:00
|
|
|
scale = 1;
|
2014-07-26 22:52:03 +02:00
|
|
|
|
|
|
|
newSize = Vector.mult(this.sizeRef, scale);
|
|
|
|
|
|
|
|
for (p = 0; p < this.points.length; p++) {
|
|
|
|
this.points[p].updateTarget(newSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.breath = 0;
|
|
|
|
},
|
|
|
|
|
2014-07-24 09:37:27 +02:00
|
|
|
update: function(engine){
|
|
|
|
var p;
|
|
|
|
|
2014-07-26 22:52:03 +02:00
|
|
|
if (this.breathing === true) {
|
|
|
|
this.breathe(engine.tick);
|
|
|
|
}
|
|
|
|
|
2014-07-24 09:37:27 +02:00
|
|
|
for (p = 0; p < this.points.length; p++) {
|
|
|
|
this.points[p].update(engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = 0; p < this.polygons.length; p++) {
|
2014-07-26 08:09:04 +02:00
|
|
|
this.polygons[p].update(engine);
|
2014-07-24 09:37:27 +02:00
|
|
|
}
|
2014-07-24 09:46:14 +02:00
|
|
|
|
|
|
|
return this;
|
2014-07-24 09:37:27 +02:00
|
|
|
},
|
|
|
|
|
2014-07-26 06:16:16 +02:00
|
|
|
draw: function(ctx, scale, engine){
|
2014-07-26 09:11:43 +02:00
|
|
|
var p, poly;
|
2014-07-24 09:37:27 +02:00
|
|
|
|
2014-07-25 06:37:48 +02:00
|
|
|
ctx.translate(
|
|
|
|
this.pos.x * scale >> 0,
|
|
|
|
this.pos.y * scale >> 0
|
|
|
|
);
|
2014-07-24 09:37:27 +02:00
|
|
|
for (p = 0; p < this.polygons.length; p++) {
|
2014-07-26 09:11:43 +02:00
|
|
|
poly = this.polygons[p];
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(
|
2014-07-28 04:33:15 +02:00
|
|
|
poly.a.pos.x * scale,
|
|
|
|
poly.a.pos.y * scale
|
2014-07-26 09:11:43 +02:00
|
|
|
);
|
|
|
|
ctx.lineTo(
|
2014-07-28 04:33:15 +02:00
|
|
|
poly.b.pos.x * scale,
|
|
|
|
poly.b.pos.y * scale
|
2014-07-26 09:11:43 +02:00
|
|
|
);
|
|
|
|
ctx.lineTo(
|
2014-07-28 04:33:15 +02:00
|
|
|
poly.c.pos.x * scale,
|
|
|
|
poly.c.pos.y * scale
|
2014-07-26 09:11:43 +02:00
|
|
|
);
|
|
|
|
ctx.closePath();
|
|
|
|
ctx.fillStyle = poly.fillStyle;
|
|
|
|
ctx.fill();
|
2014-07-28 02:19:01 +02:00
|
|
|
ctx.lineWidth = poly.strokeWidth * scale;
|
2014-07-26 09:11:43 +02:00
|
|
|
ctx.strokeStyle = poly.strokeStyle;
|
|
|
|
ctx.stroke();
|
2014-07-24 09:37:27 +02:00
|
|
|
}
|
2014-07-26 06:16:16 +02:00
|
|
|
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
|
|
ctx.translate(
|
|
|
|
engine.width / 2 * engine.scale >> 0,
|
|
|
|
engine.height / 2 * engine.scale >> 0
|
|
|
|
);
|
2014-07-24 09:46:14 +02:00
|
|
|
return this;
|
2014-07-24 09:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
})(
|
|
|
|
window.Engine,
|
|
|
|
window.Engine.Point,
|
|
|
|
window.Engine.Polygon,
|
|
|
|
window.Vector
|
|
|
|
);
|