FloorCheckComponent.re.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import * as RE from 'rogue-engine'
  2. import * as THREE from 'three'
  3. export default class FloorCheckComponent extends RE.Component {
  4. raycaster: THREE.Raycaster
  5. down = new THREE.Vector3(0, -1, 0)
  6. @RE.props.num() near: number = 0
  7. @RE.props.num() far: number = Infinity
  8. isOnFloor: boolean = false
  9. @RE.props.num() touchingFloorDistance: number = 1
  10. childrenToFilter
  11. awake() {
  12. this.raycaster = new THREE.Raycaster(this.object3d.position, this.down, this.near, this.far)
  13. this.childrenToFilter = this.getAllChildrenUuids(this.object3d)
  14. }
  15. getAllChildrenUuids(child) {
  16. return child.children.flatMap(innerChild => {if(innerChild.children.length > 0) {return this.getAllChildrenUuids(innerChild)} return innerChild.uuid})
  17. }
  18. start() {
  19. }
  20. update() {
  21. this.raycaster.set(this.object3d.position, this.down)
  22. this.isOnFloor = false
  23. let intersects = this.raycaster.intersectObjects(RE.App.currentScene.children)
  24. const notMeIntersects = intersects.filter((intersect) => {
  25. return !this.childrenToFilter.includes(intersect.object.uuid)
  26. })
  27. notMeIntersects.forEach(intersect => {
  28. this.isOnFloor = this.isOnFloor || intersect.distance <= this.touchingFloorDistance
  29. })
  30. }
  31. }
  32. RE.registerComponent(FloorCheckComponent);