SupaLidlGame/Characters/Character.cs

76 lines
1.8 KiB
C#
Raw Normal View History

2022-11-10 20:29:53 -08:00
using Godot;
namespace SupaLidlGame.Characters
{
public partial class Character : CharacterBody2D
{
2022-11-12 16:45:04 -08:00
[Export]
2022-11-18 13:53:51 -08:00
public float Speed { get; protected set; } = 32.0f;
2022-11-12 16:45:04 -08:00
2022-11-13 19:52:09 -08:00
[Export]
public float Mass
{
get => _mass;
set
{
if (value > 0)
_mass = value;
}
}
protected float _mass = 1.0f;
2022-11-10 20:29:53 -08:00
public float JumpVelocity { get; protected set; } = -400.0f;
2022-11-13 19:52:09 -08:00
2022-11-10 20:29:53 -08:00
public float AccelerationMagnitude { get; protected set; } = 256.0f;
public Vector2 Acceleration => Direction * AccelerationMagnitude;
2022-11-13 15:42:04 -08:00
public Vector2 Direction { get; set; } = Vector2.Zero;
public Vector2 Target { get; set; } = Vector2.Zero;
2022-11-13 19:52:09 -08:00
public float Health { get; set; }
2022-11-13 15:42:04 -08:00
[Export]
public State.Machine StateMachine { get; set; }
2022-11-10 20:29:53 -08:00
public override void _Process(double delta)
{
2022-11-13 15:42:04 -08:00
if (StateMachine != null)
{
StateMachine.Process(delta);
}
}
public override void _Input(InputEvent @event)
{
if (StateMachine != null)
{
StateMachine.Input(@event);
}
2022-11-10 20:29:53 -08:00
}
public override void _PhysicsProcess(double delta)
{
2022-11-13 15:42:04 -08:00
if (StateMachine != null)
{
StateMachine.PhysicsProcess(delta);
}
2022-11-10 20:29:53 -08:00
}
2022-11-13 19:52:09 -08:00
public void ApplyImpulse(Vector2 impulse, bool resetVelocity = false)
{
// delta p = F delta t
if (resetVelocity)
Velocity = Vector2.Zero;
Velocity += impulse / Mass;
}
public void _on_hurtbox_received_damage(float damage)
{
Health -= damage;
}
2022-11-10 20:29:53 -08:00
}
}