Side-Scrolling-Shooter/demo/godot/Scripts/player.gd

64 lines
1.7 KiB
GDScript

extends Player
var Invulnerable: bool = false
var InvulnerabilityTimer: float = 0
@export_category("Shooting")
@export var Bullet: PackedScene
var FireRateTimer: float
@export_category("Other")
@export var OnDeathScene: PackedScene
var PlayerControl: bool = true
signal PlayerTookDamage(Health: int)
func _physics_process(delta: float) -> void:
FireRateTimer += delta
# Invulnerability. This is the segment that runs after taking damage.
if Invulnerable == true:
InvulnerabilityTimer += delta
if InvulnerabilityTimer >= InvulnerabilityTime:
Invulnerable = false
InvulnerabilityTimer = 0
if PlayerControl:
# Shooting
if Input.get_action_raw_strength("Shoot") && FireRateTimer >= FireRate:
var bullet = Bullet.instantiate()
bullet.transform = get_node("Gun/Marker2D").global_transform
get_tree().current_scene.add_child(bullet)
FireRateTimer = 0
get_node("Gun/AudioStreamPlayer").play()
# If the player losses control (because they are dead)
else:
@warning_ignore("integer_division")
self.velocity.y = Speed / 2
self.velocity.x = move_toward(velocity.x, 0, Speed)
# Don't use move_and_collide() because it sticks to the walls.
move_and_slide()
# If an enemy collides into the player, they run this function.
func TakeDamage():
Health -= 1
Invulnerable = true
PlayerTookDamage.emit(Health)
get_node("Area2D/AudioStreamPlayer").play()
if Health == 0:
PlayerControl = false
get_node("AnimationPlayer").play("Death")
# Invulnerable so that way you won't be annoyed.
InvulnerabilityTime = 5
Invulnerable = true
else:
get_node("AnimationPlayer").play("Hit")
func _on_area_2d_body_entered(_body: Node2D):
if Invulnerable != true:
TakeDamage()
func GoToScene():
get_tree().change_scene_to_packed(OnDeathScene)