SupaLidlGame/Characters/Character.cs

319 lines
7.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]
2023-07-23 23:39:20 -07:00
public float Friction { get; 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;
2023-07-22 20:23:48 -07:00
[Export]
public Texture2D HandTexture { get; set; }
2022-11-13 15:42:04 -08:00
2023-06-03 18:21:46 -07:00
[Export]
2023-07-22 20:23:48 -07:00
public virtual float Health
2023-06-03 18:21:46 -07:00
{
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]
2023-07-21 02:54:13 -07:00
public Sprite2D 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-07-21 02:54:13 -07:00
public AnimationPlayer MovementAnimation { get; set; }
public AnimationPlayer HurtAnimation { get; set; }
2023-07-23 23:39:20 -07:00
public AnimationPlayer StunAnimation { get; set; }
2023-06-10 22:15:28 -07:00
public override void _Ready()
{
2023-07-21 02:54:13 -07:00
// TODO: 80+ char line
MovementAnimation = GetNode<AnimationPlayer>("Animations/Movement");
HurtAnimation = GetNode<AnimationPlayer>("Animations/Hurt");
2023-07-23 23:39:20 -07:00
StunAnimation = GetNode<AnimationPlayer>("Animations/Stun");
2023-07-21 02:54:13 -07:00
GD.Print(Name + " " + MovementAnimation.CurrentAnimation);
2023-06-10 22:15:28 -07:00
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-07-23 23:39:20 -07:00
if (StunTime > 0 && !StunAnimation.IsPlaying())
{
StunAnimation.Play("stun");
}
else if (StunTime < 0 && StunAnimation.IsPlaying())
{
StunAnimation.Stop();
}
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-07-23 23:39:20 -07:00
var state = StateMachine.CurrentState;
if (state is State.Character.CharacterDashState dashState)
{
Velocity *= dashState.VelocityModifier;
}
// TODO: make PlayerRollState a CharacterRollState instead
else if (state is State.Character.PlayerRollState rollState)
{
Velocity *= 2;
//Velocity *= rollState.VelocityModifier;
}
2023-06-03 18:21:46 -07:00
}
public virtual void Die()
{
2023-07-31 01:12:47 -07:00
if (HurtAnimation.HasAnimation("death"))
{
HurtAnimation.Play("death");
HurtAnimation.AnimationFinished += (StringName name) =>
QueueFree();
}
else
{
QueueFree();
}
2023-06-03 18:21:46 -07:00
}
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)
{
2023-07-23 23:39:20 -07:00
StunTime = Mathf.Max(time, StunTime);
2023-06-03 18:21:46 -07:00
}
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()
{
2023-07-31 01:12:47 -07:00
if (StunTime > 0 || !IsAlive)
2022-11-19 21:21:12 -08:00
{
2023-06-03 18:21:46 -07:00
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();
2023-07-21 02:54:13 -07:00
if (weapon.IsUsing)
{
Inventory.EmitSignal(Inventory.SignalName.UsedItem, weapon);
}
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-07-23 23:39:20 -07:00
public void DeuseCurrentItem()
{
if (Inventory.SelectedItem is Weapon weapon)
{
weapon.Deuse();
// TODO: DeusedItem signal, implement when needed
}
}
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-07-22 20:23:48 -07:00
protected virtual float ReceiveDamage(
float damage,
Character inflictor,
float knockback,
Vector2 knockbackDir = default) => damage;
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,
2023-07-21 02:54:13 -07:00
Vector2 knockbackDir = default)
2023-06-03 18:21:46 -07:00
{
2023-06-13 02:55:30 -07:00
if (Health <= 0)
{
return;
}
2023-06-10 22:15:28 -07:00
float oldHealth = Health;
2023-07-22 20:23:48 -07:00
Health -= ReceiveDamage(damage, inflictor, knockback, knockbackDir);
2023-06-03 18:21:46 -07:00
2023-07-31 01:12:47 -07:00
var hurtParticles = GetNode<GpuParticles2D>("Effects/HurtParticles");
if (hurtParticles is not null)
{
hurtParticles.SetDirection(knockbackDir);
}
2023-06-03 18:21:46 -07:00
// 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
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
// play damage animation
2023-07-24 00:30:33 -07:00
if (HurtAnimation is not null && Health > 0)
2023-06-03 18:21:46 -07:00
{
2023-07-24 00:30:33 -07:00
HurtAnimation.Stop();
HurtAnimation.Play("hurt");
HurtAnimation.Queue("hurt_flash");
2023-06-03 18:21:46 -07:00
}
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)
{
2023-07-22 20:23:48 -07:00
//plr.Camera.Shake(1, 0.4f);
2023-06-03 18:21:46 -07:00
}
2023-07-31 01:12:47 -07:00
if (this.GetNode("Effects/HurtSound") is AudioStreamPlayer2D sound)
2023-06-03 18:21:46 -07:00
{
// very small pitch deviation
2023-07-31 01:12:47 -07:00
GD.Print("hurt sound");
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
}
2023-07-24 00:56:01 -07:00
public virtual void Footstep()
{
2023-07-31 01:12:47 -07:00
if (GetNode("Effects/Footstep") is AudioStreamPlayer2D player)
{
player.Play();
}
2023-07-24 00:56:01 -07:00
}
2022-11-10 20:29:53 -08:00
}