123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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("dock").addEventListener("click", function() {
- commands.dock();
- });
- document.getElementById("reset").addEventListener("click", function() {
- if (confirm('Delete your game?')) {
- game.deleteSave();
- }
- });
|