viewport.js 2.8 KB

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