summaryrefslogtreecommitdiff
path: root/Scenes/Entities/Enemies/Flowers.gd
diff options
context:
space:
mode:
Diffstat (limited to 'Scenes/Entities/Enemies/Flowers.gd')
-rw-r--r--Scenes/Entities/Enemies/Flowers.gd82
1 files changed, 82 insertions, 0 deletions
diff --git a/Scenes/Entities/Enemies/Flowers.gd b/Scenes/Entities/Enemies/Flowers.gd
new file mode 100644
index 0000000..09e6085
--- /dev/null
+++ b/Scenes/Entities/Enemies/Flowers.gd
@@ -0,0 +1,82 @@
+extends CharacterBody2D
+
+
+@export var speed: int = 5
+@export var min_radius: int = 5
+@export var max_radius: int = 25
+@export var mode_duration: float = 4.0
+
+var follows_player: bool = false
+@onready var radius: float = float(min_radius)
+
+enum MovementMode {
+ EXPAND,
+ SHRINK,
+}
+var movement_mode: MovementMode = MovementMode.SHRINK
+
+var flowers: Array = []
+
+
+func _ready():
+ $Timer.wait_time = mode_duration
+
+ var FlowerScene = preload("res://Scenes/Entities/Enemies/Flower.tscn")
+
+ for number in range(3):
+ var flower = FlowerScene.instantiate()
+ flower.angle = deg_to_rad(120 * number)
+
+ # if last flower is removed, remove self
+ flower.get_node("Health").died.connect(func():
+ flowers.remove_at(flowers.find(flower))
+ if flowers.is_empty():
+ queue_free()
+ )
+
+ get_parent().add_child(flower)
+ flowers.push_front(flower)
+
+
+func _physics_process(delta):
+ if follows_player:
+ var player_direction = position.direction_to(Global.player.position)
+ velocity = player_direction * float(speed) * delta
+ move_and_collide(velocity)
+
+ if movement_mode == MovementMode.SHRINK and radius > min_radius:
+ radius = max(
+ min_radius,
+ radius - ((max_radius - min_radius) / mode_duration) * delta
+ )
+ elif movement_mode == MovementMode.EXPAND and radius < max_radius:
+ radius = min(
+ max_radius,
+ radius + ((max_radius - min_radius) / mode_duration) * delta
+ )
+
+ for flower in flowers:
+ flower.move_and_collide(velocity)
+ flower.position = position + Vector2(cos(flower.angle) * radius, sin(flower.angle) * radius)
+ flower.angle += float(speed) * delta
+
+
+func _on_detection_area_body_entered(body):
+ if body is Player:
+ follows_player = true
+
+
+func _on_detection_area_body_exited(body):
+ if body is Player:
+ follows_player = false
+
+
+func _on_timer_timeout():
+ if movement_mode == MovementMode.SHRINK:
+ movement_mode = MovementMode.EXPAND
+ for flower in flowers:
+ flower.expand()
+ elif movement_mode == MovementMode.EXPAND:
+ movement_mode = MovementMode.SHRINK
+ for flower in flowers:
+ flower.shrink()