diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-04-14 21:49:20 +0200 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-04-14 21:49:20 +0200 |
commit | f27937e6e9b6a7146dd09fc711d2d293ecf5abbf (patch) | |
tree | e0ddd5e6d356000e4f29af6211a736284a4972ee /Scenes/Entities/Enemies/Components/Movement.gd | |
parent | 96d36cee33d6321834565f0a5d412ef95c5ceffd (diff) |
update
Diffstat (limited to 'Scenes/Entities/Enemies/Components/Movement.gd')
-rw-r--r-- | Scenes/Entities/Enemies/Components/Movement.gd | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/Scenes/Entities/Enemies/Components/Movement.gd b/Scenes/Entities/Enemies/Components/Movement.gd new file mode 100644 index 0000000..0b30820 --- /dev/null +++ b/Scenes/Entities/Enemies/Components/Movement.gd @@ -0,0 +1,56 @@ +extends Node + +signal direction_changed + +@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 SPEED: int + +const DIRECTIONS = [Vector2.UP, Vector2.RIGHT, Vector2.DOWN, Vector2.LEFT] +var CURRENT_DIRECTION = Vector2.UP + +@onready var MovementTimer: Timer = Timer.new() + + +func _ready(): + MovementTimer.wait_time = 3.0 + MovementTimer.autostart = true + MovementTimer.connect("timeout", Callable(_on_movement_timer_timeout)) + add_child(MovementTimer) + + +func physics_process(delta): + if CURRENT_DIRECTION == Vector2.UP: + entity.velocity.y -= SPEED + sprite.play("up") + elif CURRENT_DIRECTION == Vector2.DOWN: + entity.velocity.y += SPEED + sprite.play("down") + elif CURRENT_DIRECTION == Vector2.LEFT: + entity.velocity.x -= SPEED + sprite.play("left") + elif CURRENT_DIRECTION == Vector2.RIGHT: + entity.velocity.x += SPEED + sprite.play("right") + + var collision = entity.move_and_collide(entity.velocity * delta) + entity.velocity = entity.velocity.lerp(Vector2(0, 0), 1) + + return collision + + +func _on_movement_timer_timeout(): + var directions = DIRECTIONS.duplicate() + + directions.remove_at(directions.find(CURRENT_DIRECTION)) + directions.shuffle() + + CURRENT_DIRECTION = directions[0] + + emit_signal("direction_changed", CURRENT_DIRECTION) + + MovementTimer.start() |