Adds parchís game board, improves tabletop system to support other games. (#4610)

This commit is contained in:
Vera Aguilera Puerto
2021-09-18 10:40:38 +02:00
committed by GitHub
parent 5534aec102
commit e440df03db
18 changed files with 320 additions and 82 deletions

View File

@@ -2,6 +2,8 @@
using Content.Shared.Verbs;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Tabletop.Components
{
@@ -13,6 +15,18 @@ namespace Content.Server.Tabletop.Components
{
public override string Name => "TabletopGame";
[DataField("boardName")]
public string BoardName { get; } = "tabletop-default-board-name";
[DataField("setup", required: true)]
public TabletopSetup Setup { get; } = new TabletopChessSetup();
[DataField("size")]
public Vector2i Size { get; } = (300, 300);
[DataField("cameraZoom")]
public Vector2 CameraZoom { get; } = Vector2.One;
/// <summary>
/// A verb that allows the player to start playing a tabletop game.
/// </summary>

View File

@@ -0,0 +1,80 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Tabletop
{
public class TabletopChessSetup : TabletopSetup
{
[DataField("boardPrototype")]
public string ChessBoardPrototype { get; } = "ChessBoardTabletop";
// TODO: Un-hardcode the rest of entity prototype IDs, probably.
public override void SetupTabletop(MapId mapId, IEntityManager entityManager)
{
var chessboard = entityManager.SpawnEntity(ChessBoardPrototype, new MapCoordinates(-1, 0, mapId));
chessboard.Transform.Anchored = true;
SpawnPieces(entityManager, new MapCoordinates(-4.5f, 3.5f, mapId));
}
private void SpawnPieces(IEntityManager entityManager, MapCoordinates topLeft, float separation = 1f)
{
var (mapId, x, y) = topLeft;
// Spawn all black pieces
SpawnPiecesRow(entityManager, "Black", topLeft, separation);
SpawnPawns(entityManager, "Black", new MapCoordinates(x, y - separation, mapId) , separation);
// Spawn all white pieces
SpawnPawns(entityManager, "White", new MapCoordinates(x, y - 6 * separation, mapId) , separation);
SpawnPiecesRow(entityManager, "White", new MapCoordinates(x, y - 7 * separation, mapId), separation);
// Extra queens
entityManager.SpawnEntity("BlackQueen", new MapCoordinates(x + 9 * separation + 9f / 32, y - 3 * separation, mapId));
entityManager.SpawnEntity("WhiteQueen", new MapCoordinates(x + 9 * separation + 9f / 32, y - 4 * separation, mapId));
}
// TODO: refactor to load FEN instead
private void SpawnPiecesRow(IEntityManager entityManager, string color, MapCoordinates left, float separation = 1f)
{
const string piecesRow = "rnbqkbnr";
var (mapId, x, y) = left;
for (int i = 0; i < 8; i++)
{
switch (piecesRow[i])
{
case 'r':
entityManager.SpawnEntity(color + "Rook", new MapCoordinates(x + i * separation, y, mapId));
break;
case 'n':
entityManager.SpawnEntity(color + "Knight", new MapCoordinates(x + i * separation, y, mapId));
break;
case 'b':
entityManager.SpawnEntity(color + "Bishop", new MapCoordinates(x + i * separation, y, mapId));
break;
case 'q':
entityManager.SpawnEntity(color + "Queen", new MapCoordinates(x + i * separation, y, mapId));
break;
case 'k':
entityManager.SpawnEntity(color + "King", new MapCoordinates(x + i * separation, y, mapId));
break;
}
}
}
// TODO: refactor to load FEN instead
private void SpawnPawns(IEntityManager entityManager, string color, MapCoordinates left, float separation = 1f)
{
var (mapId, x, y) = left;
for (int i = 0; i < 8; i++)
{
entityManager.SpawnEntity(color + "Pawn", new MapCoordinates(x + i * separation, y, mapId));
}
}
}
}

View File

@@ -0,0 +1,60 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Tabletop
{
public class TabletopParchisSetup : TabletopSetup
{
[DataField("boardPrototype")]
public string ParchisBoardPrototype { get; } = "ParchisBoardTabletop";
[DataField("redPiecePrototype")]
public string RedPiecePrototype { get; } = "RedParchisPiece";
[DataField("greenPiecePrototype")]
public string GreenPiecePrototype { get; } = "GreenParchisPiece";
[DataField("yellowPiecePrototype")]
public string YellowPiecePrototype { get; } = "YellowParchisPiece";
[DataField("bluePiecePrototype")]
public string BluePiecePrototype { get; } = "BlueParchisPiece";
public override void SetupTabletop(MapId mapId, IEntityManager entityManager)
{
var board = entityManager.SpawnEntity(ParchisBoardPrototype, new MapCoordinates(0, 0, mapId));
board.Transform.Anchored = true;
const float x1 = 6.25f;
const float x2 = 4.25f;
const float y1 = 6.25f;
const float y2 = 4.25f;
// Red pieces.
entityManager.SpawnEntity(RedPiecePrototype, new MapCoordinates(-x1, -y1, mapId));
entityManager.SpawnEntity(RedPiecePrototype, new MapCoordinates(-x1, -y2, mapId));
entityManager.SpawnEntity(RedPiecePrototype, new MapCoordinates(-x2, -y1, mapId));
entityManager.SpawnEntity(RedPiecePrototype, new MapCoordinates(-x2, -y2, mapId));
// Green pieces.
entityManager.SpawnEntity(GreenPiecePrototype, new MapCoordinates(x1, -y1, mapId));
entityManager.SpawnEntity(GreenPiecePrototype, new MapCoordinates(x1, -y2, mapId));
entityManager.SpawnEntity(GreenPiecePrototype, new MapCoordinates(x2, -y1, mapId));
entityManager.SpawnEntity(GreenPiecePrototype, new MapCoordinates(x2, -y2, mapId));
// Yellow pieces.
entityManager.SpawnEntity(YellowPiecePrototype, new MapCoordinates(x1, y1, mapId));
entityManager.SpawnEntity(YellowPiecePrototype, new MapCoordinates(x1, y2, mapId));
entityManager.SpawnEntity(YellowPiecePrototype, new MapCoordinates(x2, y1, mapId));
entityManager.SpawnEntity(YellowPiecePrototype, new MapCoordinates(x2, y2, mapId));
// Blue pieces.
entityManager.SpawnEntity(BluePiecePrototype, new MapCoordinates(-x1, y1, mapId));
entityManager.SpawnEntity(BluePiecePrototype, new MapCoordinates(-x1, y2, mapId));
entityManager.SpawnEntity(BluePiecePrototype, new MapCoordinates(-x2, y1, mapId));
entityManager.SpawnEntity(BluePiecePrototype, new MapCoordinates(-x2, y2, mapId));
}
}
}

View File

@@ -0,0 +1,12 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Tabletop
{
[ImplicitDataDefinitionForInheritors]
public abstract class TabletopSetup
{
public abstract void SetupTabletop(MapId mapId, IEntityManager entityManager);
}
}

View File

@@ -1,73 +0,0 @@
using Robust.Shared.Map;
namespace Content.Server.Tabletop
{
public partial class TabletopSystem
{
private void SetupChessBoard(MapId mapId)
{
var chessboard = EntityManager.SpawnEntity("ChessBoardTabletop", new MapCoordinates(-1, 0, mapId));
chessboard.Transform.Anchored = true;
SpawnPieces(new MapCoordinates(-4.5f, 3.5f, mapId));
}
private void SpawnPieces(MapCoordinates topLeft, float separation = 1f)
{
var (mapId, x, y) = topLeft;
// Spawn all black pieces
SpawnPiecesRow("Black", topLeft, separation);
SpawnPawns("Black", new MapCoordinates(x, y - separation, mapId) , separation);
// Spawn all white pieces
SpawnPawns("White", new MapCoordinates(x, y - 6 * separation, mapId) , separation);
SpawnPiecesRow("White", new MapCoordinates(x, y - 7 * separation, mapId), separation);
// Extra queens
EntityManager.SpawnEntity("BlackQueen", new MapCoordinates(x + 9 * separation + 9f / 32, y - 3 * separation, mapId));
EntityManager.SpawnEntity("WhiteQueen", new MapCoordinates(x + 9 * separation + 9f / 32, y - 4 * separation, mapId));
}
// TODO: refactor to load FEN instead
private void SpawnPiecesRow(string color, MapCoordinates left, float separation = 1f)
{
const string piecesRow = "rnbqkbnr";
var (mapId, x, y) = left;
for (int i = 0; i < 8; i++)
{
switch (piecesRow[i])
{
case 'r':
EntityManager.SpawnEntity(color + "Rook", new MapCoordinates(x + i * separation, y, mapId));
break;
case 'n':
EntityManager.SpawnEntity(color + "Knight", new MapCoordinates(x + i * separation, y, mapId));
break;
case 'b':
EntityManager.SpawnEntity(color + "Bishop", new MapCoordinates(x + i * separation, y, mapId));
break;
case 'q':
EntityManager.SpawnEntity(color + "Queen", new MapCoordinates(x + i * separation, y, mapId));
break;
case 'k':
EntityManager.SpawnEntity(color + "King", new MapCoordinates(x + i * separation, y, mapId));
break;
}
}
}
// TODO: refactor to load FEN instead
private void SpawnPawns(string color, MapCoordinates left, float separation = 1f)
{
var (mapId, x, y) = left;
for (int i = 0; i < 8; i++)
{
EntityManager.SpawnEntity(color + "Pawn", new MapCoordinates(x + i * separation, y, mapId));
}
}
}
}

View File

@@ -1,5 +1,7 @@
using System.Collections.Generic;
using Content.Server.Tabletop.Components;
using Content.Shared.ActionBlocker;
using Content.Shared.Interaction;
using Content.Shared.Tabletop;
using Content.Shared.Tabletop.Events;
using JetBrains.Annotations;
@@ -8,6 +10,7 @@ using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using DrawDepth = Content.Shared.DrawDepth.DrawDepth;
@@ -19,6 +22,7 @@ namespace Content.Server.Tabletop
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly ViewSubscriberSystem _viewSubscriberSystem = default!;
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
/// <summary>
/// All tabletop games currently in progress. Sessions are associated with an entity UID, which acts as a
@@ -31,10 +35,17 @@ namespace Content.Server.Tabletop
SubscribeNetworkEvent<TabletopMoveEvent>(OnTabletopMove);
SubscribeNetworkEvent<TabletopDraggingPlayerChangedEvent>(OnDraggingPlayerChanged);
SubscribeNetworkEvent<TabletopStopPlayingEvent>(OnStopPlaying);
SubscribeLocalEvent<TabletopGameComponent, ActivateInWorldEvent>(OnTabletopActivate);
SubscribeLocalEvent<TabletopGameComponent, ComponentShutdown>(OnGameShutdown);
SubscribeLocalEvent<TabletopDraggableComponent, ComponentGetState>(GetCompState);
}
private void OnTabletopActivate(EntityUid uid, TabletopGameComponent component, ActivateInWorldEvent args)
{
if(_actionBlockerSystem.CanInteract(args.User))
OpenTable(args.User, args.Target);
}
private void GetCompState(EntityUid uid, TabletopDraggableComponent component, ref ComponentGetState args)
{
args.State = new TabletopDraggableComponentState(component.DraggingPlayer);
@@ -47,19 +58,19 @@ namespace Content.Server.Tabletop
/// <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;
if (user.PlayerSession() is not { } playerSession
|| !table.TryGetComponent(out TabletopGameComponent? tabletop)) return;
// Make sure we have a session, and add the player to it
var session = EnsureSession(table.Uid);
var session = EnsureSession(table.Uid, tabletop);
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));
IEntity camera = CreateCamera(tabletop, 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);
RaiseNetworkEvent(new TabletopPlayEvent(table.Uid, camera.Uid, Loc.GetString(tabletop.BoardName), tabletop.Size), playerSession.ConnectedClient);
}
/// <summary>
@@ -67,7 +78,7 @@ namespace Content.Server.Tabletop
/// </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)
private TabletopSession EnsureSession(EntityUid uid, TabletopGameComponent tabletop)
{
// We already have a session, return it
// TODO: if tables are connected, treat them as a single entity
@@ -88,8 +99,7 @@ namespace Content.Server.Tabletop
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);
tabletop.Setup.SetupTabletop(session.MapId, EntityManager);
return session;
}
@@ -169,10 +179,11 @@ namespace Content.Server.Tabletop
/// <summary>
/// Create a camera entity for a user to control, and add the user to the view subscribers.
/// </summary>
/// <param name="tabletop">The tabletop to create the camera for.</param>
/// <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)
private IEntity CreateCamera(TabletopGameComponent tabletop, IEntity user, MapCoordinates coordinates)
{
// Spawn an empty entity at the coordinates
var camera = EntityManager.SpawnEntity(null, coordinates);
@@ -180,6 +191,7 @@ namespace Content.Server.Tabletop
// Add an eye component and disable FOV
var eyeComponent = camera.EnsureComponent<EyeComponent>();
eyeComponent.DrawFov = false;
eyeComponent.Zoom = tabletop.CameraZoom;
// Add the user to the view subscribers. If there is no player session, just skip this step
if (user.PlayerSession() is { } playerSession)

View File

@@ -1,5 +1,10 @@
## TabletopGameComponent
tabletop-verb-play-game = Play Game
tabletop-default-board-name = Board Game
## Chess
tabletop-chess-board-name = Chess
tabletop-chess-flip = Flip
## Parchís
tabletop-parchis-board-name = Parchís

View File

@@ -9,6 +9,9 @@
sprite: Objects/Fun/Tabletop/chessboard.rsi
state: chessboard
- type: TabletopGame
boardName: tabletop-chess-board-name
size: 338, 274
setup: !type:TabletopChessSetup
# Chessboard tabletop item (item only visible in tabletop game)
- type: entity

View File

@@ -0,0 +1,74 @@
# Parchís board item (normal in game item you can hold in your hand)
- type: entity
parent: BaseItem
id: ParchisBoard
name: parchís board
description: Cross and circle board game famous for destroying countless friendships.
components:
- type: Sprite
sprite: Objects/Fun/Tabletop/parchis.rsi
state: board
- type: TabletopGame
boardName: tabletop-parchis-board-name
size: 574, 574
setup:
!type:TabletopParchisSetup
# Parchís tabletop item (item only visible in tabletop game)
- type: entity
id: ParchisBoardTabletop
name: parchís
abstract: true
components:
- type: Sprite
sprite: Objects/Fun/Tabletop/parchis_tabletop.rsi
state: board
noRot: false
drawdepth: FloorTiles
# Parchís pieces
- type: entity
id: BaseParchisPiece
parent: BaseItem
abstract: true
components:
- type: TabletopDraggable
- type: Sprite
netsync: false
noRot: true
sprite: Objects/Fun/Tabletop/parchis_pieces.rsi
- type: Appearance
visuals:
- type: TabletopItemVisualizer
- type: entity
id: RedParchisPiece
name: red piece
parent: BaseParchisPiece
components:
- type: Sprite
state: red
- type: entity
id: GreenParchisPiece
name: green piece
parent: BaseParchisPiece
components:
- type: Sprite
state: green
- type: entity
id: YellowParchisPiece
name: yellow piece
parent: BaseParchisPiece
components:
- type: Sprite
state: yellow
- type: entity
id: BlueParchisPiece
name: blue piece
parent: BaseParchisPiece
components:
- type: Sprite
state: blue

Binary file not shown.

After

Width:  |  Height:  |  Size: 451 B

View File

@@ -0,0 +1,14 @@
{
"version": 1,
"license": "CC-BY-SA-3.0",
"copyright": "Zumorica",
"size": {
"x": 18,
"y": 18
},
"states": [
{
"name": "board"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 B

View File

@@ -0,0 +1,23 @@
{
"version": 1,
"license": "CC-BY-SA-3.0",
"copyright": "Zumorica",
"size": {
"x": 24,
"y": 24
},
"states": [
{
"name": "red"
},
{
"name": "green"
},
{
"name": "yellow"
},
{
"name": "blue"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

View File

@@ -0,0 +1,14 @@
{
"version": 1,
"license": "CC-BY-SA-3.0",
"copyright": "Zumorica",
"size": {
"x": 548,
"y": 548
},
"states": [
{
"name": "board"
}
]
}