Testing out JmonkeyEngine to make a game in Scala with Akka Actors within a pure FP layer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package wow.doge.mygame.game.controls
  2. import scala.concurrent.Future
  3. import com.jme3.math.FastMath
  4. import com.jme3.math.Quaternion
  5. import com.jme3.math.Vector3f
  6. import com.jme3.renderer.RenderManager
  7. import com.jme3.renderer.ViewPort
  8. import com.jme3.scene.Node
  9. import com.jme3.scene.Spatial
  10. import com.jme3.scene.control.AbstractControl
  11. import monix.execution.Ack
  12. import monix.execution.Cancelable
  13. import monix.execution.Scheduler
  14. import monix.reactive.Observable
  15. import monix.reactive.Observer
  16. import wow.doge.mygame.game.subsystems.input.PlayerCameraInput
  17. /**
  18. * A very low level (and error prone) camera movement control implementation.
  19. * Not used currently
  20. *
  21. * @param rotationBuf
  22. * @param obs
  23. * @param rotateFn
  24. * @param s
  25. */
  26. class CameraMovementControl(
  27. rotationBuf: Quaternion,
  28. obs: Observable[PlayerCameraInput],
  29. rotateFn: Quaternion => Unit
  30. )(implicit s: Scheduler)
  31. extends AbstractControl {
  32. private var _event: PlayerCameraInput = null
  33. private var _subscriptionToken: Cancelable = null
  34. private val sink = new Observer[PlayerCameraInput] {
  35. override def onNext(event: PlayerCameraInput): Future[Ack] = {
  36. _event = event
  37. Ack.Continue
  38. }
  39. override def onError(ex: Throwable): Unit = {}
  40. override def onComplete(): Unit = {}
  41. }
  42. override def controlUpdate(tpf: Float): Unit =
  43. if (_event != null) {
  44. _event match {
  45. case PlayerCameraInput.CameraRotateLeft =>
  46. val rot = rotationBuf
  47. .fromAngleAxis(1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  48. rotateFn(rot)
  49. case PlayerCameraInput.CameraRotateRight =>
  50. val rot = rotationBuf
  51. .fromAngleAxis(-1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  52. rotateFn(rot)
  53. case PlayerCameraInput.CameraRotateUp =>
  54. val rot = rotationBuf
  55. .fromAngleAxis(-1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_X)
  56. rotateFn(rot)
  57. case PlayerCameraInput.CameraRotateDown =>
  58. val rot = rotationBuf
  59. .fromAngleAxis(1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_X)
  60. rotateFn(rot)
  61. }
  62. _event = null
  63. }
  64. override def controlRender(
  65. x$1: RenderManager,
  66. x$2: ViewPort
  67. ): Unit = {}
  68. override def setSpatial(spatial: Spatial): Unit = {
  69. super.setSpatial(spatial)
  70. if (this.spatial != null)
  71. _subscriptionToken = obs.subscribe(sink)
  72. else {
  73. _subscriptionToken.cancel()
  74. _subscriptionToken = null
  75. }
  76. }
  77. }
  78. class FollowControl(playerNode: Node) extends AbstractControl {
  79. override def controlUpdate(tpf: Float): Unit =
  80. this.spatial.setLocalTranslation(playerNode.getLocalTranslation())
  81. override def controlRender(
  82. rm: RenderManager,
  83. vp: ViewPort
  84. ): Unit = {}
  85. }