summaryrefslogtreecommitdiff
path: root/Scenes/Entities/Enemies/Flowers.gd
blob: 5f8e9741b402dd4e1f4d7e084aa5eeb9415cf35d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
	$CollisionShape2D.shape.radius = max_radius + 16*0.5 # + tile width * 0.5
	
	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()