SupaLidlGame/Items/Weapons/Sword.cs

237 lines
5.3 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;
2023-05-26 17:42:50 -07:00
using SupaLidlGame.State.Weapon;
2022-11-13 19:52:09 -08:00
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.Items.Weapons;
public partial class Sword : Weapon, IParryable
2022-11-13 19:52:09 -08:00
{
2023-06-03 18:21:46 -07:00
public bool IsAttacking { get; protected set; }
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
public override bool IsUsing => StateMachine.CurrentState
is SwordAttackState;
2023-05-23 00:23:53 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public Hitbox Hitbox { get; set; }
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public AnimationPlayer AnimationPlayer { get; set; }
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public AnimationTree AnimationTree { get; set; }
2023-05-23 00:23:53 -07:00
2023-06-03 18:21:46 -07:00
/// <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;
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public double AttackAnimationDuration { get; set; }
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public CpuParticles2D ParryParticles { get; set; }
2023-05-15 00:20:17 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public double NPCAnticipateTime { get; set; }
2023-05-15 00:20:17 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public WeaponStateMachine StateMachine { get; set; }
2023-05-15 00:20:17 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public Node2D Anchor { get; set; }
2023-05-26 22:28:08 -07:00
2023-06-03 18:21:46 -07:00
public override bool IsParryable { get; protected set; }
2023-05-15 00:20:17 -07:00
2023-06-03 18:21:46 -07:00
public ulong ParryTimeOrigin { get; protected set; }
2023-05-15 00:20:17 -07:00
2023-06-03 18:21:46 -07:00
private Tween _currentTween;
2023-05-15 00:20:17 -07:00
2023-06-03 18:21:46 -07:00
private AnimationNodeStateMachinePlayback _playback;
2023-05-15 00:20:17 -07:00
2023-06-03 18:21:46 -07:00
public override void Equip(Character character)
{
base.Equip(character);
Hitbox.Faction = character.Faction; // character is null before base
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
public override void Unequip(Character character)
{
base.Unequip(character);
}
public override void Use()
{
// we can't use if we're still using the weapon
if (RemainingUseTime > 0)
2023-03-19 14:13:39 -07:00
{
2023-06-03 18:21:46 -07:00
//return;
2023-03-19 14:13:39 -07:00
}
2023-06-03 18:21:46 -07:00
StateMachine.Use();
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
/*
// reset state of the weapon
IsParried = false;
IsParryable = true;
ParryTimeOrigin = Time.GetTicksMsec();
2023-05-23 00:23:53 -07:00
2023-06-03 18:21:46 -07:00
_playback.Travel("use");
*/
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
// play animation depending on rotation of weapon
/*
string anim = "use";
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
if (GetNode<Node2D>("Anchor").Rotation > Mathf.DegToRad(50))
{
anim = "use2";
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
if (Character is NPC)
{
// NPCs have a slower attack
anim += "-npc";
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
AnimationPlayer.Play(anim);
*/
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
base.Use();
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
public void EnableParry()
{
IsParried = false;
IsParryable = true;
ParryTimeOrigin = Time.GetTicksMsec();
GD.Print(Character.Name);
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
public void DisableParry()
{
IsParryable = false;
}
2023-05-23 00:23:53 -07:00
2023-06-03 18:21:46 -07:00
public override void Deuse()
{
//AnimationPlayer.Stop();
Deattack();
base.Deuse();
}
2023-05-23 00:23:53 -07:00
2023-06-03 18:21:46 -07:00
public void Attack()
{
//RemainingAttackTime = AttackTime;
IsAttacking = true;
Hitbox.IsDisabled = false;
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
public void Deattack()
{
IsAttacking = false;
DisableParry();
Hitbox.IsDisabled = true;
ProcessHits();
Hitbox.ResetIgnoreList();
AnimationPlayer.SpeedScale = 1;
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
public override void _Ready()
{
Hitbox.Damage = Damage;
_playback = (AnimationNodeStateMachinePlayback)AnimationTree
.Get("parameters/playback");
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
public override void _Process(double delta)
{
StateMachine.Process(delta);
base._Process(delta);
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
public void ProcessHits()
{
if (IsParried)
2023-03-19 14:13:39 -07:00
{
2023-06-03 18:21:46 -07:00
return;
2023-03-19 14:13:39 -07:00
}
2023-06-03 18:21:46 -07:00
foreach (BoundingBox box in Hitbox.Hits)
2023-03-19 14:13:39 -07:00
{
2023-06-03 18:21:46 -07:00
GD.Print("processing hit");
if (box is Hurtbox hurtbox)
2023-03-19 14:13:39 -07:00
{
2023-06-03 18:21:46 -07:00
hurtbox.InflictDamage(Damage, Character, Knockback);
2023-03-19 14:13:39 -07:00
}
}
2023-06-03 18:21:46 -07:00
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
public void AttemptParry(Weapon otherWeapon)
{
//if (IsParryable && otherWeapon.IsParryable)
if (otherWeapon.IsParryable &&
otherWeapon is IParryable otherParryable)
2023-03-19 14:13:39 -07:00
{
2023-06-03 18:21:46 -07:00
ParryParticles.Emitting = true;
if (ParryTimeOrigin < otherParryable.ParryTimeOrigin)
2023-03-19 14:13:39 -07:00
{
2023-06-03 18:21:46 -07:00
// our character was parried
}
else
{
otherParryable.Stun();
2023-03-19 14:13:39 -07:00
}
}
2023-06-03 18:21:46 -07:00
//this.GetAncestor<TileMap>().AddChild(instance);
}
public void Stun()
{
IsParried = true;
AnimationPlayer.SpeedScale = 0.25f;
Character.Stun(1.5f);
GetNode<AudioStreamPlayer2D>("ParrySound").OnWorld().Play();
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
public override void _on_hitbox_hit(BoundingBox box)
{
if (IsParried)
2023-05-23 00:23:53 -07:00
{
2023-06-03 18:21:46 -07:00
return;
2023-05-23 00:23:53 -07:00
}
2023-06-03 18:21:46 -07:00
if (box is Hitbox hb)
2023-03-19 14:13:39 -07:00
{
2023-06-03 18:21:46 -07:00
Weapon w = hb.GetAncestor<Weapon>();
if (w is not null)
2023-03-19 14:13:39 -07:00
{
2023-06-03 18:21:46 -07:00
AttemptParry(w);
2023-03-19 14:13:39 -07:00
}
2023-06-03 18:21:46 -07:00
}
2023-03-19 14:13:39 -07:00
2023-06-03 18:21:46 -07:00
if (box is Hurtbox hurt)
{
if (hurt.GetParent() is Character c)
2023-03-19 14:13:39 -07:00
{
2023-06-03 18:21:46 -07:00
var item = c.Inventory.SelectedItem;
if (item is Weapon w)
2023-03-19 14:13:39 -07:00
{
AttemptParry(w);
}
}
}
2023-06-03 18:21:46 -07:00
}
2023-05-23 00:23:53 -07:00
2023-06-03 18:21:46 -07:00
protected void SetAnimationCondition(string condition, bool value)
{
AnimationTree.Set("parameters/conditions/" + condition, value);
2023-03-19 14:13:39 -07:00
}
2022-11-13 19:52:09 -08:00
}