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.

77 lines
2.7 KiB

4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
  1. package wow.doge.mygame.game.subsystems.movement
  2. import com.jme3.bullet.control.BetterCharacterControl
  3. import com.jme3.math.FastMath
  4. import com.jme3.math.Quaternion
  5. import com.jme3.math.Vector3f
  6. import com.jme3.scene.Spatial
  7. import com.typesafe.scalalogging.LazyLogging
  8. import wow.doge.mygame.implicits._
  9. import wow.doge.mygame.math.ImVector3f
  10. import wow.doge.mygame.subsystems.movement.RotateDir
  11. trait CanMove[-A] {
  12. // def getDirection(cam: Camera, cardinalDir: CardinalDirection): ImVector3f
  13. def move(inst: A, direction: ImVector3f, speedFactor: Float): Unit
  14. def location(inst: A): ImVector3f
  15. def jump(inst: A): Unit
  16. def stop(inst: A): Unit
  17. def rotate(inst: A, rotateDir: RotateDir): Unit
  18. }
  19. object CanMove {
  20. implicit val implCanMoveForBetterCharacterControl =
  21. new CanMove[BetterCharacterControl] {
  22. override def move(
  23. inst: BetterCharacterControl,
  24. direction: ImVector3f,
  25. speedFactor: Float
  26. ): Unit = {
  27. val dir = direction.mutable.normalizeLocal()
  28. inst.setViewDirection(dir.negate())
  29. inst.setWalkDirection(dir.mult(speedFactor))
  30. }
  31. override def location(inst: BetterCharacterControl) =
  32. inst.getSpatial().getLocalTranslation().immutable
  33. override def jump(inst: BetterCharacterControl): Unit = inst.jump()
  34. override def rotate(
  35. inst: BetterCharacterControl,
  36. rotateDir: RotateDir
  37. ): Unit = {
  38. val q =
  39. rotateDir match {
  40. case RotateDir.Left =>
  41. new Quaternion()
  42. .fromAngleNormalAxis(5 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  43. case RotateDir.Right =>
  44. new Quaternion()
  45. .fromAngleAxis(-5 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  46. }
  47. val tmp = new Vector3f()
  48. inst.getViewDirection(tmp)
  49. inst.setViewDirection(q.mult(tmp))
  50. }
  51. override def stop(inst: BetterCharacterControl) =
  52. inst.setWalkDirection(Vector3f.ZERO)
  53. }
  54. implicit val implCanMoveForSpatial = new CanMove[Spatial] with LazyLogging {
  55. override def move(
  56. inst: Spatial,
  57. direction: ImVector3f,
  58. speedFactor: Float = 1f
  59. ): Unit =
  60. inst.move(direction.mutable multLocal speedFactor)
  61. override def location(inst: Spatial) =
  62. inst.getLocalTranslation().immutable
  63. override def jump(inst: Spatial): Unit =
  64. logger.warn("`Jump` is not implemented for type `Spatial`")
  65. override def rotate(inst: Spatial, rotateDir: RotateDir): Unit =
  66. rotateDir match {
  67. case RotateDir.Left => inst.rotate(0, -0.01f, 0)
  68. case RotateDir.Right => inst.rotate(0, 0.01f, 0)
  69. }
  70. override def stop(inst: Spatial) = { /*not required*/ }
  71. }
  72. }