-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseGuard.gd
142 lines (116 loc) · 4.16 KB
/
BaseGuard.gd
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
extends Node2D
export var color = "red"
export var type = "slim"
export var direction = 0
export var view_range = 2
export var frozen_timeout = 15 # duration of time when we are frozen
var can_see_player = false
var is_frozen = false
var frozen_duration = 0
var is_alert = true
var is_transitioning = false
var _in_fixed_process = true
var colors = {"red": Color(0.8,0.2,0.2), "blue": Color(0.2,0.2,0.8), "green": Color(0.2,0.8,0.2), "yellow": Color(0.5,0.5,0.24) }
func path_a_to_b(nav2d, a, b):
if a != null and b != null:
var a_nav2dpos = nav2d.get_global_transform().xform(a.get_global_pos())
var b_nav2dpos = nav2d.get_global_transform().xform(b.get_global_pos())
var path = nav2d.get_simple_path(a_nav2dpos, b_nav2dpos)
return path
return null
func _ready():
set_fixed_process(true)
set_direction(direction)
add_to_group("Enemies")
add_to_group("Ephemerals")
var sprite = get_node("AnimatedSprite")
if sprite.tex == null:
sprite.tex = load("res://Artwork/sprites/" + color + "-" + type + "-spritesheet.png")
sprite.idle_ani(direction)
sprite.set_rot(0)
set_rot(0)
var cone = get_node("Cone of Vision")
cone.set_rot(direction * 3.14159/2)
var coneSprite = cone.get_node("Sprite")
coneSprite.set_modulate(colors[color])
coneSprite.set_scale(Vector2(view_range/8.0, view_range/8.0))
func _fixed_process(delta):
_in_fixed_process = true
var player = get_tree().get_nodes_in_group("Player")[0]
try_see_player()
if can_see_player and player.color != color and !is_frozen and is_alert and !is_transitioning:
# Player was spotted, we restart the scene!
if !get_node("/root/globals").audio_muted:
get_node("/root/LevelBGM/SamplePlayer").play("alarm", true)
player.transition_new_scene(get_tree().get_current_scene().get_filename())
is_transitioning = true
_in_fixed_process = false
func try_see_player():
if not _in_fixed_process:
print("ALERT! try_see_player() called outside _fixed_process.")
return
var sprite = get_child(0)
var player = get_tree().get_nodes_in_group("Player")[0]
# based on where the player is, and based on direction we're facing, can we see the player?
var distance = player.get_global_pos().distance_to(sprite.get_global_pos())
if distance < 0.25 * 64:
# critical closeness. no more checks needed.
can_see_player = true
return
var close_enough = distance < view_range * 64
if close_enough:
var angle = sprite.get_global_pos().angle_to_point(player.get_global_pos())
angle *= 180.0 / 3.14159
if angle < 0:
angle += 360.0
var angle_mod = 0
if (direction == 1 and (angle > 315 - angle_mod or angle < 45 + angle_mod)) or \
(direction == 2 and angle > 45 - angle_mod and angle < 135 + angle_mod) or \
(direction == 3 and angle > 135 - angle_mod and angle < 225 + angle_mod) or \
(direction == 0 and angle > 225 - angle_mod and angle < 315 + angle_mod):
var path = path_a_to_b(get_tree().get_nodes_in_group("Nav2D")[0], sprite, player)
var n_old = sprite.get_global_pos()
var distance = 0
if path.size() > 2:
can_see_player = false
else:
can_see_player = true
else:
can_see_player = false
else:
can_see_player = false
func set_direction(new_dir):
if direction != new_dir:
is_alert = false
get_node("AlertCooldown").start()
direction = new_dir
var cone = get_child(1)
cone.set_rot(direction * 3.14159 / 2)
func on_attack():
is_frozen = true
frozen_duration = 0
get_node("FrozenTimer").start()
get_node("AttackedParticles").set_emitting(true)
get_node("AttackedParticles/Timer").start()
func _on_FrozenTimer_timeout():
set_hidden(is_visible())
var cone = get_node("Cone of Vision")
cone.set_hidden(true)
var timer = get_node("FrozenTimer")
frozen_duration += timer.get_wait_time()
if frozen_duration >= frozen_timeout:
var player = get_tree().get_nodes_in_group("Player")[0]
player.lose_power(get_name())
set_hidden(false)
cone.set_hidden(false)
is_frozen = false
timer.stop()
func _on_AttackedTimer_timeout():
get_node("AttackedParticles").set_emitting(false)
func _on_AlertCooldown_timeout():
is_alert = true
func _on_Rotator_timeout():
if not is_frozen:
var new_direction = (direction + 1) % 4
set_direction(new_direction)
get_child(0).idle_ani(direction)