TileRaycastReceiver.re.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import * as RE from 'rogue-engine';
  2. import RaycastReceiver from './RaycastReceiver.re';
  3. import GameLogic from './GameLogic.re';
  4. import { SimpleTween } from './Library/SimpleTweens'
  5. import Easing from './Library/Easings';
  6. import * as THREE from 'three'
  7. export default class TileRaycastReceiver extends RaycastReceiver {
  8. @RE.Prop("Vector2") tileSize = new THREE.Vector2(1.1, 1.1)
  9. @RE.Prop("Audio") dropInSound
  10. @RE.Prop("Audio") hoverSound
  11. @RE.Prop("Audio") clearSound
  12. onMouseOver(intersect) {
  13. if(this.object3d.isAnimating) {
  14. return
  15. }
  16. this.currentObj = intersect.object
  17. this.currentObj.material = this.currentObj.highlightMaterial
  18. RE.Runtime.rogueDOMContainer.style.cursor = "pointer"
  19. if(RE.Input.mouse.getButtonPressed(0) || RE.Input.touch.touches.length == 1) {
  20. let game = RE.getComponent(GameLogic)
  21. if(game.addTile(this)) {
  22. if(this.hoverSound) {
  23. this.hoverSound.play()
  24. }
  25. }
  26. }
  27. }
  28. onMouseOut(){
  29. if(this.object3d.isAnimating || !this.currentObj) {
  30. return
  31. }
  32. if(RE.Input.mouse.getButtonPressed(0)) {
  33. //dragged out of this tile, should track state so a second "leave" will unselect
  34. }
  35. this.currentObj.material = this.currentObj.originalMaterial
  36. RE.Runtime.rogueDOMContainer.style.cursor = "default"
  37. }
  38. getOriginalMaterial() {
  39. return this.object3d.originalMaterial
  40. }
  41. getPosition() {
  42. return this.object3d.position
  43. }
  44. awake() {
  45. }
  46. start() {
  47. if(this.dropInSound) {
  48. this.dropInSound.setVolume(0.7)
  49. this.dropInSound.setLoop(false)
  50. }
  51. if(this.hoverSound) {
  52. this.hoverSound.setVolume(0.7)
  53. this.hoverSound.setLoop(false)
  54. }
  55. if(this.clearSound) {
  56. this.clearSound.setVolume(1)
  57. this.clearSound.setLoop(false)
  58. }
  59. }
  60. update() {
  61. }
  62. initialize(gridPosition, originalMaterial, hoverMaterial, tweenManager, delay, startingY, easingFunction) {
  63. this.object3d.position.copy(new THREE.Vector3(this.tileSize.x * gridPosition.x, startingY, this.tileSize.y * gridPosition.y))
  64. this.object3d.originalMaterial = originalMaterial
  65. this.object3d.highlightMaterial = hoverMaterial
  66. this.object3d.material = originalMaterial
  67. this.object3d.gridPosition = gridPosition
  68. this.object3d.isAnimating = true
  69. setTimeout(() => {
  70. if(this.dropInSound) {
  71. this.dropInSound.play()
  72. }
  73. }, (delay/3) * Math.random())
  74. tweenManager.add(new SimpleTween(this.object3d.position, {y: 1}, delay, easingFunction, () => {
  75. this.object3d.isAnimating = false
  76. }))
  77. }
  78. remove(tweenManager) {
  79. this.object3d.isAnimating = true
  80. setTimeout(() => {
  81. if(this.clearSound) {
  82. this.clearSound.play()
  83. }
  84. }, 150 * Math.random())
  85. tweenManager.add(new SimpleTween(this.object3d.position, {y: -14}, Math.floor(1500 * Math.random()) + 300, Easing.Quadratic.EaseIn, () => {
  86. this.object3d.parent.remove(this.object3d)
  87. }))
  88. }
  89. }
  90. RE.registerComponent(TileRaycastReceiver);