Reduced number of global variables

This commit is contained in:
Eduard Urbach 2024-02-16 20:04:50 +01:00
parent c228d4e25a
commit d081686275
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
8 changed files with 29 additions and 33 deletions

View File

@ -3,15 +3,14 @@ extends Node
@export var environment: Environment
@export var camera_attributes: CameraAttributesPractical
var account: Account
var camera: Camera
var player: Player
var players: PlayerManager
var account_id: String
var auth_token: String
var interacting_with_ui: bool
var instance_id: int
var username: String
var interacting_with_ui: bool
func _enter_tree():
instance_id = OS.get_process_id() % 4
username = "user%d" % instance_id
account = Account.new()
account.name = "user%d" % instance_id

View File

@ -10,10 +10,10 @@ func _ready():
var offline := args.has("--offline")
if offline:
Global.username = "Local"
Global.account_id = "test"
Global.auth_token = "test"
Global.player = %PlayerAdd.spawn_player(Global.account_id)
Global.account.id = "test"
Global.account.name = "Local"
Global.account.auth_token = "test"
Global.player = %PlayerAdd.spawn_player(Global.account.id)
%Login.success.emit()
else:
%Login.send_login()
@ -24,16 +24,16 @@ func _input(event):
get_viewport().set_input_as_handled()
func on_login():
print("[%s] Login succeeded." % Global.username)
print("[%s] ID: %s" % [Global.username, Global.account_id])
print("[%s] Auth token: %s" % [Global.username, Global.auth_token])
print("[%s] Login succeeded." % Global.account.name)
print("[%s] ID: %s" % [Global.account.name, Global.account.id])
print("[%s] Auth token: %s" % [Global.account.name, Global.account.auth_token])
DisplayServer.window_set_title("%s - %s" % [Global.username, Global.account_id])
DisplayServer.window_set_title("%s - %s" % [Global.account.name, Global.account.id])
DisplayServer.window_set_position(Vector2((Global.instance_id % 2) * 960, (Global.instance_id / 2 % 2) * 540))
pause(false)
func on_logout():
print("[%s] Logout." % Global.username)
print("[%s] Logout." % Global.account.name)
pause(true)
func pause(enabled: bool):

View File

@ -16,6 +16,6 @@ func handle_packet(data: PackedByteArray):
var player := Global.players.get_player(player_id)
var message := buffer.get_utf8_string()
print("[%s] %s: %s" % [Global.username, player.name, message])
print("[%s] %s: %s" % [Global.account.name, player.name, message])
message_received.emit("[color=#808080]%s:[/color] %s" % [player.name, message])

View File

@ -9,12 +9,12 @@ func handle_packet(data: PackedByteArray):
var error := buffer.get_8()
if error != 0:
print("[%s] Login failed." % Global.username)
print("[%s] Login failed." % Global.account.name)
failure.emit()
return
Global.account_id = buffer.get_string()
Global.auth_token = buffer.get_string()
Global.account.id = buffer.get_string()
Global.account.auth_token = buffer.get_string()
success.emit()
@ -25,9 +25,9 @@ func send_login():
var password := "password".sha256_text()
var buffer := StreamPeerBuffer.new()
buffer.put_u8(Packet.LOGIN)
buffer.put_data(JSON.stringify([Global.username, password]).to_utf8_buffer())
buffer.put_data(JSON.stringify([Global.account.name, password]).to_utf8_buffer())
%Client.send(buffer.data_array)
print("[%s] Connecting..." % Global.username)
print("[%s] Connecting..." % Global.account.name)
func is_logged_in() -> bool:
return Global.auth_token != ""
return Global.account.auth_token != ""

View File

@ -7,6 +7,6 @@ func handle_packet(_data: PackedByteArray):
success.emit()
func logout():
Global.auth_token = ""
Global.account_id = ""
Global.account.id = ""
Global.account.auth_token = ""
Global.player = null

View File

@ -19,7 +19,7 @@ func handle_packet(data: PackedByteArray):
server_position.y = buffer.get_float()
server_position.z = buffer.get_float()
print("[%s] Add player: %s %s @ %v" % [Global.username, player_id, player_name, server_position])
print("[%s] Add player: %s %s @ %v" % [Global.account.name, player_id, player_name, server_position])
var player := spawn_player(player_id)
player.position = server_position
@ -31,7 +31,7 @@ func spawn_player(id: String) -> Player:
if Global.players.has(id):
player = Global.players.get_player(id)
if id == Global.account_id:
if id == Global.account.id:
Global.player = player
return player
@ -39,7 +39,7 @@ func spawn_player(id: String) -> Player:
player = player_scene.instantiate()
player.id = id
if id == Global.account_id:
if id == Global.account.id:
player.controller = PlayerController.new(player)
Global.player = player
main_player_spawned.emit(player)

View File

@ -2,5 +2,5 @@ extends PacketHandler
func handle_packet(data: PackedByteArray):
var player_id := data.get_string_from_ascii()
print("[%s] Remove player: %s" % [Global.username, player_id])
print("[%s] Remove player: %s" % [Global.account.name, player_id])
Global.players.remove(player_id)

View File

@ -1,9 +1,6 @@
class_name Account
var username: String
var id: String
var name: String
var password: String
var position: Vector3
func _init(name: String, pw: String):
username = name
password = pw
var auth_token: String