Launcher.gd 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 GUI:
  66. GUI.Destroy()
  67. # GUI is not cleared, but all signals should be re-connected
  68. if Network.Client:
  69. Network.Client.Destroy()
  70. Network.Client = null
  71. if Player:
  72. Player.queue_free()
  73. Player = null
  74. if not serverStarted:
  75. if Network.ENetServer:
  76. Network.ENetServer.Destroy()
  77. Network.ENetServer = null
  78. if Network.WebSocketServer:
  79. Network.WebSocketServer.Destroy()
  80. Network.WebSocketServer = null
  81. if World:
  82. World.set_name("WorldDestroyed")
  83. World.Destroy()
  84. World.queue_free()
  85. World = null
  86. if SQL:
  87. SQL.set_name("SQLDestroyed")
  88. SQL.Destroy()
  89. SQL.queue_free()
  90. SQL = null
  91. func Quit():
  92. Reset(false, false)
  93. Network.Destroy()
  94. Root.remove_child(Scene)
  95. Scene.free()
  96. Network.free()
  97. get_tree().quit()
  98. #
  99. func _ready():
  100. var startClient : bool = false
  101. var startServer : bool = false
  102. Root = get_tree().get_root()
  103. if "--server" in OS.get_cmdline_args():
  104. Scene = FileSystem.LoadResource(Path.Pst + "Server" + Path.SceneExt)
  105. Root.add_child.call_deferred(Scene)
  106. Engine.set_max_fps(LauncherCommons.ServerMaxFPS)
  107. Engine.set_physics_ticks_per_second(LauncherCommons.ServerMaxFPS)
  108. startServer = true
  109. else:
  110. Scene = FileSystem.LoadResource(Path.Pst + "Client" + Path.SceneExt)
  111. Root.add_child.call_deferred(Scene)
  112. GUI = Scene.get_node("Canvas")
  113. Audio = Scene.get_node("Audio")
  114. startClient = true
  115. startServer = not LauncherCommons.isWeb and OS.is_debug_build()
  116. Conf.Init()
  117. if not Root or not Scene:
  118. printerr("Could not initialize source's base services")
  119. Quit()
  120. DB.Init()
  121. Mode(startClient, startServer)
  122. await Scene.ready
  123. _post_launch()
  124. # Call _post_launch functions for service depending on other services
  125. func _post_launch():
  126. if Camera and not Camera.isInitialized: Camera._post_launch()
  127. if Map and not Map.isInitialized: Map._post_launch()
  128. if GUI and not GUI.isInitialized: GUI._post_launch()
  129. if Debug and not Debug.isInitialized: Debug._post_launch()
  130. if World and not World.isInitialized: World._post_launch()
  131. if SQL and not SQL.isInitialized: SQL._post_launch()
  132. if Audio: Audio._post_launch()
  133. func _quit():
  134. Quit()