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.

206 lines
5.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package wow.doge.mygame.state
  2. import ammonite.runtime.Storage.Folder
  3. import ammonite.main.Defaults
  4. import ammonite.Main
  5. import javax.script.ScriptEngine
  6. import com.jme3.app.state.AppState
  7. import akka.actor.typed.scaladsl.AbstractBehavior
  8. import akka.actor.typed.scaladsl.ActorContext
  9. import akka.actor.typed.Behavior
  10. import akka.actor.typed.ActorRef
  11. import ammonite.util.Res.Success
  12. import akka.actor.typed.scaladsl.Behaviors
  13. import akka.actor.typed.SpawnProtocol
  14. class ScriptingEngineState(
  15. sse: ScalaScriptingEngine,
  16. kse: KotlinScriptingEngine
  17. ) extends MyBaseState {
  18. // implicit val actorSystem =
  19. // ActorSystem.create(MyActorSystem(), "rootActor")
  20. // implicit val timeout: Timeout = Timeout(3.seconds)
  21. // val scalaScriptActor: Future[ActorRef[ScalaScriptBehavior.Command]] =
  22. // actorSystem.ask(
  23. // SpawnProtocol.Spawn(
  24. // ScalaScriptBehavior(sse.runner),
  25. // name = "ScalaScriptCompilerActor",
  26. // Props.empty,
  27. // _
  28. // )
  29. // )
  30. override def stop(): Unit = {}
  31. // override protected def cleanup(app: Application): Unit = {
  32. // // actorSystem.terminate()
  33. // }
  34. // override protected def initialize(app: Application): Unit = {
  35. // super.initialize(app)
  36. // }
  37. override def init() = {
  38. // Future {
  39. // while (true) {
  40. // // super.update(tpf)
  41. // val (res, k) = sse.runner.runScript(
  42. // // os.Path(getClass().getResource("/hello.sc").getPath),
  43. // os.pwd / "src" / "main" / "resources" / "hello.sc",
  44. // Seq.empty
  45. // // Seq(("start", None))
  46. // // Scripts.groupArgs(List(""))
  47. // )
  48. // val ms = res.map(_.asInstanceOf[GameScript])
  49. // ms.map(_.start())
  50. // val res2 = kse.engine.eval(
  51. // os.read(os.pwd / "src" / "main" / "resources" / "hello.main.kts")
  52. // )
  53. // // val res2 = engine.eval(getClass().getResource("/hello.main.kts").getPath)
  54. // // val invoker = engine.asInstanceOf[Invocable]
  55. // // val scr = invoker.getInterface(res2, classOf[GameScript])
  56. // val scr = res2.asInstanceOf[GameScript]
  57. // scr.start()
  58. // Thread.sleep(2000)
  59. // }
  60. // }
  61. // Future {
  62. // sse.runner
  63. // .runScript(
  64. // os.pwd / "src" / "main" / "resources" / "hello2.sc",
  65. // Seq.empty
  66. // )
  67. // ._1
  68. // .map(_.asInstanceOf[MyBaseState])
  69. // .map(s => stateManager.attach(s))
  70. // ()
  71. // }
  72. // val res = scalaScriptActor
  73. // .map(
  74. // _.ask(ref =>
  75. // ScalaScriptBehavior.Compile(
  76. // ref,
  77. // os.pwd / "src" / "main" / "resources" / "hello2.sc"
  78. // // os.Path(getClass().getResource("/hello2.sc").getPath)
  79. // )
  80. // )(Timeout(10.seconds), actorSystem.scheduler)
  81. // )
  82. // .flatten
  83. // res.foreach(_ match {
  84. // case AppStateResult(state) => {
  85. // stateManager.attach(state)
  86. // }
  87. // case wow.doge.mygame.state.ScalaScriptBehavior.Error(reason) =>
  88. // println("error")
  89. // })
  90. }
  91. override def update(tpf: Float): Unit = {}
  92. override protected def onEnable(): Unit = {}
  93. override protected def onDisable(): Unit = {}
  94. }
  95. object MyActorSystem {
  96. def apply(): Behavior[SpawnProtocol.Command] =
  97. Behaviors.setup { context =>
  98. // Start initial tasks
  99. // context.spawn(...)
  100. SpawnProtocol()
  101. }
  102. }
  103. class ScalaScriptingEngine(
  104. val runner: Main = ammonite
  105. .Main(
  106. // predefCode = """
  107. // import coursierapi.MavenRepository
  108. // interp.repositories.update(
  109. // interp.repositories() ::: List(
  110. // MavenRepository.of("file://home/rohan/.m2/repository")
  111. // )
  112. // )
  113. // @
  114. // """,
  115. defaultPredef = false,
  116. storageBackend = new Folder(Defaults.ammoniteHome, isRepl = false)
  117. )
  118. ) {}
  119. class KotlinScriptingEngine(val engine: ScriptEngine) {
  120. // val manager = new ScriptEngineManager()
  121. // val engine = manager.getEngineByExtension("main.kts")
  122. }
  123. object ScalaScriptBehavior {
  124. sealed trait Result
  125. final case class AppStateResult(state: AppState) extends Result
  126. final case class Error(reason: String) extends Result
  127. sealed trait Command
  128. final case class Compile(sender: ActorRef[Result], path: os.Path)
  129. extends Command
  130. // final case class CompileScripts(sender: ActorRef[Result], paths: os.Path*)
  131. // extends Command
  132. def apply(
  133. runner: Main = ammonite
  134. .Main(
  135. storageBackend = new Folder(
  136. // os.pwd / "target"
  137. Defaults.ammoniteHome,
  138. isRepl = false
  139. )
  140. )
  141. ) =
  142. Behaviors.setup(ctx => new ScalaScriptActor(runner, ctx))
  143. private class ScalaScriptActor(
  144. val runner: Main,
  145. context: ActorContext[Command]
  146. ) extends AbstractBehavior[Command](context) {
  147. override def onMessage(msg: Command): Behavior[Command] = {
  148. msg match {
  149. case Compile(sender, path) =>
  150. context.log.debug(s"Received $path")
  151. val res = getScript(path)
  152. println(res)
  153. sender ! res
  154. Behaviors.same
  155. // case CompileScripts(sender, paths) =>
  156. }
  157. }
  158. def getScript(path: os.Path): Result = {
  159. runner
  160. .runScript(
  161. path,
  162. Seq.empty
  163. )
  164. ._1 match {
  165. case ammonite.util.Res.Exception(t, msg) => Error(msg)
  166. case Success(obj) =>
  167. obj match {
  168. case s: MyBaseState => AppStateResult(s)
  169. case _ => Error("Unknown script type")
  170. // AppStateResult(s.asInstanceOf[AppState])
  171. }
  172. case _ => Error("Failed to run script")
  173. }
  174. }
  175. }
  176. }