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.

70 lines
2.4 KiB

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