fuzzyworld

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

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

Player.gd

Blame: the commit that last changed each line · file · history

b856550a 2023-10-30 Pavel Safronov1extends KinematicBody2D
f398dcb9 2023-11-02 Pavel Safronov2class_name Player
b856550a 2023-10-30 Pavel Safronov3
57783b1a 2023-11-06 Pavel Safronov4export var move_speed = 100
f398dcb9 2023-11-02 Pavel Safronov5export var health = 10
b856550a 2023-10-30 Pavel Safronov6export var max_health = 10
4872f90d 2023-11-02 Pavel Safronov7export var shot_period_seconds = .5
b856550a 2023-10-30 Pavel Safronov8export var bullet: PackedScene
d99ccd2a 2023-11-04 Pavel Safronov9export var is_invincible = false
57783b1a 2023-11-06 Pavel Safronov10export var turn_rate = 0.03
969fd5af 2023-11-06 Pavel Safronov11export var shooting_range = 500
b856550a 2023-10-30 Pavel Safronov12
f1a0def4 2023-10-31 Pavel Safronov13var PopupControl = preload("res://Popup.tscn")
b856550a 2023-10-30 Pavel Safronov14
15var dead = false
e496c548 2023-11-06 Pavel Safronov16var velocity = Vector2.UP * move_speed
b856550a 2023-10-30 Pavel Safronov17var can_fire = true
f398dcb9 2023-11-02 Pavel Safronov18var destination: Vector2
19var is_destination_set = false
b856550a 2023-10-30 Pavel Safronov20
236d1677 2023-11-01 Pavel Safronov21func _process(_delta: float) -> void:
b856550a 2023-10-30 Pavel Safronov22 if dead:
23 return
c7a34446 2023-10-31 Pavel Safronov24 $Graphics/Alive/Control/HealthLabel.text = "%s / %s" % [health, max_health]
25 $Graphics/Alive/Control.global_rotation = 0
b856550a 2023-10-30 Pavel Safronov26
8c2ffd3f 2023-11-02 Pavel Safronov27func _physics_process(_delta: float) -> void:
b856550a 2023-10-30 Pavel Safronov28 if dead:
29 return
30 velocity = move_and_slide(velocity)
e496c548 2023-11-06 Pavel Safronov31 global_rotation = velocity.angle() + PI/2.0
b856550a 2023-10-30 Pavel Safronov32
33func restart():
236d1677 2023-11-01 Pavel Safronov34 var _error = get_tree().reload_current_scene()
b856550a 2023-10-30 Pavel Safronov35
36func kill():
d99ccd2a 2023-11-04 Pavel Safronov37 if dead or is_invincible:
b856550a 2023-10-30 Pavel Safronov38 return
39 dead = true
40 $Graphics/Dead.show()
41 $Graphics/Alive.hide()
f398dcb9 2023-11-02 Pavel Safronov42 $CanvasLayer/CenterContainer/DeathScreen.show()
b856550a 2023-10-30 Pavel Safronov43 z_index = -1
a670e302 2023-11-07 Pavel Safronov44 get_tree().paused = true
b856550a 2023-10-30 Pavel Safronov45
eb5388a0 2023-11-06 Pavel Safronov46func heal(amount: int = 1):
47 if dead:
48 return
49 health += amount
50 if health > max_health:
51 health = max_health
52
d8d02b36 2023-11-06 Pavel Safronov53func speed_up(delta: int = 10):
54 if dead:
55 return
56 move_speed += delta
57
f398dcb9 2023-11-02 Pavel Safronov58func hurt(damage_amount: int = 1):
d99ccd2a 2023-11-04 Pavel Safronov59 if dead or is_invincible:
b856550a 2023-10-30 Pavel Safronov60 return
a261d90c 2023-11-02 Pavel Safronov61 health -= damage_amount
b856550a 2023-10-30 Pavel Safronov62 if health <= 0:
63 kill()
64
e496c548 2023-11-06 Pavel Safronov65func 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
09081ce6 2023-11-06 Pavel Safronov69 var change = dir_diff.normalized() * turn_rate * move_speed
e496c548 2023-11-06 Pavel Safronov70 velocity = (velocity + change).normalized() * move_speed
71
b856550a 2023-10-30 Pavel Safronov72func 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
f398dcb9 2023-11-02 Pavel Safronov82func 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
b856550a 2023-10-30 Pavel Safronov93func allow_fire():
94 can_fire = true
f398dcb9 2023-11-02 Pavel Safronov95
96func _on_Button_pressed() -> void:
a670e302 2023-11-07 Pavel Safronov97 get_tree().paused = false
f398dcb9 2023-11-02 Pavel Safronov98 restart()