Magic spells code expansion (#692)
* split manacost in separate component * stamina spells * toggleable actions * swap activeCasting on ActiveDoAfter * remove dublication * Update CP14SharedMagicSystem.ToggleableActions.cs * EntityWoldTarget * mana glove done * fix spell scrolls interrupting * fix cooldown problem * clean up, edit stamina system
This commit is contained in:
@@ -6,6 +6,7 @@ using Content.Shared._CP14.MagicSpell.Events;
|
||||
using Content.Shared._CP14.MagicSpell.Spells;
|
||||
using Content.Shared._CP14.MagicSpellStorage;
|
||||
using Content.Shared.Actions;
|
||||
using Content.Shared.Damage.Systems;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Movement.Systems;
|
||||
@@ -13,6 +14,7 @@ using Content.Shared.Popups;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared._CP14.MagicSpell;
|
||||
|
||||
@@ -30,21 +32,34 @@ public abstract partial class CP14SharedMagicSystem : EntitySystem
|
||||
[Dependency] private readonly MetaDataSystem _meta = default!;
|
||||
[Dependency] private readonly SharedActionsSystem _action = default!;
|
||||
[Dependency] private readonly MovementSpeedModifierSystem _movement = default!;
|
||||
[Dependency] private readonly StaminaSystem _stamina = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
|
||||
private EntityQuery<CP14MagicEnergyContainerComponent> _magicContainerQuery;
|
||||
private EntityQuery<CP14MagicEffectComponent> _magicEffectQuery;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
InitializeActions();
|
||||
InitializeAspects();
|
||||
InitializeDelayedActions();
|
||||
InitializeToggleableActions();
|
||||
InitializeChecks();
|
||||
InitializeSlowdown();
|
||||
|
||||
_magicContainerQuery = GetEntityQuery<CP14MagicEnergyContainerComponent>();
|
||||
_magicEffectQuery = GetEntityQuery<CP14MagicEffectComponent>();
|
||||
|
||||
SubscribeLocalEvent<CP14MagicEffectComponent, MapInitEvent>(OnMagicEffectInit);
|
||||
SubscribeLocalEvent<CP14MagicEffectComponent, CP14CastMagicEffectAttemptEvent>(OnBeforeCastMagicEffect);
|
||||
SubscribeLocalEvent<CP14MagicEffectComponent, ComponentShutdown>(OnMagicEffectShutdown);
|
||||
|
||||
SubscribeLocalEvent<CP14MagicEffectComponent, CP14AfterCastMagicEffectEvent>(OnAfterCastMagicEffect);
|
||||
SubscribeLocalEvent<CP14MagicEffectStaminaCostComponent, CP14MagicEffectConsumeResourceEvent>(OnStaminaConsume);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
UpdateToggleableActions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -56,11 +71,20 @@ public abstract partial class CP14SharedMagicSystem : EntitySystem
|
||||
var sb = new StringBuilder();
|
||||
|
||||
sb.Append(meta.EntityDescription);
|
||||
sb.Append($"\n\n {Loc.GetString("cp14-magic-manacost")}: [color=#5da9e8]{ent.Comp.ManaCost}[/color]");
|
||||
|
||||
if (TryComp<CP14MagicEffectManaCostComponent>(ent, out var manaCost) && manaCost.ManaCost > 0)
|
||||
{
|
||||
sb.Append($"\n\n{Loc.GetString("cp14-magic-manacost")}: [color=#5da9e8]{manaCost.ManaCost}[/color]");
|
||||
}
|
||||
|
||||
if (TryComp<CP14MagicEffectStaminaCostComponent>(ent, out var staminaCost) && staminaCost.Stamina > 0)
|
||||
{
|
||||
sb.Append($"\n\n{Loc.GetString("cp14-magic-staminacost")}: [color=#3fba54]{staminaCost.Stamina}[/color]");
|
||||
}
|
||||
|
||||
if (_proto.TryIndex(ent.Comp.MagicType, out var indexedMagic))
|
||||
{
|
||||
sb.Append($"\n {Loc.GetString("cp14-magic-magic-type")}: [color={indexedMagic.Color.ToHex()}]{Loc.GetString(indexedMagic.Name)}[/color]");
|
||||
sb.Append($"\n{Loc.GetString("cp14-magic-magic-type")}: [color={indexedMagic.Color.ToHex()}]{Loc.GetString(indexedMagic.Name)}[/color]");
|
||||
}
|
||||
|
||||
if (TryComp<CP14MagicEffectVerbalAspectComponent>(ent, out var verbal))
|
||||
@@ -76,6 +100,12 @@ public abstract partial class CP14SharedMagicSystem : EntitySystem
|
||||
_meta.SetEntityDescription(ent, sb.ToString());
|
||||
}
|
||||
|
||||
private void OnMagicEffectShutdown(Entity<CP14MagicEffectComponent> ent, ref ComponentShutdown args)
|
||||
{
|
||||
if (_doAfter.IsRunning(ent.Comp.ActiveDoAfter))
|
||||
_doAfter.Cancel(ent.Comp.ActiveDoAfter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checking to see if the spell can be used at all
|
||||
/// </summary>
|
||||
@@ -90,36 +120,6 @@ public abstract partial class CP14SharedMagicSystem : EntitySystem
|
||||
return !ev.Cancelled;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Before using a spell, a mana check is made for the amount of mana to show warnings.
|
||||
/// </summary>
|
||||
private void OnBeforeCastMagicEffect(Entity<CP14MagicEffectComponent> ent, ref CP14CastMagicEffectAttemptEvent args)
|
||||
{
|
||||
var requiredMana = CalculateManacost(ent, args.Performer);
|
||||
|
||||
if (ent.Comp.SpellStorage is not null)
|
||||
{
|
||||
if (_magicContainerQuery.TryComp(ent.Comp.SpellStorage, out var magicContainer)) // We have item that provides this spell
|
||||
requiredMana = MathF.Max(0, (float)(requiredMana - magicContainer.Energy));
|
||||
|
||||
if (!ent.Comp.SpellStorage.Value.Comp.CanUseCasterMana && requiredMana > 0)
|
||||
{
|
||||
args.PushReason(Loc.GetString("cp14-magic-spell-not-enough-mana-item"));
|
||||
args.Cancel();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (requiredMana > 0 && _magicContainerQuery.TryComp(args.Performer, out var playerMana))
|
||||
{
|
||||
if (!_magicEnergy.HasEnergy(args.Performer, requiredMana, playerMana, true) && _net.IsServer)
|
||||
{
|
||||
_popup.PopupEntity(Loc.GetString("cp14-magic-spell-not-enough-mana-cast-warning-"+_random.Next(5)), args.Performer, args.Performer, PopupType.SmallCaution);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CastTelegraphy(Entity<CP14MagicEffectComponent> ent, CP14SpellEffectBaseArgs args)
|
||||
{
|
||||
if (!_net.IsServer)
|
||||
@@ -131,28 +131,10 @@ public abstract partial class CP14SharedMagicSystem : EntitySystem
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryCastSpellDelayed(ICP14DelayedMagicEffect delayedEffect, DoAfterEvent doAfter, Entity<CP14MagicEffectComponent> action, EntityUid performer)
|
||||
private void CastSpell(Entity<CP14MagicEffectComponent> ent, CP14SpellEffectBaseArgs args)
|
||||
{
|
||||
var fromItem = action.Comp.SpellStorage is not null;
|
||||
|
||||
var doAfterEventArgs = new DoAfterArgs(EntityManager, performer, delayedEffect.CastDelay, doAfter, action, used: action.Comp.SpellStorage)
|
||||
{
|
||||
BreakOnMove = delayedEffect.BreakOnMove,
|
||||
BreakOnDamage = delayedEffect.BreakOnDamage,
|
||||
Hidden = delayedEffect.Hidden,
|
||||
DistanceThreshold = 100f,
|
||||
CancelDuplicate = true,
|
||||
BlockDuplicate = true,
|
||||
BreakOnDropItem = fromItem,
|
||||
NeedHand = fromItem,
|
||||
};
|
||||
|
||||
return _doAfter.TryStartDoAfter(doAfterEventArgs);
|
||||
}
|
||||
|
||||
private void CastSpell(Entity<CP14MagicEffectComponent> ent, CP14SpellEffectBaseArgs args, float cooldown)
|
||||
{
|
||||
_action.CP14StartCustomDelay(ent, TimeSpan.FromSeconds(cooldown));
|
||||
var ev = new CP14MagicEffectConsumeResourceEvent(args.User);
|
||||
RaiseLocalEvent(ent, ref ev);
|
||||
|
||||
if (_net.IsServer)
|
||||
{
|
||||
@@ -161,57 +143,33 @@ public abstract partial class CP14SharedMagicSystem : EntitySystem
|
||||
effect.Effect(EntityManager, args);
|
||||
}
|
||||
}
|
||||
|
||||
var ev = new CP14AfterCastMagicEffectEvent(args.User);
|
||||
RaiseLocalEvent(ent, ref ev);
|
||||
}
|
||||
|
||||
private void OnAfterCastMagicEffect(Entity<CP14MagicEffectComponent> ent, ref CP14AfterCastMagicEffectEvent args)
|
||||
{
|
||||
if (_net.IsClient)
|
||||
return;
|
||||
|
||||
var requiredMana = CalculateManacost(ent, args.Performer);
|
||||
|
||||
if (ent.Comp.SpellStorage is not null &&
|
||||
_magicContainerQuery.TryComp(ent.Comp.SpellStorage, out var magicStorage))
|
||||
{
|
||||
var spellEv = new CP14SpellFromSpellStorageUsedEvent(args.Performer, ent, requiredMana);
|
||||
RaiseLocalEvent(ent.Comp.SpellStorage.Value, ref spellEv);
|
||||
|
||||
if (magicStorage.Energy > 0)
|
||||
{
|
||||
//TODO: FIX THIS SHIT
|
||||
var cashedEnergy = magicStorage.Energy;
|
||||
_magicEnergy.TryConsumeEnergy(ent.Comp.SpellStorage.Value, requiredMana, magicStorage, false);
|
||||
requiredMana = MathF.Max(0, (float)(requiredMana - cashedEnergy));
|
||||
}
|
||||
}
|
||||
|
||||
if (requiredMana > 0 &&
|
||||
_magicContainerQuery.TryComp(args.Performer, out var playerMana))
|
||||
{
|
||||
_magicEnergy.TryConsumeEnergy(args.Performer.Value, requiredMana, safe: false);
|
||||
}
|
||||
}
|
||||
|
||||
private FixedPoint2 CalculateManacost(Entity<CP14MagicEffectComponent> ent, EntityUid? caster)
|
||||
protected FixedPoint2 CalculateManacost(Entity<CP14MagicEffectManaCostComponent> ent, EntityUid? caster)
|
||||
{
|
||||
var manaCost = ent.Comp.ManaCost;
|
||||
|
||||
if (ent.Comp.CanModifyManacost)
|
||||
if (ent.Comp.CanModifyManacost && _magicEffectQuery.TryComp(ent, out var magicEffect))
|
||||
{
|
||||
var manaEv = new CP14CalculateManacostEvent(caster, ent.Comp.ManaCost, ent.Comp.MagicType);
|
||||
var manaEv = new CP14CalculateManacostEvent(caster, ent.Comp.ManaCost, magicEffect.MagicType);
|
||||
|
||||
if (caster is not null)
|
||||
RaiseLocalEvent(caster.Value, manaEv);
|
||||
|
||||
if (ent.Comp.SpellStorage is not null)
|
||||
RaiseLocalEvent(ent.Comp.SpellStorage.Value, manaEv);
|
||||
if (magicEffect.SpellStorage is not null)
|
||||
RaiseLocalEvent(magicEffect.SpellStorage.Value, manaEv);
|
||||
|
||||
manaCost = manaEv.GetManacost();
|
||||
}
|
||||
|
||||
return manaCost;
|
||||
}
|
||||
|
||||
private void OnStaminaConsume(Entity<CP14MagicEffectStaminaCostComponent> ent, ref CP14MagicEffectConsumeResourceEvent args)
|
||||
{
|
||||
if (args.Performer is null)
|
||||
return;
|
||||
|
||||
_stamina.TakeStaminaDamage(args.Performer.Value, ent.Comp.Stamina, visual: false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user