Skip to content

Commit

Permalink
Merge pull request #1 from opensuspect/main
Browse files Browse the repository at this point in the history
m e r g z
  • Loading branch information
TheRyeGuyWhoWillNowDie authored Oct 18, 2020
2 parents c77e25f + b61f9c2 commit ce4d071
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 84 deletions.
114 changes: 57 additions & 57 deletions src/assets/autoload/network.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,92 @@ enum Connection {
CLIENT # Client only, remote server
}

var connection = Connection.LOCAL
var hostName: String = '' #name of host server
var ip: String = ''
var port: int = 0
var hosting: bool = false
var server
var client
var playername
#Entry #1
#This is my chance to help develop something that many people will play! How hard could making a simple game like this be anyways?
puppet var peers: Array = [] #keeps track of network IDs of players
var connection: int = Connection.LOCAL setget toss, get_connection
var server: WebSocketServer setget toss, deny
var client: WebSocketClient setget toss, deny
var player_name: String setget toss, get_player_name
puppet var peers: Array = []
puppet var myID: int = 1

func ready():
set_network_master(1) #gives the server access to puppet functions and variables
#get_tree().connect("network_peer_connected", self, "_player_connected") #moved to after network peer is created, was causing _connected_to_server() to never be called
#get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
#get_tree().connect("connected_to_server", self, "_connected_to_server")
#get_tree().connect("connection_failed", self, "_connection_failed")
#get_tree().connect("server_disconnected", self, "_server_disconnected")
Network.name = "not network"
func server(typestr: String = "CLIENT_SERVER"):
if typestr == "DEDICATED":
connection = Connection.DEDICATED_SERVER
elif typestr == "CLIENT_SERVER":
connection = Connection.CLIENT_SERVER
hosting = true
client = null
print("Starting server")
func ready() -> void:
# give the server access to puppet functions and variables
set_network_master(1)

func client_server(port: int, player_name: String) -> void:
connection = Connection.CLIENT_SERVER
self.player_name = player_name
print("Starting server on port ", port)
server = WebSocketServer.new()
server.listen(port, PoolStringArray(), true) #3rd input must be true to use Godot's high level networking API
get_tree().set_network_peer(server)
get_tree().connect("network_peer_connected", self, "_player_connected")
get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
get_tree().connect("connected_to_server", self, "_connected_to_server")
get_tree().connect("connection_failed", self, "_connection_failed")
get_tree().connect("server_disconnected", self, "_server_disconnected")
connect_signals()
get_tree().change_scene("res://assets/main/main.tscn")

func client(IPstr: String = hostName, typestr: String = "CLIENT"):
if typestr == "CLIENT":
connection = Connection.CLIENT
else:
connection = Connection.LOCAL
hosting = false
ip = IPstr
server = null
func client(hostName: String, port: int, player_name: String) -> void:
connection = Connection.CLIENT
self.player_name = player_name
print("Connecting to ", hostName, " on port ", port)
client = WebSocketClient.new()
var url = "ws://" + ip + ":" + str(port) #use "ws://" at the beginning of address for websocket connections
var _error = client.connect_to_url(url, PoolStringArray(), true) #3rd input must be true to use Godot's high level networking API
#use "ws://" at the beginning of address for websocket connections
var url: String = "ws://" + hostName + ":" + str(port)
# 3rd argument true means use Godot's high level networking API
var _error: int = client.connect_to_url(url, PoolStringArray(), true)
if (_error):
print("Error when connecting to server: ", _error)
get_tree().quit()

get_tree().set_network_peer(client)
get_tree().connect("network_peer_connected", self, "_player_connected")
get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
get_tree().connect("connected_to_server", self, "_connected_to_server")
get_tree().connect("connection_failed", self, "_connection_failed")
get_tree().connect("server_disconnected", self, "_server_disconnected")
connect_signals()
#do not switch to main scene here, wait until the connection was successful

func _player_connected(id):
func _player_connected(id) -> void:
peers.append(id)
if get_tree().is_network_server():
rset_id(id, "myID", str(id)) #remotely sets myID var of new player to their network id
rset("peers", peers) #syncs peer list of all players
# remotely set myID var of new player to their network id
rset_id(id, "myID", str(id))
# sync peer list of all players
rset("peers", peers)

func _player_disconnected(id):
func _player_disconnected(id) -> void:
peers.erase(id)
rset("peers", peers) #syncs peer list of all players

func _connected_to_server():
print("Connection to ", hostName, " on port ", port, " succeeded")
func _connected_to_server() -> void:
print("Connection to server succeeded")
get_tree().change_scene("res://assets/main/main.tscn")
pass #here is where you would put stuff that happens when you connect, such as switching to a lobby scene

func _connection_failed():
print("Connection to ", hostName, " on port ", port, " failed")
func _connection_failed() -> void:
print("Connection to server failed")
pass #here is where you would handle the fact that the connection failed

func _server_disconnected():
func _server_disconnected() -> void:
print("server disconnected")
pass #this is called when the player is kicked, when the server crashes, or whenever the connection is severed

func _process(_delta):
func _process(_delta) -> void:
if server != null: #since this is a websocket connection, it must be manually polled
if server.is_listening():
server.poll()
elif client != null:
if client.get_connection_status() == NetworkedMultiplayerPeer.CONNECTION_CONNECTED || client.get_connection_status() == NetworkedMultiplayerPeer.CONNECTION_CONNECTING:
client.poll()

func toss(newValue) -> void:
pass

func deny() -> void:
pass

func get_connection() -> int:
return connection

