hextris/main.js

134 lines
3.5 KiB
JavaScript
Raw Normal View History

2014-05-17 14:26:55 +00:00
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
2014-05-17 19:01:16 +00:00
ct = 0;
window.requestAnimFrame = (function(){
2014-05-17 18:25:22 +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 16:50:12 +00:00
var MainClock = new Clock(65);
2014-05-17 19:08:10 +00:00
var iter = 1;
2014-05-17 18:46:14 +00:00
var lastGen = Date.now();
var nextGen = 1000;
var colors = ["green", "red"];
2014-05-17 16:50:12 +00:00
function Render() {
2014-05-17 18:46:14 +00:00
var now = Date.now();
if(now - lastGen > nextGen) {
blocks.push(new Block(randInt(0, 5), colors[randInt(0, colors.length-1)]));
lastGen = Date.now();
2014-05-17 19:08:10 +00:00
nextGen = randInt(500, 1500);
2014-05-17 18:46:14 +00:00
}
2014-05-17 15:15:53 +00:00
ctx.clearRect(0, 0, canvas.width, canvas.height);
2014-05-17 18:25:22 +00:00
var objectsToRemove = [];
MainClock.blocks.forEach(function(hexBlocks){
for (var i = 0; i < hexBlocks.length; i++) {
MainClock.doesBlockCollide(hexBlocks[i], iter, i);
if (!hexBlocks[i].settled) {
hexBlocks[i].distFromHex -= iter;
}
hexBlocks[i].draw();
2014-05-17 17:55:02 +00:00
}
});
2014-05-17 18:25:22 +00:00
for (var i in blocks) {
MainClock.doesBlockCollide(blocks[i], iter);
if (!blocks[i].settled) {
blocks[i].distFromHex -= iter;
} else {
objectsToRemove.push(blocks[i]);
}
blocks[i].draw();
}
objectsToRemove.forEach(function(o){
2014-05-17 19:01:16 +00:00
blocks.splice(o,1);
2014-05-17 18:25:22 +00:00
});
2014-05-17 19:01:16 +00:00
ct++;
2014-05-17 16:50:12 +00:00
MainClock.draw();
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 18:25:22 +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:43:56 +00:00
this.angle = 90 - (30 + 60 * this.lane);
2014-05-17 18:02:09 +00:00
this.width = 2 * this.distFromHex / Math.sqrt(3);
this.widthswag = this.width + this.height + 5;
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);
2014-05-17 18:02:09 +00:00
var p3 = rotatePoint(this.widthswag/2, -this.height/2, this.angle);
var p4 = rotatePoint(-this.widthswag/2, -this.height/2, this.angle);
2014-05-17 16:29:46 +00:00
2014-05-17 18:46:14 +00:00
ctx.fillStyle=this.color;
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 16:30:40 +00:00
}