summaryrefslogtreecommitdiff
path: root/Scenes/Entities/Enemies/Components
diff options
context:
space:
mode:
Diffstat (limited to 'Scenes/Entities/Enemies/Components')
-rw-r--r--Scenes/Entities/Enemies/Components/Collision.gd11
-rw-r--r--Scenes/Entities/Enemies/Components/Health.gd31
-rw-r--r--Scenes/Entities/Enemies/Components/Movement.gd16
3 files changed, 50 insertions, 8 deletions
diff --git a/Scenes/Entities/Enemies/Components/Collision.gd b/Scenes/Entities/Enemies/Components/Collision.gd
index 6e25402..5a82c31 100644
--- a/Scenes/Entities/Enemies/Components/Collision.gd
+++ b/Scenes/Entities/Enemies/Components/Collision.gd
@@ -1,7 +1,6 @@
+class_name ComponentCollision
extends Node
-class_name Component_Collision
-
signal collided
@@ -11,8 +10,8 @@ signal collided
@export_node_path("CollisionShape2D") var collision_shape_path: NodePath
@onready var collision_shape: CollisionShape2D = get_node(collision_shape_path)
-@export_node_path("Component_Health") var component_health_path: NodePath
-var component_health: Component_Health
+@export_node_path("ComponentHealth") var component_health_path: NodePath
+var component_health: ComponentHealth
var collision_area: Area2D
@@ -29,3 +28,7 @@ func init():
if area.is_in_group("explosions"):
component_health.take_damage()
)
+ component_health.died.connect(func():
+ collision_area.set_collision_layer_value(Utilities.Collision.Layer.ENEMY, false)
+ collision_area.set_collision_mask_value(Utilities.Collision.Layer.PLAYER, false)
+ )
diff --git a/Scenes/Entities/Enemies/Components/Health.gd b/Scenes/Entities/Enemies/Components/Health.gd
index d6b7361..6f65e79 100644
--- a/Scenes/Entities/Enemies/Components/Health.gd
+++ b/Scenes/Entities/Enemies/Components/Health.gd
@@ -1,7 +1,6 @@
+class_name ComponentHealth
extends Node
-class_name Component_Health
-
signal died
@@ -11,8 +10,22 @@ signal died
@export_node_path("AnimatedSprite2D") var spritePath: NodePath
@onready var sprite: AnimatedSprite2D = get_node(spritePath)
+@export_node_path("ComponentMovement") var component_movement_path: NodePath
+var component_movement: ComponentMovement
+
@export var health: int
+@export var drops_items: bool = true
+@export_range(1, 100) var drop_chance: int = 30
+var drop_items = [
+ preload("res://Scenes/Entities/Objects/Coin.tscn"),
+]
+
+
+func _ready():
+ if component_movement_path:
+ component_movement = get_node(component_movement_path)
+
func take_damage():
health -= 1
@@ -24,11 +37,14 @@ func take_damage():
func death():
emit_signal("died")
- entity.set_physics_process(false)
+ if component_movement: # stop all movement
+ component_movement.process_mode = Node.PROCESS_MODE_DISABLED
+ entity.set_physics_process(false)
sprite.play("death")
await sprite.animation_finished
+ assert(sprite.sprite_frames.has_animation("death_post"))
sprite.play("death_post")
var tween: Tween = get_tree().create_tween()
@@ -39,5 +55,14 @@ func death():
1 / sprite.sprite_frames.get_animation_speed("death_post")
)
tween.tween_callback(func():
+ if drops_items and randi_range(1, 100) <= drop_chance:
+ call_deferred("drop_item")
+
entity.queue_free()
)
+
+
+func drop_item():
+ var item = drop_items.pick_random().instantiate()
+ item.position = entity.position
+ get_tree().current_scene.add_child(item)
diff --git a/Scenes/Entities/Enemies/Components/Movement.gd b/Scenes/Entities/Enemies/Components/Movement.gd
index 28b374d..5470a7d 100644
--- a/Scenes/Entities/Enemies/Components/Movement.gd
+++ b/Scenes/Entities/Enemies/Components/Movement.gd
@@ -1,3 +1,4 @@
+class_name ComponentMovement
extends Node
@@ -41,7 +42,7 @@ func physics_process(delta):
sprite.play("down")
elif CURRENT_DIRECTION == Vector2.LEFT:
entity.velocity.x -= SPEED
- if sprite.sprite_frames.has_animation("leftup"):
+ if sprite.sprite_frames.has_animation("left"):
sprite.play("left")
elif CURRENT_DIRECTION == Vector2.RIGHT:
entity.velocity.x += SPEED
@@ -96,4 +97,17 @@ func _on_movement_timer_timeout():
emit_signal("direction_changed", CURRENT_DIRECTION)
LAST_DIRECTION = CURRENT_DIRECTION
+ if not sprite.sprite_frames.has_animation("default"):
+ var frame = sprite.frame
+ var progress = sprite.frame_progress
+ if CURRENT_DIRECTION == Vector2.UP and sprite.sprite_frames.has_animation("up"):
+ sprite.play("up")
+ elif CURRENT_DIRECTION == Vector2.DOWN and sprite.sprite_frames.has_animation("down"):
+ sprite.play("down")
+ elif CURRENT_DIRECTION == Vector2.LEFT and sprite.sprite_frames.has_animation("left"):
+ sprite.play("left")
+ elif CURRENT_DIRECTION == Vector2.RIGHT and sprite.sprite_frames.has_animation("right"):
+ sprite.play("right")
+ sprite.set_frame_and_progress(frame, progress)
+
MovementTimer.start()