SolokNightAdventures/scripts/player.gd
2025-02-15 17:36:04 +01:00

100 lines
2.3 KiB
GDScript

extends CharacterBody2D
@onready var healthbar: ProgressBar = $ui/Healthbar
var health
var speed
var jump_velocity
var jumpcount
const ROLLSPEED = 2
var is_rolling = false
@onready var jump: AudioStreamPlayer2D = $Jump
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
func _ready() -> void:
health = 100
speed = 130.0
jump_velocity = -300
jumpcount = 0
healthbar.init_health(health)
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
if Input.is_action_pressed("move_down"):
velocity += get_gravity() * delta * 1.7
elif velocity.y > 0:
velocity += get_gravity() * delta * 1.3
else:
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_released("jump") and velocity.y<0:
velocity.y = jump_velocity/4
# Doublejump
if jumpcount == 1 and Input.is_action_just_pressed("jump"):
velocity.y = jump_velocity
jump.play()
jumpcount = 2
# Jump
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
jump.play()
jumpcount = 1
# Doublejump reset
if !Input.is_action_just_pressed("jump") and is_on_floor():
jumpcount = 0
# Fall through
if Input.is_action_pressed("move_down"):
set_collision_mask_value(2, false)
else:
set_collision_mask_value(2, true)
# Get the input direction: -1, 0, 1
var direction := Input.get_axis("move_left", "move_right")
# Flip the Sprite
if direction > 0 :
animated_sprite.flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
# Play animations
if is_on_floor():
if Input.is_action_pressed("move_down") and direction == 0:
animated_sprite.play("crouch")
elif direction == 0:
animated_sprite.play("idle")
elif is_rolling:
animated_sprite.play("roll")
else:
animated_sprite.play("run")
else:
animated_sprite.play("jump")
# Apply Movement
# Roll
if Input.is_action_just_pressed("roll"):
if !is_rolling and direction and is_on_floor():
start_dash()
if direction:
if is_rolling:
velocity.x = direction * speed * ROLLSPEED
# Running
else:
velocity.x = direction * speed
else:
velocity.x = move_toward(velocity.x, 0, speed)
move_and_slide()
func start_dash():
is_rolling = true
$RollTimer.connect("timeout", stop_dash)
$RollTimer.start()
func stop_dash():
is_rolling = false
#Health
func _process(delta: float) -> void:
healthbar._set_health(health)