Add player scripts and stuff
This commit is contained in:
97
growth/player.gd
Normal file
97
growth/player.gd
Normal file
@@ -0,0 +1,97 @@
|
||||
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 (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 moveState != MoveState.Dashing:
|
||||
updateMoveDirection();
|
||||
velocity = moveDirection.normalize() * speed;
|
||||
|
||||
if velocity.x || velocity.y:
|
||||
moveState = MoveState.Moving;
|
||||
elif moveState != MoveState.Dashing:
|
||||
moveState = MoveState.Still;
|
||||
elif Input.is_action_pressed("dash") or actionState == MoveState.Dashing:
|
||||
if (moveState != MoveState.Dashing):
|
||||
updateMoveDirection();
|
||||
moveState = MoveState.Dashing;
|
||||
dash(delta);
|
||||
|
||||
# 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();
|
||||
|
||||
Reference in New Issue
Block a user