-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy_1.gd
97 lines (65 loc) · 1.76 KB
/
enemy_1.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
extends CharacterBody2D
const speed = 200
var direction = 1
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
const enemy_max_health = 100
var enemy_current_healh = enemy_max_health
var damage = 100
var target_position
var target
var currenthealth = 300
var maxhealth = 300
@onready var raycastleft = $Raycastleft
@onready var raycastright = $Raycastright
@onready var animated_sprite = $AnimatedSprite2D
#@onready var player = get_tree().get_node("player")
enum {MOVE,
ATTACK,
FOLLOW,
}
var state = MOVE
func _physics_process(delta):
match state:
MOVE:
move(delta)
FOLLOW:
follow(delta)
if target != null:
state = FOLLOW
target_position = target.global_position
if raycastright.is_colliding() == true or raycastleft.is_colliding() == true:
state = MOVE
$ProgressBar.value = currenthealth
func _process(delta):
pass
func move(delta):
if raycastright.is_colliding():
direction = -1
animated_sprite.flip_h = true
if raycastleft.is_colliding():
direction = 1
animated_sprite.flip_h = false
position.x += direction * speed * delta
func follow(delta):
direction = (target_position-global_position).normalized()
if raycastright.is_colliding() == false and raycastleft.is_colliding() == false:
position.x += direction.x * speed * delta
else:
target = null
func death():
queue_free()
func damage_taken(hit):
if hit < currenthealth:
currenthealth -= hit
print(currenthealth)
else:
currenthealth = 0
if currenthealth == 0:
death()
$ProgressBar.value = currenthealth
func _on_hitbox_body_entered(body):
if body.is_in_group("player"):
get_tree().call_group("player", "damage_taken", damage)
func _on_detection_body_entered(body):
if body.is_in_group("player"):
target = body