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.

61 lines
2.2 KiB

  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 monix.eval.Coeval
  7. import wow.doge.mygame.implicits._
  8. import wow.doge.mygame.math.ImVector3f
  9. import wow.doge.mygame.subsystems.movement.RotateDir
  10. // experiment to see if it would be useful to use an effect wrapper for a typeclass like this
  11. trait CanMove2[-A, F[_]] {
  12. // def getDirection(cam: Camera, cardinalDir: CardinalDirection): ImVector3f
  13. def move(inst: A, direction: ImVector3f, speedFactor: Float = 20f): F[Unit]
  14. def location(inst: A): F[ImVector3f]
  15. def jump(inst: A): F[Unit]
  16. def stop(inst: A): F[Unit]
  17. def rotate(inst: A, rotateDir: RotateDir): F[Unit]
  18. }
  19. object CanMove2 {
  20. implicit val implCanMoveForBetterCharacterControl =
  21. new CanMove2[BetterCharacterControl, Coeval] {
  22. override def move(
  23. inst: BetterCharacterControl,
  24. direction: ImVector3f,
  25. speedFactor: Float = 20f
  26. ): Coeval[Unit] =
  27. Coeval {
  28. val dir = direction.mutable.normalizeLocal()
  29. inst.setViewDirection(dir.negate())
  30. inst.setWalkDirection(dir.mult(speedFactor))
  31. }
  32. override def location(inst: BetterCharacterControl) =
  33. Coeval(inst.getSpatial().getLocalTranslation().immutable)
  34. override def jump(inst: BetterCharacterControl): Coeval[Unit] =
  35. Coeval(inst.jump())
  36. override def rotate(
  37. inst: BetterCharacterControl,
  38. rotateDir: RotateDir
  39. ): Coeval[Unit] =
  40. Coeval {
  41. val q =
  42. rotateDir match {
  43. case RotateDir.Left =>
  44. new Quaternion()
  45. .fromAngleNormalAxis(5 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  46. case RotateDir.Right =>
  47. new Quaternion()
  48. .fromAngleAxis(-5 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
  49. }
  50. val tmp = new Vector3f()
  51. inst.getViewDirection(tmp)
  52. inst.setViewDirection(q.mult(tmp))
  53. }
  54. override def stop(inst: BetterCharacterControl) =
  55. Coeval(inst.setWalkDirection(Vector3f.ZERO))
  56. }
  57. }