-
Notifications
You must be signed in to change notification settings - Fork 0
/
Possessed.gd
131 lines (112 loc) · 3.2 KB
/
Possessed.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
extends KinematicBody2D
# Declare member variables here. Examples:
var maxLife = 20
var life = maxLife
var damages = 13
var dead = false
var speed = Vector2()
var speedfactor = 150
var followingPlayer = false
var attackAnimPlaying = false
var nextToPlayer = false
var ishurt = false
onready var animations = $AnimationPlayer
var playerBodyID
var direction = "D"
var animHurtName
var animAttackName
var animIdleName
var animDeathName
var animRunningName
# Called when the node enters the scene tree for the first time.
func _ready():
playerBodyID = $"/root/PlayerGlobal".playerBody
$"/root/EnemiesGlobal".enemiesHitboxes.push_back($Attack.get_instance_id())
$"/root/EnemiesGlobal".enemiesDamages.push_back(damages)
updateDirection()
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
z_index = int(position.y)
if life <= 0 :
death()
if not ishurt :
move()
updateDirection()
updateAnimation()
func move():
if followingPlayer and !attackAnimPlaying:
speed = ($"/root/PlayerGlobal".playerPosition - position).normalized()
else :
speed = Vector2()
speed *= speedfactor
move_and_slide(speed)
# position.x += speed.x*speedfactor
# position.y += speed.y*speedfactor
func updateAnimation():
if attackAnimPlaying :
animations.play(animAttackName)
# elif ishurt :
# animations.play(animHurtName)
else :
if (speed.length()==0):
animations.play(animIdleName)
else :
animations.play(animRunningName)
func updateDirection():
if abs(speed.x) > abs(speed.y):
if speed.x<0 :
direction = "L"
if speed.x>0 :
direction = "R"
if abs(speed.x) < abs(speed.y):
if speed.y<0 :
direction = "U"
if speed.y>0 :
direction = "D"
animAttackName = "Attacking ("+direction+")"
animHurtName = "Hurt ("+direction+")"
animIdleName = "Idle ("+direction+")"
animDeathName = "Death ("+direction+")"
animRunningName = "Running ("+direction+")"
func attack():
attackAnimPlaying = true
func getHit():
ishurt = true
animations.play(animHurtName)
Sound.hitEffect()
life -= 6
func death():
if !dead:
$"/root/EnemiesGlobal".killedEnemies += 1
dead = true
animations.play(animDeathName)
func _on_AnimationPlayer_animation_finished(anim_name):
if anim_name == animHurtName:
ishurt = false
if anim_name == animAttackName and !nextToPlayer:
attackAnimPlaying = false
if anim_name == animDeathName :
self.queue_free()
pass # Replace with function body.
func _on_Search_body_entered(body):
if body.get_instance_id() == playerBodyID :
followingPlayer = true
pass # Replace with function body.
func _on_Search_body_exited(body):
if body.get_instance_id() == playerBodyID :
followingPlayer = false
pass # Replace with function body.
func _on_Body_area_shape_entered(area_id, area, area_shape, local_shape):
if (area_id==$"/root/PlayerGlobal".playerHitboxes) :
getHit()
pass # Replace with function body.
func _on_Body_body_entered(body):
if (body.get_instance_id()==$"/root/PlayerGlobal".playerBody) :
attack()
nextToPlayer = true
pass # Replace with function body.
func _on_Body_body_exited(body):
if (body.get_instance_id()==$"/root/PlayerGlobal".playerBody) :
nextToPlayer = false
pass # Replace with function body.