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.

142 lines
4.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package wow.doge.mygame.game
  2. import com.jme3.app.SimpleApplication
  3. import com.jme3.app.state.AppState
  4. import com.jme3.bullet.BulletAppState
  5. import com.jme3.bullet.collision.shapes.CapsuleCollisionShape
  6. import com.jme3.bullet.control.CharacterControl
  7. import com.jme3.bullet.control.RigidBodyControl
  8. import com.jme3.bullet.util.CollisionShapeFactory
  9. import com.jme3.scene.Spatial
  10. import com.jme3.syntax._
  11. import com.jme3.asset.plugins.ZipLocator
  12. import com.jme3.math.ColorRGBA
  13. import wow.doge.mygame.implicits._
  14. class GameApp(
  15. // actorSystem: ActorSystem[SpawnProtocol.Command],
  16. appStates: AppState*
  17. ) extends SimpleApplication(appStates: _*) {
  18. // implicit val timeout = Timeout(10.seconds)
  19. // implicit val scheduler = actorSystem.scheduler
  20. private lazy val sceneModel: Spatial = assetManager.loadModel("main.scene")
  21. private lazy val bulletAppState: BulletAppState = new BulletAppState()
  22. // bulletAppState.setThreadingType(ThreadingType.SEQUENTIAL)
  23. // We set up collision detection for the scene by creating a
  24. // compound collision shape and a static RigidBodyControl with mass zero.
  25. private lazy val sceneShape = CollisionShapeFactory.createMeshShape(
  26. sceneModel.toNode match {
  27. case util.Right(node) => node
  28. case util.Left(ex) =>
  29. throw new NotImplementedError("No fallback sceneshape")
  30. }
  31. )
  32. private lazy val landscape: RigidBodyControl =
  33. new RigidBodyControl(sceneShape, 0)
  34. // We set up collision detection for the player by creating
  35. // a capsule collision shape and a CharacterControl.
  36. // The CharacterControl offers extra settings for
  37. // size, stepheight, jumping, falling, and gravity.
  38. // We also put the player in its starting position.
  39. protected lazy val capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1)
  40. private lazy val player: CharacterControl =
  41. new CharacterControl(capsuleShape, 0.05f)
  42. override def simpleInitApp(): Unit = {
  43. discard { stateManager.attach(bulletAppState) }
  44. assetManager.registerLocator(
  45. // "src/main/resources/assets/town.zip",
  46. (os.rel / "src" / "main" / "resources" / "assets" / "town.zip"),
  47. classOf[ZipLocator]
  48. )
  49. viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f))
  50. sceneModel.setLocalScale(2f)
  51. sceneModel.addControl(landscape)
  52. discard { rootNode.attachChild(sceneModel) }
  53. bulletAppState.getPhysicsSpace.add(landscape)
  54. bulletAppState.getPhysicsSpace.add(player)
  55. println("gameapp" + Thread.currentThread().getName())
  56. // val ship = ed.createEntity()
  57. // val mbState = stateManager().state[EntityDataState]().map(_.getEntityData())
  58. // val mbState = Try(
  59. // stateManager()
  60. // .state[TestAppState]()
  61. // .entity
  62. // ).toOption.flatten.toRight(println("empty"))
  63. // // .flatMap(_.entity)
  64. // val x = mbState.flatMap(
  65. // _.query
  66. // .filter[TestComponent]("name", new Object())
  67. // // .filterOr[TestEntity](
  68. // // Filters
  69. // // .fieldEquals(classOf[TestEntity], "", null)
  70. // // )
  71. // .component[Tag]()
  72. // .component[TestComponent]()
  73. // .result
  74. // .toRight(println("failed"))
  75. // )
  76. // rootNode
  77. // .child(geom)
  78. // .child(geom)
  79. // .child(geom)
  80. // .child(geom)
  81. // .child(geom)
  82. // .child(geom)
  83. // .child(geom)
  84. // .child(geom)
  85. // Future(println("hello"))(jmeEC(this))
  86. // val wbActor: Future[ActorRef[Greeter.Greet]] = actorSystem.ask(
  87. // SpawnProtocol.Spawn(
  88. // behavior = Greeter(),
  89. // name = "listener",
  90. // DispatcherSelector.fromConfig("jme-dispatcher"),
  91. // _
  92. // )
  93. // )
  94. // wbActor.map(a => a.ask(Greeter.Greet("hello", _)).map(println))
  95. }
  96. override def simpleUpdate(tpf: Float): Unit = {
  97. // val rot2 = rot.fromAngleAxis(FastMath.PI, new Vector3f(0, 0, 1))
  98. // val rotation = geom.getLocalRotation()
  99. // rotation.add(rot2)
  100. // geom.rotate(rot2)
  101. // geom.updateModelBound()
  102. // geom.updateGeometricState()
  103. }
  104. // override def stop(): Unit = {
  105. // actorSystem.terminate()
  106. // super.stop()
  107. // }
  108. // override def start(): Unit = {
  109. // monix.eval.Task(super.start()).runToFuture(Scheduler(JMEExecutorService))
  110. // }
  111. // def start(system: ActorRef[RootActor.Command]) = {
  112. // // system ! RootActor.Start
  113. // super.start()
  114. // }
  115. // override def stop(): Unit = {
  116. // println("stopping")
  117. // }
  118. def stop[T](cb: () => T): Unit = {
  119. println("destroy")
  120. cb()
  121. super.stop()
  122. }
  123. // override def stop(): Unit = {}
  124. }