diff options
Diffstat (limited to 'Scenes/Entities/Enemies/Components')
-rw-r--r-- | Scenes/Entities/Enemies/Components/Collision.gd | 11 | ||||
-rw-r--r-- | Scenes/Entities/Enemies/Components/Health.gd | 39 | ||||
-rw-r--r-- | Scenes/Entities/Enemies/Components/Health.tscn | 6 | ||||
-rw-r--r-- | Scenes/Entities/Enemies/Components/Movement.gd | 57 |
4 files changed, 103 insertions, 10 deletions
diff --git a/Scenes/Entities/Enemies/Components/Collision.gd b/Scenes/Entities/Enemies/Components/Collision.gd index b2e8d9b..2dfba2a 100644 --- a/Scenes/Entities/Enemies/Components/Collision.gd +++ b/Scenes/Entities/Enemies/Components/Collision.gd @@ -11,8 +11,19 @@ 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 + func init(): var collision_area = Utilities.Collision.Area.new(entity, collision_shape) + collision_area.set_collision_mask_value(Utilities.Collision.Layer.PLAYER, true) collision_area.connect("collided", func(area): emit_signal("collided", area)) entity.add_child(collision_area) + + if component_health_path: + component_health = get_node(component_health_path) + collision_area.connect("collided", func(area): + if area.is_in_group("explosions"): + component_health.take_damage() + ) diff --git a/Scenes/Entities/Enemies/Components/Health.gd b/Scenes/Entities/Enemies/Components/Health.gd new file mode 100644 index 0000000..f5e231e --- /dev/null +++ b/Scenes/Entities/Enemies/Components/Health.gd @@ -0,0 +1,39 @@ +extends Node + +class_name Component_Health + + +@export_node_path("CharacterBody2D") var entityPath: NodePath +@onready var entity: CharacterBody2D = get_node(entityPath) + +@export_node_path("AnimatedSprite2D") var spritePath: NodePath +@onready var sprite: AnimatedSprite2D = get_node(spritePath) + +@export var health: int + + +func take_damage(): + health -= 1 + + if health <= 0: + death() + + +func death(): + entity.set_physics_process(false) + + sprite.play("death") + await sprite.animation_finished + + sprite.play("death_post") + + var tween: Tween = get_tree().create_tween() + tween.tween_property( + entity, + "scale", + Vector2(0, 0), + 1 / sprite.sprite_frames.get_animation_speed("death_post") + ) + tween.tween_callback(func(): + entity.queue_free() + ) diff --git a/Scenes/Entities/Enemies/Components/Health.tscn b/Scenes/Entities/Enemies/Components/Health.tscn new file mode 100644 index 0000000..6a51704 --- /dev/null +++ b/Scenes/Entities/Enemies/Components/Health.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://b3i372vgdbxk"] + +[ext_resource type="Script" path="res://Scenes/Entities/Enemies/Components/Health.gd" id="1_kjbx0"] + +[node name="Health" type="Node"] +script = ExtResource("1_kjbx0") diff --git a/Scenes/Entities/Enemies/Components/Movement.gd b/Scenes/Entities/Enemies/Components/Movement.gd index 6ded026..28b374d 100644 --- a/Scenes/Entities/Enemies/Components/Movement.gd +++ b/Scenes/Entities/Enemies/Components/Movement.gd @@ -13,10 +13,15 @@ signal direction_changed const DIRECTIONS = [Vector2.UP, Vector2.RIGHT, Vector2.DOWN, Vector2.LEFT] var CURRENT_DIRECTION = Vector2.UP +var LAST_DIRECTION = Vector2.UP +var was_colliding = false @export var timer_time: float = 3.0 @onready var MovementTimer: Timer = Timer.new() +@export var follows: bool = false +@export_range(1, 100, 1) var follow_chance: int = 100 + func _ready(): MovementTimer.wait_time = timer_time @@ -28,35 +33,67 @@ func _ready(): func physics_process(delta): if CURRENT_DIRECTION == Vector2.UP: entity.velocity.y -= SPEED - sprite.play("up") + if sprite.sprite_frames.has_animation("up"): + sprite.play("up") elif CURRENT_DIRECTION == Vector2.DOWN: entity.velocity.y += SPEED - sprite.play("down") + if sprite.sprite_frames.has_animation("down"): + sprite.play("down") elif CURRENT_DIRECTION == Vector2.LEFT: entity.velocity.x -= SPEED - sprite.play("left") + if sprite.sprite_frames.has_animation("leftup"): + sprite.play("left") elif CURRENT_DIRECTION == Vector2.RIGHT: entity.velocity.x += SPEED - sprite.play("right") + if sprite.sprite_frames.has_animation("right"): + sprite.play("right") + + if sprite.sprite_frames.has_animation("default"): + sprite.play("default") var collision = entity.move_and_collide(entity.velocity * delta) entity.velocity = entity.velocity.lerp(Vector2(0, 0), 1) # speed too low => no collision if collision: + was_colliding = true if MovementTimer.time_left > 0: _on_movement_timer_timeout() + else: + was_colliding = false return collision func _on_movement_timer_timeout(): - var directions = DIRECTIONS.duplicate() - - directions.remove_at(directions.find(CURRENT_DIRECTION)) - directions.shuffle() - - CURRENT_DIRECTION = directions[0] + var randomize_direction = false + + if follows and randi_range(0, 100) <= follow_chance: + var player_direction = entity.position.direction_to(Global.player.position) + if abs(player_direction.x) >= abs(player_direction.y): + if player_direction.x >= 0: + CURRENT_DIRECTION = Vector2.RIGHT + elif player_direction.x < 0: + CURRENT_DIRECTION = Vector2.LEFT + elif abs(player_direction.y) > abs(player_direction.x): + if player_direction.y >= 0: + CURRENT_DIRECTION = Vector2.DOWN + elif player_direction.y < 0: + CURRENT_DIRECTION = Vector2.UP + + if was_colliding and LAST_DIRECTION == CURRENT_DIRECTION: + randomize_direction = true + else: + randomize_direction = true + + if randomize_direction: + var directions = DIRECTIONS.duplicate() + + directions.remove_at(directions.find(CURRENT_DIRECTION)) + directions.shuffle() + + CURRENT_DIRECTION = directions[0] emit_signal("direction_changed", CURRENT_DIRECTION) + LAST_DIRECTION = CURRENT_DIRECTION MovementTimer.start() |