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

Ship Battle: Draw the rest of the freaking owl

This commit is contained in:
2021-11-24 12:17:07 -06:00
parent 3f4b42821b
commit af9b18243e
14 changed files with 304 additions and 211 deletions

View File

@@ -1,8 +1,10 @@
extends Control
# Ignore discarded return values
# warning-ignore:return_value_discarded
# warning-ignore-all:return_value_discarded
onready var player_list = find_node("Player List")
onready var ip = find_node("IP Address")
onready var ip_address = find_node("IP Address")
onready var name_popup = find_node("Change Name")
onready var game_popup = find_node("Connect to Game")
# TODO: Write a function to update Player List with the list of attached players
@@ -17,28 +19,33 @@ func _on_peers_updated():
func set_IP_Address_text(show):
# Print the IP address and port
if show:
ip.text = "IP: %s\nPort: %s" % [Net.get_ip(), Net.DEFAULT_PORT]
ip_address.text = "IP: %s\nPort: %s" % [Net.get_ip(), Net.DEFAULT_PORT]
else:
ip.text = ""
ip_address.text = ""
func _ready():
Net.connect("peers_updated", self, "_on_peers_updated")
Net.connect("disconnected", self, "_on_Net_disconnected")
name_popup.get_node("Name Entry").text = Net.get_hostname()
_on_peers_updated()
pass
func show_Connected_Options(show):
# Hide the host and connect buttons
func show_Connected_Options(show, host = false):
# [Hide]/Show the host options
get_node("Lobby Options/Connected Options/Host Options").visible = host
# [Hide]/Show the host and connect buttons
get_node("Lobby Options/Host or Connect").visible = !show
# Show the host options
# [Show]/Hide the host options
get_node("Lobby Options/Connected Options").visible = show
# Buttons
# Host Button: Host a game
# Hides the connect button
func _on_Host_Button_pressed():
# Make noise
AudioBus.emit_signal("button_clicked")
# Show "Connected Options"
show_Connected_Options(true)
show_Connected_Options(true, true)
# Show the host IP address
set_IP_Address_text(true)
# Begin hosting
@@ -48,6 +55,8 @@ func _on_Host_Button_pressed():
# Disconnect from (or stop hosting) a game
# Shows the host/connect buttons
func _on_Disconnect_Button_pressed():
# Make noise
AudioBus.emit_signal("button_clicked")
# Disconnect
Net.disconnect_host()
# Hide "Connected Options"
@@ -55,6 +64,13 @@ func _on_Disconnect_Button_pressed():
# Hide the host IP address
set_IP_Address_text(false)
func _on_Start_Game_pressed():
# If there are enough players for a game
if Net.peer_info.size() >= 2:
# Start the game for all players
rpc("start_game")
pass # Replace with function body.
func _on_Net_disconnected():
# Hide "Connected Options"
show_Connected_Options(false)
@@ -62,28 +78,20 @@ func _on_Net_disconnected():
set_IP_Address_text(false)
func _on_Change_Name_Button_pressed():
# Make noise
AudioBus.emit_signal("button_clicked")
# Show the Change Name dialogue
get_node("Change Name").popup_centered()
pass
func _on_Connect_Button_pressed():
# Make noise
AudioBus.emit_signal("button_clicked")
# Show the Connect to Game dialogue
get_node("Connect to Game").popup_centered()
pass
func _on_Connect_to_Game_confirmed():
# Get the IP and port specified by the player
var ipbox = find_node("IP and Port Entry")
# Split it into IP and Port segments
var ip_port = ipbox.text.split(":")
# If text exists and contains valid IP address
if ip_port.size() > 0 and ip_port[0].is_valid_ip_address():
# Connect to host
Net.callv("connect_host", ip_port)
# Show "Connected Options"
show_Connected_Options(true)
func _on_Exit_Lobby_pressed():
# Make noise
AudioBus.emit_signal("button_clicked")
# Disconnect
if Net.connected:
Net.disconnect_host()
@@ -92,6 +100,8 @@ func _on_Exit_Lobby_pressed():
func _on_IP_and_Port_Entry_text_entered(text):
# Make noise
AudioBus.emit_signal("button_clicked")
# Split it into IP and Port segments
var ip_port = text.split(":")
# If text exists and contains valid IP address
@@ -102,11 +112,21 @@ func _on_IP_and_Port_Entry_text_entered(text):
# Show "Connected Options"
show_Connected_Options(true)
# Hide the popup
find_node("Connect to Game").hide()
game_popup.hide()
func _on_Name_Entry_text_entered(text):
# Change the name
Net.change_name(text)
# Hide the popup
find_node("Change Name").hide()
# Make noise
AudioBus.emit_signal("button_clicked")
# Check the length of the name
if text.length() < 18:
# Change the name
Net.change_name(text)
# Hide the popup
name_popup.hide()
sync func start_game():
MessageBus.emit_signal("change_scene", "Gameplay")
queue_free()

View File

@@ -8,9 +8,11 @@ const LOCALHOST = "127.0.0.1"
# Enums, used for mail types
# Mail types:
# 1: REQUEST: Message is a request for information
# 0: REPLY: Message is a reply
enum {REPLY, REQUEST, READY, ACK}
# 0: REQUEST: Message is a request for information
# 1: REPLY: Message is a reply
# 2: READY: Message is "ready"
# 3: ACK: Message is an acknowledgement
enum {REQUEST, REPLY, READY, ACK}
# Signals
# incoming(mail): Sent when there's an incoming message
@@ -36,6 +38,10 @@ var local_info = {"name": ""}
# mail: The message received from the sender (implicitly JSON-decoded by JSONRPC)
# mail_type: Type of mail (see "Mail Types" enum above)
remote func receive(mail):
print_debug("recv: %s" % mail)
# Unpack the mail
# Uses json parser of unknown stability, how fun
mail = parse_json(mail)
# Get the sender's ID and force letter to be properly addressed
mail[0] = get_tree().get_rpc_sender_id()
# Add the mail to the inbox (so it can be read back later if necessary
@@ -48,6 +54,7 @@ remote func receive(mail):
# mail: Variant of a json-encodable type (non-Object) to send
# mail_type: Type of mail (see "Mail Types" enum above)
func send(id, mail, mail_type = REPLY):
print_debug("send: %d, %s, %d" % [id, mail, mail_type])
# Make the recipient receive the mail
rpc_id(id, "receive", to_json([-1, mail, mail_type]))
@@ -81,7 +88,7 @@ func accept_guests(accept:bool):
func connect_host(ip = LOCALHOST, port = DEFAULT_PORT):
get_hostname()
var peer = NetworkedMultiplayerENet.new()
var ret = peer.create_client(ip, port)
var ret = peer.create_client(ip, int(port))
get_tree().network_peer = peer
return ret
@@ -106,9 +113,10 @@ func disconnect_host():
func change_name(name):
# Change name locally
local_info["name"] = name
# Send updated info info to all peers
rpc("register_peer", local_info)
pass
# If connected, update peers
if connected:
# Send updated info info to all peers
rpc("register_peer", local_info)
# Helper Functions
# get_hostname: Asks the host machine to provide its hostname,
@@ -130,7 +138,7 @@ func get_ip():
pass
func _ready():
var _trash
var _trash
_trash = get_tree().connect("network_peer_connected", self, "_peer_connected" )
_trash = get_tree().connect("network_peer_disconnected", self, "_peer_disconnected")
_trash = get_tree().connect("connected_to_server", self, "_host_connected" )