1
0
mirror of https://github.com/JohnBreaux/Boat-Battle.git synced 2025-02-04 12:28:35 +00:00

Multiplayer: Implement message passing system

This commit is contained in:
2021-11-22 22:04:21 -06:00
parent bc1e31ac8c
commit 5ed44e6deb
4 changed files with 82 additions and 58 deletions

View File

@@ -5,13 +5,13 @@ var dark_theme = load("res://dark_theme.tres")
# Path to Player class, for instantiating new Players in code
onready var Player = preload("res://scenes/Game/Player.tscn")
var Player = preload("res://scenes/Game/Player.tscn")
onready var Victory = preload("res://scenes/Game/Victory.tscn")
var Victory = preload("res://scenes/Game/Victory.tscn")
# Array of instances of the Player class; stores the Players
var players = {} # = player1, player2, ...
var players = [] # = player1, player2, ...
var players_ready = []
# turn counter
var turn = 0
@@ -37,56 +37,40 @@ func _ready():
game_setup()
# Function used to keep track of which players are ready
mastersync func player_ready():
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:
if get_tree().is_network_server() and who in Net.peer_info and not who in players_ready:
print ("ASSERT SUCCESS")
players_ready.append(who)
if players_ready.size() == Network.peer_info.size():
if players_ready.size() == Net.peer_info.size():
rpc("game_start")
# Member functions:
# game_start: starts the game
func game_setup():
sync func game_setup():
# If there's no server connected, create one
if not Network.connected:
if not Net.connected:
# 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():
Net.start_host()
network_id = Net.get_network_id()
# Create players for every player in Net.peer_info
for k in Net.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
player.set_network_master(k)
# Add the player to the list of players
players[k] = player
players.append(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()
mastersync func game_start():
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
pass
func _on_player_ready():
print ("_on_player_ready")

View File

@@ -3,7 +3,6 @@ extends Node
# Emitted when the player is ready
signal player_ready
# Preloaded assets, to be used later
# Path to Board class, for instantiating new Boards in code
var Board = preload("res://scenes/Game/Board.tscn")
@@ -12,33 +11,33 @@ var Setup = preload("res://scenes/Game/Setup.tscn")
# Path to Fire menu, so the player may fire on the opponent
var Fire = preload("res://scenes/Game/Fire.tscn")
var pid # Player ID
# Members
var pid # Player ID
var board # Board
var fire_at_position # Position to fire at
var opponent_pid # PID of opponent
# Called when the node enters the scene tree for the first time.
func _ready():
# Set the player ID according to which network peer ID we are
pid = int(name)
board = Board.instance()
mastersync func set_up_begin():
func set_up_begin():
var setup = Setup.instance()
setup.connect("board_ready", self, "set_up")
add_child(setup)
board = Board.instance()
# Member functions:
# hit: Called when opponent fires on us.
# Update internal state, and return hit/miss/sunk
mastersync func hit(pos):
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
mastersync func mark(pos, value):
func mark(pos, value):
# Mark the position on the top board
board.fire(pos, value)
@@ -62,7 +61,7 @@ func set_up(ships):
# 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]
mastersync func turn_start():
func turn_start():
print("turn_start")
var fire = Fire.instance()
@@ -70,10 +69,9 @@ mastersync func turn_start():
yield(fire, "fire_at")
while not fire_at_position:
pass
var player_id = opponent_pid
var target = fire_at_position
fire_at_position = null
return {"id": player_id, "target": target}
return target
# getBoard: returns the player's board
# returns: board