Files
tbd-station-14/Content.Server/Tabletop/TabletopSystem.cs
Visne 973926fc9a Add chess (and mostly just tabletop backend stuff) (#4429)
* Add draggable tabletop component

* Use EntityCoordinates instead

* Don't send coordinates every frame

* Add chessboard + verb WIP

* Add documentation, verb networking works now

* Work so far
Need PVS refactor before being able to continue
Current code is broken

* viewsubscriber magic

* yes

* Fix map creation

* Add chess pieces, attempt prediction

* Add chess sprites and yml

* Clamping + other stuff

* fix

* stuff

* StopDragging() StartDragging()

* add piece grabbing

* Refactor dragging player to seperate event

* 🤣 Who did this 🤣💯👌

* 📮 sussy 📮

* Update chessboard sprite, scale piece while dragging

* yes

* ye

* y

* Close tabletop window when player dies

* Make interaction check more sane

* Fix funny behaviour when stunned

* Add icon

* Fix rsi

* Make time passed check more accurate

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Use EyeManager.PixelsPerMeter

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Add missing import

* Move viewport properties to XAML

* Make shared system and component abstract

* Use built in EntityManager

* Use RaiseNetworkEvent instead of SendSystemNetworkMessage

* Cache ViewSubscriberSystem

* Move unnecessary code to prototype

* Delete map on component shutdown instead of round restart

* Make documentation match rest of codebase

* Use ComponentManager instead of TryGetComponent

* Use TryGetComponent instead of GetComponent

* Add nullspace check to ClampPositionToViewport()

* Set world pos instead of local pos

* Improve server side verification

* Use visualizer

* Add netsync: false to sprites using visualizer

* Close window when chessboard is picked up

* Update to master

* Fix bug when opening window while another is opened

* Use ComponentManager

* Use TryGetValue

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
2021-09-13 19:58:44 +10:00

196 lines
8.7 KiB
C#

using System.Collections.Generic;
using Content.Server.Tabletop.Components;
using Content.Shared.Tabletop;
using Content.Shared.Tabletop.Events;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using DrawDepth = Content.Shared.DrawDepth.DrawDepth;
namespace Content.Server.Tabletop
{
[UsedImplicitly]
public partial class TabletopSystem : SharedTabletopSystem
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly ViewSubscriberSystem _viewSubscriberSystem = default!;
/// <summary>
/// All tabletop games currently in progress. Sessions are associated with an entity UID, which acts as a
/// key, such that an entity can only have one running tabletop game session.
/// </summary>
private readonly Dictionary<EntityUid, TabletopSession> _gameSessions = new();
public override void Initialize()
{
SubscribeNetworkEvent<TabletopMoveEvent>(OnTabletopMove);
SubscribeNetworkEvent<TabletopDraggingPlayerChangedEvent>(OnDraggingPlayerChanged);
SubscribeNetworkEvent<TabletopStopPlayingEvent>(OnStopPlaying);
SubscribeLocalEvent<TabletopGameComponent, ComponentShutdown>(OnGameShutdown);
SubscribeLocalEvent<TabletopDraggableComponent, ComponentGetState>(GetCompState);
}
private void GetCompState(EntityUid uid, TabletopDraggableComponent component, ref ComponentGetState args)
{
args.State = new TabletopDraggableComponentState(component.DraggingPlayer);
}
/// <summary>
/// For a specific user, create a table if it does not exist yet and let the user open a UI window to play it.
/// </summary>
/// <param name="user">The user entity for which to open the window.</param>
/// <param name="table">The entity with which the tabletop game session will be associated.</param>
public void OpenTable(IEntity user, IEntity table)
{
if (user.PlayerSession() is not { } playerSession) return;
// Make sure we have a session, and add the player to it
var session = EnsureSession(table.Uid);
session.StartPlaying(playerSession);
// Create a camera for the user to use
// TODO: set correct coordinates, depending on the piece the game was started from
IEntity camera = CreateCamera(user, new MapCoordinates(0, 0, session.MapId));
// Tell the client to open a viewport for the tabletop game
// TODO: use actual title/size from prototype, for now we assume its chess
RaiseNetworkEvent(new TabletopPlayEvent(table.Uid, camera.Uid, "Chess", (274 + 64, 274)), playerSession.ConnectedClient);
}
/// <summary>
/// Create a session associated to this entity UID, if it does not already exist, and return it.
/// </summary>
/// <param name="uid">The entity UID to ensure a session for.</param>
/// <returns>The created/stored tabletop game session.</returns>
private TabletopSession EnsureSession(EntityUid uid)
{
// We already have a session, return it
// TODO: if tables are connected, treat them as a single entity
if (_gameSessions.ContainsKey(uid))
{
return _gameSessions[uid];
}
// Session does not exist for this entity yet, create a map and create a session
var mapId = _mapManager.CreateMap();
// Tabletop maps do not need lighting, turn it off
var mapComponent = _mapManager.GetMapEntity(mapId).GetComponent<IMapComponent>();
mapComponent.LightingEnabled = false;
mapComponent.Dirty();
_gameSessions.Add(uid, new TabletopSession(mapId));
var session = _gameSessions[uid];
// Since this is the first time opening this session, set up the game
// TODO: don't assume we're playing chess
SetupChessBoard(session.MapId);
return session;
}
#region Event handlers
// Move an entity which is dragged by the user, but check if they are allowed to do so and to these coordinates
private void OnTabletopMove(TabletopMoveEvent msg, EntitySessionEventArgs args)
{
if (args.SenderSession as IPlayerSession is not { AttachedEntity: { } playerEntity } playerSession) return;
// Check if player is actually playing at this table
if (!_gameSessions.TryGetValue(msg.TableUid, out var tableUid) ||
!tableUid.IsPlaying(playerSession)) return;
// Return if can not see table or stunned/no hands
if (!EntityManager.TryGetEntity(msg.TableUid, out var table)) return;
if (!CanSeeTable(playerEntity, table) || StunnedOrNoHands(playerEntity)) return;
// Check if moved entity exists and has tabletop draggable component
if (!EntityManager.TryGetEntity(msg.MovedEntityUid, out var movedEntity)) return;
if (!ComponentManager.HasComponent<TabletopDraggableComponent>(movedEntity.Uid)) return;
// TODO: some permission system, disallow movement if you're not permitted to move the item
// Move the entity and dirty it (we use the map ID from the entity so noone can try to be funny and move the item to another map)
var transform = ComponentManager.GetComponent<ITransformComponent>(movedEntity.Uid);
var entityCoordinates = new EntityCoordinates(_mapManager.GetMapEntityId(transform.MapID), msg.Coordinates.Position);
transform.Coordinates = entityCoordinates;
movedEntity.Dirty();
}
private void OnDraggingPlayerChanged(TabletopDraggingPlayerChangedEvent msg)
{
var draggedEntity = EntityManager.GetEntity(msg.DraggedEntityUid);
if (!draggedEntity.TryGetComponent<TabletopDraggableComponent>(out var draggableComponent)) return;
draggableComponent.DraggingPlayer = msg.DraggingPlayer;
if (!draggedEntity.TryGetComponent<AppearanceComponent>(out var appearance)) return;
if (draggableComponent.DraggingPlayer != null)
{
appearance.SetData(TabletopItemVisuals.Scale, new Vector2(1.25f, 1.25f));
appearance.SetData(TabletopItemVisuals.DrawDepth, (int) DrawDepth.Items + 1);
}
else
{
appearance.SetData(TabletopItemVisuals.Scale, Vector2.One);
appearance.SetData(TabletopItemVisuals.DrawDepth, (int) DrawDepth.Items);
}
}
private void OnStopPlaying(TabletopStopPlayingEvent msg, EntitySessionEventArgs args)
{
if (_gameSessions.ContainsKey(msg.TableUid) && args.SenderSession as IPlayerSession is { } playerSession)
{
_gameSessions[msg.TableUid].StopPlaying(playerSession);
}
}
// TODO: needs to be refactored such that the corresponding entity on the table gets removed, instead of the whole map
private void OnGameShutdown(EntityUid uid, TabletopGameComponent component, ComponentShutdown args)
{
if (!_gameSessions.ContainsKey(uid)) return;
// Delete the map and remove it from the list of sessions
_mapManager.DeleteMap(_gameSessions[uid].MapId);
_gameSessions.Remove(uid);
}
#endregion
#region Utility
/// <summary>
/// Create a camera entity for a user to control, and add the user to the view subscribers.
/// </summary>
/// <param name="user">The user entity to create this camera for and add to the view subscribers.</param>
/// <param name="coordinates">The map coordinates to spawn this camera at.</param>
// TODO: this can probably be generalized into a "CctvSystem" or whatever
private IEntity CreateCamera(IEntity user, MapCoordinates coordinates)
{
// Spawn an empty entity at the coordinates
var camera = EntityManager.SpawnEntity(null, coordinates);
// Add an eye component and disable FOV
var eyeComponent = camera.EnsureComponent<EyeComponent>();
eyeComponent.DrawFov = false;
// Add the user to the view subscribers. If there is no player session, just skip this step
if (user.PlayerSession() is { } playerSession)
{
_viewSubscriberSystem.AddViewSubscriber(camera.Uid, playerSession);
}
return camera;
}
#endregion
}
}