Добавил немного базы для заклинаний (Она не работает)

This commit is contained in:
Tornado Tech
2024-04-28 01:24:55 +10:00
parent 45d351b2b2
commit 25c174af41
10 changed files with 203 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
using Robust.Shared.Prototypes;
namespace Content.Server._CP14.Magic.Container;
[RegisterComponent]
public sealed partial class MagicSpellContainerComponent : Component
{
[DataField]
public List<ProtoId<MagicSpellPrototype>> Spells = new();
}

View File

@@ -0,0 +1,11 @@
namespace Content.Server._CP14.Magic.Container;
public sealed class MagickSpellContainerSystem : EntitySystem
{
[Dependency] private readonly MagicSpellSystem _magicSpell = default!;
public void CastSpell(Entity<MagicSpellContainerComponent> container)
{
_magicSpell.Cast(container, container.Comp.Spells);
}
}

View File

@@ -0,0 +1,20 @@
namespace Content.Server._CP14.Magic;
[Serializable, DataDefinition]
public abstract class MagicSpell
{
protected readonly IEntityManager EntityManager;
public MagicSpell()
{
EntityManager = IoCManager.Resolve<IEntityManager>();
}
[DataField]
public virtual int BaseCost { get; set; } = 10;
public virtual void Modify(MagicSpellContext context)
{
context.Cost += BaseCost;
}
}

View File

@@ -0,0 +1,14 @@
using Robust.Shared.Map;
namespace Content.Server._CP14.Magic;
public sealed class MagicSpellContext
{
public EntityUid Caster;
public MapCoordinates SourcePoint;
public MapCoordinates TargetPoint;
public int MaxCost = 100;
public int Cost;
}

View File

@@ -0,0 +1,13 @@
using Robust.Shared.Prototypes;
namespace Content.Server._CP14.Magic;
[Prototype("magicSpell")]
public sealed class MagicSpellPrototype : IPrototype
{
[IdDataField]
public string ID { get; } = string.Empty;
[DataField]
public required MagicSpell Action;
}

View File

@@ -0,0 +1,60 @@
using System.Runtime.CompilerServices;
using Robust.Server.GameObjects;
using Robust.Shared.Prototypes;
namespace Content.Server._CP14.Magic;
public sealed class MagicSpellSystem : EntitySystem
{
[Robust.Shared.IoC.Dependency] private readonly IPrototypeManager _prototype = default!;
[Robust.Shared.IoC.Dependency] private readonly TransformSystem _transform = default!;
public IReadOnlyDictionary<ProtoId<MagicSpellPrototype>, MagicSpellPrototype> Spells => _spells;
private readonly Dictionary<ProtoId<MagicSpellPrototype>, MagicSpellPrototype> _spells = new();
public override void Initialize()
{
base.Initialize();
Enumerate();
_prototype.PrototypesReloaded += OnProtoReload;
}
public void Cast(EntityUid caster, IReadOnlyList<ProtoId<MagicSpellPrototype>> spells)
{
var casterPosition = _transform.GetMapCoordinates(caster);
var context = new MagicSpellContext
{
SourcePoint = casterPosition,
TargetPoint = casterPosition
};
foreach (var spellProto in spells)
{
if (!Spells.TryGetValue(spellProto, out var spell))
continue;
spell.Action.Modify(context);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Enumerate()
{
var prototypes = _prototype.EnumeratePrototypes<MagicSpellPrototype>();
foreach (var prototype in prototypes)
{
_spells.Add(prototype.ID, prototype);
}
}
private void OnProtoReload(PrototypesReloadedEventArgs args)
{
if (!args.WasModified<MagicSpellPrototype>())
return;
Enumerate();
}
}

View File

@@ -0,0 +1,44 @@
using Content.Shared.Mobs.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Random;
namespace Content.Server._CP14.Magic.Spells;
[Serializable, DataDefinition]
public abstract class MagicSpellPointRandomMob : MagicSpell
{
private readonly EntityLookupSystem _entityLookupSystem;
private readonly IRobustRandom _random;
private readonly TransformSystem _transform;
[DataField]
public virtual float Range { get; set; } = 5f;
[DataField]
public override int BaseCost { get; set; } = 50;
public MagicSpellPointRandomMob()
{
_entityLookupSystem = EntityManager.System<EntityLookupSystem>();
_transform = EntityManager.System<TransformSystem>();
_random = IoCManager.Resolve<IRobustRandom>();
}
public override void Modify(MagicSpellContext context)
{
base.Modify(context);
var targets = _entityLookupSystem.GetEntitiesInRange<MobStateComponent>(context.SourcePoint, Range);
if (targets.Count == 0)
return;
var target = _random.Pick(targets);
var coordinates = _transform.GetMapCoordinates(target);
ApplyCoordinates(context, coordinates);
}
public abstract void ApplyCoordinates(MagicSpellContext context, MapCoordinates coordinates);
}

View File

@@ -0,0 +1,12 @@
using Robust.Shared.Map;
namespace Content.Server._CP14.Magic.Spells.SourcePoint;
[Serializable, DataDefinition]
public sealed partial class MagicSpellSourcePointRandomMob : MagicSpellPointRandomMob
{
public override void ApplyCoordinates(MagicSpellContext context, MapCoordinates coordinates)
{
context.SourcePoint = coordinates;
}
}

View File

@@ -0,0 +1,12 @@
using Robust.Shared.Map;
namespace Content.Server._CP14.Magic.Spells.TargetPoint;
[Serializable, DataDefinition]
public sealed class MagicSpellTargetPointRandomMob : MagicSpellPointRandomMob
{
public override void ApplyCoordinates(MagicSpellContext context, MapCoordinates coordinates)
{
context.TargetPoint = coordinates;
}
}

View File

@@ -0,0 +1,7 @@
- type: magicSpell
id: sourcePointRandomMob
action:
!type:MagicSpellSourcePointRandomMob
range: 5
baseCost: 25