StormyExtraction/scenes/game/entities/player/player.gd
2024-09-09 19:36:34 +02:00

110 lines
3.2 KiB
GDScript

extends "res://scenes/game/entities/entity.gd"
@onready var rollTimer = $RollTimer
@onready var rollCooldownTimer = $RollCooldownTimer
@onready var animatedSprite = $AnimatedSprite2D
@onready var pickupArea = $PickupArea
var rollSpeed = maxSpeed * 5
const rollCooldown = 5.0
const rollTime = 0.4
var rolling = false
var canRoll = true
var carrying = false
var object = null
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 Input.is_action_just_pressed("INTERACT"):
match carrying:
false:
if pickupArea.get_overlapping_areas() != []:
pickup(pickupArea.get_overlapping_areas()[0])
true:
dropObject()
#pickupArea.get_overlapping_areas()[0].pickup(self)
#print(pickupArea.get_overlapping_areas())
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:
dropObject()
canRoll = false
setSpeed(rollSpeed)
invincible = true
rolling = true
rollTimer.start(rollTime)
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")
func _on_roll_timeout() -> void:
resetSpeed()
invincible = false
rolling = false
rollCooldownTimer.start(rollCooldown)
func _on_roll_cooldown_timer_timeout() -> void:
canRoll = true
func pickup(newObject):
if newObject.player == null and not rolling:
carrying = true
object = newObject
speed -= object.getWeight()
object.pickup(self)
func dropObject():
if carrying:
carrying = false
object.drop()
object = null
resetSpeed()