randomgeon/entities/scripts/stats_component.gd
Dan Baker 9f66ab0a73 Refactors modifier system to use StatsComponent
Moves modifier logic to utilize a central StatsComponent for managing and applying stat modifications.

This change centralizes stat management and simplifies the application of modifiers, enhancing code maintainability and reducing redundancy.
It also moves modifier files to the correct directory.
2025-05-07 15:08:11 +01:00

95 lines
No EOL
2.2 KiB
GDScript

@icon("res://assets/editor/64x64/fc681.png")
extends Node2D
class_name StatsComponent
var base_stats: Dictionary[String, Variant] = {
"base": {
"health": 100,
"max_health": 100,
"armor": 0,
"max_armor": 0,
"energy": 100,
"max_energy": 100,
"speed": 200.0,
},
"melee": {
"damage": 10,
"attack_rate": 1,
"element": Global.ELEMENTS.NONE,
},
"ranged": {
"damage": 10,
"attack_rate": 1,
"element": Global.ELEMENTS.NONE,
"projectile_speed": 500.0,
"projectile_size": 1.0,
"projectile_lifetime": 1.0,
"projectile_quantity": 1,
"projectile_explode_quantity": 0,
"projectile_explode_damage": 0.5,
"pierce_count": 0
}
}
var stats: Dictionary
func _init() -> void:
if not stats:
reset_stats()
func reset_stats() -> void:
stats = base_stats.duplicate()
Log.pr("StatsComponent reset to base stats")
func get_stat(stat_name: String) -> Variant:
var stat = get_nested_stat(stat_name)
if stat:
return stat
else:
Log.pr("Stat not found: ", stat_name)
return null
func update_stat(stat_name: String, value: Variant) -> void:
var updating_stat = get_nested_stat(stat_name)
if updating_stat:
set_nested_stat(stat_name, value)
Log.pr("Updated stat: ", stat_name, " to ", value)
else:
Log.pr("Stat not found: ", stat_name)
func get_nested_stat(path: String) -> Variant:
var keys = path.split(".")
var current = stats
for key in keys:
if current is Dictionary and current.has(key):
current = current[key]
else:
return null # Path doesn't exist
return current
func set_nested_stat(path: String, value) -> bool:
var keys = path.split(".")
var current = stats
# Navigate to the parent of the final key
for i in range(keys.size() - 1):
var key = keys[i]
# Check if key exists and is a dictionary
if not current.has(key) or not current[key] is Dictionary:
Log.error("Invalid stat path: " + path + " (key '" + key + "' doesn't exist or isn't a dictionary)")
return false
current = current[key]
# Check if final key exists
var final_key = keys[keys.size() - 1]
if not current.has(final_key):
Log.error("Invalid stat path: " + path + " (key '" + final_key + "' doesn't exist)")
return false
# Set the value at the final key
current[final_key] = value
return true