game.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var Game = function(player, viewport, story) {
  2. var elapsedms = 0;
  3. var deleteGame = false;
  4. this.setup = function() {
  5. this.load();
  6. if (player.getData('started') == null) {
  7. player.addItem('₩', 10000);
  8. player.addItem('scrap', 2);
  9. player.addItem('spare part', 1);
  10. player.addItem('hydrogen', 100);
  11. player.setShip('damage', 2);
  12. player.setShip('state', 'adrift');
  13. player.setData('started', new Date().getTime());
  14. }
  15. var replication = document.getElementById("replication");
  16. for ( var recipe in recipies) {
  17. var recipieData = recipies[recipe];
  18. var ingredientData = [];
  19. for ( var ingredient in recipieData['ingredients']) {
  20. ingredientData.push(recipieData['ingredients'][ingredient]
  21. + " " + ingredient);
  22. }
  23. var button = document.createElement("button");
  24. button.innerHTML = ingredientData.join(",") + " = "
  25. + recipieData['amount'] + " " + recipe;
  26. button.id = "craft_" + recipe.split(" ").join('_');
  27. button.dataset.item = recipe;
  28. button.addEventListener("click", function() {
  29. commands.craft(this.dataset.item, 1);
  30. });
  31. replication.appendChild(button);
  32. replication.appendChild(document.createElement("br"));
  33. }
  34. };
  35. this.load = function() {
  36. var jsonInventory = localStorage.getItem('inventory') || "{}";
  37. player.inventory = JSON.parse(jsonInventory);
  38. var jsonState = localStorage.getItem('player') || "{}";
  39. player.state = JSON.parse(jsonState);
  40. var jsonShip = localStorage.getItem('ship') || "{}";
  41. player.ship = JSON.parse(jsonShip);
  42. console.log(player.inventory);
  43. console.log(player.state);
  44. console.log(player.ship);
  45. };
  46. this.save = function() {
  47. if (!deleteGame) {
  48. localStorage.setItem('inventory', JSON.stringify(player.inventory));
  49. localStorage.setItem('player', JSON.stringify(player.state));
  50. localStorage.setItem('ship', JSON.stringify(player.ship));
  51. } else {
  52. localStorage.removeItem('inventory');
  53. localStorage.removeItem('player');
  54. localStorage.removeItem('ship');
  55. localStorage.clear();
  56. location.reload();
  57. }
  58. };
  59. this.deleteSave = function() {
  60. deleteGame = true;
  61. };
  62. this.draw = function draw(delta) {
  63. elapsedms += delta;
  64. if (elapsedms >= 1.0) {
  65. this.save();
  66. }
  67. while (elapsedms >= 1.0) {
  68. this.secondTick();
  69. elapsedms -= 1.0;
  70. }
  71. };
  72. this.setMeter = function(display, duration, callback) {
  73. meter.set(display, duration, callback);
  74. };
  75. this.hideMeter = function() {
  76. meter.hide();
  77. };
  78. this.secondTick = function() {
  79. meter.update();
  80. story.update();
  81. // todo: update something on a second delay
  82. };
  83. };
  84. game = new Game(player, viewport, story);
  85. document.addEventListener('DOMContentLoaded', function() {
  86. viewport.setup();
  87. game.setup();
  88. requestAnimationFrame(animate);
  89. }, false);
  90. var lastTime = new Date().getTime();
  91. var currentTime = 0;
  92. function animate(elapsed) {
  93. currentTime = new Date().getTime();
  94. requestAnimationFrame(animate);
  95. commands.update();
  96. game.draw((currentTime - lastTime) / 1000);
  97. viewport.draw((currentTime - lastTime) / 1000);
  98. lastTime = currentTime;
  99. }
  100. document.getElementById("repair").addEventListener("click", function() {
  101. commands.repair();
  102. });
  103. document.getElementById("travel").addEventListener("click", function() {
  104. commands.travel();
  105. });
  106. document.getElementById("reset").addEventListener("click", function() {
  107. if (confirm('Delete your game?')) {
  108. game.deleteSave();
  109. }
  110. });