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.
This commit is contained in:
parent
19cc8cb573
commit
9f66ab0a73
21 changed files with 135 additions and 97 deletions
|
|
@ -2,7 +2,7 @@
|
|||
extends Node2D
|
||||
class_name StatsComponent
|
||||
|
||||
var stats: Dictionary[String, Variant] = {
|
||||
var base_stats: Dictionary[String, Variant] = {
|
||||
"base": {
|
||||
"health": 100,
|
||||
"max_health": 100,
|
||||
|
|
@ -30,3 +30,66 @@ var stats: Dictionary[String, Variant] = {
|
|||
"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
|
||||
Loading…
Add table
Add a link
Reference in a new issue