2014-05-17 14:26:55 +00:00
|
|
|
var canvas = document.getElementById('canvas');
|
|
|
|
var ctx = canvas.getContext('2d');
|
2014-05-17 21:15:55 +00:00
|
|
|
|
2014-05-17 21:10:35 +00:00
|
|
|
var gameState = 1; // 0 - start, 1 - playing, 2 - end
|
|
|
|
var framerate = 60;
|
|
|
|
|
2014-05-17 21:38:11 +00:00
|
|
|
var score = 0;
|
|
|
|
var scoreScalar = 1;
|
2014-05-17 21:10:35 +00:00
|
|
|
|
2014-05-17 19:01:16 +00:00
|
|
|
ct = 0;
|
2014-05-17 21:15:55 +00:00
|
|
|
|
2014-05-17 14:22:40 +00:00
|
|
|
window.requestAnimFrame = (function(){
|
2014-05-17 21:10:35 +00:00
|
|
|
return window.requestAnimationFrame ||
|
|
|
|
window.webkitRequestAnimationFrame ||
|
|
|
|
window.mozRequestAnimationFrame ||
|
|
|
|
function( callback ) {
|
|
|
|
window.setTimeout(callback, 1000 / framerate);
|
|
|
|
};
|
2014-05-17 14:22:40 +00:00
|
|
|
})();
|
|
|
|
|
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();
|
2014-05-17 21:38:11 +00:00
|
|
|
var prevScore = Date.now();
|
2014-05-17 18:46:14 +00:00
|
|
|
var nextGen = 1000;
|
|
|
|
|
2014-05-17 21:10:35 +00:00
|
|
|
var colors = ["#e74c3c", "#f1c40f","#3498db"];
|
|
|
|
var hexagonBackgroundColor = '#ecf0f1';
|
2014-05-17 21:15:55 +00:00
|
|
|
var swegBlue = '#2c3e50'; //tumblr?
|
2014-05-17 16:50:12 +00:00
|
|
|
|
2014-05-17 21:10:35 +00:00
|
|
|
function render() {
|
2014-05-17 21:38:11 +00:00
|
|
|
document.getElementById("score").innerHTML = score + " (x"+scoreScalar+")";
|
2014-05-17 18:46:14 +00:00
|
|
|
var now = Date.now();
|
|
|
|
if(now - lastGen > nextGen) {
|
2014-05-17 21:10:35 +00:00
|
|
|
blocks.push(new Block(randInt(0, 6), colors[randInt(0, colors.length)]));
|
2014-05-17 18:46:14 +00:00
|
|
|
lastGen = Date.now();
|
2014-05-17 22:22:12 +00:00
|
|
|
nextGen = randInt(500/iter, 1500/iter);
|
2014-05-17 18:46:14 +00:00
|
|
|
}
|
2014-05-17 21:38:11 +00:00
|
|
|
if(now - prevScore > 1000) {
|
|
|
|
score += 5 * scoreScalar;
|
|
|
|
prevScore = now;
|
2014-05-17 22:22:12 +00:00
|
|
|
iter += 0.1;
|
2014-05-17 21:38:11 +00:00
|
|
|
}
|
2014-05-17 15:15:53 +00:00
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
2014-05-17 21:10:35 +00:00
|
|
|
drawPolygon(canvas.width / 2, canvas.height / 2, 6, canvas.width / 2, 30, hexagonBackgroundColor);
|
2014-05-17 18:25:22 +00:00
|
|
|
var objectsToRemove = [];
|
2014-05-17 20:48:55 +00:00
|
|
|
var i;
|
|
|
|
for (i in MainClock.blocks) {
|
|
|
|
for (var j = 0; j < MainClock.blocks[i].length; j++) {
|
|
|
|
var block = MainClock.blocks[i][j];
|
|
|
|
MainClock.doesBlockCollide(block, iter);
|
|
|
|
block.draw();
|
2014-05-17 17:55:02 +00:00
|
|
|
}
|
2014-05-17 20:48:55 +00:00
|
|
|
}
|
2014-05-17 18:25:22 +00:00
|
|
|
|
2014-05-17 20:48:55 +00:00
|
|
|
for (i in blocks) {
|
2014-05-17 18:25:22 +00:00
|
|
|
MainClock.doesBlockCollide(blocks[i], iter);
|
|
|
|
if (!blocks[i].settled) {
|
|
|
|
blocks[i].distFromHex -= iter;
|
2014-05-17 20:48:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
objectsToRemove.push(i);
|
2014-05-17 18:25:22 +00:00
|
|
|
}
|
|
|
|
blocks[i].draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
objectsToRemove.forEach(function(o){
|
2014-05-17 20:48:55 +00:00
|
|
|
blocks.splice(o, 1);
|
2014-05-17 18:25:22 +00:00
|
|
|
});
|
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(){
|
2014-05-17 21:10:35 +00:00
|
|
|
if (gameState == 1) {
|
|
|
|
requestAnimFrame(animloop);
|
|
|
|
render();
|
|
|
|
checkGameOver();
|
|
|
|
}
|
|
|
|
else if (gameState == 2) {
|
2014-05-17 22:22:12 +00:00
|
|
|
showModal('Game Over ' + score + ' pts');
|
2014-05-17 21:10:35 +00:00
|
|
|
}
|
2014-05-17 16:29:46 +00:00
|
|
|
})();
|
2014-05-17 14:52:25 +00:00
|
|
|
|
2014-05-17 21:10:35 +00:00
|
|
|
function drawPolygon(x, y, sides, radius, theta, color) { // can make more elegant, reduce redundancy, fix readability
|
|
|
|
ctx.fillStyle = color;
|
2014-05-17 16:42:56 +00:00
|
|
|
ctx.beginPath();
|
2014-05-17 21:10:35 +00:00
|
|
|
var coords = rotatePoint(0, radius, theta);
|
|
|
|
ctx.moveTo(coords.x + x, coords.y + y);
|
|
|
|
var oldX = coords.x;
|
|
|
|
var oldY = coords.y;
|
2014-05-17 16:42:56 +00:00
|
|
|
for (var i = 0; i < sides; i++) {
|
2014-05-17 21:10:35 +00:00
|
|
|
coords = rotatePoint(oldX, oldY, 360 / sides);
|
2014-05-17 16:42:56 +00:00
|
|
|
ctx.lineTo(coords.x + x, coords.y + y);
|
2014-05-17 21:10:35 +00:00
|
|
|
// ctx.moveTo(coords.x + x, coords.y + y);
|
2014-05-17 16:42:56 +00:00
|
|
|
oldX = coords.x;
|
|
|
|
oldY = coords.y;
|
|
|
|
}
|
|
|
|
ctx.closePath();
|
|
|
|
ctx.fill();
|
2014-05-17 21:10:35 +00:00
|
|
|
};
|
2014-05-17 21:15:55 +00:00
|
|
|
|
2014-05-17 21:10:35 +00:00
|
|
|
function checkGameOver() { // fix font, fix size of hex
|
|
|
|
for(var i=0; i<MainClock.sides;i++) {
|
2014-05-17 22:22:12 +00:00
|
|
|
if(MainClock.blocks[i].length>8)
|
2014-05-17 21:10:35 +00:00
|
|
|
{
|
|
|
|
gameState = 2;
|
|
|
|
}
|
2014-05-17 14:52:25 +00:00
|
|
|
}
|
2014-05-17 16:30:40 +00:00
|
|
|
}
|
2014-05-17 21:10:35 +00:00
|
|
|
|
|
|
|
function showModal(text) {
|
|
|
|
drawPolygon(canvas.width / 2, canvas.height / 2, 6, canvas.width / 2, 30, hexagonBackgroundColor);
|
|
|
|
ctx.fillStyle = swegBlue;
|
|
|
|
ctx.font = '40pt "Helvetica Neue"';
|
|
|
|
ctx.textAlign = 'center';
|
|
|
|
ctx.fillText(text, canvas.width / 2, canvas.height / 2);
|
|
|
|
}
|