Add terrain hit effects, progressive damage component, and update collectable signals

This commit is contained in:
2025-05-29 01:00:19 +02:00
parent 37b96c0f11
commit 26af7a591d
29 changed files with 406 additions and 22 deletions

View File

@@ -3,6 +3,8 @@ extends Node
@export var root: Node2D
@export var area2d: Area2D
@export var hit_terrain_fx: TerrainHitFx
@export var bullet_sprite: Sprite2D
func _ready() -> void:
@@ -10,9 +12,26 @@ func _ready() -> void:
area2d.area_entered.connect(on_area2d_area_entered)
func on_area2d_body_entered(_body: Node2D) -> void:
func on_area2d_body_entered(body: Node2D) -> void:
if body is TileMapLayer:
if bullet_sprite:
bullet_sprite.visible = false
play_terrain_hit_fx()
return
root.queue_free()
func on_area2d_area_entered(_area: Area2D) -> void:
root.queue_free()
func play_terrain_hit_fx() -> void:
if not hit_terrain_fx:
return
await hit_terrain_fx.trigger_fx()
root.queue_free()

View File

@@ -8,7 +8,7 @@ var has_fade_away: bool = false
@export var collision_shape: CollisionShape2D
@export var collectable_data: CollectableResource
@export var sfx: AudioStreamPlayer2D
signal collected(amount: int, type: CollectableResource.CollectableType)
signal collected(amount: Variant, type: CollectableResource.CollectableType, body: Node2D)
func _ready() -> void:
@@ -25,11 +25,12 @@ func _ready() -> void:
func _on_area2d_body_entered(body: Node2D) -> void:
if body.has_node("CanPickUpComponent"):
collected.emit(collectable_data.amount, collectable_data.type)
collected.emit(collectable_data.amount, collectable_data.type, body)
if collision_shape:
collision_shape.call_deferred("set_disabled", true)
if sfx:
sfx.play()
if not has_fade_away:
if sfx:
await sfx.finished
root.queue_free()

View File

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

View File

@@ -0,0 +1,40 @@
class_name HealComponent
extends Node
@export var heal_fx: GPUParticles2D
@export var collectable: CollectableComponent
func _ready() -> void:
if not collectable:
printerr("HealComponent: No CollectableComponent assigned.")
return
collectable.collected.connect(on_collected)
func on_collected(amount: float, type: CollectableResource.CollectableType, _body: Node2D) -> void:
if type != CollectableResource.CollectableType.HEALTH:
return
if not collectable:
printerr("HealComponent: No CollectableComponent assigned.")
return
var health_component := _body.get_node_or_null("HealthComponent") as HealthComponent
if not health_component or not health_component is HealthComponent:
printerr("HealComponent: No HealthComponent found on collected body.")
return
health_component.increase_health(amount)
if heal_fx:
play_heal_fx()
else:
owner.queue_free()
func play_heal_fx() -> void:
if not heal_fx:
return
heal_fx.restart()
heal_fx.emitting = true

View File

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

View File

@@ -56,7 +56,7 @@ func decrease_health(value: float):
func increase_health(value: float):
var delta = _get_delta(value)
var delta := _get_delta(value)
health += value
@@ -65,7 +65,6 @@ func increase_health(value: float):
on_health_change.emit(delta, health)
return
health += value
if heal_fx:
heal_fx.play()
on_health_change.emit(delta, health)

View File

@@ -43,6 +43,8 @@ func _process(_delta: float) -> void:
else:
rotation_target.rotation = 0
calculate_jump_vars()
func _physics_process(delta) -> void:
if not body or not enabled:
@@ -106,6 +108,11 @@ func handle_direction(input_dir: float) -> Vector2:
return last_direction
func on_ship_entered() -> void:
rotation_target.rotation = 0
rotation_target.rotation = 0
func calculate_jump_vars() -> void:
jump_velocity = ((2.0 * jump_height) / jump_time_to_peak) * -1.0
jump_gravity = ((-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1.0
fall_gravity = ((-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1.0

View File

@@ -0,0 +1,67 @@
class_name ProgressiveDamageComponent
extends Node
@export var health_component: HealthComponent
@export var sprite: Sprite2D
@export var platform_movement: PlatformMovement
@export var min_jump_height: float = 60.0
@export var jump_reduction_percentage: float = 0.1 # this is a percentage of the jump height per hit
@onready var max_health: float = health_component.max_health
var og_jump_height: float = 0.0
func _ready() -> void:
if not health_component:
printerr("ProgressiveDamageComponent: health_component is not set.")
return
if not sprite:
printerr("ProgressiveDamageComponent: sprite is not set.")
return
health_component.on_health_change.connect(on_health_change)
if platform_movement:
og_jump_height = platform_movement.jump_height
func get_damage_frame() -> int:
if not sprite or not health_component:
return 0
var frames_count := sprite.get_hframes()
if frames_count == 0:
return 0
var current_health := health_component.health
var health_ratio := current_health / max_health
return int(frames_count * (1.0 - health_ratio))
func get_jump_height() -> float:
if not platform_movement:
return 0.0
var jump_height := og_jump_height
if jump_height <= 0:
return 0.0
var damage_frame := get_damage_frame()
if damage_frame < 0 or damage_frame >= sprite.get_hframes():
return jump_height
var reduction := jump_reduction_percentage * jump_height
var calculated_jump_height := jump_height - (damage_frame * reduction)
return max(calculated_jump_height, min_jump_height)
func on_health_change(_delta: float, _total_health: float) -> void:
var frame := get_damage_frame()
if frame < 0 or frame >= sprite.get_hframes():
return
sprite.frame = frame
if platform_movement:
platform_movement.jump_height = get_jump_height()

View File

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

View File

@@ -19,7 +19,8 @@ func _ready() -> void:
collectable.collected.connect(on_collected)
func on_collected(amount: int, type: CollectableResource.CollectableType) -> void:
func on_collected(amount: int, type: CollectableResource.CollectableType, _body: Node2D) -> void:
if type != requirement_type:
return
add_progress(amount)

View File

@@ -0,0 +1,24 @@
class_name TerrainHitFx
extends Node
var gpu_particles: Array[GPUParticles2D] = []
func _ready() -> void:
if owner is GPUParticles2D:
gpu_particles.append(owner)
for child in get_children():
if child is GPUParticles2D:
gpu_particles.append(child)
func trigger_fx() -> Signal:
for fx in gpu_particles:
if fx:
fx.restart()
fx.emitting = true
return gpu_particles[0].finished

View File

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