game.js 3.8 KB

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