hextris/main.js

105 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-05-17 14:26:55 +00:00
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
window.requestAnimFrame = (function(){
2014-05-17 15:11:54 +00:00
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
2014-05-17 15:55:58 +00:00
function( callback ) {
window.setTimeout(callback, 1000 / 60);
};
})();
2014-05-17 16:43:27 +00:00
var clock = new Clock(6);
2014-05-17 16:16:56 +00:00
2014-05-17 16:42:56 +00:00
var blocks = [];
2014-05-17 17:55:02 +00:00
for (var i = 0; i < 6; i++) {
blocks.push(new Block(i, 'green'));
}
2014-05-17 16:50:12 +00:00
var MainClock = new Clock(65);
2014-05-17 17:55:02 +00:00
var iter = 1/100;
2014-05-17 16:50:12 +00:00
function Render() {
2014-05-17 15:15:53 +00:00
ctx.clearRect(0, 0, canvas.width, canvas.height);
blocks.forEach(function(o){
2014-05-17 17:55:02 +00:00
MainClock.doesBlockCollide(o, iter);
if (!o.settled) {
o.distFromHex -= iter;
}
o.draw();
});
2014-05-17 16:50:12 +00:00
MainClock.draw();
2014-05-17 15:15:53 +00:00
requestAnimFrame(Render);
2014-05-17 14:26:55 +00:00
}
2014-05-17 16:29:46 +00:00
(function animloop(){
requestAnimFrame(animloop);
Render();
})();
2014-05-17 16:42:56 +00:00
function drawPolygon(x, y, sides, radius, theta) {
ctx.beginPath();
ctx.moveTo(x, y + radius);
var oldX = 0;
var oldY = radius;
for (var i = 0; i < sides; i++) {
2014-05-17 17:03:50 +00:00
var coords = rotatePoint(oldX, oldY, 360 / sides);
2014-05-17 16:42:56 +00:00
ctx.lineTo(coords.x + x, coords.y + y);
ctx.moveTo(coords.x + x, coords.y + y);
oldX = coords.x;
oldY = coords.y;
}
ctx.closePath();
ctx.fill();
ctx.stroke();
}
2014-05-17 17:55:02 +00:00
function Block(lane, color, distFromHex, settled) {
this.settled = (settled == undefined) ? 0 : 1;
2014-05-17 16:29:46 +00:00
this.height = 20;
this.width = 65;
this.lane = lane;
this.angle = 90 - (30 + 60 * lane);
if (this.angle < 0) {
this.angle += 360;
}
2014-05-17 14:26:55 +00:00
2014-05-17 16:29:46 +00:00
this.color = color;
if (distFromHex) {
this.distFromHex = distFromHex;
}
else {
2014-05-17 16:29:46 +00:00
this.distFromHex = 300;
}
2014-05-17 16:29:46 +00:00
this.draw = function() {
2014-05-17 17:34:57 +00:00
this.width = 2 * this.distFromHex / Math.sqrt(3) + this.height;
2014-05-17 16:29:46 +00:00
var p1 = rotatePoint(-this.width/2, this.height/2, this.angle);
var p2 = rotatePoint(this.width/2, this.height/2, this.angle);
var p3 = rotatePoint(this.width/2, -this.height/2, this.angle);
var p4 = rotatePoint(-this.width/2, -this.height/2, this.angle);
ctx.fillStyle="#FF0000";
2014-05-17 17:03:50 +00:00
var baseX = canvas.width/2 + Math.sin((this.angle) * (Math.PI/180)) * (this.distFromHex + this.height/2);
var baseY = canvas.height/2 - Math.cos((this.angle) * (Math.PI/180)) * (this.distFromHex + this.height/2);
2014-05-17 16:29:46 +00:00
ctx.beginPath();
2014-05-17 16:42:56 +00:00
ctx.moveTo(Math.round(baseX + p1.x), Math.round(baseY + p1.y));
ctx.lineTo(Math.round(baseX + p2.x), Math.round(baseY + p2.y));
ctx.lineTo(Math.round(baseX + p3.x), Math.round(baseY + p3.y));
ctx.lineTo(Math.round(baseX + p4.x), Math.round(baseY + p4.y));
ctx.lineTo(Math.round(baseX + p1.x), Math.round(baseY + p1.y));
2014-05-17 16:29:46 +00:00
ctx.closePath();
ctx.fill();
2014-05-17 15:25:33 +00:00
2014-05-17 17:34:57 +00:00
// ctx.strokeStyle = '#322'
// ctx.beginPath();
// ctx.moveTo(canvas.width/2, canvas.height/2);
// ctx.lineTo(canvas.width/2 + Math.sin((this.angle) * (Math.PI/180)) * (this.distFromHex + this.height), canvas.height/2 - Math.cos((this.angle) * (Math.PI/180)) * (this.distFromHex + this.height));
// ctx.closePath();
// ctx.stroke();
2014-05-17 16:29:46 +00:00
};
2014-05-17 17:03:50 +00:00
}