Ship.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import * as THREE from './threejs/build/three.module.js';
  2. import { keys } from './Core.js';
  3. import { GLTFLoader } from './threejs/examples/jsm/loaders/GLTFLoader.js';
  4. export class Ship {
  5. constructor(scene) {
  6. this.scene = scene;
  7. this.targetAngle = 0;
  8. this.angle = 0;
  9. this.speed = 0.01;
  10. this.turnSpeed = 60 * Math.PI / 180;
  11. this.position = new THREE.Vector3();
  12. this.velocity = new THREE.Vector3();
  13. this.acceleration = new THREE.Vector3();
  14. this.drag = new THREE.Vector3(0.97, 1, 0.97);
  15. this.clock = new THREE.Clock();
  16. }
  17. static loadShip() {
  18. return new Promise((resolve, reject) => {
  19. var gltfloader = new GLTFLoader();
  20. return gltfloader.load(
  21. './models/Models/glTF format/ship_light.gltf',
  22. function (gltf) {
  23. resolve(gltf);
  24. },
  25. function (xhr) { },
  26. function (error) {
  27. reject(error);
  28. }
  29. );
  30. });
  31. }
  32. init() {
  33. this.scene.traverse(function (node) {
  34. if (node.isMesh) { /*node.castShadow = true;*/ node.receiveShadow = true; }
  35. });
  36. let starboardLight = new THREE.PointLight(0x80ffff, 0.8, 5, 1);
  37. starboardLight.position.set(1.25, 2.05, 2);
  38. starboardLight.castShadow = true;
  39. this.scene.add(starboardLight);
  40. let portLight = new THREE.PointLight(0xff80ff, 0.8, 5, 1);
  41. portLight.position.set(-2.5, 2.05, 2);
  42. portLight.castShadow = true;
  43. this.scene.add(portLight);
  44. let bowLight = new THREE.PointLight(0xffff80, 0.8, 5, 1);
  45. bowLight.position.set(-0.75, 1.40, -4.75);
  46. bowLight.castShadow = true;
  47. this.scene.add(bowLight);
  48. }
  49. handleInput() {
  50. if (keys["KeyA"]) { //left
  51. this.targetAngle += this.turnSpeed;
  52. }
  53. if (keys["KeyD"]) { //right
  54. this.targetAngle += -this.turnSpeed;
  55. }
  56. this.acceleration.set(0, 0, 0);
  57. if (keys["KeyW"]) { //up
  58. this.acceleration.set(this.speed * Math.sin((180 + this.angle) * Math.PI / 180), 0, this.speed * Math.cos((180 + this.angle) * Math.PI / 180));
  59. //this.targetAngle = 0;
  60. }
  61. if (keys["KeyS"]) { //down
  62. //this.acceleration.set(this.speed * Math.cos(this.angle), 0, this.speed * Math.sin(this.angle));
  63. //this.targetAngle = 180;
  64. }
  65. }
  66. computeWave() {
  67. let u_time = this.clock.getElapsedTime();
  68. let p = this.position;
  69. let wave = {
  70. x: 0,
  71. z: 0
  72. };
  73. wave.x = Math.sin(2.5 * (u_time + (p.z * 0.1)));
  74. wave.x += Math.sin(3.5 * (u_time + (p.x * 0.1)));
  75. wave.z = Math.sin(2.5 * (u_time + (p.z * 0.1)));
  76. wave.z += Math.sin(3.5 * (u_time + (p.x * 0.1)));
  77. return wave;
  78. }
  79. updateModel() {
  80. if (this.angle != this.targetAngle) {
  81. this.angle += this.turnSpeed * (this.angle - this.targetAngle > 0 ? -1 : 1);
  82. if (Math.abs(this.angle - this.targetAngle) < this.turnSpeed) {
  83. this.angle = this.targetAngle;
  84. }
  85. }
  86. let wave = this.computeWave();
  87. this.position.add(this.velocity.add(this.acceleration).multiply(this.drag));
  88. this.position.setY(0.3 * wave.x + 0.4);
  89. //this.position.setY(0.20 * Math.sin(new Date().getTime() / 500) - 0.5);
  90. this.scene.rotation.set(
  91. 0.05 * wave.x,
  92. this.angle * Math.PI / 180,
  93. 0.05 * wave.z);
  94. this.scene.position.set(this.position.x, this.position.y, this.position.z);
  95. }
  96. }