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.

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