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.

175 lines
5.5 KiB

4 years ago
  1. package wow.doge.mygame
  2. import com.jme3.app.SimpleApplication
  3. import com.jme3.app.state.AppStateManager
  4. import scala.reflect.ClassTag
  5. import com.jme3.app.state.AppState
  6. import com.jme3.scene.Node
  7. import com.jme3.scene.Spatial
  8. import com.simsilica.es.EntityData
  9. import com.simsilica.es.EntityComponent
  10. import com.simsilica.es.EntityId
  11. import akka.actor.typed.ActorRef
  12. import akka.util.Timeout
  13. import akka.actor.typed.Scheduler
  14. import monix.eval.Task
  15. import com.jme3.input.InputManager
  16. import com.jme3.input.controls.Trigger
  17. import com.jme3.input.controls.InputListener
  18. import com.jme3.math.Vector3f
  19. import wow.doge.mygame.math.ImVector3f
  20. import com.jme3.scene.Geometry
  21. import wow.doge.mygame.state.CardinalDirection
  22. import wow.doge.mygame.state.CanMove
  23. import com.jme3.renderer.Camera
  24. package object implicits {
  25. implicit class StateManagerExt(val sm: AppStateManager) extends AnyVal {
  26. def state[S <: AppState]()(implicit c: ClassTag[S]): S =
  27. sm.getState(c.runtimeClass.asInstanceOf[Class[S]])
  28. }
  29. implicit class SimpleApplicationExt(val sa: SimpleApplication)
  30. extends AnyVal {
  31. def stateManager() = sa.getStateManager()
  32. }
  33. implicit class NodeExt(val n: Node) extends AnyVal {
  34. def child(s: Spatial): Node = {
  35. n.attachChild(s)
  36. n
  37. }
  38. }
  39. implicit class EntityDataExt(val ed: EntityData) extends AnyVal {
  40. def query = new EntityQuery(ed)
  41. // def entities[T <: EntityComponent](entities: Seq[T])
  42. }
  43. implicit class EntityExt(val e: EntityId) extends AnyVal {
  44. def withComponents(classes: EntityComponent*)(implicit
  45. ed: EntityData
  46. ): EntityId = {
  47. ed.setComponents(e, classes: _*)
  48. e
  49. }
  50. }
  51. implicit class ActorRefExt[Req](val a: ActorRef[Req]) extends AnyVal {
  52. import akka.actor.typed.scaladsl.AskPattern._
  53. def askT[Res](
  54. replyTo: ActorRef[Res] => Req
  55. )(implicit timeout: Timeout, scheduler: Scheduler): Task[Res] = {
  56. Task.deferFuture(a.ask(replyTo)(timeout, scheduler))
  57. }
  58. }
  59. // def ?[Res](replyTo: ActorRef[Res] => Req)(implicit timeout: Timeout, scheduler: Scheduler): Future[Res] = {
  60. // ask(replyTo)(timeout, scheduler)
  61. // }
  62. implicit class InputManagerExt(val inputManager: InputManager)
  63. extends AnyVal {
  64. def withMapping(mapping: String, triggers: Trigger*): InputManager = {
  65. inputManager.addMapping(mapping, triggers: _*)
  66. inputManager
  67. }
  68. def withListener(listener: InputListener, mappings: String*) = {
  69. inputManager.addListener(listener, mappings: _*)
  70. inputManager
  71. }
  72. }
  73. implicit class Vector3fExt(val v: Vector3f) extends AnyVal {
  74. def +=(that: Vector3f) = v.addLocal(that)
  75. def *=(that: Vector3f) = v.multLocal(that)
  76. def -=(that: Vector3f) = v.subtractLocal(that)
  77. def /=(that: Vector3f) = v.divideLocal(that)
  78. def unary_- = v.negateLocal()
  79. def immutable = ImVector3f(v.x, v.y, v.z)
  80. }
  81. implicit class ImVector3fExt(val v: ImVector3f) extends AnyVal {
  82. def +(that: ImVector3f) = v.copy(v.x + that.x, v.y + that.y, v.z + that.z)
  83. def *(that: ImVector3f) = v.copy(v.x * that.x, v.y * that.y, v.z * that.z)
  84. def *(f: Float): ImVector3f =
  85. // v.copy(v.x * f, v.y * f, v.x * f)
  86. v * ImVector3f(f, f, f)
  87. def -(that: ImVector3f) = v.copy(v.x - that.x, v.y - that.y, v.z - that.z)
  88. def /(that: ImVector3f) = v.copy(v.x / that.x, v.y / that.y, v.z / that.z)
  89. def unary_- = v.copy(-v.x, -v.y, -v.z)
  90. // def unary_-(that: ImVector3f) = this.copy(this.x, this.y, this.z)
  91. def mutable = new Vector3f(v.x, v.y, v.z)
  92. }
  93. // implicit val implVector3fForVector3 = new Vector3[Vector3f] {
  94. // override def +(implicit v: Vector3f, that: com.jme3.math.Vector3f): Unit =
  95. // v += that
  96. // override def *(implicit v: Vector3f, that: Vector3f): Unit = v *= that
  97. // override def -(implicit v: Vector3f, that: Vector3f): Unit = v -= that
  98. // override def /(implicit v: Vector3f, that: Vector3f): Unit = v /= that
  99. // }
  100. // implicit val implImVector3fForVector3 = new Vector3[ImVector3f] {
  101. // override def +(implicit v: ImVector3f, that: ImVector3f): Unit =
  102. // v + that
  103. // override def *(implicit v: ImVector3f, that: ImVector3f): Unit = v * that
  104. // override def -(implicit v: ImVector3f, that: ImVector3f): Unit = v - that
  105. // override def /(implicit v: ImVector3f, that: ImVector3f): Unit = v / that
  106. // }
  107. // def test[T](v: T)(implicit ev: Vector3[T]) = {
  108. // import ev._
  109. // ev.+
  110. // }
  111. implicit val implCanMoveForGeom = new CanMove[Geometry] {
  112. override def getDirection(
  113. cam: Camera,
  114. cardinalDir: CardinalDirection
  115. ): ImVector3f = {
  116. val camDir =
  117. cam.getDirection().immutable * 0.6f
  118. val camLeft = cam.getLeft().immutable * 0.4f
  119. val zero = ImVector3f.ZERO
  120. val dir = cardinalDir
  121. val walkDir = {
  122. val mutWalkDir = new Vector3f()
  123. if (dir.left) {
  124. // ctx.log.trace("left")
  125. mutWalkDir += (zero + camLeft).mutable
  126. }
  127. if (dir.right) {
  128. // ctx.log.trace("right")
  129. mutWalkDir += (zero + -camLeft).mutable
  130. }
  131. if (dir.up) {
  132. // ctx.log.trace("up")
  133. mutWalkDir += (zero + camDir).mutable
  134. }
  135. if (dir.down) {
  136. // ctx.log.trace("down")
  137. mutWalkDir += (zero + -camDir).mutable
  138. }
  139. mutWalkDir.immutable
  140. }
  141. walkDir
  142. }
  143. override def move(geom: Geometry, direction: ImVector3f): Unit = {
  144. val v = geom.getLocalTranslation()
  145. geom.setLocalTranslation(v += direction.mutable)
  146. }
  147. }
  148. }