Hive.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import * as THREE from '../threejs/build/three.module.js';
  2. import { GLTFLoader } from '../threejs/examples/jsm/loaders/GLTFLoader.js';
  3. import { Bee } from './Bee.js';
  4. import { Waypoint } from './Waypoint.js';
  5. export class Hive {
  6. static collection = [];
  7. constructor(scene) {
  8. this.scene = scene;
  9. this.position = new THREE.Vector3();
  10. this.rotation = new THREE.Vector3();
  11. this.model = null;
  12. this.honeyReserve = 0;
  13. this.beeSlots = 10;
  14. this.queenSlots = 1;
  15. this.entranceWaypoint = null;
  16. this.billboard = null;
  17. this.yWobble = 0;
  18. this.swarm;
  19. }
  20. static loadModel() {
  21. return new Promise((resolve, reject) => {
  22. var gltfloader = new GLTFLoader();
  23. return gltfloader.load(
  24. './beehive/scene.gltf',
  25. function (gltf) {
  26. resolve(gltf);
  27. },
  28. function (xhr) { },
  29. function (error) {
  30. reject(error);
  31. }
  32. );
  33. });
  34. }
  35. async load(model) {
  36. this.model = model.scene.clone() ?? await Hive.loadModel().scene;
  37. this.billboardCanvas = document.createElement('canvas');
  38. this.billboardCanvas.width = 500;
  39. this.billboardCanvas.height = 300;
  40. this.billboardContext = this.billboardCanvas.getContext('2d');
  41. this.billboardTexture = new THREE.CanvasTexture(this.billboardCanvas);
  42. this.billboard = new THREE.Sprite(new THREE.SpriteMaterial({ map: this.billboardTexture }));
  43. }
  44. init(i) {
  45. this.model.scale.set(50, 50, 50);
  46. this.model.traverse(function (node) {
  47. if (node.isMesh) { node.castShadow = true; node.receiveShadow = true; }
  48. });
  49. this.position.x = (70 * i) - 70;
  50. this.position.z = Math.abs((20 * i) - 20);
  51. this.rotation.y = ((Math.PI / 4)) - ((Math.PI / 4) * i);
  52. this.entranceWaypoint = Waypoint.addNewHive(this.scene, this);
  53. this.billboard.position.set(this.position.x, this.position.y + 100, this.position.z);
  54. this.billboard.scale.set(this.billboardCanvas.width / 10, this.billboardCanvas.height / 10, 1);
  55. }
  56. addToScene(scene) {
  57. scene.add(this.model);
  58. }
  59. update(delta, timer) {
  60. this.model.position.set(this.position.x, this.position.y, this.position.z);
  61. this.model.rotation.set(this.rotation.x, this.rotation.y, this.rotation.z);
  62. this.yWobble = Math.cos(timer * 0.008) * 4;
  63. this.billboardContext.clearRect(0, 0, this.billboardCanvas.width, this.billboardCanvas.height);
  64. this.canvasDraw(this.billboardContext);
  65. this.billboardTexture.needsUpdate = true;
  66. }
  67. canvasDraw(context) {
  68. context.fillStyle = "#EB9605";
  69. context.beginPath();
  70. context.moveTo(40, this.yWobble + 72);
  71. context.bezierCurveTo(40, this.yWobble + 72, 70, this.yWobble + 72, 40, this.yWobble + 20);
  72. context.bezierCurveTo(40, this.yWobble + 20, 10, this.yWobble + 72, 40, this.yWobble + 72);
  73. context.fill();
  74. context.beginPath();
  75. context.arc(40, this.yWobble + 124, 20, 0, 2 * Math.PI);
  76. context.fill();
  77. context.fillStyle = "white";
  78. context.font = "72px Arial";
  79. context.textAlign = "left";
  80. context.fillText(`Honey ${this.honeyReserve}`, 72, 72);
  81. context.fillStyle = "white";
  82. context.font = "72px Arial";
  83. context.textAlign = "left";
  84. context.fillText(`Bees ${10 - this.beeSlots}`, 72, 144);
  85. }
  86. visitFromBee(bee) {
  87. this.honeyReserve += bee.carryingHoney;
  88. bee.carryingHoney = 0;
  89. //let randomFlower = Flower.collection[Math.floor(Flower.collection.length * Math.random())];
  90. //bee.setTarget(randomFlower);
  91. bee.setTarget(this.entranceWaypoint);
  92. this.spawnBee();
  93. }
  94. spawnBee() {
  95. if (this.honeyReserve > 15 && this.beeSlots > 0) {
  96. this.honeyReserve -= 15;
  97. this.beeSlots--;
  98. let newBee = new Bee();
  99. newBee.model = Bee.baseModel.clone();
  100. newBee.model.material = newBee.model.material.clone();
  101. newBee.init();
  102. newBee.setSwarm(this.swarm);
  103. newBee.setHive(this);
  104. newBee.setPosition(this.position.x, this.position.y, this.position.z);
  105. newBee.setLastPosition(this.position.x, this.position.y, this.position.z);
  106. // newBee.setTarget(Flower.collection[0]);
  107. newBee.setTarget(this.entranceWaypoint);
  108. newBee.addToScene(this.scene);
  109. Bee.collection.push(newBee);
  110. }
  111. if (this.honeyReserve > 200 && this.queenSlots > 0) {
  112. this.honeyReserve -= 200;
  113. this.queenSlots--;
  114. let newBee = new Bee();
  115. newBee.model = Bee.baseModel.clone();
  116. newBee.model.material = newBee.model.material.clone();
  117. newBee.init();
  118. let claimedHives = Bee.collection.map(bee => bee.homeHive);
  119. let nextHive = Hive.collection.filter(hive => !claimedHives.includes(hive))[0];
  120. if (!nextHive) {
  121. return;
  122. }
  123. newBee.setSwarm(this.swarm);
  124. newBee.setHive(nextHive);
  125. newBee.setPosition(this.position.x, this.position.y, this.position.z);
  126. newBee.setLastPosition(nextHive.position.x, nextHive.position.y, nextHive.position.z);
  127. newBee.setTarget(nextHive);
  128. newBee.addToScene(this.scene);
  129. Bee.collection.push(newBee);
  130. }
  131. }
  132. toggleFlower(flower) {
  133. if (!this.entranceWaypoint.contains(flower)) {
  134. this.entranceWaypoint.addLink(flower);
  135. this.entranceWaypoint.addLine(flower);
  136. } else {
  137. this.entranceWaypoint.removeLink(flower);
  138. this.entranceWaypoint.removeLine(flower);
  139. }
  140. }
  141. setSwarm(id) {
  142. this.swarm = id;
  143. }
  144. }