Added effects manager and two effects to test attached to the player

scene.
This commit is contained in:
Henry Faber 2026-06-24 14:48:02 +01:00
parent 1200ac7e32
commit b4c4a66ce8
19 changed files with 322 additions and 40 deletions

View file

@ -3,14 +3,20 @@ extends Resource
@export var duration: float = 0.5
var _elapsed: float = 0.0
# Called when the effect is applied
func start(target: Node) -> void:
pass
_elapsed = 0.0
# Called every frame (if needed)
func process(delta: float) -> void:
pass
func process(delta: float, target: Node) -> void:
_elapsed += delta
# Clean up any changes to the target
func stop(target: Node) -> void:
pass
# Returns true when the effect has run its course
func is_finished() -> bool:
return _elapsed >= duration

View file

@ -0,0 +1,90 @@
class_name BlinkerEffect
extends BaseEffect
## How many blink cycles per second.
@export_range(0.0, 60.0) var blink_frequency: float = 8.0
## Fraction of each cycle the sprite is visible (0.01.0).
@export_range(0.0, 1.0) var blink_length: float = 0.5
## Minimum alpha when "off" (0.0 = fully invisible, 1.0 = fully visible).
@export_range(0.0, 1.0) var min_alpha: float = 0.0
## Maximum alpha when "on" (0.01.0).
@export_range(0.0, 1.0) var max_alpha: float = 1.0
## Duration of the fade-in at the start of the effect (seconds).
@export_range(0.0, 5.0) var start_ease: float = 0.0
## Easing curve for the fade-in.
@export_enum("Linear", "Smooth", "Ease-In", "Ease-Out", "Ease-In-Out") var start_ease_mode: int = 1
## Duration of the fade-out at the end of the effect (seconds).
@export_range(0.0, 5.0) var end_ease: float = 0.0
## Easing curve for the fade-out.
@export_enum("Linear", "Smooth", "Ease-In", "Ease-Out", "Ease-In-Out") var end_ease_mode: int = 1
var _original_modulate: Color
func start(target: Node) -> void:
super.start(target)
if target is CanvasItem:
_original_modulate = target.modulate
func process(delta: float, target: Node) -> void:
super.process(delta, target)
if target is CanvasItem:
var alpha: float = _compute_alpha(_elapsed)
target.modulate = Color(_original_modulate.r, _original_modulate.g, _original_modulate.b, alpha)
func stop(target: Node) -> void:
if target is CanvasItem:
target.modulate = _original_modulate
## Compute the combined eased alpha for a given elapsed time.
func _compute_alpha(elapsed: float) -> float:
if duration <= 0.0:
return 1.0
var t: float = min(elapsed, duration)
# Blink pulse: on/off cycling at the given frequency and duty cycle
var period := 1.0 / blink_frequency if blink_frequency > 0.0 else 1.0
var phase := fmod(t, period) / period
var pulse: float = min_alpha
if phase < blink_length:
var pulse_progress = phase / blink_length if blink_length > 0.0 else 1.0
if pulse_progress < 0.5:
pulse = min_alpha + (max_alpha - min_alpha) * _ease_value(pulse_progress * 2.0, 2)
else:
pulse = min_alpha + (max_alpha - min_alpha) * _ease_value((1.0 - pulse_progress) * 2.0, 3)
# Start ease: fade from fully visible into the blink pattern
if t < start_ease and start_ease > 0.0:
var fade_in = _ease_value(t / start_ease, start_ease_mode)
return lerp(max_alpha, pulse, fade_in)
# End ease: fade from blink pattern back to fully visible
if t > duration - end_ease and end_ease > 0.0:
var fade_progress = (t - (duration - end_ease)) / end_ease
var fade_out = _ease_value(fade_progress, end_ease_mode)
return lerp(pulse, max_alpha, fade_out)
return pulse
## Apply easing to a 0.01.0 normalized value based on the selected mode.
func _ease_value(value: float, mode: int) -> float:
match mode:
0: return value # Linear
1: return smoothstep(0.0, 1.0, value) # Smooth
2: return ease(value, -2.0) # Ease-In (accelerating)
3: return ease(value, 2.0) # Ease-Out (decelerating)
4: return ease(value, 3.0) # Ease-In-Out (S-curve)
return value

View file

@ -0,0 +1 @@
uid://cj4grtaun2h5a

View file

@ -1,24 +1,33 @@
class_name EffectComponent
extends Node2D
extends Node
@export var target_node: Node
## The node this component applies effects to.
## If left null, defaults to the first parent CanvasItem.
@export var target_node: Node = null
var active_effects: Array[BaseEffect] = []
func _ready() -> void:
if target_node == null:
# Auto-resolve to the parent if nothing is set
target_node = get_parent()
func _process(delta: float) -> void:
if target_node == null:
return
# Process all active effects backwards so we can safely remove finished ones
for i in range(active_effects.size() - 1, -1, -1):
var effect = active_effects[i]
effect.process(delta, target_node)
# Example: simple time-based expiration
effect.duration -= delta
if effect.duration <= 0.0:
if effect.is_finished():
remove_effect(effect)
func apply_effect(effect: BaseEffect) -> void:
effect.start(target_node)
active_effects.append(effect)
var instance = effect.duplicate()
instance.start(target_node)
active_effects.append(instance)
func remove_effect(effect: BaseEffect) -> void:
effect.stop(target_node)

View file

