Added some box and pellet logic

This commit is contained in:
Martin Berg Alstad 2023-05-22 22:57:45 +02:00
parent 485014d28d
commit 1fd30f1a16
12 changed files with 86 additions and 20 deletions

View File

@ -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([]);

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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;

View File

@ -6,6 +6,7 @@ public enum GameAction
{
RollDice,
MoveCharacter,
AppendBox
}
public class ActionMessage<T>

View File

@ -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; }
}

View File

@ -1,6 +0,0 @@
namespace pacMan.Game.Interfaces;
public interface IOrb
{
void Use();
}

View File

@ -0,0 +1,12 @@
namespace pacMan.Game.Interfaces;
public enum PelletType
{
Normal,
PowerPellet
}
public interface IPellet
{
PelletType Get { get; set; }
}

View File

@ -0,0 +1,6 @@
namespace pacMan.Game.Interfaces;
public interface IPlayer
{
IBox Box { get; init; }
}

View File

@ -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();
}

View File

@ -0,0 +1,8 @@
using pacMan.Game.Interfaces;
namespace pacMan.Game.Items;
public class Pellet : IPellet
{
public PelletType Get { get; set; }
}

View File

@ -0,0 +1,8 @@
using pacMan.Game.Interfaces;
namespace pacMan.Game.Items;
public class Player : IPlayer
{
public required IBox Box { get; init; }
}