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.

138 lines
3.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
  1. package wow.doge.mygame.game
  2. import scala.collection.immutable.Queue
  3. import scala.concurrent.ExecutionContext.Implicits.global
  4. import scala.concurrent.Future
  5. import com.jme3.app.SimpleApplication
  6. import com.jme3.app.state.AppState
  7. import com.jme3.bullet.BulletAppState
  8. import monix.bio.Task
  9. import monix.execution.CancelableFuture
  10. import monix.execution.CancelablePromise
  11. import monix.execution.Scheduler
  12. import monix.execution.atomic.Atomic
  13. import wow.doge.mygame.executors.GUIExecutorService
  14. import wow.doge.mygame.executors.Schedulers
  15. // import wow.doge.mygame.implicits._
  16. class SimpleAppExt(
  17. schedulers: Schedulers,
  18. val bulletAppState: BulletAppState,
  19. appStates: AppState*
  20. ) extends SimpleApplication(appStates: _*) {
  21. import SimpleAppExt._
  22. /**
  23. * A non blocking synchronized queue using an immutable scala queue and monix's atomic class
  24. */
  25. private val taskQueue2 = Atomic(Queue.empty[MyTask[_]])
  26. private val startSignal: CancelablePromise[Unit] = CancelablePromise()
  27. var cancelToken: Option[() => Future[Unit]] = None
  28. def started: CancelableFuture[Unit] = startSignal.future
  29. override def simpleInitApp(): Unit = {
  30. stateManager.attach(bulletAppState)
  31. startSignal.success(())
  32. }
  33. override def simpleUpdate(tpf: Float): Unit = {}
  34. override def stop(waitFor: Boolean): Unit = {
  35. cancelToken match {
  36. case Some(value) =>
  37. value().foreach { _ =>
  38. pprint.log("Called cancel in simpleapp")
  39. super.stop(true)
  40. }
  41. case None =>
  42. pprint.log("Called cancel in simpleapp")
  43. super.stop(true)
  44. }
  45. }
  46. def enqueueFuture[T](cb: () => T): CancelableFuture[T] = {
  47. val p = CancelablePromise[T]()
  48. taskQueue2.transform(_ :+ MyTask(p, cb))
  49. p.future
  50. }
  51. def enqueueL[T](cb: () => T): Task[T] =
  52. Task.deferFuture(enqueueFuture(cb))
  53. override protected def runQueuedTasks(): Unit = {
  54. taskQueue2.transform { current =>
  55. current.dequeueOption.fold(current) {
  56. case (MyTask(p, cb), updated) =>
  57. p.success(cb())
  58. updated
  59. }
  60. }
  61. super.runQueuedTasks()
  62. }
  63. object JMEExecutorService extends GUIExecutorService {
  64. override def execute(command: Runnable): Unit =
  65. enqueue(command)
  66. }
  67. val scheduler = Scheduler(JMEExecutorService)
  68. }
  69. object SimpleAppExt {
  70. private[game] case class MyTask[T](p: CancelablePromise[T], cb: () => T)
  71. }
  72. // val ship = ed.createEntity()
  73. // val mbState = stateManager().state[EntityDataState]().map(_.getEntityData())
  74. // val mbState = Try(
  75. // stateManager()
  76. // .state[TestAppState]()
  77. // .entity
  78. // ).toOption.flatten.toRight(println("empty"))
  79. // // .flatMap(_.entity)
  80. // val x = mbState.flatMap(
  81. // _.query
  82. // .filter[TestComponent]("name", new Object())
  83. // // .filterOr[TestEntity](
  84. // // Filters
  85. // // .fieldEquals(classOf[TestEntity], "", null)
  86. // // )
  87. // .component[Tag]()
  88. // .component[TestComponent]()
  89. // .result
  90. // .toRight(println("failed"))
  91. // )
  92. // rootNode
  93. // .child(geom)
  94. // .child(geom)
  95. // .child(geom)
  96. // .child(geom)
  97. // .child(geom)
  98. // .child(geom)
  99. // .child(geom)
  100. // .child(geom)
  101. // Future(println("hello"))(jmeEC(this))
  102. // val wbActor: Future[ActorRef[Greeter.Greet]] = actorSystem.ask(
  103. // SpawnProtocol.Spawn(
  104. // behavior = Greeter(),
  105. // name = "listener",
  106. // DispatcherSelector.fromConfig("jme-dispatcher"),
  107. // _
  108. // )
  109. // )
  110. // wbActor.map(a => a.ask(Greeter.Greet("hello", _)).map(println))
  111. // Task(Promise[T]()).flatMap { p =>
  112. // Task(taskQueue2.transform(_ :+ MyTask(p, cb))) >>
  113. // Task.fromCancelablePromise(p)
  114. // }
  115. // Task.fromCancelablePromise {
  116. // val p = Promise[T]()
  117. // taskQueue2.transform(_ :+ MyTask(p, cb))
  118. // p
  119. // }