Xenoarchaeology artifacts (#6069)
Co-authored-by: Alexander Evgrashin <evgrashin.adl@gmail.com>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using Content.Server.Clothing.Components;
|
||||
using Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Components;
|
||||
using Content.Server.Xenoarchaeology.XenoArtifacts.Events;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Systems;
|
||||
|
||||
public class SpawnArtifactSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<SpawnArtifactComponent, ComponentInit>(OnInit);
|
||||
SubscribeLocalEvent<SpawnArtifactComponent, ArtifactActivatedEvent>(OnActivate);
|
||||
}
|
||||
private void OnInit(EntityUid uid, SpawnArtifactComponent component, ComponentInit args)
|
||||
{
|
||||
ChooseRandomPrototype(uid, component);
|
||||
}
|
||||
|
||||
private void OnActivate(EntityUid uid, SpawnArtifactComponent component, ArtifactActivatedEvent args)
|
||||
{
|
||||
if (component.Prototype == null)
|
||||
return;
|
||||
if (component.SpawnsCount >= component.MaxSpawns)
|
||||
return;
|
||||
|
||||
// select spawn position near artifact
|
||||
var artifactCord = Transform(uid).Coordinates;
|
||||
var dx = _random.NextFloat(-component.Range, component.Range);
|
||||
var dy = _random.NextFloat(-component.Range, component.Range);
|
||||
var spawnCord = artifactCord.Offset(new Vector2(dx, dy));
|
||||
|
||||
// spawn entity
|
||||
var spawned = EntityManager.SpawnEntity(component.Prototype, spawnCord);
|
||||
component.SpawnsCount++;
|
||||
|
||||
// if there is an user - try to put spawned item in their hands
|
||||
// doesn't work for spawners
|
||||
if (args.Activator != null &&
|
||||
EntityManager.TryGetComponent(args.Activator.Value, out SharedHandsComponent? hands) &&
|
||||
EntityManager.HasComponent<ItemComponent>(spawned))
|
||||
{
|
||||
hands.TryPutInAnyHand(spawned);
|
||||
}
|
||||
}
|
||||
|
||||
private void ChooseRandomPrototype(EntityUid uid, SpawnArtifactComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
|
||||
if (!component.RandomPrototype)
|
||||
return;
|
||||
if (component.PossiblePrototypes.Count == 0)
|
||||
return;
|
||||
|
||||
var proto = _random.Pick(component.PossiblePrototypes);
|
||||
component.Prototype = proto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Components;
|
||||
using Content.Server.Xenoarchaeology.XenoArtifacts.Events;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Systems;
|
||||
|
||||
public class TelepathicArtifactSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly IEntityLookup _lookup = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<TelepathicArtifactComponent, ArtifactActivatedEvent>(OnActivate);
|
||||
}
|
||||
|
||||
private void OnActivate(EntityUid uid, TelepathicArtifactComponent component, ArtifactActivatedEvent args)
|
||||
{
|
||||
// try to find victims nearby
|
||||
var victims = _lookup.GetEntitiesInRange(uid, component.Range);
|
||||
foreach (var victimUid in victims)
|
||||
{
|
||||
if (!EntityManager.HasComponent<ActorComponent>(victimUid))
|
||||
continue;
|
||||
|
||||
// roll if msg should be usual or drastic
|
||||
var isDrastic = _random.NextFloat() <= component.DrasticMessageProb;
|
||||
var msgArr = isDrastic ? component.DrasticMessages : component.Messages;
|
||||
|
||||
// pick a random message
|
||||
var msgId = _random.Pick(msgArr);
|
||||
var msg = Loc.GetString(msgId);
|
||||
|
||||
// show it as a popup, but only for the victim
|
||||
_popupSystem.PopupEntity(msg, victimUid, Filter.Entities(victimUid));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user