SupaLidlGame/Characters/Character.cs

255 lines
5.7 KiB
C#
Raw Normal View History

2022-11-10 20:29:53 -08:00
using Godot;
2023-06-13 02:55:30 -07:00
using GodotUtilities;
2022-11-25 09:11:46 -08:00
using SupaLidlGame.Extensions;
2022-11-19 21:21:12 -08:00
using SupaLidlGame.Items;
using SupaLidlGame.Utils;
2023-05-25 15:28:33 -07:00
using SupaLidlGame.State.Character;
2022-11-10 20:29:53 -08:00
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.Characters;
public partial class Character : CharacterBody2D, IFaction
2022-11-10 20:29:53 -08:00
{
2023-06-03 18:21:46 -07:00
[Export]
public float Speed { get; protected set; } = 32.0f;
2022-11-12 16:45:04 -08:00
2023-06-03 18:21:46 -07:00
[Export]
public float Friction { get; protected set; } = 4.0f;
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
[Export]
public float Mass
{
get => _mass;
set
2022-11-13 19:52:09 -08:00
{
2023-06-03 18:21:46 -07:00
if (value > 0)
_mass = value;
2022-11-13 19:52:09 -08:00
}
2023-06-03 18:21:46 -07:00
}
2022-11-13 19:52:09 -08:00
2023-06-06 18:39:23 -07:00
[Signal]
public delegate void HurtEventHandler(Events.HealthChangedArgs args);
[Signal]
public delegate void DeathEventHandler(Events.HealthChangedArgs args);
2023-06-03 18:21:46 -07:00
protected float _mass = 1.0f;
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
public Vector2 NetImpulse { get; set; } = Vector2.Zero;
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
public Vector2 Direction { get; set; } = Vector2.Zero;
2022-11-13 15:42:04 -08:00
2023-06-03 18:21:46 -07:00
public Vector2 Target { get; set; } = Vector2.Zero;
2022-11-13 15:42:04 -08:00
2023-06-03 18:21:46 -07:00
[Export]
public float Health
{
get => _health;
set
2022-11-19 21:21:12 -08:00
{
2023-06-03 18:21:46 -07:00
if (!IsAlive && value < 0)
2022-11-19 21:21:12 -08:00
{
2023-06-03 18:21:46 -07:00
return;
}
_health = value;
if (_health <= 0)
{
Die();
2022-11-19 21:21:12 -08:00
}
}
2023-06-03 18:21:46 -07:00
}
2022-11-19 21:21:12 -08:00
2023-06-03 18:21:46 -07:00
public bool IsAlive => Health > 0;
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
protected float _health = 100f;
2022-11-19 21:21:12 -08:00
2023-06-03 18:21:46 -07:00
public double StunTime { get; set; }
2022-11-19 21:21:12 -08:00
2023-06-03 18:21:46 -07:00
[Export]
public AnimatedSprite2D Sprite { get; set; }
2022-11-19 21:21:12 -08:00
2023-06-03 18:21:46 -07:00
[Export]
public Inventory Inventory { get; set; }
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
[Export]
public CharacterStateMachine StateMachine { get; set; }
2022-11-10 20:29:53 -08:00
2023-06-10 22:15:28 -07:00
[Export]
public BoundingBoxes.Hurtbox Hurtbox { get; set; }
2023-06-03 18:21:46 -07:00
[Export]
public ushort Faction { get; set; }
2022-11-19 21:21:12 -08:00
2023-06-10 22:15:28 -07:00
public override void _Ready()
{
Hurtbox.ReceivedDamage += OnReceivedDamage;
}
2023-06-03 18:21:46 -07:00
public override void _Process(double delta)
{
if (StateMachine != null)
2022-11-10 20:29:53 -08:00
{
2023-06-03 18:21:46 -07:00
StateMachine.Process(delta);
2022-11-13 15:42:04 -08:00
}
2023-06-03 18:21:46 -07:00
Sprite.FlipH = Target.X < 0;
DrawTarget();
}
2022-11-10 20:29:53 -08:00
2023-06-03 18:21:46 -07:00
public override void _PhysicsProcess(double delta)
{
if (StateMachine != null)
2022-11-19 21:21:12 -08:00
{
2023-06-03 18:21:46 -07:00
StateMachine.PhysicsProcess(delta);
2022-11-19 21:21:12 -08:00
}
2023-06-03 18:21:46 -07:00
}
2022-11-19 21:21:12 -08:00
2023-06-03 18:21:46 -07:00
/// <summary>
/// Modify the <c>Character</c>'s velocity
/// </summary>
public virtual void ModifyVelocity()
{
if (StunTime > 0)
2022-11-19 21:21:12 -08:00
{
2023-06-03 18:21:46 -07:00
Velocity *= 0.25f;
2022-11-19 21:21:12 -08:00
}
2023-06-03 18:21:46 -07:00
}
public virtual void Die()
{
GD.Print("lol died");
QueueFree();
}
public void ApplyImpulse(Vector2 impulse, bool resetVelocity = false)
{
// delta p = F delta t
if (resetVelocity)
Velocity = Vector2.Zero;
NetImpulse += impulse / Mass;
}
2022-11-19 21:21:12 -08:00
2023-06-03 18:21:46 -07:00
public virtual void Stun(float time)
{
StunTime += time;
}
2023-06-10 22:15:28 -07:00
protected virtual void DrawTarget()
2023-06-03 18:21:46 -07:00
{
Vector2 target = Target;
float angle = Mathf.Atan2(target.Y, Mathf.Abs(target.X));
Vector2 scale = Inventory.Scale;
if (target.X < 0)
2022-11-13 19:52:09 -08:00
{
2023-06-03 18:21:46 -07:00
scale.Y = -1;
angle = Mathf.Pi - angle;
2022-11-25 09:11:46 -08:00
}
2023-06-03 18:21:46 -07:00
else
2022-11-25 09:11:46 -08:00
{
2023-06-03 18:21:46 -07:00
scale.Y = 1;
2022-11-13 19:52:09 -08:00
}
2023-06-03 18:21:46 -07:00
Inventory.Scale = scale;
Inventory.Rotation = angle;
}
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
public void UseCurrentItem()
{
if (StunTime > 0)
2022-11-19 21:21:12 -08:00
{
2023-06-03 18:21:46 -07:00
GD.Print("tried to use weapon but stunned");
return;
2022-11-19 21:21:12 -08:00
}
2023-06-03 18:21:46 -07:00
if (Inventory.SelectedItem is Weapon weapon)
2022-11-25 09:11:46 -08:00
{
2023-06-03 18:21:46 -07:00
weapon.Use();
2022-11-25 09:11:46 -08:00
}
2023-06-03 18:21:46 -07:00
}
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
public void LookTowardsDirection()
{
if (!Direction.IsZeroApprox())
2023-05-28 17:54:48 -07:00
{
2023-06-03 18:21:46 -07:00
Target = Direction;
2023-05-28 17:54:48 -07:00
}
2023-06-03 18:21:46 -07:00
}
2023-05-28 17:54:48 -07:00
2023-06-10 22:15:28 -07:00
public virtual void OnReceivedDamage(
2023-06-03 18:21:46 -07:00
float damage,
Character inflictor,
float knockback,
Vector2 knockbackOrigin = default,
Vector2 knockbackVector = default)
{
2023-06-13 02:55:30 -07:00
if (Health <= 0)
{
return;
}
2023-06-10 22:15:28 -07:00
float oldHealth = Health;
2023-06-03 18:21:46 -07:00
Health -= damage;
// create damage text
var textScene = GD.Load<PackedScene>("res://UI/FloatingText.tscn");
var instance = textScene.Instantiate<UI.FloatingText>();
instance.Text = Mathf.Round(damage).ToString();
instance.GlobalPosition = GlobalPosition;
this.GetAncestor<TileMap>().AddChild(instance);
// apply knockback
Vector2 knockbackDir = knockbackVector;
if (knockbackDir == default)
2022-11-13 19:52:09 -08:00
{
2023-06-03 18:21:46 -07:00
if (knockbackOrigin == default)
2022-11-19 21:21:12 -08:00
{
2023-06-03 18:21:46 -07:00
knockbackOrigin = inflictor.GlobalPosition;
2022-11-19 21:21:12 -08:00
}
2023-06-03 18:21:46 -07:00
knockbackDir = knockbackOrigin.DirectionTo(GlobalPosition);
}
2022-11-25 11:59:55 -08:00
2023-06-03 18:21:46 -07:00
ApplyImpulse(knockbackDir.Normalized() * knockback);
2023-05-28 10:57:23 -07:00
2023-06-03 18:21:46 -07:00
GD.Print("lol");
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
// play damage animation
var anim = GetNode<AnimationPlayer>("FlashAnimation");
if (anim != null)
{
anim.Stop();
anim.Play("Hurt");
}
2022-11-26 14:53:24 -08:00
2023-06-03 18:21:46 -07:00
// if anyone involved is a player, shake their screen
Player plr = inflictor as Player ?? this as Player;
if (plr is not null)
{
plr.Camera.Shake(1, 0.4f);
}
if (this.GetNode("HurtSound") is AudioStreamPlayer2D sound)
{
// very small pitch deviation
2023-06-13 02:55:30 -07:00
sound.At(GlobalPosition).WithPitchDeviation(0.125f).PlayOneShot();
2022-11-13 19:52:09 -08:00
}
2023-06-10 22:15:28 -07:00
Events.HealthChangedArgs args = new Events.HealthChangedArgs
{
Attacker = inflictor,
OldHealth = oldHealth,
NewHealth = Health,
Damage = damage,
};
2023-06-13 02:55:30 -07:00
2023-06-10 22:15:28 -07:00
EmitSignal(SignalName.Hurt, args);
if (Health <= 0)
{
EmitSignal(SignalName.Death, args);
2023-06-13 02:55:30 -07:00
GetNode<GpuParticles2D>("DeathParticles")
.CloneOnWorld<GpuParticles2D>()
.EmitOneShot();
2023-06-10 22:15:28 -07:00
}
2022-11-10 20:29:53 -08:00
}
}