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.

82 lines
2.4 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
  1. package wow.doge.mygame.game.entities
  2. import akka.actor.typed.Behavior
  3. import akka.actor.typed.LogOptions
  4. import akka.actor.typed.scaladsl.ActorContext
  5. import akka.actor.typed.scaladsl.Behaviors
  6. import com.jme3.math.FastMath
  7. import com.jme3.math.Quaternion
  8. import com.jme3.math.Vector3f
  9. import com.jme3.scene.Node
  10. import com.typesafe.scalalogging.Logger
  11. import org.slf4j.event.Level
  12. object PlayerCameraActor {
  13. sealed trait Command
  14. case object RotateLeft extends Command
  15. case object RotateRight extends Command
  16. case object RotateUp extends Command
  17. case object RotateDown extends Command
  18. case object Tick extends Command
  19. class Props(
  20. val cameraPivotNode: Node,
  21. val enqueueR: Function1[() => Unit, Unit],
  22. val getPlayerLocation: Function0[Vector3f]
  23. ) {
  24. def create =
  25. Behaviors.logMessages(
  26. LogOptions()
  27. .withLevel(Level.TRACE)
  28. .withLogger(
  29. Logger[PlayerCameraActor].underlying
  30. ),
  31. Behaviors.setup[Command] { ctx =>
  32. new PlayerCameraActor(ctx, this).receive()
  33. }
  34. )
  35. }
  36. case class State()
  37. object State {
  38. val empty = State()
  39. }
  40. }
  41. class PlayerCameraActor(
  42. ctx: ActorContext[PlayerCameraActor.Command],
  43. props: PlayerCameraActor.Props
  44. ) {
  45. import PlayerCameraActor._
  46. def receive(
  47. rotationBuf: Quaternion = new Quaternion(),
  48. state: State = State.empty
  49. ): Behavior[Command] =
  50. Behaviors.receiveMessage {
  51. case RotateLeft =>
  52. val rot = rotationBuf
  53. .fromAngleAxis(1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  54. props.enqueueR(() => props.cameraPivotNode.rotate(rot))
  55. Behaviors.same
  56. case RotateRight =>
  57. val rot = rotationBuf
  58. .fromAngleAxis(-1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  59. props.enqueueR(() => props.cameraPivotNode.rotate(rot))
  60. Behaviors.same
  61. case RotateUp =>
  62. val rot = rotationBuf
  63. .fromAngleAxis(-1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_X)
  64. props.enqueueR(() => props.cameraPivotNode.rotate(rot))
  65. Behaviors.same
  66. case RotateDown =>
  67. val rot = rotationBuf
  68. .fromAngleAxis(1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_X)
  69. props.enqueueR(() => props.cameraPivotNode.rotate(rot))
  70. Behaviors.same
  71. case Tick =>
  72. props.enqueueR(() => {
  73. val location = props.getPlayerLocation()
  74. props.cameraPivotNode.setLocalTranslation(location)
  75. })
  76. Behaviors.same
  77. }
  78. }