fuzzyworld

godot-shooter / change-html-template / files / Player.gd

Branch change-html-template · default branch · all branches

Player.gd

2.2 KB · history · blame

1extends KinematicBody2D
2class_name Player
3
4export var move_speed = 100
5export var health = 10
6export var max_health = 10
7export var shot_period_seconds = .5
8export var bullet: PackedScene
9export var is_invincible = false
10export var turn_rate = 0.03
11export var shooting_range = 500
12
13var PopupControl = preload("res://Popup.tscn")
14
15var dead = false
16var velocity = Vector2.UP * move_speed
17var can_fire = true
18var destination: Vector2
19var is_destination_set = false
20
21func _process(_delta: float) -> void:
22 if dead:
23 return
24 $Graphics/Alive/Control/HealthLabel.text = "%s / %s" % [health, max_health]
25 $Graphics/Alive/Control.global_rotation = 0
26
27func _physics_process(_delta: float) -> void:
28 if dead:
29 return
30 velocity = move_and_slide(velocity)
31 global_rotation = velocity.angle() + PI/2.0
32
33func restart():
34 var _error = get_tree().reload_current_scene()
35
36func kill():
37 if dead or is_invincible:
38 return
39 dead = true
40 $Graphics/Dead.show()
41 $Graphics/Alive.hide()
42 $CanvasLayer/CenterContainer/DeathScreen.show()
43 z_index = -1
44 get_tree().paused = true
45
46func heal(amount: int = 1):
47 if dead:
48 return
49 health += amount
50 if health > max_health:
51 health = max_health
52
53func speed_up(delta: int = 10):
54 if dead:
55 return
56 move_speed += delta
57
58func hurt(damage_amount: int = 1):
59 if dead or is_invincible:
60 return
61 health -= damage_amount
62 if health <= 0:
63 kill()
64
65func turn_towards(pos: Vector2):
66 var to_dir = (pos - global_position).normalized()
67 var curr_dir = velocity.normalized()
68 var dir_diff = to_dir - curr_dir
69 var change = dir_diff.normalized() * turn_rate * move_speed
70 velocity = (velocity + change).normalized() * move_speed
71
72func shoot():
73 if !can_fire:
74 return
75 can_fire = false
76 $ShotTimer.wait_time = shot_period_seconds
77 $ShotTimer.start()
78 var b = bullet.instance()
79 owner.add_child(b)
80 b.transform = $Barrel.global_transform
81
82func shoot_at(pos: Vector2):
83 if !can_fire:
84 return
85 can_fire = false
86 $ShotTimer.wait_time = shot_period_seconds
87 $ShotTimer.start()
88 var b = bullet.instance() as Bullet
89 owner.add_child(b)
90 b.global_position = global_position
91 b.global_rotation = global_position.angle_to_point(pos) + PI / 2.0
92
93func allow_fire():
94 can_fire = true
95
96func _on_Button_pressed() -> void:
97 get_tree().paused = false
98 restart()