Launcher.gd 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. extends Node
  2. # Common singletons
  3. var Root : Node = null
  4. var Scene : Node2D = null
  5. # Client services
  6. var Action : ServiceBase = null
  7. var Audio : AudioStreamPlayer = null
  8. var GUI : ServiceBase = null
  9. var Debug : ServiceBase = null
  10. var Camera : ServiceBase = null
  11. var Map : ServiceBase = null
  12. # Server services
  13. var World : ServiceBase = null
  14. var SQL : ServiceBase = null
  15. # Accessors
  16. var Player : Entity = null
  17. # Signals
  18. signal launchModeUpdated
  19. #
  20. func Mode(launchClient : bool = false, launchServer : bool = false) -> bool:
  21. var isClientConnected : bool = Network.Client != null
  22. var isServerConnected : bool = Network.ENetServer != null or Network.WebSocketServer != null
  23. if isClientConnected == launchClient and isServerConnected == launchServer:
  24. return false
  25. Launcher.Reset(false, false)
  26. Network.Destroy()
  27. if launchClient: Client()
  28. if launchServer: Server()
  29. Network.Mode(launchClient, launchServer)
  30. _post_launch()
  31. launchModeUpdated.emit(launchClient, launchServer)
  32. return true
  33. func Client():
  34. if OS.is_debug_build():
  35. Debug = FileSystem.LoadSource("debug/Debug.gd")
  36. # Load then low-prio services on which the order is not important
  37. Action = FileSystem.LoadSource("input/Action.gd", "Action")
  38. Camera = FileSystem.LoadSource("camera/Camera.gd")
  39. Map = FileSystem.LoadSource("map/Map.gd")
  40. add_child.call_deferred(Action)
  41. func Server():
  42. World = FileSystem.LoadSource("world/World.gd", "World")
  43. SQL = FileSystem.LoadSource("sql/SQL.gd", "SQL")
  44. add_child.call_deferred(World)
  45. add_child.call_deferred(SQL)
  46. func Reset(clientStarted : bool, serverStarted : bool):
  47. if not clientStarted:
  48. if Debug:
  49. Debug.Destroy()
  50. Debug.queue_free()
  51. Debug = null
  52. if Action:
  53. Action.set_name("ActionDestroyed")
  54. Action.Destroy()
  55. Action.queue_free()
  56. Action = null
  57. if Camera:
  58. Camera.Destroy()
  59. Camera.queue_free()
  60. Camera = null
  61. if Map:
  62. Map.Destroy()
  63. Map.queue_free()
  64. Map = null
  65. if Network.Client:
  66. Network.Client.Destroy()
  67. Network.Client = null
  68. if Player:
  69. Player.queue_free()
  70. Player = null
  71. if not serverStarted:
  72. if Network.ENetServer:
  73. Network.ENetServer.Destroy()
  74. Network.ENetServer = null
  75. if Network.WebSocketServer:
  76. Network.WebSocketServer.Destroy()
  77. Network.WebSocketServer = null
  78. if World:
  79. World.set_name("WorldDestroyed")
  80. World.Destroy()
  81. World.queue_free()
  82. World = null
  83. if SQL:
  84. SQL.set_name("SQLDestroyed")
  85. SQL.Destroy()
  86. SQL.queue_free()
  87. SQL = null
  88. func Quit():
  89. Reset(false, false)
  90. Network.Destroy()
  91. get_tree().quit()
  92. #
  93. func _ready():
  94. var startClient : bool = false
  95. var startServer : bool = false
  96. Root = get_tree().get_root()
  97. if "--server" in OS.get_cmdline_args():
  98. Scene = FileSystem.LoadResource(Path.Pst + "Server" + Path.SceneExt)
  99. Root.add_child.call_deferred(Scene)
  100. startServer = true
  101. else:
  102. Scene = FileSystem.LoadResource(Path.Pst + "Client" + Path.SceneExt)
  103. Root.add_child.call_deferred(Scene)
  104. GUI = Scene.get_node("Canvas")
  105. Audio = Scene.get_node("Audio")
  106. startClient = true
  107. startServer = not LauncherCommons.isWeb and OS.is_debug_build()
  108. Conf.Init()
  109. if not Root or not Scene:
  110. printerr("Could not initialize source's base services")
  111. Quit()
  112. DB.Init()
  113. Mode(startClient, startServer)
  114. await Scene.ready
  115. _post_launch()
  116. # Call _post_launch functions for service depending on other services
  117. func _post_launch():
  118. if Camera and not Camera.isInitialized: Camera._post_launch()
  119. if Map and not Map.isInitialized: Map._post_launch()
  120. if GUI and not GUI.isInitialized: GUI._post_launch()
  121. if Debug and not Debug.isInitialized: Debug._post_launch()
  122. if World and not World.isInitialized: World._post_launch()
  123. if SQL and not SQL.isInitialized: SQL._post_launch()
  124. if Audio: Audio._post_launch()
  125. func _quit():
  126. Quit()