/
Player.gd
65 lines (42 loc) · 1.16 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
extends KinematicBody2D
signal death
signal update_health(value)
export var speed = 400
export var health = 100
var velocity = Vector2()
var screen_size
enum PLAYER_COLLISION_SHAPES {WALK_COLLISION = 0, HEALTH_COLLISION = 1}
func _ready():
screen_size = get_viewport_rect().size
func get_input():
velocity = Vector2()
if Input.is_action_pressed('ui_right'):
velocity.x += 1
if Input.is_action_pressed('ui_left'):
velocity.x -= 1
if Input.is_action_pressed('ui_down'):
velocity.y += 1
if Input.is_action_pressed('ui_up'):
velocity.y -= 1
velocity = velocity.normalized() * speed
func _physics_process(delta):
get_input()
velocity = move_and_slide(velocity)
for i in get_slide_count():
var collision = get_slide_collision(i)
velocity = velocity.bounce(collision.normal)#
func start(pos):
position = pos
show()
$HealthCollision.disabled = false
func take_damage():
# print("take_damage")
health -= 10
emit_signal('update_health', health)
if health <= 0:
print('death')
hide()
$WalkCollision.set_deferred("disabled", true)
$HealthCollision.set_deferred("disabled", true)
emit_signal("death")
# position = position - 50