-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.gd
54 lines (43 loc) · 1.4 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
54
class_name Enemy extends CharacterBody2D
const SPEED = 200.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var direction = -1
var is_dead = false
@onready var ground_areas: Array[Area2D] = [$LeftGround, $RightGround]
func _ready():
$AnimatedSprite2D.play()
for ground_area in ground_areas:
ground_area.body_exited.connect(self.ground_area_check)
func _physics_process(delta):
if is_dead:
return
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
for collision_index in range(get_slide_collision_count()):
var collision = get_slide_collision(collision_index)
var collider = collision.get_collider()
if collider is Player:
collider.kill()
else:
var collision_direction = collision.get_normal().x
if collision_direction != 0 and sign(collision_direction) != sign(direction):
# hit a wall
direction *= -1
func ground_area_check(_body):
var direction_area = ground_areas[0 if direction < 0 else 1]
if len(direction_area.get_overlapping_bodies()) == 0:
direction *= -1
func kill():
$AnimatedSprite2D.animation = "flat"
direction = 0
is_dead = true
$CollisionShape2D.disabled = true
await get_tree().create_timer(1).timeout
queue_free()