import * as RE from 'rogue-engine'; import * as THREE from 'three' import { Vector2 } from 'three'; import RaycastReceiver from './RaycastReceiver.re'; export default class RaycastReporter extends RE.Component { // @RE.Prop("Select") something; // somethingOptions = ["One", "Two"] @RE.PropList("String") receiverClass = ["RaycastReceiver"] allHovered = [] awake() { } start() { this.mouse = new THREE.Vector2(0,0) this.raycaster = new THREE.Raycaster() this.updated = false RE.Input.touch.enabled = true } normalizeScreenInput(browserVector, gameVector) { const bounds = RE.Runtime.rogueDOMContainer.getBoundingClientRect(); gameVector.x = ( ( browserVector.x - bounds.left ) / bounds.width ) * 2 - 1; gameVector.y = -( ( browserVector.y - bounds.top ) / bounds.height ) * 2 + 1 } getMouseInput() { if (!RE.Input.mouse.isMoving) { return } this.normalizeScreenInput(RE.Input.mouse, this.mouse) this.updated = true } getTouchInput() { if(RE.Input.touch.touches.length == 0) { return } this.normalizeScreenInput(RE.Input.touch.touches[0], this.mouse) this.updated = true } update() { this.updated = false this.getMouseInput() this.getTouchInput() if(!this.updated) { return } this.raycaster.setFromCamera(this.mouse, this.object3d) let intersects = this.raycaster.intersectObjects(RE.App.currentScene.children) let listeningComponents = [] intersects.forEach(intersect => { let object3d = RE.App.currentScene.getObjectByProperty('uuid', intersect.object.uuid) let component = RE.getComponent(RaycastReceiver, object3d) while(component == null && object3d.parent != null) { object3d = object3d.parent component = RE.getComponent(RaycastReceiver, object3d) } if(component != null) { listeningComponents.push( {component: component, intersect: intersect}) } }) for(let i = 0; i < this.allHovered.length; i++) { let dataThing = this.allHovered[i] if(!listeningComponents.includes(dataThing)) { dataThing.component.onMouseOut() } } this.allHovered = [] for(let i = 0; i < listeningComponents.length; i++) { let component = listeningComponents[i].component if(component.onMouseOver(listeningComponents[i].intersect) === false) { this.allHovered.push(listeningComponents[i]) break; } this.allHovered.push(listeningComponents[i]) } } } RE.registerComponent(RaycastReporter);