-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.gd
113 lines (95 loc) · 2.57 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
extends CharacterBody2D
const speed = 200
var enemy_inattack_range = false
var enemy_attak_cooldown = true
var player_alive = true
var attack_ip = false
var current_dir = "none"
var canpick = true
var spawn_position : Vector2
var health = 100
var Direction
@onready var animatedSprite : AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(_delta):
World.pos = position
if (velocity.x == 0):
animatedSprite.play("idle")
Direction = Input.get_vector("leftmove", "rightmove", "upmove", "downmove")
if (Direction != Vector2.ZERO):
velocity.x = Direction.x * speed
velocity.y = Direction.y * speed
if (velocity.x < 0):
current_dir = "going"
animatedSprite.play("going")
$sword.rotation_degrees = 180
animatedSprite.flip_h = true
if (velocity.x > 0):
current_dir = "going"
animatedSprite.play("going")
$sword.rotation_degrees = 0
animatedSprite.flip_h = false
if (velocity.y < 0):
current_dir = "up"
animatedSprite.play("up")
animatedSprite.flip_h = false
if (velocity.y > 0):
current_dir = "down"
animatedSprite.play("down")
animatedSprite.flip_h = false
else:
velocity.x= move_toward(velocity.x, 0, speed)
velocity.y= move_toward(velocity.y, 0, speed)
move_and_slide()
enemy_attack()
attack()
update_health()
if health == 0:
player_alive = false
World.player_health = 0
print("player has been killed")
get_tree().change_scene_to_file("res://panel.tscn")
func player():
pass
func update_health():
var healthbar = $ProgressBar
healthbar.value = health
if health >= 100:
healthbar.visible = false
else:
healthbar.visible = true
func _on_timer_timeout():
if health < 100:
health = health + 20
if health > 100:
health = 100
if health <= 0:
health = 0
func _on_player_hitbox_body_entered(body):
if body.has_method("enemy"):
enemy_inattack_range = true
func _on_player_hitbox_body_exited(body):
if body.has_method("enemy"):
enemy_inattack_range = false
func enemy_attack():
if enemy_inattack_range and enemy_attak_cooldown == true:
health = health - 10
enemy_attak_cooldown = false
$attack_cooldown.start()
print(health)
func _on_attack_cooldown_timeout():
enemy_attak_cooldown = true
func attack():
var air = current_dir
if Input.is_action_just_pressed("attack"):
World.player_current_attack = true
attack_ip = true
if air == "going":
$deal_attack_timer.start()
if air == "down":
$deal_attack_timer.start()
if air == "up":
$deal_attack_timer.start()
func _on_deal_attack_timer_timeout():
$deal_attack_timer.stop()
World.player_current_attack = false
attack_ip = false