Files
slopvivors/scenes/player.gd

64 lines
1.7 KiB
GDScript

extends CharacterBody2D
@export var camera: Camera2D
@export var main_ui: MainUI
@export var default_movement_speed: float = 200.0
@export var default_max_health: float = 50.0
@onready var sprite_2d: Sprite2D = $Sprite2D
var movement_speed: float
var max_health: float
var health: float
var dead: bool = false
var death_anim_done: bool = false
var god_mode: bool = false
func _ready() -> void:
camera.position = global_position
movement_speed = default_movement_speed
max_health = default_max_health
health = max_health
main_ui.player_ui.set_hp(100)
GlobalConst.sig_debug_god_mode.connect(toggle_god_mode)
func _physics_process(delta: float) -> void:
if dead:
return
var input_direction = Input.get_vector("left", "right", "up", "down")
velocity = input_direction * movement_speed
move_and_slide()
func _process(delta: float) -> void:
camera.position_smoothing_enabled = true
camera.position = global_position
if dead and !death_anim_done:
death_animation(delta)
func take_damage(value: float) -> void:
if dead or god_mode:
return
health -= value
print_debug("player took damage. now has %s hp" % health)
main_ui.player_ui.set_hp(health / max_health * 100)
if health <= 0:
die()
func die():
dead = true
remove_from_group("damagable")
get_tree().call_group("enemy", "cheer_anim")
sprite_2d.z_index += 10
func death_animation(delta: float):
# Engine.time_scale = clampf(lerpf(Engine.time_scale, 0.25, 0.5), 0.25, 0.9*delta)
camera.zoom = lerp(camera.zoom, Vector2(8,8), 0.99*delta)
rotation_degrees = lerpf(rotation_degrees, 90, 0.99*delta)
if rotation_degrees > 88:
main_ui.pause_ui.toggle_pause_ui()
death_anim_done = true
func toggle_god_mode(value: bool):
god_mode = value