Portals & hand teleporter (#13266)

* basic system with portals & linked ents

* hand tele sprites, no impl

* hand tele and teleportation works

* fancy it up

* oog

* special case projectiles

* predict portal-to-portal teleportation

* this stuff

* check nullspace

* sloth

* give to rd instead

* i guess this can probably happen

* docs
This commit is contained in:
Kara
2023-01-02 19:58:25 -06:00
committed by GitHub
parent a21a4711b6
commit c821ca71aa
23 changed files with 592 additions and 3 deletions

View File

@@ -0,0 +1,36 @@
using Content.Shared.Audio;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Teleportation.Components;
/// <summary>
/// Creates portals. If two are created, both are linked together--otherwise the first teleports randomly.
/// Using it with both portals active deactivates both.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class HandTeleporterComponent : Component
{
[ViewVariables, DataField("firstPortal")]
public EntityUid? FirstPortal = null;
[ViewVariables, DataField("secondPortal")]
public EntityUid? SecondPortal = null;
[DataField("firstPortalPrototype", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
public string FirstPortalPrototype = "PortalRed";
[DataField("secondPortalPrototype", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
public string SecondPortalPrototype = "PortalBlue";
[DataField("newPortalSound")]
public SoundSpecifier NewPortalSound = new SoundPathSpecifier("/Audio/Machines/high_tech_confirm.ogg")
{
Params = AudioParams.Default.WithVolume(-2f)
};
[DataField("clearPortalsSound")]
public SoundSpecifier ClearPortalsSound = new SoundPathSpecifier("/Audio/Machines/button.ogg");
}

View File

@@ -0,0 +1,42 @@
using Content.Shared.Teleportation.Systems;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Teleportation.Components;
/// <summary>
/// Represents an entity which is linked to other entities (perhaps portals), and which can be walked through/
/// thrown into to teleport an entity.
/// </summary>
[RegisterComponent, Access(typeof(LinkedEntitySystem)), NetworkedComponent]
public sealed class LinkedEntityComponent : Component
{
/// <summary>
/// The entities that this entity is linked to.
/// </summary>
[DataField("linkedEntities")]
public HashSet<EntityUid> LinkedEntities = new();
/// <summary>
/// Should this entity be deleted if all of its links are removed?
/// </summary>
[DataField("deleteOnEmptyLinks")]
public bool DeleteOnEmptyLinks = false;
}
[Serializable, NetSerializable]
public sealed class LinkedEntityComponentState : ComponentState
{
public HashSet<EntityUid> LinkedEntities;
public LinkedEntityComponentState(HashSet<EntityUid> linkedEntities)
{
LinkedEntities = linkedEntities;
}
}
[Serializable, NetSerializable]
public enum LinkedEntityVisuals : byte
{
HasAnyLinks
}

View File

@@ -0,0 +1,31 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared.Teleportation.Components;
/// <summary>
/// Marks an entity as being a 'portal' which teleports entities sent through it to linked entities.
/// Relies on <see cref="LinkedEntityComponent"/> being set up.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class PortalComponent : Component
{
/// <summary>
/// Sound played on arriving to this portal, centered on the destination.
/// The arrival sound of the entered portal will play if the destination is not a portal.
/// </summary>
[DataField("arrivalSound")]
public SoundSpecifier ArrivalSound = new SoundPathSpecifier("/Audio/Effects/teleport_arrival.ogg");
/// <summary>
/// Sound played on departing from this portal, centered on the original portal.
/// </summary>
[DataField("departureSound")]
public SoundSpecifier DepartureSound = new SoundPathSpecifier("/Audio/Effects/teleport_departure.ogg");
/// <summary>
/// If no portals are linked, the subject will be teleported a random distance at maximum this far away.
/// </summary>
[DataField("maxRandomRadius")]
public float MaxRandomRadius = 10.0f;
}

View File

@@ -0,0 +1,29 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Teleportation.Components;
/// <summary>
/// Attached to an entity after portal transit to mark that they should not immediately be portaled back
/// at the end destination.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class PortalTimeoutComponent : Component
{
/// <summary>
/// The portal that was entered. Null if coming from a hand teleporter, etc.
/// </summary>
[ViewVariables, DataField("enteredPortal")]
public EntityUid? EnteredPortal = null;
}
[Serializable, NetSerializable]
public sealed class PortalTimeoutComponentState : ComponentState
{
public EntityUid? EnteredPortal;
public PortalTimeoutComponentState(EntityUid? enteredPortal)
{
EnteredPortal = enteredPortal;
}
}

View File

@@ -0,0 +1,115 @@
using System.Linq;
using Content.Shared.Teleportation.Components;
using Robust.Shared.GameStates;
namespace Content.Shared.Teleportation.Systems;
/// <summary>
/// Handles symmetrically linking two entities together, and removing links properly.
/// This does not do anything on its own (outside of deleting entities that have 0 links, if that option is true)
/// Systems can do whatever they please with the linked entities, such as <see cref="PortalSystem"/>.
/// </summary>
public sealed class LinkedEntitySystem : EntitySystem
{
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LinkedEntityComponent, ComponentShutdown>(OnLinkShutdown);
SubscribeLocalEvent<LinkedEntityComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<LinkedEntityComponent, ComponentHandleState>(OnHandleState);
}
private void OnGetState(EntityUid uid, LinkedEntityComponent component, ref ComponentGetState args)
{
args.State = new LinkedEntityComponentState(component.LinkedEntities);
}
private void OnHandleState(EntityUid uid, LinkedEntityComponent component, ref ComponentHandleState args)
{
if (args.Current is LinkedEntityComponentState state)
component.LinkedEntities = state.LinkedEntities;
}
private void OnLinkShutdown(EntityUid uid, LinkedEntityComponent component, ComponentShutdown args)
{
// Remove any links to this entity when deleted.
foreach (var ent in component.LinkedEntities.ToArray())
{
if (LifeStage(ent) < EntityLifeStage.Terminating && TryComp<LinkedEntityComponent>(ent, out var link))
{
TryUnlink(uid, ent, component, link);
}
}
}
#region Public API
/// <summary>
/// Links two entities together. Does not require the existence of <see cref="LinkedEntityComponent"/> on either
/// already. Linking is symmetrical, so order doesn't matter.
/// </summary>
/// <param name="first">The first entity to link</param>
/// <param name="second">The second entity to link</param>
/// <param name="deleteOnEmptyLinks">Whether both entities should now delete once their links are removed</param>
/// <returns>Whether linking was successful (e.g. they weren't already linked)</returns>
public bool TryLink(EntityUid first, EntityUid second, bool deleteOnEmptyLinks=false)
{
var firstLink = EnsureComp<LinkedEntityComponent>(first);
var secondLink = EnsureComp<LinkedEntityComponent>(second);
firstLink.DeleteOnEmptyLinks = deleteOnEmptyLinks;
secondLink.DeleteOnEmptyLinks = deleteOnEmptyLinks;
_appearance.SetData(first, LinkedEntityVisuals.HasAnyLinks, true);
_appearance.SetData(second, LinkedEntityVisuals.HasAnyLinks, true);
Dirty(firstLink);
Dirty(secondLink);
return firstLink.LinkedEntities.Add(second)
&& secondLink.LinkedEntities.Add(first);
}
/// <summary>
/// Unlinks two entities. Deletes either entity if <see cref="LinkedEntityComponent.DeleteOnEmptyLinks"/>
/// was true and its links are now empty. Symmetrical, so order doesn't matter.
/// </summary>
/// <param name="first">The first entity to unlink</param>
/// <param name="second">The second entity to unlink</param>
/// <param name="firstLink">Resolve comp</param>
/// <param name="secondLink">Resolve comp</param>
/// <returns>Whether unlinking was successful (e.g. they both were actually linked to one another)</returns>
public bool TryUnlink(EntityUid first, EntityUid second,
LinkedEntityComponent? firstLink=null, LinkedEntityComponent? secondLink=null)
{
if (!Resolve(first, ref firstLink))
return false;
if (!Resolve(second, ref secondLink))
return false;
var success = firstLink.LinkedEntities.Remove(second)
&& secondLink.LinkedEntities.Remove(first);
_appearance.SetData(first, LinkedEntityVisuals.HasAnyLinks, firstLink.LinkedEntities.Any());
_appearance.SetData(second, LinkedEntityVisuals.HasAnyLinks, secondLink.LinkedEntities.Any());
Dirty(firstLink);
Dirty(secondLink);
if (firstLink.LinkedEntities.Count == 0 && firstLink.DeleteOnEmptyLinks)
QueueDel(first);
if (secondLink.LinkedEntities.Count == 0 && secondLink.DeleteOnEmptyLinks)
QueueDel(second);
return success;
}
#endregion
}

View File

@@ -0,0 +1,146 @@
using System.Linq;
using Content.Shared.Projectiles;
using Content.Shared.Teleportation.Components;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Physics.Events;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Shared.Teleportation.Systems;
/// <summary>
/// This handles teleporting entities through portals, and creating new linked portals.
/// </summary>
public sealed class PortalSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
private const string PortalFixture = "portalFixture";
private const string ProjectileFixture = "projectile";
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<PortalComponent, StartCollideEvent>(OnCollide);
SubscribeLocalEvent<PortalComponent, EndCollideEvent>(OnEndCollide);
SubscribeLocalEvent<PortalTimeoutComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<PortalTimeoutComponent, ComponentHandleState>(OnHandleState);
}
private void OnGetState(EntityUid uid, PortalTimeoutComponent component, ref ComponentGetState args)
{
args.State = new PortalTimeoutComponentState(component.EnteredPortal);
}
private void OnHandleState(EntityUid uid, PortalTimeoutComponent component, ref ComponentHandleState args)
{
if (args.Current is PortalTimeoutComponentState state)
component.EnteredPortal = state.EnteredPortal;
}
private bool ShouldCollide(Fixture our, Fixture other)
{
// most non-hard fixtures shouldn't pass through portals, but projectiles are non-hard as well
// and they should still pass through
return our.ID == PortalFixture && (other.Hard || other.ID == ProjectileFixture);
}
private void OnCollide(EntityUid uid, PortalComponent component, ref StartCollideEvent args)
{
if (!ShouldCollide(args.OurFixture, args.OtherFixture))
return;
var subject = args.OtherFixture.Body.Owner;
// best not.
if (Transform(subject).Anchored)
return;
// if they came from another portal, just return and wait for them to exit the portal
if (HasComp<PortalTimeoutComponent>(subject))
{
return;
}
if (TryComp<LinkedEntityComponent>(uid, out var link))
{
if (!link.LinkedEntities.Any())
return;
// client can't predict outside of simple portal-to-portal interactions due to randomness involved
// --also can't predict if the target doesn't exist on the client / is outside of PVS
if (_netMan.IsClient)
{
var first = link.LinkedEntities.First();
var exists = Exists(first);
if (link.LinkedEntities.Count != 1 || !exists || (exists && Transform(first).MapID == MapId.Nullspace))
return;
}
// pick a target and teleport there
var target = _random.Pick(link.LinkedEntities);
if (HasComp<PortalComponent>(target))
{
// if target is a portal, signal that they shouldn't be immediately portaled back
var timeout = EnsureComp<PortalTimeoutComponent>(subject);
timeout.EnteredPortal = uid;
Dirty(timeout);
}
TeleportEntity(uid, subject, Transform(target).Coordinates, target);
return;
}
if (_netMan.IsClient)
return;
// no linked entity--teleport randomly
var randVector = _random.NextVector2(component.MaxRandomRadius);
var newCoords = Transform(uid).Coordinates.Offset(randVector);
TeleportEntity(uid, subject, newCoords);
}
private void OnEndCollide(EntityUid uid, PortalComponent component, ref EndCollideEvent args)
{
if (!ShouldCollide(args.OurFixture, args.OtherFixture))
return;
var subject = args.OtherFixture.Body.Owner;
// if they came from (not us), remove the timeout
if (TryComp<PortalTimeoutComponent>(subject, out var timeout) && timeout.EnteredPortal != uid)
{
RemComp<PortalTimeoutComponent>(subject);
}
}
private void TeleportEntity(EntityUid portal, EntityUid subject, EntityCoordinates target, EntityUid? targetEntity=null,
PortalComponent? portalComponent = null)
{
if (!Resolve(portal, ref portalComponent))
return;
var arrivalSound = CompOrNull<PortalComponent>(targetEntity)?.ArrivalSound ?? portalComponent.ArrivalSound;
var departureSound = portalComponent.DepartureSound;
// Some special cased stuff: projectiles should stop ignoring shooter when they enter a portal, to avoid
// stacking 500 bullets in between 2 portals and instakilling people--you'll just hit yourself instead
// (as expected)
if (TryComp<ProjectileComponent>(subject, out var projectile))
{
projectile.IgnoreShooter = false;
}
Transform(subject).Coordinates = target;
_audio.PlayPredicted(departureSound, portal, subject);
_audio.PlayPredicted(arrivalSound, subject, subject);
}
}