ingredient.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { loadGltf } from './loadgltf.js'
  2. import gsap from 'gsap'
  3. import * as THREE from 'three'
  4. export default class Ingredient {
  5. constructor(ingredientInfo, game) {
  6. this.ingredientInfo = ingredientInfo
  7. this.model = null
  8. this.game = game
  9. }
  10. getName() {
  11. return this.ingredientInfo.name
  12. }
  13. async spawn(shelfSlot, stageData) {
  14. this.model = await loadGltf(this.game, this.ingredientInfo.model)
  15. this.shelfSlot = shelfSlot
  16. this.model.position.x = shelfSlot.position.x
  17. this.model.position.y = shelfSlot.position.y
  18. this.model.position.z = shelfSlot.position.z
  19. this.game.scene.add(this.model)
  20. return this.model
  21. }
  22. beginBrew() {
  23. this.model.moving = true
  24. clearTimeout(this.model.timeoutId)
  25. gsap.to(this.model.scale, {
  26. duration: 2.5, x: 0, y: 0, z: 0, onComplete: () => {
  27. this.model.visible = false
  28. this.model.timeoutId = setTimeout(() => {
  29. this.model.position.x = this.shelfSlot.position.x
  30. this.model.position.y = this.ingredientInfo.wobble.amplitude * Math.sin(this.ingredientInfo.wobble.frequency * this.game.clock.getElapsedTime() + 700) + (this.shelfSlot.position.y + this.ingredientInfo.shelfOffset)
  31. this.model.position.z = this.shelfSlot.position.z
  32. this.model.visible = true
  33. gsap.to(this.model.scale, {
  34. duration: 0.7, x: 1, y: 1, z: 1, ease: "bounce", onComplete: () => {
  35. this.model.moving = false
  36. }
  37. })
  38. }, 3000)
  39. }
  40. })
  41. gsap.to(this.model.position, {
  42. duration: 1.5, motionPath: this.shelfSlot.motionPath, onComplete: () => {
  43. }
  44. })
  45. }
  46. wobble() {
  47. if (!this.model.moving) {
  48. this.model.position.y = this.ingredientInfo.wobble.amplitude * Math.sin( this.ingredientInfo.wobble.frequency * this.game.clock.getElapsedTime()) + (this.shelfSlot.position.y + this.ingredientInfo.shelfOffset)
  49. this.model.rotateOnAxis(new THREE.Vector3(0, 1, 0), - this.ingredientInfo.wobble.rotation)
  50. }
  51. }
  52. }