1
0
mirror of https://github.com/JohnBreaux/Boat-Battle.git synced 2024-11-15 05:25:57 +00:00

At least it doesn't crash now, as long as 2 peers are connected.

This commit is contained in:
John 2021-11-18 09:50:17 -06:00
parent 919576638c
commit bc1e31ac8c
2 changed files with 18 additions and 20 deletions

View File

@ -37,14 +37,10 @@ func _ready():
game_setup() game_setup()
# Function used to keep track of which players are ready # Function used to keep track of which players are ready
remote func player_ready(pid): mastersync func player_ready():
print (get_tree().is_network_server()) var who = get_tree().get_rpc_sender_id()
var who = pid if get_tree().is_network_server() and who in Network.peer_info and not who in players_ready:
# Here are some checks you can do, for example print ("ASSERT SUCCESS")
assert(get_tree().is_network_server())
assert(who in Network.peer_info) # Exists
assert(not who in players_ready) # Was not added yet
players_ready.append(who) players_ready.append(who)
if players_ready.size() == Network.peer_info.size(): if players_ready.size() == Network.peer_info.size():
@ -58,11 +54,13 @@ func game_setup():
# TODO: Create a fake peer who we can automate, for single-player mode # TODO: Create a fake peer who we can automate, for single-player mode
Network.start_server() Network.start_server()
network_id = Network.get_network_id() network_id = Network.get_network_id()
var count = 0
# Create players for every player in Network.peer_info # Create players for every player in Network.peer_info
for k in Network.peer_info.keys(): for k in Network.peer_info.keys():
# Create a new player # Create a new player
var player = Player.instance() var player = Player.instance()
# Set the player's opponent, for now # Set the player's opponent, for now
player.opponent_pid = Network.peer_info.keys()[1 - count]
# Give the player a recognizable name, like "1", instead of "@@97" # Give the player a recognizable name, like "1", instead of "@@97"
player.name = str(k) player.name = str(k)
# The player controls themselves # The player controls themselves
@ -71,28 +69,28 @@ func game_setup():
players[k] = player players[k] = player
# Add the player to the scene tree # Add the player to the scene tree
add_child(player) add_child(player)
count += 1
pass pass
# Connect to your own player_ready signal # Connect to your own player_ready signal
players[network_id].connect("player_ready", self, "_on_player_ready") players[network_id].connect("player_ready", self, "_on_player_ready")
# Have your player set up the board: # Have your player set up the board:
players[network_id].set_up_begin() players[network_id].set_up_begin()
func game_start(): mastersync func game_start():
# Make sure we're the server # Make sure we're the server
assert(get_tree().is_network_server()) assert(get_tree().is_network_server())
while not winner: while not winner:
for id in players.keys(): for id in players.keys():
# TODO: RPC always returns nothing.
# Figure out how to work around this.
var hit = players[id].rpc_id(id, "turn_start") var hit = players[id].rpc_id(id, "turn_start")
var result = players[hit["id"]].rpc_id(hit["id"], "hit", hit["target"]) var result = players[hit["id"]].rpc_id(hit["id"], "hit", hit["target"])
players[id].rpc_id(id, "mark", hit["target"], result) players[id].rpc_id(id, "mark", hit["target"], result)
pass pass
func _on_player_ready(pid): func _on_player_ready():
print ("_on_player_ready") print ("_on_player_ready")
match pid: rpc_id(1, "player_ready")
1: player_ready(pid)
_: rpc("player_ready", pid)
# victory_screen: display the victory screen # victory_screen: display the victory screen
func victory_screen(): func victory_screen():

View File

@ -24,7 +24,7 @@ func _ready():
pid = int(name) pid = int(name)
board = Board.instance() board = Board.instance()
remote func set_up_begin(): mastersync func set_up_begin():
var setup = Setup.instance() var setup = Setup.instance()
setup.connect("board_ready", self, "set_up") setup.connect("board_ready", self, "set_up")
add_child(setup) add_child(setup)
@ -32,13 +32,13 @@ remote func set_up_begin():
# Member functions: # Member functions:
# hit: Called when opponent fires on us. # hit: Called when opponent fires on us.
# Update internal state, and return hit/miss/sunk # Update internal state, and return hit/miss/sunk
remote func hit(pos): mastersync func hit(pos):
var res = board.hit(pos) var res = board.hit(pos)
return res return res
# mark: Called when the opponent returns hit/miss/sunk # mark: Called when the opponent returns hit/miss/sunk
# Update internal state, return ack/nak # Update internal state, return ack/nak
remote func mark(pos, value): mastersync func mark(pos, value):
# Mark the position on the top board # Mark the position on the top board
board.fire(pos, value) board.fire(pos, value)
@ -57,12 +57,12 @@ func set_up(ships):
place_ship(i[0], i[1], i[2], i[3]) place_ship(i[0], i[1], i[2], i[3])
# Add the board to the tree # Add the board to the tree
add_child(board) add_child(board)
emit_signal("player_ready", pid) emit_signal("player_ready")
# turn_start: start player's turn # turn_start: start player's turn
# Initiates the player's turn, and blocks until the player selects a location to fire upon # Initiates the player's turn, and blocks until the player selects a location to fire upon
# returns: fire = [player id, target coordinates] # returns: fire = [player id, target coordinates]
remote func turn_start(): mastersync func turn_start():
print("turn_start") print("turn_start")
var fire = Fire.instance() var fire = Fire.instance()