viewport.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. var Viewport = function() {
  2. var canvas, context, backgroundOffset, angleRadians, shipDamage, velocity, isThrusting, isDocked;
  3. var shipImage, backgroundImageshipDamageImage, shipThrustImage, spaceStationImage;
  4. this.setup = function() {
  5. canvas = document.getElementById('viewport');
  6. if (screen.width < 360) {
  7. canvas.width = screen.width;
  8. canvas.height = screen.width;
  9. } else {
  10. canvas.width = 360;
  11. canvas.height = 360;
  12. }
  13. context = canvas.getContext('2d');
  14. shipImage = document.getElementById("shipImage");
  15. shipDamageImage = [];
  16. shipDamageImage.push(document.getElementById("shipDamage1Image"));
  17. shipDamageImage.push(document.getElementById("shipDamage2Image"));
  18. shipDamageImage.push(document.getElementById("shipDamage3Image"));
  19. shipThrustImage = document.getElementById("shipThrustImage");
  20. backgroundImage = document.getElementById("backgroundImage");
  21. spaceStationImage = document.getElementById("spaceStationImage");
  22. backgroundOffset = {
  23. x : 0,
  24. y : 0
  25. };
  26. shipDamage = 0;
  27. this.setStateIdle();
  28. };
  29. this.update = function() {
  30. backgroundOffset.x += velocity.x;
  31. backgroundOffset.y += velocity.y;
  32. backgroundOffset.x = backgroundOffset.x % backgroundImage.width;
  33. backgroundOffset.y = backgroundOffset.y % backgroundImage.height;
  34. };
  35. this.draw = function() {
  36. this.update();
  37. context.clearRect(0, 0, canvas.width, canvas.height);
  38. for (var x = -1; x < Math.ceil(canvas.width / backgroundImage.width) + 2; x++) {
  39. for (var y = -1; y < Math.ceil(canvas.height
  40. / backgroundImage.height) + 2; y++) {
  41. context.drawImage(backgroundImage, x * backgroundImage.width
  42. + backgroundOffset.x, y * backgroundImage.height
  43. + backgroundOffset.y);
  44. }
  45. }
  46. context.save();
  47. context.translate(canvas.width / 2, canvas.height / 2);
  48. context.rotate(angleRadians);
  49. if (isThrusting) {
  50. context.drawImage(shipThrustImage, -(shipThrustImage.width / 2),
  51. shipImage.height / 2);
  52. }
  53. context.drawImage(shipImage, -(shipImage.width / 2),
  54. -(shipImage.height / 2));
  55. if (shipDamage > 0) {
  56. context.drawImage(shipDamageImage[shipDamage - 1],
  57. -(shipImage.width / 2), -(shipImage.height / 2));
  58. }
  59. context.restore();
  60. if (isDocked) {
  61. context.save();
  62. context.translate(-(spaceStationImage.width / 2),
  63. -(spaceStationImage.height / 2));
  64. context.translate(canvas.width * 3 / 4, canvas.height * 1 / 8);
  65. context.scale(4, 4);
  66. context.drawImage(spaceStationImage, 0, 0);
  67. context.restore();
  68. }
  69. };
  70. this.setDamage = function(amount) {
  71. shipDamage = amount % 4;
  72. return shipDamage;
  73. }
  74. this.setState = function(state) {
  75. switch (state) {
  76. case 'adrift':
  77. this.setStateAdrift();
  78. break;
  79. case 'idle':
  80. this.setStateIdle();
  81. break;
  82. case 'travel':
  83. this.setStateTravel();
  84. break;
  85. case 'docking':
  86. this.setStateDocking();
  87. break;
  88. case 'dock':
  89. this.setStateDock();
  90. break;
  91. }
  92. };
  93. this.setStateAdrift = function() {
  94. angleRadians = 37 * (Math.PI / 180);
  95. velocity = {
  96. x : 0.2,
  97. y : 0.2
  98. };
  99. isThrusting = false;
  100. }
  101. this.setStateIdle = function() {
  102. angleRadians = 0 * (Math.PI / 180);
  103. velocity = {
  104. x : 0.0,
  105. y : 0.0
  106. };
  107. isThrusting = false;
  108. }
  109. this.setStateTravel = function() {
  110. angleRadians = 0 * (Math.PI / 180);
  111. velocity = {
  112. x : 0.0,
  113. y : 5.0
  114. };
  115. isThrusting = true;
  116. };
  117. this.setStateDocking = function() {
  118. angleRadians = 90 * (Math.PI / 180);
  119. velocity = {
  120. x : -1.0,
  121. y : 0.0
  122. };
  123. isThrusting = false;
  124. };
  125. this.setStateDock = function() {
  126. isDocked = true;
  127. angleRadians = 90 * (Math.PI / 180);
  128. velocity = {
  129. x : 0.0,
  130. y : 0.0
  131. };
  132. isThrusting = false;
  133. };
  134. };
  135. var viewport = new Viewport();