Files
hsgj25-growth/growth/player.gd
2025-10-04 12:51:45 +02:00

97 lines
2.4 KiB
GDScript

extends Node2D
# ActionState should probably be expandable to
enum ActionState {None, Deploying, Firing, Charging}
enum MoveState {Still, Moving, Dashing, Loading}
@export var speed = 100 # How fast the player will move (pixels/sec).
@export var dashSpeed = 200
@export var dashEnd = 0.8
@export var dashCooldown = 0.5
var screen_size # Size of the game window.
var actionState = ActionState.None
var moveState = MoveState.Still
var chargeLevel = 0;
var chargeRate = 1;
var charged = false;
var dashTime = 0;
var dashCooldownTime = 0;
var dashOnCooldown = false;
var dashEaseFactorMinimum = speed / float(dashSpeed)
var moveDirection = Vector2.ZERO;
var velocity = Vector2.ZERO # The player's movement vector.
func updateMoveDirection():
if Input.is_action_pressed("move_right"):
moveDirection.x += 1
if moveDirection.is_action_pressed("move_left"):
moveDirection.x -= 1
if Input.is_action_pressed("move_down"):
moveDirection.y += 1
if Input.is_action_pressed("move_up"):
moveDirection.y -= 1
func charge():
# Charge attack
if not charged and actionState == ActionState.Charging:
chargeLevel += chargeRate;
if chargeLevel >= 100:
charged = false;
chargeLevel = 0;
elif chargeLevel > 0:
# Gradual charge dropoff
chargeLevel = max(chargeLevel - chargeRate * 2, 0);
func dash(delta):
dashTime += delta;
const easeFactor = 1;
velocity = moveDirection.normalize() * dashSpeed * easeFactor;
if (dashTime >= dashEnd):
dashOnCooldown = true;
dashTime = 0;
func _ready():
screen_size = get_viewport_rect().size
func _process(delta):
if (moveState != MoveState.Dashing):
updateMoveDirection();
if (dashOnCooldown):
dashCooldownTime += delta;
if (dashCooldownTime >= dashCooldown):
dashOnCooldown = false;
dashCooldownTime = 0;
# handle moveState
if moveState == MoveState.Loading:
null
# Idk dude, thought it might be a fun effect when you take damage
elif Input.is_action_pressed("dash") or actionState == MoveState.Dashing:
moveState = MoveState.Dashing;
dash(delta);
else:
velocity = moveDirection.normalize() * speed;
if velocity.x || velocity.y:
moveState = MoveState.Moving;
else:
moveState = MoveState.Still;
# Handle actionState
if Input.is_action_pressed("deploy"):
actionState = ActionState.Deploying;
# Deploy
elif Input.is_action_pressed("fire"):
actionState = ActionState.Firing;
# Fire
elif Input.is_action_pressed("charge"):
actionState = ActionState.Charging;
charge();