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.

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