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.

65 lines
1.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package wow.doge.mygame.subsystems.scriptsystem
  2. import akka.actor.typed.ActorRef
  3. import akka.actor.typed.Scheduler
  4. import akka.actor.typed.SpawnProtocol
  5. import akka.util.Timeout
  6. import cats.effect.Resource
  7. import wow.doge.mygame.scriptsystem.ScriptCachingActor
  8. import wow.doge.mygame.utils.AkkaUtils
  9. import monix.bio.Task
  10. /**
  11. * Scripts can either be searched and compiled at startup (Eager mode)
  12. * or compiled on demand (Lazy mode)
  13. */
  14. sealed trait ScriptInitMode
  15. object ScriptInitMode {
  16. case object Eager extends ScriptInitMode
  17. case object Lazy extends ScriptInitMode
  18. }
  19. class ScriptSystemResource(
  20. path: os.Path,
  21. spawnProtocol: ActorRef[SpawnProtocol.Command],
  22. mode: ScriptInitMode = ScriptInitMode.Lazy
  23. )(implicit timeout: Timeout, scheduler: Scheduler) {
  24. val make = {
  25. // throw new Exception("boom")
  26. findScriptFiles(os.pwd / "assets" / "scripts")
  27. lazy val scriptCacheActor = AkkaUtils.spawnActorL(
  28. spawnProtocol,
  29. "scriptCachingActor",
  30. ScriptCachingActor()
  31. )
  32. Resource.liftF(scriptCacheActor)
  33. }
  34. val init = for {
  35. scriptFiles <- Task(findScriptFiles(os.pwd / "assets" / "scripts"))
  36. scriptCacheActor <- AkkaUtils.spawnActorL(
  37. spawnProtocol,
  38. "scriptCachingActor",
  39. ScriptCachingActor()
  40. )
  41. } yield (scriptCacheActor)
  42. def findScriptFiles(wd: os.Path) =
  43. os.walk
  44. .stream(wd)
  45. .filter(p =>
  46. os.isFile(p) &&
  47. (p.ext == "sc" || (p.baseName + "." + p.ext)
  48. .contains(".main.kts") || p.ext == "groovy")
  49. )
  50. .toList
  51. // def findExternalScriptFiles =
  52. // findScriptFiles(os.pwd / "assets" / "scripts")
  53. // def findInternalScriptFiles =
  54. // findScriptFiles((os.resource / "assets" / "scripts"))
  55. // def finalInternalScriptFiles =
  56. }
  57. // pwd / 'src / 'main / 'resources / 'assets / 'scripts