65 lines
2 KiB
GDScript
65 lines
2 KiB
GDScript
extends CharacterBody2D
|
|
|
|
@export var speed = 200
|
|
@export_range(0, 1) var damping_factor = 0.6
|
|
@export var animation_player : AnimationPlayer
|
|
@export var end_screen_displayer : Node
|
|
@export var health_label : Label
|
|
|
|
var health : float = 100
|
|
|
|
func _ready():
|
|
health = 100
|
|
|
|
func _physics_process(delta : float):
|
|
update_player_movement(delta)
|
|
pass
|
|
|
|
func get_move_input_vector() -> Vector2:
|
|
var input_direction = Input.get_vector("move_left", "move_right", "move_up", "move_down").normalized()
|
|
var input_direction_controller = Input.get_vector("move_left_controller", "move_right_controller", "move_up_controller", "move_down_controller")
|
|
return (input_direction if not input_direction_controller.length() > input_direction.length() else input_direction_controller.normalized())
|
|
|
|
func update_player_movement(delta : float):
|
|
var input = get_move_input_vector()
|
|
|
|
# Damp Movement if not moving, Accelerate if Moving
|
|
var is_moving = input != Vector2.ZERO
|
|
self.velocity = input * speed * delta * 60 if is_moving else self.velocity * (1 - min(1, damping_factor * 60 * delta))
|
|
|
|
update_player_animation()
|
|
|
|
# Update Objects Physics calculations
|
|
self.move_and_slide()
|
|
pass
|
|
|
|
func take_damage(dmg : float):
|
|
if health >= dmg and health >= 0:
|
|
health -= dmg
|
|
health_label.text = "%d" % [health]
|
|
else:
|
|
health_label.text = "You Died!"
|
|
die()
|
|
|
|
|
|
func die():
|
|
end_screen_displayer.call("show_loss_screen")
|
|
get_tree().paused = true
|
|
pass
|
|
|
|
func update_player_animation():
|
|
match get_move_input_vector():
|
|
Vector2.ZERO: animation_player.play("Idle")
|
|
_:
|
|
var left_dot = Vector2.LEFT.dot(self.velocity)
|
|
var right_dot = Vector2.RIGHT.dot(self.velocity)
|
|
var up_dot = Vector2.UP.dot(self.velocity)
|
|
var down_dot = Vector2.DOWN.dot(self.velocity)
|
|
|
|
var max_dot = maxf(left_dot, maxf(right_dot, maxf(up_dot, down_dot)))
|
|
match max_dot:
|
|
left_dot: animation_player.play("MoveLeft")
|
|
right_dot: animation_player.play("MoveRight")
|
|
up_dot: animation_player.play("MoveUp")
|
|
down_dot: animation_player.play("MoveDown")
|
|
pass
|