1
0

TabletopSystem.Session.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Numerics;
  2. using Content.Server.Tabletop.Components;
  3. using Content.Shared.Tabletop.Events;
  4. using Robust.Shared.Player;
  5. using Robust.Shared.Utility;
  6. namespace Content.Server.Tabletop
  7. {
  8. public sealed partial class TabletopSystem
  9. {
  10. /// <summary>
  11. /// Ensures that a <see cref="TabletopSession"/> exists on a <see cref="TabletopGameComponent"/>.
  12. /// Creates it and sets it up if it doesn't.
  13. /// </summary>
  14. /// <param name="tabletop">The tabletop game in question.</param>
  15. /// <returns>The session for the given tabletop game.</returns>
  16. public TabletopSession EnsureSession(TabletopGameComponent tabletop)
  17. {
  18. // We already have a session, return it
  19. // TODO: if tables are connected, treat them as a single entity. This can be done by sharing the session.
  20. if (tabletop.Session != null)
  21. return tabletop.Session;
  22. // We make sure that the tabletop map exists before continuing.
  23. EnsureTabletopMap();
  24. // Create new session.
  25. var session = new TabletopSession(TabletopMap, GetNextTabletopPosition());
  26. tabletop.Session = session;
  27. // Since this is the first time opening this session, set up the game
  28. tabletop.Setup.SetupTabletop(session, EntityManager);
  29. Log.Info($"Created tabletop session number {tabletop} at position {session.Position}.");
  30. return session;
  31. }
  32. /// <summary>
  33. /// Cleans up a tabletop game session, deleting every entity in it.
  34. /// </summary>
  35. /// <param name="uid">The UID of the tabletop game entity.</param>
  36. public void CleanupSession(EntityUid uid)
  37. {
  38. if (!EntityManager.TryGetComponent(uid, out TabletopGameComponent? tabletop))
  39. return;
  40. if (tabletop.Session is not { } session)
  41. return;
  42. foreach (var (player, _) in session.Players)
  43. {
  44. CloseSessionFor(player, uid);
  45. }
  46. foreach (var euid in session.Entities)
  47. {
  48. EntityManager.QueueDeleteEntity(euid);
  49. }
  50. tabletop.Session = null;
  51. }
  52. /// <summary>
  53. /// Adds a player to a tabletop game session, sending a message so the tabletop window opens on their end.
  54. /// </summary>
  55. /// <param name="player">The player session in question.</param>
  56. /// <param name="uid">The UID of the tabletop game entity.</param>
  57. public void OpenSessionFor(ICommonSession player, EntityUid uid)
  58. {
  59. if (!EntityManager.TryGetComponent(uid, out TabletopGameComponent? tabletop) || player.AttachedEntity is not {Valid: true} attachedEntity)
  60. return;
  61. // Make sure we have a session, and add the player to it if not added already.
  62. var session = EnsureSession(tabletop);
  63. if (session.Players.ContainsKey(player))
  64. return;
  65. if(EntityManager.TryGetComponent(attachedEntity, out TabletopGamerComponent? gamer))
  66. CloseSessionFor(player, gamer.Tabletop, false);
  67. // Set the entity as an absolute GAMER.
  68. EnsureComp<TabletopGamerComponent>(attachedEntity).Tabletop = uid;
  69. // Create a camera for the gamer to use
  70. var camera = CreateCamera(tabletop, player);
  71. session.Players[player] = new TabletopSessionPlayerData { Camera = camera };
  72. // Tell the gamer to open a viewport for the tabletop game
  73. RaiseNetworkEvent(new TabletopPlayEvent(GetNetEntity(uid), GetNetEntity(camera), Loc.GetString(tabletop.BoardName), tabletop.Size), player.Channel);
  74. }
  75. /// <summary>
  76. /// Removes a player from a tabletop game session, and sends them a message so their tabletop window is closed.
  77. /// </summary>
  78. /// <param name="player">The player in question.</param>
  79. /// <param name="uid">The UID of the tabletop game entity.</param>
  80. /// <param name="removeGamerComponent">Whether to remove the <see cref="TabletopGamerComponent"/> from the player's attached entity.</param>
  81. public void CloseSessionFor(ICommonSession player, EntityUid uid, bool removeGamerComponent = true)
  82. {
  83. if (!EntityManager.TryGetComponent(uid, out TabletopGameComponent? tabletop) || tabletop.Session is not { } session)
  84. return;
  85. if (!session.Players.TryGetValue(player, out var data))
  86. return;
  87. if(removeGamerComponent && player.AttachedEntity is {} attachedEntity && EntityManager.TryGetComponent(attachedEntity, out TabletopGamerComponent? gamer))
  88. {
  89. // We invalidate this to prevent an infinite feedback from removing the component.
  90. gamer.Tabletop = EntityUid.Invalid;
  91. // You stop being a gamer.......
  92. EntityManager.RemoveComponent<TabletopGamerComponent>(attachedEntity);
  93. }
  94. session.Players.Remove(player);
  95. session.Entities.Remove(data.Camera);
  96. // Deleting the view subscriber automatically cleans up subscriptions, no need to do anything else.
  97. EntityManager.QueueDeleteEntity(data.Camera);
  98. }
  99. /// <summary>
  100. /// A helper method that creates a camera for a specified player, in a tabletop game session.
  101. /// </summary>
  102. /// <param name="tabletop">The tabletop game component in question.</param>
  103. /// <param name="player">The player in question.</param>
  104. /// <param name="offset">An offset from the tabletop position for the camera. Zero by default.</param>
  105. /// <returns>The UID of the camera entity.</returns>
  106. private EntityUid CreateCamera(TabletopGameComponent tabletop, ICommonSession player, Vector2 offset = default)
  107. {
  108. DebugTools.AssertNotNull(tabletop.Session);
  109. var session = tabletop.Session!;
  110. // Spawn an empty entity at the coordinates
  111. var camera = EntityManager.SpawnEntity(null, session.Position.Offset(offset));
  112. // Add an eye component and disable FOV
  113. var eyeComponent = EnsureComp<EyeComponent>(camera);
  114. _eye.SetDrawFov(camera, false, eyeComponent);
  115. _eye.SetZoom(camera, tabletop.CameraZoom, eyeComponent);
  116. // Add the user to the view subscribers. If there is no player session, just skip this step
  117. _viewSubscriberSystem.AddViewSubscriber(camera, player);
  118. return camera;
  119. }
  120. }
  121. }