2022-01-22 15:55:11 +03:00
|
|
|
using Content.Server.Xenoarchaeology.XenoArtifacts.Events;
|
|
|
|
|
using Robust.Shared.Random;
|
|
|
|
|
using Robust.Shared.Timing;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.Xenoarchaeology.XenoArtifacts;
|
|
|
|
|
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class ArtifactSystem : EntitySystem
|
2022-01-22 15:55:11 +03:00
|
|
|
{
|
|
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
|
|
|
[Dependency] private readonly IComponentFactory _componentFactory = default!;
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2022-03-26 03:08:56 +13:00
|
|
|
SubscribeLocalEvent<ArtifactComponent, MapInitEvent>(OnInit);
|
2022-01-22 15:55:11 +03:00
|
|
|
}
|
|
|
|
|
|
2022-03-26 03:08:56 +13:00
|
|
|
private void OnInit(EntityUid uid, ArtifactComponent component, MapInitEvent args)
|
2022-01-22 15:55:11 +03:00
|
|
|
{
|
|
|
|
|
if (component.RandomTrigger)
|
|
|
|
|
{
|
|
|
|
|
AddRandomTrigger(uid, component);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 22:16:49 +03:00
|
|
|
private void AddRandomTrigger(EntityUid uid, ArtifactComponent? component = null)
|
2022-01-22 15:55:11 +03:00
|
|
|
{
|
|
|
|
|
if (!Resolve(uid, ref component))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var triggerName = _random.Pick(component.PossibleTriggers);
|
|
|
|
|
var trigger = (Component) _componentFactory.GetComponent(triggerName);
|
|
|
|
|
trigger.Owner = uid;
|
|
|
|
|
|
2022-03-26 03:08:56 +13:00
|
|
|
if (EntityManager.HasComponent(uid, trigger.GetType()))
|
|
|
|
|
{
|
|
|
|
|
Logger.Error($"Attempted to add a random artifact trigger ({triggerName}) to an entity ({ToPrettyString(uid)}), but it already has the trigger");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 15:55:11 +03:00
|
|
|
EntityManager.AddComponent(uid, trigger);
|
2022-06-22 09:53:41 +10:00
|
|
|
RaiseLocalEvent(uid, new RandomizeTriggerEvent(), true);
|
2022-01-22 15:55:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryActivateArtifact(EntityUid uid, EntityUid? user = null,
|
|
|
|
|
ArtifactComponent? component = null)
|
|
|
|
|
{
|
|
|
|
|
if (!Resolve(uid, ref component))
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-04-29 01:03:39 +03:00
|
|
|
// check if artifact is under suppression field
|
|
|
|
|
if (component.IsSuppressed)
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-01-22 15:55:11 +03:00
|
|
|
// check if artifact isn't under cooldown
|
|
|
|
|
var timeDif = _gameTiming.CurTime - component.LastActivationTime;
|
|
|
|
|
if (timeDif.TotalSeconds < component.CooldownTime)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ForceActivateArtifact(uid, user, component);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ForceActivateArtifact(EntityUid uid, EntityUid? user = null,
|
|
|
|
|
ArtifactComponent? component = null)
|
|
|
|
|
{
|
|
|
|
|
if (!Resolve(uid, ref component))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
component.LastActivationTime = _gameTiming.CurTime;
|
|
|
|
|
|
|
|
|
|
var ev = new ArtifactActivatedEvent()
|
|
|
|
|
{
|
|
|
|
|
Activator = user
|
|
|
|
|
};
|
2022-06-22 09:53:41 +10:00
|
|
|
RaiseLocalEvent(uid, ev, true);
|
2022-01-22 15:55:11 +03:00
|
|
|
}
|
|
|
|
|
}
|