-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.gd
191 lines (158 loc) · 4.64 KB
/
player.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
extends CharacterBody2D
signal hit
signal died
signal heal
signal victory
@export var SPEED = 80 # How fast the player will move (pixels/sec).
@export var KB_DIST = 3
@export var KB_DURATION = 16
@export var KB_REDUCTION = 0.99
var screen_size # Size of the game window.
var knockback_velocity = Vector2(0,0)
var knockback_counter = 0
var MAX_HEALTH = 0
var health = MAX_HEALTH
var power = 0
var swingReady = true
var swingUp = false
var swingDown = false
var lockDirection = false
var lookingLeft = false
var paused = false
func pause():
paused = true
func unpause():
paused = false
# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
$SwordSwing/SwordCollision.set_deferred("disabled",true)
func _process(delta):
if paused:
return
if health <= 0:
$BodySpriteAnimation.play()
$BodySpriteAnimation.animation = "death"
if $BodySpriteAnimation.frame == 21:
hide()
return
if(swingReady == true):
$SwordSwing/SwordCollision.set_deferred("disabled",true)
velocity = Vector2.ZERO # The player's movement vector.
if Input.is_action_pressed("move_right"):
velocity.x += 1
if not lockDirection:
lookingLeft = false
if Input.is_action_pressed("move_left"):
velocity.x -= 1
if not lockDirection:
lookingLeft = true
if Input.is_action_pressed("move_down"):
velocity.y += 1
if Input.is_action_pressed("move_up"):
velocity.y -= 1
if Input.is_action_just_pressed("Attack") and swingReady == true:
lockDirection = true
swingReady = false
swingUp = true
swordSwingAnimation(delta)
$BodySpriteAnimation.play()
if velocity.length() > 0:
velocity = velocity.normalized() * SPEED
if velocity.length() > 0:
$BodySpriteAnimation.animation = "walk"
else:
$BodySpriteAnimation.animation = "idle"
if velocity.x != 0 and lockDirection == false:
# See the note below about boolean assignment.
$BodySpriteAnimation.flip_h = velocity.x < 0
$SwordSprite.flip_h = velocity.x < 0
if velocity.x > 0:
$SwordSprite.position.x = -6
elif velocity.x < 0:
$SwordSprite.position.x = 6
if knockback_counter > 0:
knockback_counter -= 1
velocity += knockback_velocity
knockback_velocity *= KB_REDUCTION
move_and_slide()
func swordSwingAnimation(delta):
var swingSpeed = 1
if $BodySpriteAnimation.flip_h:
if swingUp:
if !$swordSlash.playing:
$swordSlash.play()
$SwordSprite.rotation += delta * 20 * swingSpeed
if $SwordSprite.rotation > .75 * PI:
swingUp = false
swingDown = true
if swingDown:
$SwordSprite.play()
$SwordSprite.animation = "swing"
$SwordSprite.rotation -= delta * 30 * swingSpeed
if $SwordSprite.rotation < 0:
$SwordSprite.rotation = 0
swingDown = false
swingReady = true
lockDirection = false
swordSwingCollision(delta)
else:
if swingUp:
if !$swordSlash.playing:
$swordSlash.play()
$SwordSprite.rotation -= delta * 20 * swingSpeed
if $SwordSprite.rotation < -.75 * PI:
swingUp = false
swingDown = true
if swingDown:
$SwordSprite.play()
$SwordSprite.animation = "swing"
$SwordSprite.rotation += delta * 30 * swingSpeed
if $SwordSprite.rotation > 0:
$SwordSprite.rotation = 0
swingDown = false
swingReady = true
lockDirection = false
swordSwingCollision(delta)
func swordSwingCollision(delta):
if(lookingLeft):
$SwordSwing/SwordCollision.scale.x=-1
else:
$SwordSwing/SwordCollision.scale.x=1
$SwordSwing/SwordCollision.set_deferred("disabled",false)
func damaged(origin, damage, KBbool):
if knockback_counter > 0:
return
health -= damage
if health <= 0:
# But first do a nice animation
died.emit()
# Probably freeze the game here
if KBbool:
var knockback = (position - origin)
knockback_velocity = knockback.normalized() * KB_DIST * SPEED
knockback_counter = KB_DURATION
hit.emit()
var tween: Tween = create_tween()
tween.tween_property($BodySpriteAnimation, "modulate:v", 1, 0.25).from(15)
func healed(healAmount):
if health < MAX_HEALTH:
health += healAmount
if health > MAX_HEALTH:
health = MAX_HEALTH
hit.emit()
$heal.play()
func gainCoin(coinAmount):
get_parent().get_parent().crawling_coins += 5
get_parent().get_parent().updateCoins()
$coin.play()
func _on_sword_swing_area_entered(area):
if area.is_in_group("Enemy"):
area.damaged(position, power)
$SwordSwing/SwordCollision.set_deferred("disabled",true)
func _on_sword_swing_body_entered(body):
if body.is_in_group("HeroCrawling"):
body.damaged(position, power,true)
$SwordSwing/SwordCollision.set_deferred("disabled",true)
func _on_sword_swing_area_shape_exited(area_rid, area, area_shape_index, local_shape_index):
pass # Replace with function body.