factor.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import * as three from "three-js";
  2. class factor {
  3. #mesh;
  4. constructor(mesh) {
  5. if (!(mesh instanceof three.Object3D)) {
  6. throw new TypeError("Must initialize with Object3D.");
  7. }
  8. this.#mesh = mesh;
  9. }
  10. get mesh() {
  11. return this.#mesh;
  12. }
  13. loop() {
  14. return null;
  15. }
  16. }
  17. class functional_factor extends factor {
  18. #animation;
  19. constructor (init, animation = null) {
  20. if (typeof(init) !== "function") {
  21. throw new TypeError("Init must be an function.");
  22. }
  23. if (animation !== null && typeof(animation) !== "function") {
  24. throw new TypeError("Animation could only be null or function.");
  25. }
  26. const mesh = init();
  27. if (!(mesh instanceof three.Object3D)) {
  28. throw new TypeError("Factor initializer must return Object3D.");
  29. }
  30. super(mesh);
  31. this.#animation = animation;
  32. }
  33. loop() {
  34. if (this.#animation !== null) {
  35. this.#animation(this);
  36. }
  37. }
  38. }
  39. export { factor, functional_factor }