forked from nova/jmonkey-test
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.
150 lines
4.2 KiB
150 lines
4.2 KiB
package wow.doge.mygame.game
|
|
|
|
import com.jme3.app.SimpleApplication
|
|
|
|
import com.jme3.app.state.AppState
|
|
import monix.execution.{CancelablePromise => Promise}
|
|
import monix.execution.CancelableFuture
|
|
import monix.bio.Task
|
|
import monix.execution.Scheduler
|
|
import wow.doge.mygame.executors.GUIExecutorService
|
|
import monix.reactive.subjects.ConcurrentSubject
|
|
import monix.reactive.MulticastStrategy
|
|
import monix.reactive.Observable
|
|
import monix.execution.atomic.Atomic
|
|
import scala.collection.immutable.Queue
|
|
|
|
class GameApp(
|
|
// actorSystem: ActorSystem[SpawnProtocol.Command],
|
|
appStates: AppState*
|
|
) extends SimpleApplication(appStates: _*) {
|
|
import GameApp._
|
|
// implicit val timeout = Timeout(10.seconds)
|
|
// implicit val scheduler = actorSystem.scheduler
|
|
|
|
// private lazy val taskQueueS = new ConcurrentLinkedQueue[MyTask[_]]()
|
|
private lazy val taskQueue2 = Atomic(Queue.empty[MyTask[_]])
|
|
|
|
private val tickSubject =
|
|
ConcurrentSubject[Float](multicast = MulticastStrategy.publish)(
|
|
monix.execution.Scheduler.Implicits.global
|
|
)
|
|
// (scheduler)
|
|
|
|
override def simpleInitApp(): Unit = {
|
|
|
|
println("gameapp" + Thread.currentThread().getName())
|
|
|
|
// val ship = ed.createEntity()
|
|
// val mbState = stateManager().state[EntityDataState]().map(_.getEntityData())
|
|
// val mbState = Try(
|
|
// stateManager()
|
|
// .state[TestAppState]()
|
|
// .entity
|
|
// ).toOption.flatten.toRight(println("empty"))
|
|
// // .flatMap(_.entity)
|
|
// val x = mbState.flatMap(
|
|
// _.query
|
|
// .filter[TestComponent]("name", new Object())
|
|
// // .filterOr[TestEntity](
|
|
// // Filters
|
|
// // .fieldEquals(classOf[TestEntity], "", null)
|
|
// // )
|
|
// .component[Tag]()
|
|
// .component[TestComponent]()
|
|
// .result
|
|
// .toRight(println("failed"))
|
|
// )
|
|
|
|
// rootNode
|
|
// .child(geom)
|
|
// .child(geom)
|
|
// .child(geom)
|
|
// .child(geom)
|
|
// .child(geom)
|
|
// .child(geom)
|
|
// .child(geom)
|
|
// .child(geom)
|
|
// Future(println("hello"))(jmeEC(this))
|
|
// val wbActor: Future[ActorRef[Greeter.Greet]] = actorSystem.ask(
|
|
// SpawnProtocol.Spawn(
|
|
// behavior = Greeter(),
|
|
// name = "listener",
|
|
// DispatcherSelector.fromConfig("jme-dispatcher"),
|
|
// _
|
|
// )
|
|
// )
|
|
|
|
// wbActor.map(a => a.ask(Greeter.Greet("hello", _)).map(println))
|
|
|
|
}
|
|
|
|
def tickObservable: Observable[Float] = tickSubject
|
|
|
|
override def simpleUpdate(tpf: Float): Unit = {
|
|
// val rot2 = rot.fromAngleAxis(FastMath.PI, new Vector3f(0, 0, 1))
|
|
// val rotation = geom.getLocalRotation()
|
|
// rotation.add(rot2)
|
|
// geom.rotate(rot2)
|
|
|
|
// geom.updateModelBound()
|
|
// geom.updateGeometricState()
|
|
tickSubject.onNext(tpf)
|
|
}
|
|
|
|
override def stop(): Unit = {
|
|
tickSubject.onComplete()
|
|
super.stop()
|
|
}
|
|
|
|
def enqueueScala[T](cb: () => T): CancelableFuture[T] = {
|
|
val p = Promise[T]()
|
|
// p.success(cb())
|
|
// taskQueueS.add(MyTask(p, cb))
|
|
taskQueue2.transform(_ :+ MyTask(p, cb))
|
|
p.future
|
|
}
|
|
// taskQueue2.transform(_ :+ MyTask(p, cb))
|
|
// p
|
|
def enqueueL[T](cb: () => T): Task[T] =
|
|
// Task(Promise[T]()).flatMap { p =>
|
|
// Task(taskQueue2.transform(_ :+ MyTask(p, cb))) >>
|
|
// Task.fromCancelablePromise(p)
|
|
// }
|
|
// Task.fromCancelablePromise {
|
|
// val p = Promise[T]()
|
|
// taskQueue2.transform(_ :+ MyTask(p, cb))
|
|
// p
|
|
// }
|
|
Task.deferFuture(enqueueScala(cb))
|
|
|
|
// taskQueueS.add(MyTask(p, cb))
|
|
|
|
override protected def runQueuedTasks(): Unit = {
|
|
// Option(taskQueueS.poll()).foreach {
|
|
// case MyTask(p, cb) =>
|
|
// p.success(cb())
|
|
// }
|
|
taskQueue2.transform { current =>
|
|
current.dequeueOption.fold(current) {
|
|
case (MyTask(p, cb), updated) =>
|
|
p.success(cb())
|
|
updated
|
|
}
|
|
}
|
|
|
|
super.runQueuedTasks()
|
|
}
|
|
|
|
object JMEExecutorService extends GUIExecutorService {
|
|
override def execute(command: Runnable) =
|
|
enqueue(command)
|
|
// new SingleThreadEventExecutor()
|
|
// sys.addShutdownHook(JMEExecutorService.shutdown())
|
|
}
|
|
|
|
lazy val scheduler = Scheduler(JMEExecutorService)
|
|
}
|
|
object GameApp {
|
|
private[game] case class MyTask[T](p: Promise[T], cb: () => T)
|
|
}
|