Public repository for MUR pre alpha
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.

152 lines
4.4 KiB

  1. extends Node
  2. const MAX_PEERS = 4
  3. const LOBBY_READY_WAIT_TIME = 3
  4. var Game = preload("res://scenes/game/Game.tscn")
  5. var Player = preload("res://scenes/player/Player.tscn")
  6. var HumanPlayer = preload("res://scenes/player/HumanPlayer.tscn")
  7. var BotPlayer = preload("res://scenes/player/BotPlayer.tscn")
  8. var game:Game
  9. var players = {}
  10. var game_name = ""
  11. var bots:bool = false
  12. var game_running:bool = false
  13. signal players_changed(players)
  14. signal game_ready(ready)
  15. signal game_started()
  16. signal game_ended()
  17. remote func set_player(id, new_name:String = " ", new_color:Color = Color.black, new_ready:bool = false):
  18. players[id] = {"name" : new_name, "color" : new_color, "ready" : new_ready, "prepared" : false}
  19. emit_signal("players_changed", players)
  20. if get_tree().is_network_server():
  21. var all_ready = true
  22. for p_id in players:
  23. rpc_id(id, "set_player", p_id, players[p_id].name, players[p_id].color, players[p_id].ready)
  24. rpc_id(p_id, "set_player", id, new_name, new_color, new_ready)
  25. all_ready = all_ready && players[p_id].ready
  26. rpc("game_ready", all_ready)
  27. func update_player(new_name:String = " ", new_color:Color = Color.black, new_ready:bool = false):
  28. if get_tree().is_network_server():
  29. set_player(get_tree().get_network_unique_id(), new_name, new_color, new_ready)
  30. else:
  31. rpc("set_player",get_tree().get_network_unique_id(), new_name, new_color, new_ready)
  32. remotesync func remove_player(id):
  33. players.erase(id)
  34. if game != null:
  35. var game_players = game.get_node("players")
  36. if game_players.has_node(str(id)):
  37. game_players.remove_child(game_players.get_node(str(id)))
  38. emit_signal("players_changed", players)
  39. remotesync func game_ready(all_ready:bool):
  40. if get_tree().is_network_server():
  41. if all_ready:
  42. game_running = true
  43. else:
  44. game_running = false
  45. emit_signal("game_ready", all_ready)
  46. remotesync func prepare_game():
  47. get_tree().set_pause(true)
  48. game = Game.instance()
  49. get_tree().get_root().add_child(game)
  50. get_tree().get_root().get_node("main_menu").hide()
  51. for p_id in players:
  52. var controls
  53. var player = players[p_id]
  54. var player_scene = Player.instance()
  55. player_scene.set_network_master(p_id)
  56. player_scene.set_name(str(p_id))
  57. game.get_node("players").add_child(player_scene)
  58. player_scene.set_player_name(player.name)
  59. player_scene.set_player_color(player.color)
  60. if p_id == get_tree().get_network_unique_id():
  61. controls = HumanPlayer.instance()
  62. controls.set_name("HumanControl_" + str(p_id))
  63. game.add_child(controls)
  64. controls.set_player(player_scene.get_path())
  65. controls.set_network_master(p_id)
  66. player_scene.set_start(player_scene.get_index())
  67. if bots:
  68. for index in range(MAX_PEERS-players.size()):
  69. var bot_index = players.size() + index
  70. var player_scene = Player.instance()
  71. player_scene.set_name("bot_player" + str(bot_index))
  72. game.get_node("players").add_child(player_scene)
  73. player_scene.set_player_name("Bot " + str(bot_index))
  74. player_scene.set_player_color(Color(randf(),randf(),randf()))
  75. player_scene.add_to_group("bots")
  76. if get_tree().is_network_server():
  77. var bot = BotPlayer.instance()
  78. bot.set_name("bot" + str(bot_index))
  79. game.add_child(bot)
  80. bot.set_player(player_scene.get_path())
  81. bot.set_network_master(get_tree().get_network_unique_id())
  82. player_scene.set_start(bot_index)
  83. if players.has(get_tree().get_network_unique_id()):
  84. rpc_id(1, "player_prepared", get_tree().get_network_unique_id())
  85. if get_tree().is_network_server():
  86. if players.has(get_tree().get_network_unique_id()):
  87. player_prepared(get_tree().get_network_unique_id())
  88. else:
  89. rpc_id(1, "player_prepared", get_tree().get_network_unique_id())
  90. remotesync func player_prepared(id):
  91. if get_tree().is_network_server():
  92. players[id].prepared = true
  93. var all_prepared = true
  94. for p_id in players:
  95. all_prepared = all_prepared && players[p_id].prepared
  96. if all_prepared:
  97. rpc("post_start_game")
  98. remotesync func post_start_game():
  99. get_tree().set_pause(false)
  100. emit_signal("game_started")
  101. remotesync func set_bots(has_bots:bool):
  102. bots = has_bots
  103. func quit_game():
  104. if game != null:
  105. game.queue_free()
  106. game = null
  107. players.clear()
  108. if get_tree().get_root().has_node("multiplayer_menu"):
  109. get_tree().get_root().get_node("multiplayer_menu").show()
  110. get_tree().get_root().get_node("main_menu").hide()
  111. elif get_tree().get_root().has_node("main_menu"):
  112. get_tree().get_root().get_node("main_menu").show()
  113. game_running = false
  114. bots = false
  115. game_name = ""
  116. emit_signal("game_ended")