2026-02-11 17:03:15 +00:00
@ icon ( " res://assets/icons/game.svg " )
2026-02-11 03:01:44 +00:00
class_name GameManager
extends Node2D
2026-02-18 03:17:25 +00:00
## -- EXPORTS --
@ export var game_ball : Ball
2026-02-11 03:01:44 +00:00
@ export var court : Court
2026-02-13 03:39:13 +00:00
@ export_group ( " Teams " )
@ export var home_team : Team
@ export var away_team : Team
2026-02-18 03:17:25 +00:00
## -- VARIABLES --
2026-02-13 03:39:13 +00:00
var players : Array
var active_player : Player : set = set_active_player
2026-02-18 03:17:25 +00:00
var ball_handler : Player : set = set_ball_handler
2026-02-13 03:39:13 +00:00
2026-02-18 03:17:25 +00:00
## -- OVERRIDES --
2026-02-13 03:39:13 +00:00
func _ready ( ) - > void :
players = get_tree ( ) . get_nodes_in_group ( " players " )
for player in players :
2026-02-18 03:17:25 +00:00
player = player as Player
_connect_player_signals ( player )
2026-02-15 03:17:23 +00:00
player . is_active = false
2026-02-18 03:17:25 +00:00
player . current_ball = game_ball
2026-02-13 03:39:13 +00:00
active_player = players [ 0 ] as Player
2026-02-18 03:17:25 +00:00
## -- RECEIVERS --
func on_player_ball_dropped ( ) - > void :
print_debug ( " %s has dropped the ball " % ball_handler . attributes . player_name )
game_ball . is_loose = true
ball_handler = null
func on_player_grab_attempted ( ) - > void :
print_debug ( " %s has attempted to grab the ball " % active_player . attributes . player_name )
if game_ball . is_loose :
print_debug ( " The ball is loose. This should be easy. " )
active_player . has_ball = true
ball_handler = active_player
game_ball . is_loose = false
else :
print_debug ( " %s has the ball. This could be tough. " % ball_handler . attributes . player_name )
_determine_steal_outcome ( active_player , ball_handler )
2026-02-13 03:39:13 +00:00
func on_player_turn_finished ( ) - > void :
print_debug ( " Moving to the next player in the turn order " )
if players . find ( active_player ) < players . size ( ) - 1 :
active_player = players . get ( players . find ( active_player ) + 1 )
else :
active_player = players . get ( 0 )
2026-02-18 03:17:25 +00:00
## -- SETTERS --
func set_active_player ( a_p ) - > void :
active_player = a_p
2026-02-13 03:39:13 +00:00
print_debug ( " The game is setting the active player to %s " % active_player . attributes . player_name )
active_player . is_active = true
2026-02-18 03:17:25 +00:00
func set_ball_handler ( b_h : Player ) - > void :
ball_handler = b_h
if ball_handler :
print_debug ( " %s has the ball " % ball_handler . attributes . player_name )
## -- HELPERS
func _connect_player_signals ( p : Player ) - > void :
p . connect ( " ball_dropped " , on_player_ball_dropped )
p . connect ( " turn_finished " , on_player_turn_finished )
p . connect ( " grab_attempted " , on_player_grab_attempted )
func _determine_steal_outcome ( grabber : Player , handler : Player ) - > void :
print_debug ( " %s attempting a steal from %s " % [ grabber . attributes . player_name , handler . attributes . player_name ] )
var grabber_dexterity_mod : int = grabber . attributes . dexterity
var handler_handling_mod : int = handler . attributes . ball_handling
var grabber_roll : int = Dice . roll ( Dice . DiceType . d20 , 1 )
var grabber_total : int = grabber_roll + grabber_dexterity_mod
print_debug ( " %s rolled a %s and has a %s dexterity modifier, for a total of %s " % [ grabber . attributes . player_name , grabber_roll , grabber_dexterity_mod , grabber_total ] )
var handler_roll : int = Dice . roll ( Dice . DiceType . d20 , 1 )
var handler_total : int = handler_roll + handler_handling_mod
print_debug ( " %s rolled a %s and has a %s ball-handling modifier, for a total of %s " % [ handler . attributes . player_name , handler_roll , handler_handling_mod , handler_total ] )
var steal_successful : bool = grabber_total > handler_total
if steal_successful :
print_debug ( " Steal successful! Changing possession from %s to %s " % [ handler . attributes . player_name , grabber . attributes . player_name ] )
ball_handler . has_ball = false
ball_handler = active_player
active_player . has_ball = true
else :
print_debug ( " Steal unsuccessful! %s keeps possession " % [ handler . attributes . player_name ] )