@ -0,0 +1,105 @@
class_name FlashEffect
extends BaseEffect
## Shader applied to the target sprite during the flash.
@export var flash_shader: Shader = preload("res://shaders/flash.gdshader")
## Color of the flash overlay.
@export var flash_color: Color = Color.WHITE
## How many flash cycles per second.
@export_range(0.0, 60.0) var flash_frequency: float = 10.0
## Fraction of each cycle the flash is visible (0.01.0, i.e. duty cycle).
@export_range(0.0, 1.0) var flash_length: float = 0.5
## How the flash blends with the original sprite.
@export_enum("Mix", "Additive", "Screen") var blend_mode: int = 0
## Additional brightness added to the flash color (0.0 = none, 1.0 = double).
@export_range(0.0, 1.0) var brightness_boost: float = 0.0
## Duration of the fade-in at the start of the effect (seconds).
@export_range(0.0, 5.0) var start_ease: float = 0.0
## Easing curve for the fade-in.
@export_enum("Linear", "Smooth", "Ease-In", "Ease-Out", "Ease-In-Out") var start_ease_mode: int = 1
## Duration of the fade-out at the end of the effect (seconds).
@export_range(0.0, 5.0) var end_ease: float = 0.0
## Easing curve for the fade-out.
@export_enum("Linear", "Smooth", "Ease-In", "Ease-Out", "Ease-In-Out") var end_ease_mode: int = 1
var _original_material: Material
func start(target: Node) -> void:
super.start(target)
if target is CanvasItem:
_original_material = target.material
var flash_material := ShaderMaterial.new()
flash_material.shader = flash_shader
flash_material.set_shader_parameter("flash_color", flash_color)
flash_material.set_shader_parameter("flash_intensity", 0.0)
flash_material.set_shader_parameter("blend_mode", blend_mode)
flash_material.set_shader_parameter("brightness_boost", brightness_boost)
target.material = flash_material
func process(delta: float, target: Node) -> void:
super.process(delta, target)
if target.material is ShaderMaterial:
var intensity := _compute_intensity(_elapsed)
target.material.set_shader_parameter("flash_intensity", intensity)
func stop(target: Node) -> void:
if target is CanvasItem:
target.material = _original_material
## Compute the combined eased flash intensity for a given elapsed time.
## Returns a value 0.01.0 where 1.0 means full flash visibility.
func _compute_intensity(elapsed: float) -> float:
if duration <= 0.0:
return 0.0
# Clamp elapsed to duration to avoid overshoot on the final frame
var t: float = min(elapsed, duration)
# Overall envelope: fade in during start_ease, hold, fade out during end_ease
var envelope: float = 1.0
if t < start_ease and start_ease > 0.0:
envelope = _ease_value(t / start_ease, start_ease_mode)
elif t > duration - end_ease and end_ease > 0.0:
var fade_progress = (t - (duration - end_ease)) / end_ease
envelope = 1.0 - _ease_value(fade_progress, end_ease_mode)
# Flash pattern: on/off cycling at the given frequency and duty cycle
# Each flash pulse also eases in and out for a smoother look
var period := 1.0 / flash_frequency if flash_frequency > 0.0 else 1.0
var phase := fmod(t, period) / period # 0.01.0 position within the current cycle
var pulse: float = 0.0
if phase < flash_length:
# Inside the "on" portion of this cycle — ease within the pulse itself
var pulse_progress = phase / flash_length if flash_length > 0.0 else 1.0
# Ease in for the first half of the pulse (build up), ease out for the second (fade down)
if pulse_progress < 0.5:
pulse = _ease_value(pulse_progress * 2.0, 2) # Ease-In (build up)
else:
pulse = _ease_value((1.0 - pulse_progress) * 2.0, 3) # Ease-Out (fade down)
return envelope * pulse
## Apply easing to a 0.01.0 normalized value based on the selected mode.
func _ease_value(value: float, mode: int) -> float:
match mode:
0: return value # Linear
1: return smoothstep(0.0, 1.0, value) # Smooth
2: return ease(value, -2.0) # Ease-In (accelerating)
3: return ease(value, 2.0) # Ease-Out (decelerating)
4: return ease(value, 3.0) # Ease-In-Out (S-curve)
return value

View file

@ -0,0 +1 @@
uid://deak7vcr7ss5l

View file

@ -30,11 +30,13 @@ func tick(delta: float):
%Ship.frame = 2
%Ship/Thrusters.flip_h = true
%Ship/Thrusters.animation = "banked"
%Ship/Thrusters.position.x = %Ship.position.x+1
elif input.x < 0:
player.position.x = player.position.x-1
%Ship.frame = 1
%Ship/Thrusters.flip_h = false
%Ship/Thrusters.animation = "banked"
%Ship/Thrusters.position.x = %Ship.position.x-1
else:
%Ship.frame = 0
player.position.x = player.position.x

View file

@ -2,9 +2,12 @@ class_name WeaponComponent extends Node
@export var weapon_data: WeaponShot = null
@export var available_weapons: Array[WeaponShot] = []
@export var weapon_change_flash: BaseEffect = null
var _current_weapon_index: int = 0
@onready var effects_component: EffectComponent = %EffectsComponent
func _ready() -> void:
# Set the default weapon to be stock, i.e. index 1
@ -36,10 +39,16 @@ func select_weapon_by_name(name: String) -> bool:
print("Switched to: ", name)
_emit_weapon_changed()
_trigger_change_flash()
return true
return false
func _trigger_change_flash() -> void:
if weapon_change_flash and effects_component:
effects_component.apply_effect(weapon_change_flash)
func cycle_weapon() -> void: # Used for testing weapon cycling
if available_weapons.is_empty():
@ -50,3 +59,4 @@ func cycle_weapon() -> void: # Used for testing weapon cycling
print("Switched to: ", weapon_data.shot_name)
_emit_weapon_changed()
_trigger_change_flash()