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 22:34:02 +00:00
|
|
|
var gameState = 0; // 0 - start, 1 - playing, 2 - end
|
2014-05-17 21:10:35 +00:00
|
|
|
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:16:56 +00:00
|
|
|
|
2014-05-17 16:42:56 +00:00
|
|
|
var blocks = [];
|
2014-05-17 22:47:37 +00:00
|
|
|
var MainClock;
|
|
|
|
var iter;
|
|
|
|
var lastGen;
|
|
|
|
var prevScore;
|
|
|
|
var nextGen;
|
2014-05-17 18:46:14 +00:00
|
|
|
|
2014-05-17 22:47:37 +00:00
|
|
|
function init() {
|
|
|
|
score = 0;
|
|
|
|
scoreScalar = 1;
|
|
|
|
gameState = 1;
|
|
|
|
ct = 0;
|
|
|
|
blocks = [];
|
|
|
|
MainClock = new Clock(65);
|
|
|
|
iter = 1;
|
|
|
|
lastGen = Date.now();
|
|
|
|
prevScore = Date.now();
|
|
|
|
nextGen = 1000;
|
2014-05-17 18:46:14 +00:00
|
|
|
|
2014-05-17 22:47:37 +00:00
|
|
|
}
|
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 18:46:14 +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 22:34:02 +00:00
|
|
|
requestAnimFrame(animloop);
|
|
|
|
|
|
|
|
if (gameState == 0) {
|
|
|
|
showModal('Start!', 'Press enter to start!');
|
|
|
|
}
|
|
|
|
else if (gameState == 1) {
|
2014-05-17 21:10:35 +00:00
|
|
|
render();
|
|
|
|
checkGameOver();
|
|
|
|
}
|
|
|
|
else if (gameState == 2) {
|
2014-05-17 22:47:37 +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
|
|
|
|
2014-05-17 22:34:02 +00:00
|
|
|
function showModal(text, secondaryText) {
|
|
|
|
var buttonSize = 150;
|
|
|
|
var fontSizeLarge = 50;
|
|
|
|
var fontSizeSmall = 25;
|
2014-05-17 21:10:35 +00:00
|
|
|
drawPolygon(canvas.width / 2, canvas.height / 2, 6, canvas.width / 2, 30, hexagonBackgroundColor);
|
|
|
|
ctx.fillStyle = swegBlue;
|
2014-05-17 22:34:02 +00:00
|
|
|
// drawPolygon(canvas.width / 2, canvas.height / 2, 6, buttonSize, 30, swegBlue);
|
|
|
|
ctx.font = fontSizeLarge+'px "Roboto"';
|
2014-05-17 21:10:35 +00:00
|
|
|
ctx.textAlign = 'center';
|
2014-05-17 22:34:02 +00:00
|
|
|
// ctx.fillStyle = hexagonBackgroundColor;
|
|
|
|
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + (fontSizeLarge / 4));
|
|
|
|
ctx.font = fontSizeSmall+'px "Roboto"';
|
|
|
|
ctx.fillText(secondaryText, canvas.width/2, canvas.height/2 + fontSizeLarge / 4 + fontSizeSmall / 4 + 30);
|
|
|
|
}
|