Improves projectile handling and modifiers

Refactors projectile spawning to allow for customized spawn locations and stat assignment.

Applies modifiers to projectiles at the time of spawning, enabling dynamic adjustments to projectile behavior.
This commit is contained in:
Dan Baker 2025-05-09 13:42:09 +01:00
parent 70839387ca
commit fac3327c22
6 changed files with 74 additions and 11 deletions

View file

@ -11,6 +11,8 @@ class_name RangedWeaponComponent
var can_fire: bool = true
var cooldown: Timer
var projectile_spawn_location: Vector2
func _init() -> void:
cooldown = Timer.new()
add_child(cooldown)
@ -33,7 +35,7 @@ func fire(direction: Vector2, target_position: Vector2) -> void:
if !can_fire:
return
spawn_projectile(global_position, direction, target_position)
spawn_projectile(direction, target_position)
can_fire = false
cooldown.start(1 / stats.get_stat("ranged.attack_rate"))
@ -42,21 +44,33 @@ func set_projectile_scene() -> void:
projectile_scene = basic_projectile
modifier_manager.check_and_call("set_projectile_scene", [self])
func spawn_projectile(spawn_position: Vector2, spawn_direction: Vector2, target_position: Vector2) -> void:
## Get the projectile spawn location
## This defaults to the global position of the player
## but can be overridden by modifiers
func get_projectile_spawn_location() -> Vector2:
projectile_spawn_location = global_position
modifier_manager.check_and_call("get_projectile_spawn_location", [self])
return projectile_spawn_location
func spawn_projectile(spawn_direction: Vector2, target_position: Vector2) -> void:
Log.pr("Spawning projectile")
modifier_manager.check_and_call("spawn_projectile", [self])
## TODO: Handle multiple shots per fire
var projectile = projectile_scene.instantiate()
projectile.global_position = spawn_position
projectile.global_position = get_projectile_spawn_location()
projectile.target_position = target_position
projectile.speed = 200 # stats.get_stat("projectile_speed")
projectile.damage = 10 # stats.get_stat("damage")
projectile.lifetime = 2 # stats.get_stat("projectile_lifetime")
projectile.set_stats(stats.duplicate())
projectile.source_weapon = self
var size = stats.get_stat("ranged.projectile_size")
projectile.set_projectile_scale(Vector2(size, size))
for modifier in modifier_manager.modifiers:
Log.pr("Adding modifier to projectile: ", modifier)
if projectile.has_method("add_modifier"):
projectile.add_modifier(modifier)
if get_tree() and get_tree().get_root():
get_tree().get_root().add_child(projectile)