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.

161 lines
4.7 KiB

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