54 lines
1.4 KiB
C#
54 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;
|
|
protected bool IsMoving
|
|
=> Velocity != Vector2.Zero;
|
|
|
|
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).Normalized();
|
|
|
|
Velocity = direction * Speed;
|
|
|
|
if (Input.IsActionPressed(PrimaryAction) && !IsPrimaryOnCooldown)
|
|
{
|
|
_UsePrimary();
|
|
primaryCooldown = PrimaryCooldown;
|
|
}
|
|
else if (IsPrimaryOnCooldown)
|
|
{
|
|
primaryCooldown -= (float)delta;
|
|
}
|
|
}
|
|
}
|