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:
Dan Baker 2025-05-07 15:08:11 +01:00
parent 19cc8cb573
commit 9f66ab0a73
21 changed files with 135 additions and 97 deletions

View file

@ -17,15 +17,14 @@ var base_stats = {
}
# Components
var stats: ModifierManager
#var stats: ModifierManager
var can_fire: bool = true
var fire_timer: Timer
func _init() -> void:
stats = ModifierManager.new(base_stats)
Log.pr(stats)
add_child(stats)
#stats = ModifierManager.new(base_stats)
#Log.pr(stats)
#add_child(stats)
# Setup fire timer
fire_timer = Timer.new()
add_child(fire_timer)
@ -34,7 +33,7 @@ func _init() -> void:
projectile_scene = preload("res://assets/projectiles/projectile_lightning.tscn")
func _ready():
stats.connect("stats_updated", _on_stats_updated)
#stats.connect("stats_updated", _on_stats_updated)
fire_timer.connect("timeout", _on_fire_timer_timeout)
# Initial update
@ -48,13 +47,13 @@ func fire(direction: Vector2, target_position: Vector2):
_spawn_projectile(global_position, direction, target_position)
can_fire = false
Log.pr("Cooldown", stats.get_stat("fire_rate"))
fire_timer.start(stats.get_stat("fire_rate"))
#Log.pr("Cooldown", stats.get_stat("fire_rate"))
#fire_timer.start(stats.get_stat("fire_rate"))
func _spawn_projectile(spawn_position: Vector2, spawn_direction: Vector2, target_position: Vector2):
# Get projectile quantity and spread from stats
var quantity = stats.get_stat("projectile_quantity")
var spread_angle = stats.get_stat("projectile_spread")
var quantity = 1 # stats.get_stat("projectile_quantity")
var spread_angle = 0 # stats.get_stat("projectile_spread")
# Calculate the angle between each projectile
var angle_step = 0.0
@ -79,18 +78,18 @@ func _spawn_projectile(spawn_position: Vector2, spawn_direction: Vector2, target
projectile.direction = direction
# Apply stats to projectile
projectile.speed = stats.get_stat("projectile_speed")
projectile.damage = stats.get_stat("damage")
projectile.lifetime = stats.get_stat("projectile_lifetime")
projectile.speed = 200 # stats.get_stat("projectile_speed")
projectile.damage = 10 # stats.get_stat("damage")
projectile.lifetime = 200 # stats.get_stat("projectile_lifetime")
projectile.source_weapon = self
# Set base size
var size = stats.get_stat("projectile_size")
var size = 1 # stats.get_stat("projectile_size")
projectile.set_projectile_scale(Vector2(size, size))
# Allow modifiers to directly modify the projectile
for modifier in stats.modifiers:
modifier.modify_projectile(projectile)
#for modifier in stats.modifiers:
# modifier.modify_projectile(projectile)
# Add to scene tree
if get_tree() and get_tree().get_root():
@ -102,10 +101,11 @@ func _spawn_projectile(spawn_position: Vector2, spawn_direction: Vector2, target
func add_modifier(modifier: Modifier):
Log.pr("Adding modifier: ", modifier)
stats.add_modifier(modifier)
#stats.add_modifier(modifier)
func remove_modifier(modifier_id: String):
stats.remove_modifier(modifier_id)
pass
#stats.remove_modifier(modifier_id)
func _on_stats_updated():
# Update any visual components based on new stats