Bee.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import * as THREE from 'three';
  2. import { Hive } from './Hive.js';
  3. export class Bee {
  4. static collection = [];
  5. static baseModel = null;
  6. constructor() {
  7. this.position = new THREE.Vector3();
  8. this.rotation = new THREE.Vector3();
  9. this.lastPosition = new THREE.Vector3();
  10. this.targetPosition = new THREE.Vector3();
  11. this.maxSpeed = 0.5;
  12. this.model = null;
  13. this.homeHive = null;
  14. this.targetObject = null;
  15. this.carryingHoney = 0;
  16. this.swarm = 0;
  17. }
  18. static async loadGeo() {
  19. return new THREE.IcosahedronGeometry(3, 0);
  20. }
  21. static async loadMat() {
  22. return new THREE.MeshPhongMaterial({ color: 0xcccc00 });
  23. }
  24. static async loadModel() {
  25. return new THREE.Mesh(await Bee.loadGeo(), await Bee.loadMat());
  26. }
  27. async load(model) {
  28. this.model = model.clone() ?? await Bee.loadModel();
  29. this.init();
  30. }
  31. init() {
  32. this.model.castShadow = true;
  33. this.model.receiveShadow = true;
  34. this.targetPosition.x = 0;
  35. this.targetPosition.y = 15;
  36. this.targetPosition.z = 0;
  37. this.homeHive = Hive.collection[0];
  38. }
  39. addToScene(scene) {
  40. scene.add(this.model);
  41. }
  42. setHive(homeHive) {
  43. this.homeHive = homeHive;
  44. }
  45. setSwarm(id) {
  46. this.swarm = id;
  47. switch (this.swarm) {
  48. case 2:
  49. this.model.material.color.set(0xff0000);
  50. break;
  51. case 1:
  52. default:
  53. this.model.material.color.set(0xffff00);
  54. break;
  55. }
  56. }
  57. setPosition(x, y, z) {
  58. this.position.x = x;
  59. this.position.y = y;
  60. this.position.z = z;
  61. }
  62. setLastPosition(x, y, z) {
  63. this.lastPosition.x = x;
  64. this.lastPosition.y = y;
  65. this.lastPosition.z = z;
  66. }
  67. setTarget(target) {
  68. this.targetObject = target;
  69. this.setLastPosition(this.targetPosition.x, this.targetPosition.y, this.targetPosition.z);
  70. this.targetPosition.x = this.targetObject.position.x;
  71. this.targetPosition.y = this.targetObject.position.y;
  72. this.targetPosition.z = this.targetObject.position.z;
  73. }
  74. nextAction() {
  75. this.targetObject.visitFromBee(this);
  76. }
  77. update(delta, timer) {
  78. let fullDistance = Math.hypot(this.targetPosition.x - this.lastPosition.x, this.targetPosition.z - this.lastPosition.z);
  79. let distanceDone = Math.hypot(this.position.x - this.lastPosition.x, this.position.z - this.lastPosition.z);
  80. let radiansToTarget = Math.atan2(this.targetPosition.z - this.position.z, this.targetPosition.x - this.position.x);
  81. // why do these do weird things?
  82. // let fullDistance = this.targetPosition.distanceTo(this.lastPosition);
  83. // let distanceDone = this.position.distanceTo(this.lastPosition);
  84. // let radiansToTarget = this.position.angleTo(this.targetPosition);
  85. let progressRatio = distanceDone / fullDistance;
  86. if (progressRatio >= 0.99) {
  87. this.nextAction();
  88. return;
  89. }
  90. this.position.x += this.maxSpeed * Math.cos(radiansToTarget);
  91. this.position.y = Math.cos(timer * 0.008) * 1 + 15;
  92. this.position.z += this.maxSpeed * Math.sin(radiansToTarget);
  93. this.rotation.x = 0;
  94. this.rotation.y = Math.sin(timer * 0.0005);
  95. this.rotation.z = Math.sin(timer * 0.0005);
  96. this.model.position.set(this.position.x, this.position.y, this.position.z);
  97. this.model.rotation.set(this.rotation.x, this.rotation.y, this.rotation.z);
  98. }
  99. }