StormyExtraction/scenes/game/entities/player/player.gd
2024-09-08 23:03:55 +02:00

78 lines
2.6 KiB
GDScript

extends "res://scenes/game/entities/entity.gd"
@onready var rollTimer = $RollTimer
@onready var rollCooldownTimer = $RollCooldownTimer
@onready var animatedSprite = $AnimatedSprite2D
var rollSpeed = maxSpeed * 5
const rollCooldown = 5.0
const rollTime = 0.4
var rolling = false
var canRoll = true
var lastDirection : Vector2
var lastdirectionVector : Vector2
func _physics_process(delta: float) -> void:
var direction : Vector2 = Input.get_vector("MOVE_LEFT", "MOVE_RIGHT", "MOVE_UP", "MOVE_DOWN").normalized()
#var direction : Vector2 = Vector2(
#Input.get_action_strength("MOVE_RIGHT") - Input.get_action_strength("MOVE_LEFT"),
#Input.get_action_strength("MOVE_DOWN") - Input.get_action_strength("MOVE_UP")
#)
var directionVector : Vector2 = position.direction_to(position + direction)
if direction != Vector2.ZERO and not rolling:
lastDirection = direction
lastdirectionVector = directionVector
if Input.is_action_just_pressed("ROLL") and lastDirection != Vector2.ZERO:
roll()
if rolling:
move(lastdirectionVector * speed, acceleration)
else:
move(directionVector * speed, acceleration)
setAnimation()
move_and_slide()
func move(newVelocity : Vector2, acc):
velocity.x = move_toward(velocity.x,
newVelocity.x,
acc)
velocity.y = move_toward(velocity.y,
newVelocity.y,
acc)
func roll():
if not rolling and canRoll:
canRoll = false
setSpeed(rollSpeed)
invincible = true
rolling = true
rollTimer.start(rollTime)
func _on_roll_timeout() -> void:
resetSpeed()
invincible = false
rolling = false
rollCooldownTimer.start(rollCooldown)
func _on_roll_cooldown_timer_timeout() -> void:
canRoll = true
func setAnimation():
if not rolling:
if Input.is_action_pressed("MOVE_LEFT") and not Input.is_action_pressed("MOVE_RIGHT") and animatedSprite.animation != "LEFT":
animatedSprite.play("LEFT")
elif Input.is_action_pressed("MOVE_RIGHT") and not Input.is_action_pressed("MOVE_LEFT") and animatedSprite.animation != "RIGHT":
animatedSprite.play("RIGHT")
elif Input.is_action_pressed("MOVE_UP") and animatedSprite.animation != "UP" and not Input.is_action_pressed("MOVE_LEFT") and not Input.is_action_pressed("MOVE_RIGHT"):
animatedSprite.play("UP")
elif Input.is_action_pressed("MOVE_DOWN") and animatedSprite.animation != "DOWN" and not Input.is_action_pressed("MOVE_LEFT") and not Input.is_action_pressed("MOVE_RIGHT"):
animatedSprite.play("DOWN")
elif not (Input.is_action_pressed("MOVE_UP") or Input.is_action_pressed("MOVE_RIGHT") or Input.is_action_pressed("MOVE_DOWN") or Input.is_action_pressed("MOVE_LEFT")):
animatedSprite.play("IDLE")