1
0
mirror of https://github.com/JohnBreaux/Boat-Battle.git synced 2024-11-14 21:15:58 +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,15 +37,11 @@ func _ready():
game_setup()
# Function used to keep track of which players are ready
remote func player_ready(pid):
print (get_tree().is_network_server())
var who = pid
# Here are some checks you can do, for example
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)
mastersync func player_ready():
var who = get_tree().get_rpc_sender_id()
if get_tree().is_network_server() and who in Network.peer_info and not who in players_ready:
print ("ASSERT SUCCESS")
players_ready.append(who)
if players_ready.size() == Network.peer_info.size():
rpc("game_start")
@ -58,11 +54,13 @@ func game_setup():
# TODO: Create a fake peer who we can automate, for single-player mode
Network.start_server()
network_id = Network.get_network_id()
var count = 0
# Create players for every player in Network.peer_info
for k in Network.peer_info.keys():
# Create a new player
var player = Player.instance()
# 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"
player.name = str(k)
# The player controls themselves
@ -71,28 +69,28 @@ func game_setup():
players[k] = player
# Add the player to the scene tree
add_child(player)
count += 1
pass
# Connect to your own player_ready signal
players[network_id].connect("player_ready", self, "_on_player_ready")
# Have your player set up the board:
players[network_id].set_up_begin()
func game_start():
mastersync func game_start():
# Make sure we're the server
assert(get_tree().is_network_server())
while not winner:
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 result = players[hit["id"]].rpc_id(hit["id"], "hit", hit["target"])
players[id].rpc_id(id, "mark", hit["target"], result)
pass
func _on_player_ready(pid):
func _on_player_ready():
print ("_on_player_ready")
match pid:
1: player_ready(pid)
_: rpc("player_ready", pid)
rpc_id(1, "player_ready")
# victory_screen: display the victory screen
func victory_screen():

View File

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