godot-shooter / change-html-template / files / Player.gd
Branch change-html-template · default branch · all branches
Player.gd
| 1 | extends KinematicBody2D |
| 2 | class_name Player |
| 3 | |
| 4 | export var move_speed = 100 |
| 5 | export var health = 10 |
| 6 | export var max_health = 10 |
| 7 | export var shot_period_seconds = .5 |
| 8 | export var bullet: PackedScene |
| 9 | export var is_invincible = false |
| 10 | export var turn_rate = 0.03 |
| 11 | export var shooting_range = 500 |
| 12 | |
| 13 | var PopupControl = preload("res://Popup.tscn") |
| 14 | |
| 15 | var dead = false |
| 16 | var velocity = Vector2.UP * move_speed |
| 17 | var can_fire = true |
| 18 | var destination: Vector2 |
| 19 | var is_destination_set = false |
| 20 | |
| 21 | func _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 | |
| 27 | func _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 | |
| 33 | func restart(): |
| 34 | var _error = get_tree().reload_current_scene() |
| 35 | |
| 36 | func 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 | |
| 46 | func heal(amount: int = 1): |
| 47 | if dead: |
| 48 | return |
| 49 | health += amount |
| 50 | if health > max_health: |
| 51 | health = max_health |
| 52 | |
| 53 | func speed_up(delta: int = 10): |
| 54 | if dead: |
| 55 | return |
| 56 | move_speed += delta |
| 57 | |
| 58 | func hurt(damage_amount: int = 1): |
| 59 | if dead or is_invincible: |
| 60 | return |
| 61 | health -= damage_amount |
| 62 | if health <= 0: |
| 63 | kill() |
| 64 | |
| 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 |
| 69 | var change = dir_diff.normalized() * turn_rate * move_speed |
| 70 | velocity = (velocity + change).normalized() * move_speed |
| 71 | |
| 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 | |
| 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 | |
| 93 | func allow_fire(): |
| 94 | can_fire = true |
| 95 | |
| 96 | func _on_Button_pressed() -> void: |
| 97 | get_tree().paused = false |
| 98 | restart() |