gamestate.gd 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. extends Node
  2. # Default game server port. Can be any number between 1024 and 49151.
  3. # Not on the list of registered or common ports as of November 2020:
  4. # https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
  5. const DEFAULT_PORT = 10567
  6. # Max number of players.
  7. const MAX_PEERS = 12
  8. var peer = null
  9. # Name for my player.
  10. var player_name = "The Warrior"
  11. # Names for remote players in id:name format.
  12. var players = {}
  13. var players_ready = []
  14. # Signals to let lobby GUI know what's going on.
  15. signal player_list_changed()
  16. signal connection_failed()
  17. signal connection_succeeded()
  18. signal game_ended()
  19. signal game_error(what)
  20. # Callback from SceneTree.
  21. func _player_connected(id):
  22. # Registration of a client beings here, tell the connected player that we are here.
  23. register_player.rpc_id(id, player_name)
  24. # Callback from SceneTree.
  25. func _player_disconnected(id):
  26. if has_node("/root/World"): # Game is in progress.
  27. if multiplayer.is_server():
  28. game_error.emit("Player " + players[id] + " disconnected")
  29. end_game()
  30. else: # Game is not in progress.
  31. # Unregister this player.
  32. unregister_player(id)
  33. # Callback from SceneTree, only for clients (not server).
  34. func _connected_ok():
  35. # We just connected to a server
  36. connection_succeeded.emit()
  37. # Callback from SceneTree, only for clients (not server).
  38. func _server_disconnected():
  39. game_error.emit("Server disconnected")
  40. end_game()
  41. # Callback from SceneTree, only for clients (not server).
  42. func _connected_fail():
  43. multiplayer.set_network_peer(null) # Remove peer
  44. connection_failed.emit()
  45. # Lobby management functions.
  46. @rpc("any_peer")
  47. func register_player(new_player_name):
  48. var id = multiplayer.get_remote_sender_id()
  49. players[id] = new_player_name
  50. player_list_changed.emit()
  51. func unregister_player(id):
  52. players.erase(id)
  53. player_list_changed.emit()
  54. @rpc("call_local")
  55. func load_world():
  56. # Change scene.
  57. var world = load("res://world.tscn").instantiate()
  58. get_tree().get_root().add_child(world)
  59. get_tree().get_root().get_node("Lobby").hide()
  60. # Set up score.
  61. world.get_node("Score").add_player(multiplayer.get_unique_id(), player_name)
  62. for pn in players:
  63. world.get_node("Score").add_player(pn, players[pn])
  64. get_tree().set_pause(false) # Unpause and unleash the game!
  65. func host_game(new_player_name):
  66. player_name = new_player_name
  67. peer = ENetMultiplayerPeer.new()
  68. peer.create_server(DEFAULT_PORT, MAX_PEERS)
  69. multiplayer.set_multiplayer_peer(peer)
  70. func join_game(ip, new_player_name):
  71. player_name = new_player_name
  72. peer = ENetMultiplayerPeer.new()
  73. peer.create_client(ip, DEFAULT_PORT)
  74. multiplayer.set_multiplayer_peer(peer)
  75. func get_player_list():
  76. return players.values()
  77. func get_player_name():
  78. return player_name
  79. func begin_game():
  80. assert(multiplayer.is_server())
  81. load_world.rpc()
  82. var world = get_tree().get_root().get_node("World")
  83. var player_scene = load("res://player.tscn")
  84. # Create a dictionary with peer id and respective spawn points, could be improved by randomizing.
  85. var spawn_points = {}
  86. spawn_points[1] = 0 # Server in spawn point 0.
  87. var spawn_point_idx = 1
  88. for p in players:
  89. spawn_points[p] = spawn_point_idx
  90. spawn_point_idx += 1
  91. for p_id in spawn_points:
  92. var spawn_pos = world.get_node("SpawnPoints/" + str(spawn_points[p_id])).position
  93. var player = player_scene.instantiate()
  94. player.synced_position = spawn_pos
  95. player.name = str(p_id)
  96. player.set_player_name(player_name if p_id == multiplayer.get_unique_id() else players[p_id])
  97. world.get_node("Players").add_child(player)
  98. func end_game():
  99. if has_node("/root/World"): # Game is in progress.
  100. # End it
  101. get_node("/root/World").queue_free()
  102. game_ended.emit()
  103. players.clear()
  104. func _ready():
  105. multiplayer.peer_connected.connect(self._player_connected)
  106. multiplayer.peer_disconnected.connect(self._player_disconnected)
  107. multiplayer.connected_to_server.connect(self._connected_ok)
  108. multiplayer.connection_failed.connect(self._connected_fail)
  109. multiplayer.server_disconnected.connect(self._server_disconnected)