Add spaceship interaction components and update player movement logic

This commit is contained in:
2025-05-25 19:26:41 +02:00
parent 129fc17f13
commit bac0a8c5f7
19 changed files with 286 additions and 36 deletions

View File

@@ -0,0 +1,69 @@
class_name ChaseLevelComponent
extends Node
@export var chase_speed: float = 200.0
@export var chase_target: Marker2D
@export var phantom_camera: PhantomCamera2D
@export var minimum_distance: float = 10.0
signal chase_started
signal chase_stopped
var is_chasing: bool = false
var previous_camera_follow_target: Node2D = null
func _process(delta: float) -> void:
if not is_chasing:
return
if not chase_target:
printerr("ChaseLevelComponent: chase_target is not set.")
return
if check_if_reached_target():
stop_chasing()
return
var target_position: Vector2 = chase_target.global_position
var current_position: Vector2 = owner.global_position
var direction: Vector2 = (target_position - current_position).normalized()
owner.global_position += direction * chase_speed * delta
func on_ship_entered() -> void:
if not chase_target:
printerr("ChaseLevelComponent: chase_target is not set.")
return
if not phantom_camera:
printerr("ChaseLevelComponent: phantom_camera is not set.")
return
previous_camera_follow_target = phantom_camera.get_follow_target()
phantom_camera.set_follow_target(owner as Node2D)
chase_started.emit()
is_chasing = true
func on_ship_exited() -> void:
stop_chasing()
func check_if_reached_target() -> bool:
if not chase_target:
printerr("ChaseLevelComponent: chase_target is not set.")
return false
var target_position: Vector2 = chase_target.global_position
var current_position: Vector2 = owner.global_position
return current_position.distance_to(target_position) < minimum_distance
func stop_chasing() -> void:
if not phantom_camera:
printerr("ChaseLevelComponent: phantom_camera is not set.")
return
phantom_camera.set_follow_target(previous_camera_follow_target)
chase_stopped.emit()
is_chasing = false

View File

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

View File

@@ -0,0 +1,24 @@
class_name KillPlayerOutOfScreen
extends Node
@export var screen_notifier: VisibleOnScreenNotifier2D
@export var health_component: HealthComponent
func _ready() -> void:
if not screen_notifier:
printerr("KillPlayerOutOfScreen: screen_notifier is not set.")
return
if not health_component:
printerr("KillPlayerOutOfScreen: health_component is not set.")
return
screen_notifier.screen_exited.connect(out_of_screen)
func out_of_screen() -> void:
if not health_component:
return
health_component.decrease_health(6000)

View File

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

View File

@@ -19,6 +19,7 @@ func _ready() -> void:
return
area2d.body_entered.connect(_on_body_entered)
area2d.area_entered.connect(_on_area_entered)
func _on_body_entered(body: Node2D) -> void:
@@ -29,6 +30,15 @@ func _on_body_entered(body: Node2D) -> void:
activate()
func _on_area_entered(area: Area2D) -> void:
var trigger_lever: TriggerLeverComponent = area.get_node_or_null("TriggerLeverComponent")
if not trigger_lever:
return
activate()
func activate() -> void:
activated.emit()
if sfx:

View File

@@ -104,3 +104,8 @@ func handle_direction(input_dir: float) -> Vector2:
elif input_dir < 0:
return Vector2.LEFT
return last_direction
func on_ship_entered() -> void:
rotation_target.rotation = 0

View File

@@ -6,7 +6,12 @@ extends Node
@export var bullet_spawn: Marker2D
@export var shoot_sfx: AudioStreamPlayer2D
var can_shoot: bool = true
var can_shoot: bool = false
func _ready() -> void:
set_process(false)
func _process(_delta: float) -> void:
@@ -30,4 +35,16 @@ func shoot() -> void:
can_shoot = false
await get_tree().create_timer(fire_rate).timeout
can_shoot = true
can_shoot = true
func on_ship_entered():
can_shoot = true
set_process(true)
func on_ship_exited():
can_shoot = false
set_process(false)
if shoot_sfx:
shoot_sfx.stop()

View File

@@ -0,0 +1,21 @@
class_name SpaceshipEnterComponent
extends Node
@export var area2d: Area2D
signal spaceship_entered
func _ready() -> void:
if not area2d:
printerr("SpaceshipEnterComponent: area2d is not set.")
return
area2d.body_entered.connect(_on_area2d_body_entered)
func _on_area2d_body_entered(body: Node2D) -> void:
if not body is PlayerController:
return
spaceship_entered.emit()
owner.queue_free()

View File

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