var Game = function(player, viewport, story) { var elapsedms = 0; var deleteGame = false; this.setup = function() { this.load(); if (player.getData('started') == null) { player.addItem('₩', 10000); player.addItem('scrap', 2); player.addItem('spare part', 1); player.addItem('hydrogen', 100); player.setShip('damage', 2); player.setShip('state', 'adrift'); player.setData('started', new Date().getTime()); } var replication = document.getElementById("replication"); for ( var recipe in recipies) { var recipieData = recipies[recipe]; var ingredientData = []; for ( var ingredient in recipieData['ingredients']) { ingredientData.push(recipieData['ingredients'][ingredient] + " " + ingredient); } var button = document.createElement("button"); button.innerHTML = ingredientData.join(",") + " = " + recipieData['amount'] + " " + recipe; button.id = "craft_" + recipe.split(" ").join('_'); button.dataset.item = recipe; button.addEventListener("click", function() { commands.craft(this.dataset.item, 1); }); replication.appendChild(button); replication.appendChild(document.createElement("br")); } }; this.load = function() { var jsonInventory = localStorage.getItem('inventory') || "{}"; player.inventory = JSON.parse(jsonInventory); var jsonState = localStorage.getItem('player') || "{}"; player.state = JSON.parse(jsonState); var jsonShip = localStorage.getItem('ship') || "{}"; player.ship = JSON.parse(jsonShip); console.log(player.inventory); console.log(player.state); console.log(player.ship); }; this.save = function() { if (!deleteGame) { localStorage.setItem('inventory', JSON.stringify(player.inventory)); localStorage.setItem('player', JSON.stringify(player.state)); localStorage.setItem('ship', JSON.stringify(player.ship)); } else { localStorage.removeItem('inventory'); localStorage.removeItem('player'); localStorage.removeItem('ship'); localStorage.clear(); location.reload(); } }; this.deleteSave = function() { deleteGame = true; }; this.draw = function draw(delta) { elapsedms += delta; if (elapsedms >= 1.0) { this.save(); } while (elapsedms >= 1.0) { this.secondTick(); elapsedms -= 1.0; } }; this.setMeter = function(display, duration, callback) { meter.set(display, duration, callback); }; this.hideMeter = function() { meter.hide(); }; this.secondTick = function() { meter.update(); story.update(); // todo: update something on a second delay }; }; game = new Game(player, viewport, story); document.addEventListener('DOMContentLoaded', function() { viewport.setup(); game.setup(); requestAnimationFrame(animate); }, false); var lastTime = new Date().getTime(); var currentTime = 0; function animate(elapsed) { currentTime = new Date().getTime(); requestAnimationFrame(animate); commands.update(); game.draw((currentTime - lastTime) / 1000); viewport.draw((currentTime - lastTime) / 1000); lastTime = currentTime; } document.getElementById("repair").addEventListener("click", function() { commands.repair(); }); document.getElementById("travel").addEventListener("click", function() { commands.travel(); }); document.getElementById("reset").addEventListener("click", function() { if (confirm('Delete your game?')) { game.deleteSave(); } });