Files
slapstick/Characters/CharacterBase.cs
2026-01-17 18:33:45 -06:00

52 lines
1.4 KiB
C#

using Godot;
public abstract partial class CharacterBase : CharacterBody2D
{
protected const string InputUp = "character_move_up";
protected const string InputDown = "character_move_down";
protected const string InputLeft = "character_move_left";
protected const string InputRight = "character_move_right";
protected const string PrimaryAction = "character_primary_action";
[ExportCategory("Movement")]
[Export] public float Speed { get; set; }
[ExportCategory("Abilities")]
[Export] public float PrimaryCooldown { get; set; }
public float FacingRotation
=> (GetGlobalMousePosition() - GlobalPosition).Angle();
protected bool IsPrimaryOnCooldown
=> primaryCooldown > 0f;
float primaryCooldown;
public override void _PhysicsProcess(double delta)
{
HandleInput(delta);
MoveAndSlide();
}
protected virtual void _UsePrimary()
{
}
void HandleInput(double delta)
{
var direction = Input.GetVector(InputLeft, InputRight, InputUp, InputDown);
Velocity = direction * Speed;
if (Input.IsActionPressed(PrimaryAction) && !IsPrimaryOnCooldown)
{
_UsePrimary();
primaryCooldown = PrimaryCooldown;
}
else if (IsPrimaryOnCooldown)
{
primaryCooldown -= (float)delta;
}
}
}