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.

71 lines
2.2 KiB

3 years ago
  1. package wow.doge.mygame.game.controls
  2. import com.jme3.math.Quaternion
  3. import com.jme3.scene.Node
  4. import com.jme3.scene.control.AbstractControl
  5. import com.jme3.renderer.RenderManager
  6. import com.jme3.renderer.ViewPort
  7. import monix.reactive.Observable
  8. import wow.doge.mygame.game.subsystems.input.PlayerCameraInput
  9. import com.jme3.scene.Spatial
  10. import monix.{eval => me}
  11. import com.jme3.math.FastMath
  12. import com.jme3.math.Vector3f
  13. import monix.execution.Cancelable
  14. import monix.execution.Scheduler
  15. class CameraMovementControl(
  16. rotationBuf: Quaternion,
  17. obs: Observable[PlayerCameraInput],
  18. rotateFn: Quaternion => Unit
  19. )(implicit s: Scheduler)
  20. extends AbstractControl {
  21. private var _event: PlayerCameraInput = null
  22. private var _subscriptionToken: Cancelable = null
  23. override def controlUpdate(tpf: Float): Unit =
  24. if (_event != null)
  25. _event match {
  26. case PlayerCameraInput.CameraRotateLeft =>
  27. val rot = rotationBuf
  28. .fromAngleAxis(1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  29. rotateFn(rot)
  30. case PlayerCameraInput.CameraRotateRight =>
  31. val rot = rotationBuf
  32. .fromAngleAxis(-1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  33. rotateFn(rot)
  34. case PlayerCameraInput.CameraRotateUp =>
  35. val rot = rotationBuf
  36. .fromAngleAxis(-1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_X)
  37. rotateFn(rot)
  38. case PlayerCameraInput.CameraRotateDown =>
  39. val rot = rotationBuf
  40. .fromAngleAxis(1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_X)
  41. rotateFn(rot)
  42. }
  43. override def controlRender(
  44. x$1: RenderManager,
  45. x$2: ViewPort
  46. ): Unit = {}
  47. override def setSpatial(spatial: Spatial): Unit = {
  48. super.setSpatial(spatial)
  49. if (this.spatial != null)
  50. _subscriptionToken =
  51. obs.doOnNext(event => me.Task { _event = event }).subscribe()
  52. else {
  53. _subscriptionToken.cancel()
  54. _subscriptionToken = null
  55. }
  56. }
  57. }
  58. class FollowControl(playerNode: Node) extends AbstractControl {
  59. override def controlUpdate(tpf: Float): Unit =
  60. this.spatial.setLocalTranslation(playerNode.getLocalTranslation())
  61. override def controlRender(
  62. rm: RenderManager,
  63. vp: ViewPort
  64. ): Unit = {}
  65. }