Flower.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import * as THREE from '../threejs/build/three.module.js';
  2. import { GLTFLoader } from '../threejs/examples/jsm/loaders/GLTFLoader.js';
  3. export class Flower {
  4. static collection = [];
  5. constructor() {
  6. this.position = new THREE.Vector3();
  7. this.rotation = new THREE.Vector3();
  8. this.model = null;
  9. this.nectarRemaining = 100;
  10. }
  11. static loadModel() {
  12. return new Promise((resolve, reject) => {
  13. var gltfloader = new GLTFLoader();
  14. return gltfloader.load(
  15. './flowers/scene.gltf',
  16. function (gltf) {
  17. resolve(gltf);
  18. },
  19. function (xhr) { },
  20. function (error) {
  21. reject(error);
  22. }
  23. );
  24. });
  25. }
  26. async load(model) {
  27. this.model = model.scene.clone() ?? await Flower.loadModel().scene;
  28. this.init();
  29. }
  30. init() {
  31. this.model.scale.set(0.5, 0.5, 0.5);
  32. this.model.traverse(function (node) {
  33. if (node.isMesh) { node.castShadow = true; node.receiveShadow = true; }
  34. });
  35. this.position.z = 100;
  36. }
  37. addToScene(scene) {
  38. scene.add(this.model);
  39. }
  40. update(delta, timer) {
  41. this.model.position.set(this.position.x, this.position.y, this.position.z);
  42. this.model.rotation.set(this.rotation.x, this.rotation.y, this.rotation.z);
  43. }
  44. visitFromBee(bee) {
  45. bee.carryingHoney += 10;
  46. this.nectarRemaining -= 10;
  47. bee.setTarget(bee.homeHive.entranceWaypoint);
  48. }
  49. }