Skip to content

Commit

Permalink
minor refactoring: singleton can be accessed directly
Browse files Browse the repository at this point in the history
  • Loading branch information
esbudylin committed Nov 11, 2024
1 parent 2e88ae0 commit 656a21c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
1 change: 1 addition & 0 deletions godot/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ window/stretch/aspect="keep"
[global]

main=false
single=false

[gui]

Expand Down
8 changes: 4 additions & 4 deletions godot/src/Config.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extends Node

const colors = ['white', 'black']

const chessmen_values = {'Queen': 9, 'Rook': 5, 'Knight': 3,
var chessmen_values = {'Queen': 9, 'Rook': 5, 'Knight': 3,
'Bishop': 4, 'Pawn': 1, 'King': 10}

var master_color
Expand All @@ -14,13 +14,13 @@ var chess_type setget , get_chess_type

var config_path = "res://config.cfg"

func get_chess_type():
func get_chess_type() -> String:
if not chess_type:
chess_type = call_config().get_value('options', 'chess_type', 'Glinski')

return chess_type

func get_chessmen_values():
func get_chessmen_values() -> Dictionary:
var config = call_config()
var values = {}

Expand All @@ -29,7 +29,7 @@ func get_chessmen_values():

return values

func call_config():
func call_config() -> ConfigFile:
var config = ConfigFile.new()

var err = config.load(config_path)
Expand Down
9 changes: 4 additions & 5 deletions godot/src/Game.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ var current_color
onready var Board = $ChessEngine
onready var tilemap = $TileMap

onready var config_node = get_node('/root/Config')
onready var chess_type = config_node.chess_type
onready var chess_type = Config.chess_type

func _ready():
var game_type_node
Expand Down Expand Up @@ -52,14 +51,14 @@ func _ready():
func handle_player_colors():
if is_multiplayer:
if is_server:
self.player_colors = [config_node.master_color]
self.player_colors = [Config.master_color]
else:
self.player_colors = [config_node.puppet_color]
self.player_colors = [Config.puppet_color]
else:
self.player_colors = ['white', 'black']

func prepare_game():
Board.set_board(chess_type, config_node.get_chessmen_values())
Board.set_board(chess_type, Config.get_chessmen_values())

tilemap.tile_colors = Board.get_tile_colors()

Expand Down
18 changes: 8 additions & 10 deletions godot/src/Lobby.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ onready var text_panel = $TextPanel
onready var host_button = $VBoxContainer/Host
onready var join_button = $VBoxContainer/Join

onready var config_node = get_node('/root/Config')

var peer = null
var color_index

Expand All @@ -41,22 +39,22 @@ func generate_color_index():

func set_colors(color_index_local):
if color_index_local == 0:
config_node.master_color = 'white'
config_node.puppet_color = 'black'
Config.master_color = 'white'
Config.puppet_color = 'black'
else:
config_node.master_color = 'black'
config_node.puppet_color = 'white'
Config.master_color = 'black'
Config.puppet_color = 'white'

if get_tree().is_network_server():
set_chess_types()

func set_chess_types(chess_type = null):
if chess_type == null:
chess_type = config_node.chess_type
chess_type = Config.chess_type
rpc('set_chess_types', chess_type)

else:
config_node.chess_type = chess_type
Config.chess_type = chess_type

# warning-ignore:return_value_discarded
get_tree().change_scene("res://scenes/board.tscn")
Expand Down Expand Up @@ -86,7 +84,7 @@ func _on_Host_pressed():
port_forward_label.visible = true

peer = NetworkedMultiplayerENet.new()
config_node.peer = peer
Config.peer = peer

var err = peer.create_server(DEFAULT_PORT, 1)
if err != OK:
Expand All @@ -107,7 +105,7 @@ func _on_OkButton_pressed():
return

peer = NetworkedMultiplayerENet.new()
config_node.peer = peer
Config.peer = peer

peer.create_client(ip, DEFAULT_PORT)
get_tree().set_network_peer(peer)
Expand Down
13 changes: 6 additions & 7 deletions godot/src/Multiplayer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ extends Node
var new_game_request

onready var gs = $"../Game"
onready var config_node = get_node('/root/Config')

onready var peer = config_node.peer
onready var peer = Config.peer

func multiplayer_configs():
gs.rpc_config("game_over", 1)
Expand Down Expand Up @@ -98,12 +97,12 @@ func draw_offer():
$'../Game/HUD/Announcement/Announcement'.text = 'a draw offered'

func swap_colors():
if config_node.master_color == 'white':
config_node.master_color = 'black'
config_node.puppet_color = 'white'
if Config.master_color == 'white':
Config.master_color = 'black'
Config.puppet_color = 'white'
else:
config_node.master_color = 'white'
config_node.puppet_color = 'black'
Config.master_color = 'white'
Config.puppet_color = 'black'

func reload_scene():
swap_colors()
Expand Down

0 comments on commit 656a21c

Please sign in to comment.