blob: a87ecf01b8aa2dad5de720dab6ca081e824846ed (
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
extends CharacterBody2D
@export var speed: float = 10
static var detected_bombs = []
var did_detect_bomb = false
var detected_bomb: Bomb
var detected_bomb_direction = Vector2.UP
var is_full = false
var is_animating_state = false
var animations_default = {
"up": "up",
"down": "down",
"left": "left",
"right": "right",
}
var animations_full = {
"up": "up_full",
"down": "down_full",
"left": "left_full",
"right": "right_full",
}
var current_delta
@onready var component_collision: ComponentCollision = $Collision
@onready var component_movement: ComponentMovement = $Movement
func _ready():
add_to_group("enemies")
component_collision.init()
component_movement.SPEED = speed
func _physics_process(delta):
current_delta = delta
if not did_detect_bomb and not is_full:
component_movement.SPEED = speed
for ray: RayCast2D in $Rays.get_children():
if (
ray.is_colliding() and
ray.get_collider() and
ray.get_collider().is_in_group("bombs") and
not detected_bombs.has(ray.get_collider())
):
did_detect_bomb = true
detected_bomb = ray.get_collider()
detected_bombs.push_front(detected_bomb)
detected_bomb.tree_exited.connect(func():
did_detect_bomb = false
detected_bombs.erase(detected_bomb)
detected_bomb = null
)
if ray.name == "Down":
detected_bomb_direction = Vector2.DOWN
elif ray.name == "Up":
detected_bomb_direction = Vector2.UP
elif ray.name == "Left":
detected_bomb_direction = Vector2.LEFT
elif ray.name == "Right":
detected_bomb_direction = Vector2.RIGHT
component_movement.SPEED = speed*4
if $DetectionArea.get_overlapping_bodies().has(detected_bomb):
_on_detection_area_body_entered(detected_bomb)
elif did_detect_bomb:
component_movement.CURRENT_DIRECTION = detected_bomb_direction
if not is_animating_state:
component_movement.physics_process(delta)
func set_animating_state(state: bool):
is_animating_state = state
if is_animating_state:
component_movement.MovementTimer.stop()
else:
component_movement.MovementTimer.start()
func _on_detection_area_body_entered(_body):
if did_detect_bomb and detected_bomb:
set_animating_state(true)
add_collision_exception_with(detected_bomb)
detected_bomb.stop()
self.z_index = detected_bomb.z_index + 1
if detected_bomb_direction == Vector2.UP:
$AnimatedSprite2D.play("up_bite")
smooth_over("up_bite")
elif detected_bomb_direction == Vector2.DOWN:
$AnimatedSprite2D.play("down_bite")
smooth_over("down_bite")
elif detected_bomb_direction == Vector2.LEFT:
$AnimatedSprite2D.play("left_bite")
smooth_over("left_bite")
elif detected_bomb_direction == Vector2.RIGHT:
$AnimatedSprite2D.play("right_bite")
smooth_over("right_bite")
await $AnimatedSprite2D.animation_finished
if detected_bomb:
detected_bomb.queue_free()
component_movement.set_animations(animations_full)
is_full = true
component_movement.SPEED = speed/2
$Timer.start()
set_animating_state(false)
func smooth_over (animation):
create_tween().tween_property(
self,
"position",
detected_bomb.position,
$AnimatedSprite2D.sprite_frames.get_animation_speed(animation) / $AnimatedSprite2D.sprite_frames.get_frame_count(animation)
)
func _on_timer_timeout():
set_animating_state(true)
if component_movement.CURRENT_DIRECTION == Vector2.UP:
$AnimatedSprite2D.play("up_shrink")
elif component_movement.CURRENT_DIRECTION == Vector2.DOWN:
$AnimatedSprite2D.play("down_shrink")
elif component_movement.CURRENT_DIRECTION == Vector2.LEFT:
$AnimatedSprite2D.play("left_shrink")
elif component_movement.CURRENT_DIRECTION == Vector2.RIGHT:
$AnimatedSprite2D.play("right_shrink")
await $AnimatedSprite2D.animation_finished
component_movement.set_animations(animations_default)
is_full = false
component_movement.SPEED = speed
set_animating_state(false)
|