From b52b063d6f2158eb56ff9c22e936b5d3e29ff145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?/=CA=8Ani=C9=B9=C9=91=CB=90/?= Date: Sun, 3 Sep 2023 08:16:09 +0200 Subject: [PATCH] refactor checkerboard (#18359) --- .../Tabletop/TabletopCheckerSetup.cs | 120 +++++++++++------- .../Objects/Fun/Tabletop/checkers.yml | 28 ++-- ..._checker_queen.png => b_checker_crown.png} | Bin .../Fun/Tabletop/checker_pieces.rsi/meta.json | 4 +- ..._checker_queen.png => w_checker_crown.png} | Bin 5 files changed, 89 insertions(+), 63 deletions(-) rename Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/{b_checker_queen.png => b_checker_crown.png} (100%) rename Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/{w_checker_queen.png => w_checker_crown.png} (100%) diff --git a/Content.Server/Tabletop/TabletopCheckerSetup.cs b/Content.Server/Tabletop/TabletopCheckerSetup.cs index 4a441c502f..aa38bac7a3 100644 --- a/Content.Server/Tabletop/TabletopCheckerSetup.cs +++ b/Content.Server/Tabletop/TabletopCheckerSetup.cs @@ -9,74 +9,96 @@ namespace Content.Server.Tabletop public sealed partial class TabletopCheckerSetup : TabletopSetup { - // TODO: Un-hardcode the rest of entity prototype IDs, probably. + [DataField("prototypePieceWhite", customTypeSerializer: typeof(PrototypeIdSerializer))] + public string PrototypePieceWhite = default!; + + [DataField("prototypeCrownWhite", customTypeSerializer: typeof(PrototypeIdSerializer))] + public string PrototypeCrownWhite = default!; + + [DataField("prototypePieceBlack", customTypeSerializer: typeof(PrototypeIdSerializer))] + public string PrototypePieceBlack = default!; + + [DataField("prototypeCrownBlack", customTypeSerializer: typeof(PrototypeIdSerializer))] + public string PrototypeCrownBlack = default!; public override void SetupTabletop(TabletopSession session, IEntityManager entityManager) { - var checkerboard = entityManager.SpawnEntity(BoardPrototype, session.Position.Offset(-1, 0)); - - session.Entities.Add(checkerboard); + session.Entities.Add( + entityManager.SpawnEntity(BoardPrototype, session.Position.Offset(-1, 0)) + ); SpawnPieces(session, entityManager, session.Position.Offset(-4.5f, 3.5f)); } - private void SpawnPieces(TabletopSession session, IEntityManager entityManager, MapCoordinates topLeft, float separation = 1f) + private void SpawnPieces(TabletopSession session, IEntityManager entityManager, MapCoordinates left) { - var (mapId, x, y) = topLeft; + static float GetOffset(float offset) => offset * 1f /* separation */; - // Spawn all black pieces - SpawnPieces(session, entityManager, "Black", topLeft, separation); + Span pieces = stackalloc EntityUid[42]; + var pieceIndex = 0; - // Spawn all white pieces - SpawnPieces(session, entityManager, "White", new MapCoordinates(x, y - 7 * separation, mapId), separation); - - // Queens - for (int i = 1; i < 4; i++) + // Pieces + for (var offsetY = 0; offsetY < 3; offsetY++) { - EntityUid tempQualifier = entityManager.SpawnEntity("BlackCheckerQueen", new MapCoordinates(x + 9 * separation + 9f / 32, y - (i - 1) * separation, mapId)); - session.Entities.Add(tempQualifier); + var checker = offsetY % 2; - EntityUid tempQualifier1 = entityManager.SpawnEntity("WhiteCheckerQueen", new MapCoordinates(x + 8 * separation + 9f / 32, y - (i - 1) * separation, mapId)); - session.Entities.Add(tempQualifier1); + for (var offsetX = 0; offsetX < 8; offsetX += 2) + { + // Prevents an extra piece on the middle row + if (checker + offsetX > 8) continue; + + pieces[pieceIndex] = entityManager.SpawnEntity( + PrototypePieceBlack, + left.Offset(GetOffset(offsetX + (1 - checker)), GetOffset(offsetY * -1)) + ); + pieces[pieceIndex] = entityManager.SpawnEntity( + PrototypePieceWhite, + left.Offset(GetOffset(offsetX + checker), GetOffset(offsetY - 7)) + ); + pieceIndex += 2; + } } - var spares = new List(); - for (var i = 1; i < 7; i++) - { - var step = 3 + 0.25f * (i - 1); - spares.Add( - entityManager.SpawnEntity("BlackCheckerPiece", - new MapCoordinates(x + 9 * separation + 9f / 32, y - step * separation, mapId) - )); + const int NumCrowns = 3; + const float Overlap = 0.25f; + const float xOffset = 9f / 32; + const float xOffsetBlack = 9 + xOffset; + const float xOffsetWhite = 8 + xOffset; - spares.Add( - entityManager.SpawnEntity("WhiteCheckerPiece", - new MapCoordinates(x + 8 * separation + 9f / 32, y - step * separation, mapId) - )); + // Crowns + for (var i = 0; i < NumCrowns; i++) + { + var step = -(Overlap * i); + pieces[pieceIndex] = entityManager.SpawnEntity( + PrototypeCrownBlack, + left.Offset(GetOffset(xOffsetBlack), GetOffset(step)) + ); + pieces[pieceIndex + 1] = entityManager.SpawnEntity( + PrototypeCrownWhite, + left.Offset(GetOffset(xOffsetWhite), GetOffset(step)) + ); + pieceIndex += 2; } - session.Entities.UnionWith(spares); - } - private void SpawnPieces(TabletopSession session, IEntityManager entityManager, string color, MapCoordinates left, float separation = 1f) - { - var (mapId, x, y) = left; - // If white is being placed it must go from bottom->up - var reversed = (color == "White") ? 1 : -1; - - for (int i = 0; i < 3; i++) + // Spares + for (var i = 0; i < 6; i++) { - var x_offset = i % 2; - if (reversed == -1) x_offset = 1 - x_offset; // Flips it - - for (int j = 0; j < 8; j += 2) - { - // Prevents an extra piece on the middle row - if (x_offset + j > 8) continue; - - EntityUid tempQualifier4 = entityManager.SpawnEntity(color + "CheckerPiece", new MapCoordinates(x + (j + x_offset) * separation, y + i * reversed * separation, mapId)); - session.Entities.Add(tempQualifier4); + var step = -((Overlap * (NumCrowns + 2)) + (Overlap * i)); + pieces[pieceIndex] = entityManager.SpawnEntity( + PrototypePieceBlack, + left.Offset(GetOffset(xOffsetBlack), GetOffset(step)) + ); + pieces[pieceIndex] = entityManager.SpawnEntity( + PrototypePieceWhite, + left.Offset(GetOffset(xOffsetWhite), GetOffset(step)) + ); + pieceIndex += 2; + } + + for (var i = 0; i < pieces.Length; i++) + { + session.Entities.Add(pieces[i]); } - } } } } diff --git a/Resources/Prototypes/Entities/Objects/Fun/Tabletop/checkers.yml b/Resources/Prototypes/Entities/Objects/Fun/Tabletop/checkers.yml index c6aa12db3e..69302548d1 100644 --- a/Resources/Prototypes/Entities/Objects/Fun/Tabletop/checkers.yml +++ b/Resources/Prototypes/Entities/Objects/Fun/Tabletop/checkers.yml @@ -13,11 +13,15 @@ size: 338, 274 setup: !type:TabletopCheckerSetup - boardPrototype: CheckerBoardTabletop + boardPrototype: &checkerboard CheckerBoardTabletop + prototypePieceWhite: &pieceWhite CheckerPieceWhite + prototypeCrownWhite: &crownWhite CheckerCrownWhite + prototypePieceBlack: &pieceBlack CheckerPieceBlack + prototypeCrownBlack: &crownBlack CheckerCrownBlack # Checkerboard tabletop item (item only visible in tabletop game) - type: entity - id: CheckerBoardTabletop + id: *checkerboard name: checkerboard parent: BaseBoardTabletop noSpawn: true @@ -28,8 +32,8 @@ # The pieces - type: entity - id: WhiteCheckerPiece - name: white piece + id: *pieceWhite + name: white checker piece parent: BaseTabletopPiece components: - type: Sprite @@ -37,17 +41,17 @@ state: w_checker_piece - type: entity - id: WhiteCheckerQueen - name: white queen + id: *crownWhite + name: white checker crown parent: BaseTabletopPiece components: - type: Sprite sprite: Objects/Fun/Tabletop/checker_pieces.rsi - state: w_checker_queen + state: w_checker_crown - type: entity - id: BlackCheckerPiece - name: black piece + id: *pieceBlack + name: black checker piece parent: BaseTabletopPiece components: - type: Sprite @@ -55,10 +59,10 @@ state: b_checker_piece - type: entity - id: BlackCheckerQueen - name: black queen + id: *crownBlack + name: black checker crown parent: BaseTabletopPiece components: - type: Sprite sprite: Objects/Fun/Tabletop/checker_pieces.rsi - state: b_checker_queen + state: b_checker_crown diff --git a/Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/b_checker_queen.png b/Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/b_checker_crown.png similarity index 100% rename from Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/b_checker_queen.png rename to Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/b_checker_crown.png diff --git a/Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/meta.json b/Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/meta.json index ceb785d8bd..d34294192a 100644 --- a/Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/meta.json +++ b/Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/meta.json @@ -11,13 +11,13 @@ "name": "w_checker_piece" }, { - "name": "w_checker_queen" + "name": "w_checker_crown" }, { "name": "b_checker_piece" }, { - "name": "b_checker_queen" + "name": "b_checker_crown" } ] } diff --git a/Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/w_checker_queen.png b/Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/w_checker_crown.png similarity index 100% rename from Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/w_checker_queen.png rename to Resources/Textures/Objects/Fun/Tabletop/checker_pieces.rsi/w_checker_crown.png