Ship.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. class Ship {
  2. constructor() {
  3. }
  4. init() {
  5. this.status = "adrift";
  6. this.name = "Dulce et Decorum est";
  7. this.position = { x: 0, y: 0 };
  8. this.rotation = -37;
  9. this.damage = 2;
  10. this.targetAngle = -37;
  11. }
  12. update(delta) {
  13. switch (this.status) {
  14. case "adrift":
  15. this.rotation -= 0.05 * delta;
  16. break;
  17. case "active":
  18. if (this.rotation != this.targetAngle) {
  19. if (Math.abs(this.rotation - this.targetAngle) >= 180) {
  20. this.rotation -= Math.min(Math.abs(this.rotation - this.targetAngle), 1);
  21. } else {
  22. this.rotation += Math.min((this.targetAngle - this.rotation), 1);
  23. }
  24. }
  25. break;
  26. }
  27. this.rotation %= 360;
  28. }
  29. draw(context, delta) {
  30. this.update(delta);
  31. context.save();
  32. context.translate(game.center.x, game.center.y);
  33. context.rotate(this.rotation * (Math.PI / 180));
  34. game.atlas.drawCentered(context, "playerShip1_blue.png", this.position);
  35. if (this.damage > 0) {
  36. game.atlas.drawCentered(context, `playerShip1_damage${this.damage}.png`, this.position);
  37. }
  38. if (this.status == "traveling") {
  39. game.atlas.drawBottomCentered(context, `fire10.png`, { x: this.position.x + 1, y: this.position.y + 17 });
  40. }
  41. context.restore();
  42. }
  43. repair() {
  44. this.damage = Math.max(this.damage - 1, 0);
  45. if (this.damage == 0) {
  46. this.status = "active";
  47. game.playerHud.ship.updateShipStatus();
  48. return true;
  49. }
  50. return false;
  51. }
  52. move() {
  53. this.status = "traveling";
  54. game.playerHud.ship.updateShipStatus();
  55. }
  56. stop() {
  57. this.status = "active";
  58. game.playerHud.ship.updateShipStatus();
  59. }
  60. setLocation(newLocation) {
  61. }
  62. handleOrientation(event) {
  63. }
  64. animateRotation(targetAngle) {
  65. this.targetAngle = targetAngle;
  66. }
  67. dock() {
  68. this.status = "docked";
  69. game.playerHud.dialog.rightSay("Ship Docking Coupling Engaged", "");
  70. }
  71. undock() {
  72. this.status = "active";
  73. }
  74. isDocked() {
  75. return this.status == "docked";
  76. }
  77. }