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.

104 lines
2.9 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.nodes
  2. import com.jme3.scene.Node
  3. import com.jme3.scene.CameraNode
  4. import com.jme3.scene.Geometry
  5. import com.jme3.renderer.Camera
  6. import com.jme3.asset.AssetManager
  7. import wow.doge.mygame.state.MyMaterial
  8. import com.jme3.math.Vector3f
  9. import com.jme3.scene.control.CameraControl.ControlDirection
  10. import com.jme3.syntax._
  11. import com.jme3.scene.shape.Box
  12. import com.jme3.bullet.control.BetterCharacterControl
  13. import com.jme3.bullet.BulletAppState
  14. import wow.doge.mygame.game.GameApp
  15. import wow.doge.mygame.implicits._
  16. import wow.doge.mygame.math.ImVector3f
  17. import com.jme3.math.FastMath
  18. // class PlayerNode(val name: String) extends Node(name) {}
  19. trait Player
  20. object PlayerController {
  21. def defaultMesh = {
  22. lazy val b = new Box(1, 1, 1)
  23. lazy val geom = new Geometry("playerMesh", b)
  24. geom
  25. }
  26. def defaultTexture(assetManager: AssetManager) =
  27. MyMaterial(
  28. assetManager = assetManager,
  29. path = os.rel / "Common" / "MatDefs" / "Misc" / "Unshaded.j3md"
  30. )
  31. def apply(
  32. app: GameApp,
  33. modelPath: os.RelPath,
  34. cam: Camera
  35. )(assetManager: AssetManager, bulletAppState: BulletAppState) = {
  36. lazy val playerPos = ImVector3f.ZERO
  37. lazy val playerPhysicsControl = new BetterCharacterControl(1.5f, 6f, 1f)
  38. .withJumpForce(ImVector3f(0, 5f, 0))
  39. lazy val playerNode = new Node("PlayerNode")
  40. .withChildren(
  41. new CameraNode("CameraNode", cam)
  42. .withControlDir(ControlDirection.SpatialToCamera)
  43. .withLocalTranslation(ImVector3f(0, 1.5f, 10))
  44. .withLookAt(playerPos, ImVector3f.UNIT_Y),
  45. assetManager
  46. .loadModel(modelPath)
  47. .asInstanceOf[Node]
  48. .withRotate(0, FastMath.PI, 0)
  49. )
  50. .withLocalTranslation(playerPos)
  51. .withControl(playerPhysicsControl)
  52. {
  53. bulletAppState.physicsSpace += playerNode
  54. bulletAppState.physicsSpace += playerPhysicsControl
  55. }
  56. {
  57. app.rootNode += playerNode
  58. }
  59. playerPhysicsControl
  60. }
  61. def apply(
  62. mesh: Geometry = defaultMesh,
  63. texturePath: os.RelPath =
  64. os.rel / "Common" / "MatDefs" / "Misc" / "Unshaded.j3md",
  65. cam: Camera
  66. )(assetManager: AssetManager): Node = {
  67. lazy val playerNode = new Node("PlayerNode")
  68. lazy val camNode = new CameraNode("CameraNode", cam)
  69. {
  70. val mat = MyMaterial(
  71. assetManager = assetManager,
  72. path = texturePath
  73. )
  74. mesh.setMaterial(mat)
  75. }
  76. // lazy val camNode = new CameraNode("CameraNode", simpleApp.getCamera())
  77. // camNode.setCamera(simpleApp.getCamera())
  78. discard {
  79. playerNode
  80. .withChild(camNode)
  81. .withChild(mesh)
  82. // playerNode.children(Seq(camNode, geom))
  83. }
  84. {
  85. camNode.setControlDir(ControlDirection.SpatialToCamera)
  86. camNode.setLocalTranslation(
  87. new Vector3f(0, 1.5f, 10)
  88. )
  89. camNode.lookAt(playerNode.getLocalTranslation(), Vector3f.UNIT_Y)
  90. }
  91. playerNode
  92. }
  93. }