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.

88 lines
2.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package wow.doge.mygame.game.subsystems.movement
  2. import cats.Id
  3. import com.jme3.bullet.control.BetterCharacterControl
  4. import com.jme3.math.FastMath
  5. import com.jme3.math.Quaternion
  6. import com.jme3.math.Vector3f
  7. import monix.eval.Coeval
  8. import wow.doge.mygame.implicits._
  9. import wow.doge.mygame.math.ImVector3f
  10. import wow.doge.mygame.subsystems.movement.RotateDir
  11. // experiment to see if it would be useful to use an effect wrapper for a typeclass like this
  12. trait CanMove2[-A, F[_]] {
  13. // def getDirection(cam: Camera, cardinalDir: CardinalDirection): ImVector3f
  14. def move(inst: A, direction: ImVector3f, speedFactor: Float = 20f): F[Unit]
  15. def location(inst: A): F[ImVector3f]
  16. def jump(inst: A): F[Unit]
  17. def stop(inst: A): F[Unit]
  18. def rotate(inst: A, rotateDir: RotateDir): F[Unit]
  19. }
  20. object Test {
  21. val x = new BetterCharacterControl(4, 10, 5)
  22. def test[T](x: T)(implicit cm: CanMove2[T, Id]) =
  23. cm.move(x, ImVector3f.Zero)
  24. }
  25. object CanMove2 {
  26. implicit val testImpl = new CanMove2[BetterCharacterControl, Id] {
  27. override def move(
  28. inst: BetterCharacterControl,
  29. direction: ImVector3f,
  30. speedFactor: Float
  31. ): Id[Unit] = {}
  32. override def location(inst: BetterCharacterControl): Id[ImVector3f] =
  33. ImVector3f.Zero
  34. override def jump(inst: BetterCharacterControl): Id[Unit] = ???
  35. override def stop(inst: BetterCharacterControl): Id[Unit] = ???
  36. override def rotate(
  37. inst: BetterCharacterControl,
  38. rotateDir: RotateDir
  39. ): Id[Unit] = ???
  40. }
  41. implicit val implCanMoveForBetterCharacterControl =
  42. new CanMove2[BetterCharacterControl, Coeval] {
  43. override def move(
  44. inst: BetterCharacterControl,
  45. direction: ImVector3f,
  46. speedFactor: Float = 20f
  47. ): Coeval[Unit] =
  48. Coeval {
  49. val dir = direction.mutable.normalizeLocal()
  50. inst.setViewDirection(dir.negate())
  51. inst.setWalkDirection(dir.mult(speedFactor))
  52. }
  53. override def location(inst: BetterCharacterControl) =
  54. Coeval(inst.getSpatial().getLocalTranslation().immutable)
  55. override def jump(inst: BetterCharacterControl): Coeval[Unit] =
  56. Coeval(inst.jump())
  57. override def rotate(
  58. inst: BetterCharacterControl,
  59. rotateDir: RotateDir
  60. ): Coeval[Unit] =
  61. Coeval {
  62. val q =
  63. rotateDir match {
  64. case RotateDir.Left =>
  65. new Quaternion()
  66. .fromAngleNormalAxis(5 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  67. case RotateDir.Right =>
  68. new Quaternion()
  69. .fromAngleAxis(-5 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  70. }
  71. val tmp = new Vector3f()
  72. inst.getViewDirection(tmp)
  73. inst.setViewDirection(q.mult(tmp))
  74. }
  75. override def stop(inst: BetterCharacterControl) =
  76. Coeval(inst.setWalkDirection(Vector3f.ZERO))
  77. }
  78. }