godot-shooter / change-html-template / files / Player.gd
Branch change-html-template · default branch · all branches
Player.gd
b856550a 2023-10-30 Pavel Safronov | 1 | extends KinematicBody2D |
f398dcb9 2023-11-02 Pavel Safronov | 2 | class_name Player |
b856550a 2023-10-30 Pavel Safronov | 3 | |
57783b1a 2023-11-06 Pavel Safronov | 4 | export var move_speed = 100 |
f398dcb9 2023-11-02 Pavel Safronov | 5 | export var health = 10 |
b856550a 2023-10-30 Pavel Safronov | 6 | export var max_health = 10 |
4872f90d 2023-11-02 Pavel Safronov | 7 | export var shot_period_seconds = .5 |
b856550a 2023-10-30 Pavel Safronov | 8 | export var bullet: PackedScene |
d99ccd2a 2023-11-04 Pavel Safronov | 9 | export var is_invincible = false |
57783b1a 2023-11-06 Pavel Safronov | 10 | export var turn_rate = 0.03 |
969fd5af 2023-11-06 Pavel Safronov | 11 | export var shooting_range = 500 |
b856550a 2023-10-30 Pavel Safronov | 12 | |
f1a0def4 2023-10-31 Pavel Safronov | 13 | var PopupControl = preload("res://Popup.tscn") |
b856550a 2023-10-30 Pavel Safronov | 14 | |
| 15 | var dead = false | |
e496c548 2023-11-06 Pavel Safronov | 16 | var velocity = Vector2.UP * move_speed |
b856550a 2023-10-30 Pavel Safronov | 17 | var can_fire = true |
f398dcb9 2023-11-02 Pavel Safronov | 18 | var destination: Vector2 |
| 19 | var is_destination_set = false | |
b856550a 2023-10-30 Pavel Safronov | 20 | |
236d1677 2023-11-01 Pavel Safronov | 21 | func _process(_delta: float) -> void: |
b856550a 2023-10-30 Pavel Safronov | 22 | if dead: |
| 23 | return | |
c7a34446 2023-10-31 Pavel Safronov | 24 | $Graphics/Alive/Control/HealthLabel.text = "%s / %s" % [health, max_health] |
| 25 | $Graphics/Alive/Control.global_rotation = 0 | |
b856550a 2023-10-30 Pavel Safronov | 26 | |
8c2ffd3f 2023-11-02 Pavel Safronov | 27 | func _physics_process(_delta: float) -> void: |
b856550a 2023-10-30 Pavel Safronov | 28 | if dead: |
| 29 | return | |
| 30 | velocity = move_and_slide(velocity) | |
e496c548 2023-11-06 Pavel Safronov | 31 | global_rotation = velocity.angle() + PI/2.0 |
b856550a 2023-10-30 Pavel Safronov | 32 | |
| 33 | func restart(): | |
236d1677 2023-11-01 Pavel Safronov | 34 | var _error = get_tree().reload_current_scene() |
b856550a 2023-10-30 Pavel Safronov | 35 | |
| 36 | func kill(): | |
d99ccd2a 2023-11-04 Pavel Safronov | 37 | if dead or is_invincible: |
b856550a 2023-10-30 Pavel Safronov | 38 | return |
| 39 | dead = true | |
| 40 | $Graphics/Dead.show() | |
| 41 | $Graphics/Alive.hide() | |
f398dcb9 2023-11-02 Pavel Safronov | 42 | $CanvasLayer/CenterContainer/DeathScreen.show() |
b856550a 2023-10-30 Pavel Safronov | 43 | z_index = -1 |
a670e302 2023-11-07 Pavel Safronov | 44 | get_tree().paused = true |
b856550a 2023-10-30 Pavel Safronov | 45 | |
eb5388a0 2023-11-06 Pavel Safronov | 46 | func 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 Safronov | 53 | func speed_up(delta: int = 10): |
| 54 | if dead: | |
| 55 | return | |
| 56 | move_speed += delta | |
| 57 | ||
f398dcb9 2023-11-02 Pavel Safronov | 58 | func hurt(damage_amount: int = 1): |
d99ccd2a 2023-11-04 Pavel Safronov | 59 | if dead or is_invincible: |
b856550a 2023-10-30 Pavel Safronov | 60 | return |
a261d90c 2023-11-02 Pavel Safronov | 61 | health -= damage_amount |
b856550a 2023-10-30 Pavel Safronov | 62 | if health <= 0: |
| 63 | kill() | |
| 64 | ||
e496c548 2023-11-06 Pavel Safronov | 65 | func 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 Safronov | 69 | var change = dir_diff.normalized() * turn_rate * move_speed |
e496c548 2023-11-06 Pavel Safronov | 70 | velocity = (velocity + change).normalized() * move_speed |
| 71 | ||
b856550a 2023-10-30 Pavel Safronov | 72 | func 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 Safronov | 82 | func 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 Safronov | 93 | func allow_fire(): |
| 94 | can_fire = true | |
f398dcb9 2023-11-02 Pavel Safronov | 95 | |
| 96 | func _on_Button_pressed() -> void: | |
a670e302 2023-11-07 Pavel Safronov | 97 | get_tree().paused = false |
f398dcb9 2023-11-02 Pavel Safronov | 98 | restart() |