Fix brick throwing, add enemy death
This commit is contained in:
@@ -32,7 +32,7 @@ func throw_brick() -> void:
|
||||
var brick_instance: Node2D = brick_scene.instantiate()
|
||||
var brick: BulletComponent = brick_instance.get_node("BulletComponent")
|
||||
brick_instance.position = player_controller.position
|
||||
brick.direction = Vector2.RIGHT if player_controller.velocity.x >= 0.0 else Vector2.LEFT
|
||||
brick.direction = player_controller.last_direction
|
||||
get_tree().current_scene.add_child(brick_instance)
|
||||
|
||||
can_throw = false
|
||||
|
@@ -14,6 +14,7 @@ func _ready() -> void:
|
||||
root = get_parent()
|
||||
visibility_notifier.screen_exited.connect(_on_screen_exited)
|
||||
area2d.body_entered.connect(on_area2d_body_entered)
|
||||
area2d.area_entered.connect(on_area2d_area_entered)
|
||||
|
||||
timer.wait_time = life_time
|
||||
timer.timeout.connect(on_timer_timeout)
|
||||
@@ -31,5 +32,9 @@ func on_area2d_body_entered(_body: Node2D) -> void:
|
||||
root.queue_free()
|
||||
|
||||
|
||||
func on_area2d_area_entered(_area: Area2D) -> void:
|
||||
root.queue_free()
|
||||
|
||||
|
||||
func on_timer_timeout() -> void:
|
||||
root.queue_free()
|
28
scripts/components/enemy_death.gd
Normal file
28
scripts/components/enemy_death.gd
Normal file
@@ -0,0 +1,28 @@
|
||||
class_name EnemyDeathComponent
|
||||
extends Node
|
||||
|
||||
@export var root: Node2D
|
||||
@export var tween_duration: float = 0.5
|
||||
@export var collision_shape_2d: CollisionShape2D
|
||||
@export var health_component: HealthComponent
|
||||
|
||||
func _ready() -> void:
|
||||
if not collision_shape_2d:
|
||||
printerr("No CollisionShape2D assigned!")
|
||||
return
|
||||
if not health_component:
|
||||
printerr("No HealthComponent assigned!")
|
||||
return
|
||||
|
||||
health_component.on_death.connect(_on_health_component_on_death)
|
||||
|
||||
func _on_health_component_on_death() -> void:
|
||||
call_deferred("die")
|
||||
|
||||
|
||||
func die() -> void:
|
||||
collision_shape_2d.disabled = true
|
||||
var tween := create_tween()
|
||||
tween.tween_property(root, "scale", Vector2(0, 0), tween_duration)
|
||||
await (tween.finished)
|
||||
root.queue_free()
|
@@ -6,7 +6,7 @@ extends Node2D
|
||||
@export var player_controller: PlayerController
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
var velocity = player_controller.velocity
|
||||
var velocity := player_controller.last_direction
|
||||
if velocity.x < 0:
|
||||
eye_left.frame = 1
|
||||
eye_right.frame = 1
|
||||
|
@@ -4,6 +4,7 @@ extends CharacterBody2D
|
||||
@export var speed: float = 300.0
|
||||
|
||||
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
||||
var last_direction: Vector2 = Vector2.RIGHT
|
||||
|
||||
@onready var root = $Root
|
||||
@onready var coyote_timer: Timer = $CoyoteTimer
|
||||
@@ -52,7 +53,10 @@ func _physics_process(delta):
|
||||
if Input.is_action_just_pressed("down"):
|
||||
position.y += 1
|
||||
|
||||
var direction = Input.get_axis("left", "right")
|
||||
var direction := Input.get_axis("left", "right")
|
||||
if direction != 0:
|
||||
last_direction = handle_direction(direction)
|
||||
|
||||
if direction:
|
||||
velocity.x = direction * speed
|
||||
else:
|
||||
@@ -69,3 +73,11 @@ func calculate_gravity() -> float:
|
||||
|
||||
func on_coyote_timer_timeout():
|
||||
coyote_mode = false
|
||||
|
||||
|
||||
func handle_direction(input_dir: float) -> Vector2:
|
||||
if input_dir > 0:
|
||||
return Vector2.RIGHT
|
||||
elif input_dir < 0:
|
||||
return Vector2.LEFT
|
||||
return last_direction
|
Reference in New Issue
Block a user