using Content.Shared._CP14.Demiplane.Components; using Robust.Shared.Random; namespace Content.Server._CP14.Demiplane; public sealed partial class CP14DemiplaneSystem { private void InitConnections() { SubscribeLocalEvent(OnRiftInit); SubscribeLocalEvent(OnRiftShutdown); } private void OnRiftInit(Entity rift, ref MapInitEvent args) { var map = Transform(rift).MapUid; if (_demiplaneQuery.TryComp(map, out var demiplane)) // In demiplane { if (rift.Comp.TryAutoLinkToMap) rift.Comp.Demiplane = map.Value; if (rift.Comp.ActiveTeleport) AddDemiplaneRandomEntryPoint((map.Value, demiplane), rift); } else if (rift.Comp.Demiplane is not null) //We out of demiplane { if (_demiplaneQuery.TryComp(rift.Comp.Demiplane, out var riftDemiplane)) { if (rift.Comp.ActiveTeleport) AddDemiplaneRandomExitPoint((rift.Comp.Demiplane.Value, riftDemiplane), rift); } } } private void OnRiftShutdown(Entity rift, ref ComponentShutdown args) { if (rift.Comp.Demiplane is null) return; if (!_demiplaneQuery.TryComp(rift.Comp.Demiplane, out var riftDemiplane)) return; RemoveDemiplaneRandomEntryPoint((rift.Comp.Demiplane.Value, riftDemiplane), rift); RemoveDemiplaneRandomExitPoint((rift.Comp.Demiplane.Value, riftDemiplane), rift); } /// ///Add a position in the real world where you can get out of this demiplane /// private void AddDemiplaneRandomExitPoint(Entity demiplane, Entity exitPoint) { demiplane.Comp.ExitPoints.Add(exitPoint); exitPoint.Comp.Demiplane = demiplane; } /// /// Removing the demiplane exit point, one of which the player can exit to /// private void RemoveDemiplaneRandomExitPoint(Entity? demiplane, EntityUid exitPoint) { if (!TryComp(exitPoint, out var riftComp)) return; if (demiplane is not null && demiplane.Value.Comp.ExitPoints.Contains(exitPoint)) { demiplane.Value.Comp.ExitPoints.Remove(exitPoint); riftComp.Demiplane = null; } if (riftComp.DeleteAfterDisconnect && exitPoint.Valid) QueueDel(exitPoint); } /// /// Add a position within the demiplane that can be entered into the demiplane /// private void AddDemiplaneRandomEntryPoint(Entity demiplane, Entity entryPoint) { demiplane.Comp.EntryPoints.Add(entryPoint); entryPoint.Comp.Demiplane = demiplane; } private void RemoveDemiplaneRandomEntryPoint(Entity? demiplane, EntityUid entryPoint) { if (!TryComp(entryPoint, out var riftComp)) return; if (demiplane is not null && demiplane.Value.Comp.EntryPoints.Contains(entryPoint)) { demiplane.Value.Comp.EntryPoints.Remove(entryPoint); riftComp.Demiplane = null; } if (riftComp.DeleteAfterDisconnect && entryPoint.Valid) QueueDel(entryPoint); } public bool TryGetDemiplaneEntryPoint(Entity demiplane, out EntityUid? entryPoint) { entryPoint = null; if (demiplane.Comp.EntryPoints.Count == 0) return false; entryPoint = _random.Pick(demiplane.Comp.EntryPoints); return true; } public bool TryGetDemiplaneExitPoint(Entity demiplane, out EntityUid? exitPoint) { exitPoint = null; if (demiplane.Comp.ExitPoints.Count == 0) return false; exitPoint = _random.Pick(demiplane.Comp.ExitPoints); return true; } }