1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- 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 = listeningComponents.filter((dataThing) => {
- dataThing.component.object3d.uuid != component.object3d.uuid
- })
- 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 dataThing = listeningComponents[i]
- this.allHovered.push(dataThing)
- if (dataThing.component.onMouseOver(dataThing.intersect) === false) {
- break;
- }
- }
- }
- }
- RE.registerComponent(RaycastReporter);
|