-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.gd
86 lines (66 loc) · 1.94 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
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
class_name Enemy
extends KinematicBody2D
# Simple, base enemy entity
signal die
export(int, 0, 200) var speed := 30
export(int, 0, 500) var gravity := 200
export(int, 1, 5) var base_hp := 1
export(Vector2) var size := Vector2(1, 1)
var is_dead := false
var _direction := Vector2.ZERO
var _velocity := Vector2.ZERO
var _hp := 1
onready var animation := $AnimatedSprite as AnimatedSprite
onready var ground_check := $RayCast2D as RayCast2D
onready var death_timer := $DecayTimer as Timer
onready var collider := $CollisionShape2D as CollisionShape2D
onready var screen_size := get_viewport_rect().size
func _ready():
_direction.x = -1
ground_check.position.x = -abs(ground_check.position.x)
# init run-time stat
_hp = base_hp
self.scale = size
func _physics_process(delta: float) -> void:
# update move
self._update_move(delta)
_velocity = move_and_slide(_velocity, Vector2.UP)
# handle slide:
for index in range(get_slide_count()):
var body: Node = get_slide_collision(index).collider as Node
if body.is_in_group("player"):
body.dead()
return
func _on_DecayTimer_timeout():
call_deferred("queue_free")
func _update_move(delta: float) -> void:
# turn on contracting walls or ledges
if is_on_wall() or not ground_check.is_colliding():
_direction.x *= -1
ground_check.position.x *= -1
_velocity.x = speed * _direction.x
_velocity.y += gravity * delta
animation.flip_h = _direction.x < 0
animation.play("walk")
func take_damage(amount: int) -> void:
if (amount <= 0):
return
_hp -= amount
if _hp <= 0:
self.dead()
func dead() -> void:
if is_dead:
return
is_dead = true
# freeze
set_physics_process(false)
# turn off collider --> can pass
collider.set_deferred("disabled", true)
animation.play("dead")
# just in-case manager class need to keep track
emit_signal("die")
# schedule destroy
death_timer.start()
# if big, shake screen
if self.scale > Vector2(1, 1):
get_node("%ScreenShake").shake_screen(1, 10, 100)