From 1fd30f1a16c3005bf95fef7f2c95e76cd9ae43df Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad <600878@stud.hvl.no> Date: Mon, 22 May 2023 22:57:45 +0200 Subject: [PATCH] Added some box and pellet logic --- .../ClientApp/src/components/gameBoard.tsx | 2 +- .../ClientApp/src/game/character.ts | 23 ++++++++++++------- .../src/game/possibleMovesAlgorithm.ts | 7 +++--- .../Controllers/GameController.cs | 10 ++++++++ pac-man-board-game/Game/Actions.cs | 1 + pac-man-board-game/Game/Interfaces/IBox.cs | 6 +++-- pac-man-board-game/Game/Interfaces/IOrb.cs | 6 ----- pac-man-board-game/Game/Interfaces/IPellet.cs | 12 ++++++++++ pac-man-board-game/Game/Interfaces/IPlayer.cs | 6 +++++ pac-man-board-game/Game/Items/Box.cs | 17 ++++++++++++++ pac-man-board-game/Game/Items/Pellet.cs | 8 +++++++ pac-man-board-game/Game/Items/Player.cs | 8 +++++++ 12 files changed, 86 insertions(+), 20 deletions(-) delete mode 100644 pac-man-board-game/Game/Interfaces/IOrb.cs create mode 100644 pac-man-board-game/Game/Interfaces/IPellet.cs create mode 100644 pac-man-board-game/Game/Interfaces/IPlayer.cs create mode 100644 pac-man-board-game/Game/Items/Box.cs create mode 100644 pac-man-board-game/Game/Items/Pellet.cs create mode 100644 pac-man-board-game/Game/Items/Player.cs diff --git a/pac-man-board-game/ClientApp/src/components/gameBoard.tsx b/pac-man-board-game/ClientApp/src/components/gameBoard.tsx index 2387dab..9324cf1 100644 --- a/pac-man-board-game/ClientApp/src/components/gameBoard.tsx +++ b/pac-man-board-game/ClientApp/src/components/gameBoard.tsx @@ -58,7 +58,7 @@ const Board: Component<BoardProps> = ( useEffect(() => { if (selectedCharacter && selectedDice) { - const possiblePositions = findPossiblePositions(map, selectedCharacter.position, selectedDice.value); + const possiblePositions = findPossiblePositions(map, selectedCharacter, selectedDice.value); setPossiblePositions(possiblePositions); } else { setPossiblePositions([]); diff --git a/pac-man-board-game/ClientApp/src/game/character.ts b/pac-man-board-game/ClientApp/src/game/character.ts index cdfa64c..33bdfcb 100644 --- a/pac-man-board-game/ClientApp/src/game/character.ts +++ b/pac-man-board-game/ClientApp/src/game/character.ts @@ -1,31 +1,38 @@ type CharacterColor = "red" | "blue" | "yellow" | "green" | "purple"; +type Direction = "up" | "right" | "down" | "left"; export abstract class Character { public color: CharacterColor; public position: Position; + public direction: Direction = "up"; + public isEatable: boolean = false; - public constructor(color: CharacterColor, startPosition: Position = {x: 0, y: 0}) { + protected constructor(color: CharacterColor, startPosition: Position = {x: 0, y: 0}) { this.color = color; this.position = startPosition; } - public abstract moveTo(position: Position): void; - + public moveTo(position: Position): void { + this.position = position; + } + public isAt(position: Position): boolean { return this.position.x === position.x && this.position.y === position.y; } } export class PacMan extends Character { - moveTo(position: Position): void { - this.position = position; + + constructor(color: CharacterColor, startPosition: Position = {x: 0, y: 0}) { + super(color, startPosition); + this.isEatable = true; } } export class Ghost extends Character { - moveTo(position: Position): void { - this.position = position; - } + constructor(color: CharacterColor, startPosition: Position = {x: 0, y: 0}) { + super(color, startPosition); + } } diff --git a/pac-man-board-game/ClientApp/src/game/possibleMovesAlgorithm.ts b/pac-man-board-game/ClientApp/src/game/possibleMovesAlgorithm.ts index ef31ed6..0e00969 100644 --- a/pac-man-board-game/ClientApp/src/game/possibleMovesAlgorithm.ts +++ b/pac-man-board-game/ClientApp/src/game/possibleMovesAlgorithm.ts @@ -1,14 +1,15 @@ import {TileType} from "./tileType"; +import {Character} from "./character"; /** * Finds all the possible positions for the character to move to * @param board The board the character is on - * @param currentPos The current position of the character + * @param character The current position of the character * @param steps The number of steps the character can move */ -export default function findPossiblePositions(board: number[][], currentPos: Position, steps: number): Position[] { +export default function findPossiblePositions(board: number[][], character: Character, steps: number): Position[] { const possiblePositions: Position[] = []; - findPossibleRecursive(board, currentPos, steps, possiblePositions, []); + findPossibleRecursive(board, character.position, steps, possiblePositions, []); return possiblePositions; } diff --git a/pac-man-board-game/Controllers/GameController.cs b/pac-man-board-game/Controllers/GameController.cs index d1b468d..c0de2cf 100644 --- a/pac-man-board-game/Controllers/GameController.cs +++ b/pac-man-board-game/Controllers/GameController.cs @@ -13,10 +13,15 @@ namespace pacMan.Controllers; public class GameController : GenericController { private readonly IDiceCup _diceCup; + private readonly IPlayer _player; public GameController(ILogger<GameController> logger, IWebSocketService wsService) : base(logger, wsService) { _diceCup = new DiceCup(); + _player = new Player + { + Box = new Box() + }; } [HttpGet] @@ -43,6 +48,11 @@ public class GameController : GenericController message.Data = rolls; break; + case GameAction.AppendBox: + // TODO + // Add pellets to box + // Forward box to all clients + break; default: Logger.Log(LogLevel.Information, "Forwarding message to all clients"); break; diff --git a/pac-man-board-game/Game/Actions.cs b/pac-man-board-game/Game/Actions.cs index 0bf0cd9..447b5d4 100644 --- a/pac-man-board-game/Game/Actions.cs +++ b/pac-man-board-game/Game/Actions.cs @@ -6,6 +6,7 @@ public enum GameAction { RollDice, MoveCharacter, + AppendBox } public class ActionMessage<T> diff --git a/pac-man-board-game/Game/Interfaces/IBox.cs b/pac-man-board-game/Game/Interfaces/IBox.cs index e21f934..337fcd0 100644 --- a/pac-man-board-game/Game/Interfaces/IBox.cs +++ b/pac-man-board-game/Game/Interfaces/IBox.cs @@ -1,6 +1,8 @@ namespace pacMan.Game.Interfaces; -public interface IBox : IEnumerable<IOrb> +public interface IBox : IEnumerable<IPellet> { - void Add(IOrb orb); + void Add(IPellet pellet); + + int CountNormal { get; } } \ No newline at end of file diff --git a/pac-man-board-game/Game/Interfaces/IOrb.cs b/pac-man-board-game/Game/Interfaces/IOrb.cs deleted file mode 100644 index 8b1a372..0000000 --- a/pac-man-board-game/Game/Interfaces/IOrb.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace pacMan.Game.Interfaces; - -public interface IOrb -{ - void Use(); -} \ No newline at end of file diff --git a/pac-man-board-game/Game/Interfaces/IPellet.cs b/pac-man-board-game/Game/Interfaces/IPellet.cs new file mode 100644 index 0000000..529af4c --- /dev/null +++ b/pac-man-board-game/Game/Interfaces/IPellet.cs @@ -0,0 +1,12 @@ +namespace pacMan.Game.Interfaces; + +public enum PelletType +{ + Normal, + PowerPellet +} + +public interface IPellet +{ + PelletType Get { get; set; } +} \ No newline at end of file diff --git a/pac-man-board-game/Game/Interfaces/IPlayer.cs b/pac-man-board-game/Game/Interfaces/IPlayer.cs new file mode 100644 index 0000000..d6531ae --- /dev/null +++ b/pac-man-board-game/Game/Interfaces/IPlayer.cs @@ -0,0 +1,6 @@ +namespace pacMan.Game.Interfaces; + +public interface IPlayer +{ + IBox Box { get; init; } +} \ No newline at end of file diff --git a/pac-man-board-game/Game/Items/Box.cs b/pac-man-board-game/Game/Items/Box.cs new file mode 100644 index 0000000..95e5259 --- /dev/null +++ b/pac-man-board-game/Game/Items/Box.cs @@ -0,0 +1,17 @@ +using System.Collections; +using pacMan.Game.Interfaces; + +namespace pacMan.Game.Items; + +public class Box : IBox +{ + private readonly IList<IPellet> _pellets = new List<IPellet>(); + + public int CountNormal => _pellets.Count(pellet => pellet.Get == PelletType.Normal); + + public void Add(IPellet pellet) => _pellets.Add(pellet); + + public IEnumerator<IPellet> GetEnumerator() => _pellets.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); +} \ No newline at end of file diff --git a/pac-man-board-game/Game/Items/Pellet.cs b/pac-man-board-game/Game/Items/Pellet.cs new file mode 100644 index 0000000..6a80f60 --- /dev/null +++ b/pac-man-board-game/Game/Items/Pellet.cs @@ -0,0 +1,8 @@ +using pacMan.Game.Interfaces; + +namespace pacMan.Game.Items; + +public class Pellet : IPellet +{ + public PelletType Get { get; set; } +} \ No newline at end of file diff --git a/pac-man-board-game/Game/Items/Player.cs b/pac-man-board-game/Game/Items/Player.cs new file mode 100644 index 0000000..7767065 --- /dev/null +++ b/pac-man-board-game/Game/Items/Player.cs @@ -0,0 +1,8 @@ +using pacMan.Game.Interfaces; + +namespace pacMan.Game.Items; + +public class Player : IPlayer +{ + public required IBox Box { get; init; } +} \ No newline at end of file