import * as RE from 'rogue-engine'; import RaycastReceiver from './RaycastReceiver.re'; import GameLogic from './GameLogic.re'; import { SimpleTween } from './Library/SimpleTweens' import Easing from './Library/Easings'; import * as THREE from 'three' export default class TileRaycastReceiver extends RaycastReceiver { @RE.Prop("Vector2") tileSize = new THREE.Vector2(1.1, 1.1) @RE.Prop("Audio") dropInSound @RE.Prop("Audio") hoverSound @RE.Prop("Audio") clearSound onMouseOver(intersect) { if(this.object3d.isAnimating) { return } this.currentObj = intersect.object this.currentObj.material = this.currentObj.highlightMaterial RE.Runtime.rogueDOMContainer.style.cursor = "pointer" if(RE.Input.mouse.getButtonPressed(0) || RE.Input.touch.touches.length == 1) { let game = RE.getComponent(GameLogic) if(game.addTile(this)) { if(this.hoverSound) { this.hoverSound.play() } } } } onMouseOut(){ if(this.object3d.isAnimating || !this.currentObj) { return } if(RE.Input.mouse.getButtonPressed(0)) { //dragged out of this tile, should track state so a second "leave" will unselect } this.currentObj.material = this.currentObj.originalMaterial RE.Runtime.rogueDOMContainer.style.cursor = "default" } getOriginalMaterial() { return this.object3d.originalMaterial } getPosition() { return this.object3d.position } awake() { } start() { if(this.dropInSound) { this.dropInSound.setVolume(0.7) this.dropInSound.setLoop(false) } if(this.hoverSound) { this.hoverSound.setVolume(0.7) this.hoverSound.setLoop(false) } if(this.clearSound) { this.clearSound.setVolume(1) this.clearSound.setLoop(false) } } update() { } initialize(gridPosition, originalMaterial, hoverMaterial, tweenManager, delay, startingY, easingFunction) { this.object3d.position.copy(new THREE.Vector3(this.tileSize.x * gridPosition.x, startingY, this.tileSize.y * gridPosition.y)) this.object3d.originalMaterial = originalMaterial this.object3d.highlightMaterial = hoverMaterial this.object3d.material = originalMaterial this.object3d.gridPosition = gridPosition this.object3d.isAnimating = true setTimeout(() => { if(this.dropInSound) { this.dropInSound.play() } }, (delay/3) * Math.random()) tweenManager.add(new SimpleTween(this.object3d.position, {y: 1}, delay, easingFunction, () => { this.object3d.isAnimating = false })) } remove(tweenManager) { this.object3d.isAnimating = true setTimeout(() => { if(this.clearSound) { this.clearSound.play() } }, 150 * Math.random()) tweenManager.add(new SimpleTween(this.object3d.position, {y: -14}, Math.floor(1500 * Math.random()) + 300, Easing.Quadratic.EaseIn, () => { this.object3d.parent.remove(this.object3d) })) } } RE.registerComponent(TileRaycastReceiver);