SupaLidlGame/Items/Weapons/Sword.cs

180 lines
4.5 KiB
C#
Raw Normal View History

2022-11-18 13:53:51 -08:00
using Godot;
using SupaLidlGame.BoundingBoxes;
2022-11-13 19:52:09 -08:00
using SupaLidlGame.Characters;
2022-11-25 09:11:46 -08:00
using SupaLidlGame.Extensions;
2022-11-13 19:52:09 -08:00
namespace SupaLidlGame.Items.Weapons
{
public partial class Sword : Weapon
{
2022-11-25 09:11:46 -08:00
public bool IsAttacking { get; protected set; }
2022-11-19 21:21:12 -08:00
2022-11-18 13:53:51 -08:00
[Export]
public Hitbox Hitbox { get; set; }
2022-11-13 19:52:09 -08:00
2022-11-19 21:21:12 -08:00
[Export]
public AnimationPlayer AnimationPlayer { get; set; }
/// <summary>
/// The time frame in seconds for which the weapon will deal damage.
/// </summary>
/// <remarks>
/// The value of <c>AttackTime</c> should be less than the
/// value of <c>UseTime</c>
/// </remarks>
[Export]
public double AttackTime { get; set; } = 0;
2022-11-25 09:11:46 -08:00
[Export]
public CPUParticles2D ParryParticles { get; set; }
public override bool IsParryable { get; protected set; }
2022-11-13 19:52:09 -08:00
public override void Equip(Character character)
{
2022-11-19 21:21:12 -08:00
Visible = true;
2022-11-13 19:52:09 -08:00
base.Equip(character);
2022-11-19 21:21:12 -08:00
Hitbox.Faction = character.Faction; // character is null before base
}
public override void Unequip(Character character)
{
Visible = false;
base.Unequip(character);
2022-11-13 19:52:09 -08:00
}
public override void Use()
{
2022-11-19 21:21:12 -08:00
if (RemainingUseTime > 0)
{
return;
}
2022-11-25 09:11:46 -08:00
IsParried = false;
AnimationPlayer.Stop();
if (GetNode<Node2D>("Anchor").Rotation < Mathf.DegToRad(50))
{
AnimationPlayer.Play("use");
}
else
{
AnimationPlayer.Play("use2");
}
2022-11-19 21:21:12 -08:00
base.Use();
2022-11-18 13:53:51 -08:00
}
public override void Deuse()
{
2022-11-25 09:11:46 -08:00
//AnimationPlayer.Stop();
2022-11-19 21:21:12 -08:00
Deattack();
base.Deuse();
}
2022-11-25 09:11:46 -08:00
public void Attack()
2022-11-19 21:21:12 -08:00
{
2022-11-25 09:11:46 -08:00
ParryTimeOrigin = Time.GetTicksMsec();
IsParryable = true;
//RemainingAttackTime = AttackTime;
IsAttacking = true;
Hitbox.IsDisabled = false;
}
public void Deattack()
{
IsAttacking = false;
IsParryable = false;
2022-11-19 21:21:12 -08:00
Hitbox.IsDisabled = true;
2022-11-25 09:11:46 -08:00
ProcessHits();
2022-11-19 21:21:12 -08:00
Hitbox.ResetIgnoreList();
2022-11-25 09:11:46 -08:00
AnimationPlayer.PlaybackSpeed = 1;
2022-11-19 21:21:12 -08:00
}
public override void _Ready()
{
Hitbox.Damage = Damage;
}
public override void _Process(double delta)
{
2022-11-25 09:11:46 -08:00
/*
2022-11-19 21:21:12 -08:00
if (RemainingAttackTime > 0)
{
if ((RemainingAttackTime -= delta) <= 0)
{
Deattack();
}
}
2022-11-25 09:11:46 -08:00
*/
2022-11-19 21:21:12 -08:00
base._Process(delta);
2022-11-13 19:52:09 -08:00
}
2022-11-25 09:11:46 -08:00
public void ProcessHits()
{
if (IsParried)
{
return;
}
foreach (BoundingBox box in Hitbox.Hits)
{
GD.Print("processing hit");
if (box is Hurtbox hurtbox)
{
hurtbox.InflictDamage(Damage, Character, Knockback);
}
}
}
public void AttemptParry(Weapon otherWeapon)
{
if (IsParryable && otherWeapon.IsParryable)
{
ParryParticles.Emitting = true;
if (ParryTimeOrigin < otherWeapon.ParryTimeOrigin)
{
// our character was parried
IsParried = true;
AnimationPlayer.PlaybackSpeed = 0.25f;
Character.Stun(1.5f);
}
}
//this.GetAncestor<TileMap>().AddChild(instance);
}
public override void _on_hitbox_hit(BoundingBox box)
{
if (IsParried)
{
return;
}
if (box is Hitbox hb)
{
Weapon w = hb.GetAncestor<Weapon>();
if (w is not null)
{
//Vector2 a = new Vector2(2, 2) * new Vector2(5, 2);
AttemptParry(w);
}
}
if (box is Hurtbox hurt)
{
if (hurt.GetParent() is Character c)
{
var item = c.Inventory.SelectedItem;
if (item is Weapon w)
{
AttemptParry(w);
}
}
}
//base._on_hitbox_hit(box);
}
2022-11-13 19:52:09 -08:00
}
}