gamestate.gd 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. extends Node
  2. # Default game port
  3. const DEFAULT_PORT = 10567
  4. # Max number of players
  5. const MAX_PEERS = 12
  6. # Name for my player
  7. var player_name = "The Warrior"
  8. # Names for remote players in id:name format
  9. var players = {}
  10. # Signals to let lobby GUI know what's going on
  11. signal player_list_changed()
  12. signal connection_failed()
  13. signal connection_succeeded()
  14. signal game_ended()
  15. signal game_error(what)
  16. # Callback from SceneTree
  17. func _player_connected(id):
  18. # This is not used in this demo, because _connected_ok is called for clients
  19. # on success and will do the job.
  20. pass
  21. # Callback from SceneTree
  22. func _player_disconnected(id):
  23. if get_tree().is_network_server():
  24. if has_node("/root/world"): # Game is in progress
  25. emit_signal("game_error", "Player " + players[id] + " disconnected")
  26. end_game()
  27. else: # Game is not in progress
  28. # If we are the server, send to the new dude all the already registered players
  29. unregister_player(id)
  30. for p_id in players:
  31. # Erase in the server
  32. rpc_id(p_id, "unregister_player", id)
  33. # Callback from SceneTree, only for clients (not server)
  34. func _connected_ok():
  35. # Registration of a client beings here, tell everyone that we are here
  36. rpc("register_player", get_tree().get_network_unique_id(), player_name)
  37. emit_signal("connection_succeeded")
  38. # Callback from SceneTree, only for clients (not server)
  39. func _server_disconnected():
  40. emit_signal("game_error", "Server disconnected")
  41. end_game()
  42. # Callback from SceneTree, only for clients (not server)
  43. func _connected_fail():
  44. get_tree().set_network_peer(null) # Remove peer
  45. emit_signal("connection_failed")
  46. # Lobby management functions
  47. remote func register_player(id, new_player_name):
  48. if get_tree().is_network_server():
  49. # If we are the server, let everyone know about the new player
  50. rpc_id(id, "register_player", 1, player_name) # Send myself to new dude
  51. for p_id in players: # Then, for each remote player
  52. rpc_id(id, "register_player", p_id, players[p_id]) # Send player to new dude
  53. rpc_id(p_id, "register_player", id, new_player_name) # Send new dude to player
  54. players[id] = new_player_name
  55. emit_signal("player_list_changed")
  56. remote func unregister_player(id):
  57. players.erase(id)
  58. emit_signal("player_list_changed")
  59. remote func pre_start_game(spawn_points):
  60. # Change scene
  61. var world = load("res://world.tscn").instance()
  62. get_tree().get_root().add_child(world)
  63. get_tree().get_root().get_node("lobby").hide()
  64. var player_scene = load("res://player.tscn")
  65. for p_id in spawn_points:
  66. var spawn_pos = world.get_node("spawn_points/" + str(spawn_points[p_id])).position
  67. var player = player_scene.instance()
  68. player.set_name(str(p_id)) # Use unique ID as node name
  69. player.position=spawn_pos
  70. player.set_network_master(p_id) #set unique id as master
  71. if p_id == get_tree().get_network_unique_id():
  72. # If node for this peer id, set name
  73. player.set_player_name(player_name)
  74. else:
  75. # Otherwise set name from peer
  76. player.set_player_name(players[p_id])
  77. world.get_node("players").add_child(player)
  78. # Set up score
  79. world.get_node("score").add_player(get_tree().get_network_unique_id(), player_name)
  80. for pn in players:
  81. world.get_node("score").add_player(pn, players[pn])
  82. if not get_tree().is_network_server():
  83. # Tell server we are ready to start
  84. rpc_id(1, "ready_to_start", get_tree().get_network_unique_id())
  85. elif players.size() == 0:
  86. post_start_game()
  87. remote func post_start_game():
  88. get_tree().set_pause(false) # Unpause and unleash the game!
  89. var players_ready = []
  90. remote func ready_to_start(id):
  91. assert(get_tree().is_network_server())
  92. if not id in players_ready:
  93. players_ready.append(id)
  94. if players_ready.size() == players.size():
  95. for p in players:
  96. rpc_id(p, "post_start_game")
  97. post_start_game()
  98. func host_game(new_player_name):
  99. player_name = new_player_name
  100. var host = NetworkedMultiplayerENet.new()
  101. host.create_server(DEFAULT_PORT, MAX_PEERS)
  102. get_tree().set_network_peer(host)
  103. func join_game(ip, new_player_name):
  104. player_name = new_player_name
  105. var host = NetworkedMultiplayerENet.new()
  106. host.create_client(ip, DEFAULT_PORT)
  107. get_tree().set_network_peer(host)
  108. func get_player_list():
  109. return players.values()
  110. func get_player_name():
  111. return player_name
  112. func begin_game():
  113. assert(get_tree().is_network_server())
  114. # Create a dictionary with peer id and respective spawn points, could be improved by randomizing
  115. var spawn_points = {}
  116. spawn_points[1] = 0 # Server in spawn point 0
  117. var spawn_point_idx = 1
  118. for p in players:
  119. spawn_points[p] = spawn_point_idx
  120. spawn_point_idx += 1
  121. # Call to pre-start game with the spawn points
  122. for p in players:
  123. rpc_id(p, "pre_start_game", spawn_points)
  124. pre_start_game(spawn_points)
  125. func end_game():
  126. if has_node("/root/world"): # Game is in progress
  127. # End it
  128. get_node("/root/world").queue_free()
  129. emit_signal("game_ended")
  130. players.clear()
  131. get_tree().set_network_peer(null) # End networking
  132. func _ready():
  133. get_tree().connect("network_peer_connected", self, "_player_connected")
  134. get_tree().connect("network_peer_disconnected", self,"_player_disconnected")
  135. get_tree().connect("connected_to_server", self, "_connected_ok")
  136. get_tree().connect("connection_failed", self, "_connected_fail")
  137. get_tree().connect("server_disconnected", self, "_server_disconnected")