-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.gd
37 lines (26 loc) · 933 Bytes
/
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
extends CharacterBody2D
class_name Enemy
enum STATE { PATROL, CHASE }
@export var patrol_speed = 50
@export var chase_speed = 70
@export var patrol_path: Curve2D
@onready var sprite := $AnimatedSprite2D
@onready var raycast := $PlayerRaycast
@onready var nav_agent := $NavigationAgent2D
var state = STATE.PATROL
func _physics_process(delta: float) -> void:
raycast.look_at(Globals.player.position)
match state:
STATE.PATROL: patrol_state(delta)
STATE.CHASE: chase_state()
func patrol_state(_delta: float):
if raycast.get_collider() is Player:
state = STATE.CHASE
func chase_state():
var direction = to_local(nav_agent.get_next_path_position()).normalized()
velocity = direction * chase_speed
sprite.flip_h = direction.x < 0
if not raycast.get_collider() is Player: state = STATE.PATROL
move_and_slide()
func _on_navigation_timer_timeout() -> void:
nav_agent.target_position = Globals.player.global_position