fuzzyworld

godot-shooter / meteor / files / Player.gd

Branch meteor · default branch · all branches

Player.gd

1.7 KB · history · blame

1extends KinematicBody2D
2class_name Player
3
4export var move_speed = 35
5export var health = 10
6export var max_health = 10
7export var shot_period_seconds = .5
8export var bullet: PackedScene
9export var is_invincible = false
10
11var PopupControl = preload("res://Popup.tscn")
12
13var dead = false
14var velocity = Vector2.ZERO
15var can_fire = true
16var destination: Vector2
17var is_destination_set = false
18
19func _process(_delta: float) -> void:
20 if dead:
21 return
22 $Graphics/Alive/Control/HealthLabel.text = "%s / %s" % [health, max_health]
23 $Graphics/Alive/Control.global_rotation = 0
24
25func _physics_process(_delta: float) -> void:
26 if dead:
27 return
28 global_rotation = global_position.direction_to(get_global_mouse_position()).angle() + PI/2.0
29 velocity = move_and_slide(velocity)
30
31func restart():
32 var _error = get_tree().reload_current_scene()
33
34func kill():
35 if dead or is_invincible:
36 return
37 dead = true
38 $Graphics/Dead.show()
39 $Graphics/Alive.hide()
40 $CanvasLayer/CenterContainer/DeathScreen.show()
41 z_index = -1
42
43func hurt(damage_amount: int = 1):
44 if dead or is_invincible:
45 return
46 health -= damage_amount
47 if health <= 0:
48 kill()
49
50func shoot():
51 if !can_fire:
52 return
53 can_fire = false
54 $ShotTimer.wait_time = shot_period_seconds
55 $ShotTimer.start()
56 var b = bullet.instance()
57 owner.add_child(b)
58 b.transform = $Barrel.global_transform
59
60func shoot_at(pos: Vector2):
61 if !can_fire:
62 return
63 can_fire = false
64 $ShotTimer.wait_time = shot_period_seconds
65 $ShotTimer.start()
66 var b = bullet.instance() as Bullet
67 owner.add_child(b)
68 b.global_position = global_position
69 b.global_rotation = global_position.angle_to_point(pos) + PI / 2.0
70
71func allow_fire():
72 can_fire = true
73
74func _on_Button_pressed() -> void:
75 restart()