func connect_signals() -> void:
get_tree().connect("network_peer_connected", self, "_player_connected")
get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
get_tree().connect("connected_to_server", self, "_connected_to_server")
get_tree().connect("connection_failed", self, "_connection_failed")
get_tree().connect("server_disconnected", self, "_server_disconnected")

func get_player_name() -> String:
return player_name
4 changes: 2 additions & 2 deletions src/assets/main/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func _enter_tree():
# Called on the server when a new client connects
func _player_connected(id):
rpc_id(id,"getname",id, version)
rpc_id(id,"serverinfo",Network.playername, version)
rpc_id(id,"serverinfo",Network.get_player_name(), version)
remote func serverinfo(sname,sversion):
player_join(1,sname)
remote func getname(id,sversion):
rpc_id(1,"playerjoin_proper",Network.playername,id)
rpc_id(1,"playerjoin_proper",Network.get_player_name(),id)
if not version == sversion:
print("HEY! YOU! YOU FORGOT TO UPDATE YOUR CLIENT. RE EXPORT AND TRY AGAIN!")
remote func playerjoin_proper(thename,id):
Expand Down
2 changes: 1 addition & 1 deletion src/assets/player/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func _ready():
if "--server" in OS.get_cmdline_args():
main_player = false
if main_player:
ourname = Network.playername
ourname = Network.get_player_name()
print(ourname)
$Label.text = ourname
# Only called when main_player is true
Expand Down
24 changes: 10 additions & 14 deletions src/assets/ui/lobbyui/chatbox/chatbox.gd
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
extends WindowDialog

onready var chatbox = get_node("Control/chatboxText")

onready var textbox = get_node("Control/HBoxContainer/TextEdit")

var defaultColor: String = "white"

var emptyChars: Array = [" ", " ", "\n", "\r", "\r\n"] #chars considered empty (spaces, tabs, etc.)

var breakChars: Array = ["\n", "\r", "\r\n"]

var maxChars: int = 140

var currentText: String = ""

var cursorCoord: Vector2 = Vector2(0,0) #x is line, y is column

var sentSide: String = "right" #side of chatbox sent messages are on

var receivedSide: String = "left" #side of chatbox received messages are on

func _ready():
pass
#popup()

func sendMessage(content, color: String = defaultColor):
if isEmpty(content) or hasLineBreaks(content):
Expand All @@ -32,24 +23,28 @@ func sendMessage(content, color: String = defaultColor):
showMessage("You", content, color, sentSide)
textbox.text = ""
currentText = ""
rpc("receiveMessage", Network.myID, content, color, Network.playername) #TODO: switch to getting the color from locally stored data to avoid sending false colors, same with names
#TODO: switch to getting the color from locally stored data to avoid sending false colors, same with names
rpc("receiveMessage", Network.myID, content, color, Network.get_player_name())

remote func receiveMessage(sender: int, content: String, color: String, sentname: String): #TODO: switch to getting the color from locally stored data to avoid sending false colors, same with names
#TODO: switch to getting the color from locally stored data to avoid sending false colors, same with names
remote func receiveMessage(sender: int, content: String, color: String, sentname: String):
#add checks here to make sure it's valid (correct color-sender combo, etc.)
if sender != get_tree().get_rpc_sender_id():
#having the sender be sent and then checked allows to double check if get_rpc_sender_id returns the wrong id, most likely won't happen as long as we stay single threaded
pass
if isEmpty(content) or hasLineBreaks(content):
return
showMessage(str(sentname), content, color, receivedSide) #eventually use id to find the player's name
#eventually use id to find the player's name
showMessage(str(sentname), content, color, receivedSide)

func showMessage(sender, content, color, align: String = ""):
if sender == "" or content == "":
return
chatbox.pop()
var newMessage: String
var scroller = chatbox.get_v_scroll()
if scroller.max_value - scroller.page <= scroller.value: #if scrolled all the way down, automatically scroll down to show new message
#if scrolled all the way down, automatically scroll down to show new message
if scroller.max_value - scroller.page <= scroller.value:
chatbox.scroll_following = true
else:
chatbox.scroll_following = false
Expand All @@ -68,7 +63,8 @@ func restrictText():
else:
currentText = newText

func isEmpty(inputStr): #tests if the string is full of empty chars, like tabs and spaces
#tests if the string is full of empty chars, like tabs and spaces
func isEmpty(inputStr):
var emptyCount = 0
for i in emptyChars:
emptyCount += inputStr.count(i)
Expand Down
18 changes: 8 additions & 10 deletions src/assets/ui/mainmenu/playgame/playgame.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ func _ready() -> void:
pass

func _on_Connect_pressed() -> void:
Network.connection = Network.Connection.CLIENT
Network.hostName = $JoinGameMenu/HostnameLine/HostnameField.text
Network.port = $JoinGameMenu/Port/PortField.text
Network.client()
Network.playername = $JoinGameMenu/Name/NameField.text
var hostName: String = $JoinGameMenu/HostnameLine/HostnameField.text
var port: int = $JoinGameMenu/Port/PortField.text as int
var playerName: String = $JoinGameMenu/Name/NameField.text
Network.client(hostName, port, playerName)

func _on_Create_pressed() -> void:
Network.connection = Network.Connection.CLIENT_SERVER
Network.hostName = 'localhost'
Network.port = $CreateGameMenu/PortLine/PortField.text
Network.server()
Network.playername = $CreateGameMenu/Name/NameField.text
var port: int = $CreateGameMenu/PortLine/PortField.text as int
var playerName: String = $CreateGameMenu/Name/NameField.text
Network.client_server(port, playerName)

func _on_JoinGame_pressed() -> void:
show_only('JoinGameMenu')
Expand Down

0 comments on commit ce4d071

Please sign in to comment.