-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathEnemy.gd
53 lines (43 loc) · 1.24 KB
/
Enemy.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
extends Area2D
signal explode
signal shoot
export (PackedScene) var bullet
var follow
var target = null
var health = global.enemy_health
var pulse_timer
func _ready():
var path = $EnemyPaths.get_children()[randi() % $EnemyPaths.get_child_count()]
follow = PathFollow2D.new()
path.add_child(follow)
follow.set_loop(false)
$ShootTimer.wait_time = 1.5 # TODO: vary by level
$ShootTimer.start()
func _process(delta):
follow.set_offset(follow.get_offset() + global.enemy_speed * delta)
position = follow.global_position
if follow.get_unit_offset() > 1:
queue_free()
func take_damage(amount):
$HitSound.play()
health -= amount
$AnimationPlayer.play("hit")
if health <= 0:
emit_signal("explode", global_position)
queue_free()
func shoot1():
var dir = (target.global_position - global_position).rotated(rand_range(-global.enemy_accuracy, global.enemy_accuracy))
$LaserSound.play()
emit_signal("shoot", bullet, global_position, dir.angle())
func _on_ShootTimer_timeout():
if target.is_visible():
shoot_pulse(2, 0.2)
func shoot_pulse(n, delay):
for i in range(n):
shoot1()
pulse_delay(delay)
yield($PulseTimer, "timeout")
func pulse_delay(delay):
$PulseTimer.wait_time = delay
$PulseTimer.process_mode = 0
$PulseTimer.start()