simple scoreboard system

This commit is contained in:
anyreso 2024-04-13 18:59:32 -04:00
parent 3f19e3e033
commit e6cd2a79c6
2 changed files with 51 additions and 0 deletions

View file

@ -0,0 +1,45 @@
class_name Scoreboard extends Node
# define the properties
@export var num_teams: int = 1
@export var num_players_per_team: int = 1
# define the scoreboard dictionary
var scoreboard := {}
# initialize the scoreboard
func _init() -> void:
scoreboard = {}
for team in range(1, num_teams + 1):
scoreboard[team] = {}
for player in range(1, num_players_per_team + 1):
scoreboard[team][player] = {'O': 0, 'D': 0, 'S': 0}
# update score function
func update_score(team: int, player: int, points: int, category: String) -> void:
if not scoreboard.has(team) or not scoreboard[team].has(player):
push_warning("Team or player not found in the scoreboard.")
return
if category not in ['O', 'D', 'S']:
push_warning("Invalid category. Please use 'O' for Offense, 'D' for Defense, or 'S' for Style.")
return
scoreboard[team][player][category] += points
# get player score function
func get_player_score(team: int, player: int) -> Dictionary:
if not scoreboard.has(team) or not scoreboard[team].has(player):
push_warning("Team or player not found in the scoreboard.")
return {}
return scoreboard[team][player]
# get team score function
func get_team_score(team: int) -> Dictionary:
if not scoreboard.has(team):
push_warning("Team not found in the scoreboard.")
return {}
var team_score = {'O': 0, 'D': 0, 'S': 0}
for player_scores in scoreboard[team].values():
team_score['O'] += player_scores['O']
team_score['D'] += player_scores['D']
team_score['S'] += player_scores['S']
return team_score

View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://cupyjjyhjh5bq"]
[ext_resource type="Script" path="res://systems/scoreboard/scoreboard.gd" id="1_alsat"]
[node name="Scoreboard" type="Node2D"]
script = ExtResource("1_alsat")