Refactored names

This commit is contained in:
martin 2023-06-25 22:21:47 +02:00
parent c469b92739
commit 49b8d05f97
7 changed files with 67 additions and 55 deletions

View File

@ -1,3 +1,3 @@
PORT=44435 PORT=44435
HTTPS=true HTTPS=true
VITE_API=wss://localhost:3000/api/game

View File

@ -5,28 +5,25 @@ import GameBoard from "./gameBoard";
import {Character, Ghost, PacMan} from "../game/character"; import {Character, Ghost, PacMan} from "../game/character";
import WebSocketService from "../websockets/WebSocketService"; import WebSocketService from "../websockets/WebSocketService";
import {testMap} from "../game/map"; import {testMap} from "../game/map";
import {Direction} from "../game/direction";
import {TileType} from "../game/tileType"; import {TileType} from "../game/tileType";
import Player from "../game/player"; import Player from "../game/player";
const wsService = new WebSocketService("wss://localhost:3000/api/game"); const wsService = new WebSocketService(import.meta.env.VITE_API);
const ghosts = [
new Ghost({Colour: "purple"}),
new Ghost({Colour: "purple"})
];
export const GameComponent: Component<{ player: Player }> = ( export const GameComponent: Component<{ player: Player }> = (
{ {
player = new Player({ player = new Player({
name: "Martin", Name: "Martin",
colour: "yellow", Colour: "yellow",
}) })
}) => { }) => {
// TODO find spawn points // TODO find spawn points
const [characters, setCharacters] = useState([ const [characters, setCharacters] = useState<Character[]>();
new Ghost({
colour: "purple", spawnPosition: {At: {x: 7, y: 3}, Direction: Direction.up}
}),
new Ghost({
colour: "purple", spawnPosition: {At: {x: 3, y: 7}, Direction: Direction.down}
})
]);
const [dice, setDice] = useState<number[]>(); const [dice, setDice] = useState<number[]>();
const [selectedDice, setSelectedDice] = useState<SelectedDice>(); const [selectedDice, setSelectedDice] = useState<SelectedDice>();
@ -64,8 +61,11 @@ export const GameComponent: Component<{ player: Player }> = (
removeEatenPellets(parsed); removeEatenPellets(parsed);
break; break;
case GameAction.playerInfo: case GameAction.playerInfo:
const players = parsed.Data as Player[]; const players = parsed.Data as PlayerProps[];
// TODO set all characters const pacMen = players.filter(p => p.PacMan).map(p => new PacMan(p.PacMan!));
console.log(pacMen);
// TODO find spawn points
setCharacters([...pacMen, ...ghosts]);
break; break;
} }
} }
@ -129,15 +129,19 @@ export const GameComponent: Component<{ player: Player }> = (
</div> </div>
<AllDice values={dice} onclick={handleDiceClick} selectedDiceIndex={selectedDice?.index}/> <AllDice values={dice} onclick={handleDiceClick} selectedDiceIndex={selectedDice?.index}/>
{ {
(characters.filter(c => c instanceof PacMan) as PacMan[]).map(c => (characters?.filter(c => c.isPacMan()) as PacMan[] | undefined)?.map(c =>
<div key={c.Colour} className={"mx-auto w-fit m-2"}> <div key={c.Colour} className={"mx-auto w-fit m-2"}>
<p className={currentPlayer === player ? "underline" : ""}>Player: {player.Colour}</p> <p className={currentPlayer === player ? "underline" : ""}>Player: {player.Colour}</p>
<p>Pellets: {player.Box.count}</p> <p>Pellets: {player.Box.count}</p>
<p>PowerPellets: {player.Box.countPowerPellets}</p> <p>PowerPellets: {player.Box.countPowerPellets}</p>
</div>) </div>)
} }
<GameBoard className={"mx-auto my-2"} characters={characters} selectedDice={selectedDice} {characters &&
onMove={onCharacterMove} map={testMap}/> <GameBoard className={"mx-auto my-2"}
characters={characters}
selectedDice={selectedDice}
onMove={onCharacterMove} map={testMap}/>
}
</div> </div>
); );
}; };

View File

@ -15,21 +15,21 @@ export class Character {
public constructor( public constructor(
{ {
colour, Colour,
position = null, Position = null,
type = CharacterType.dummy, Type = CharacterType.dummy,
isEatable = type === CharacterType.pacMan, IsEatable = Type === CharacterType.pacMan,
spawnPosition = null SpawnPosition = null
}: CharacterProps) { }: CharacterProps) {
this.Colour = colour; this.Colour = Colour;
this.IsEatable = isEatable; this.IsEatable = IsEatable;
this.SpawnPosition = spawnPosition; this.SpawnPosition = SpawnPosition;
this.Position = position ?? spawnPosition ? { this.Position = Position ?? SpawnPosition ? {
End: spawnPosition!.At, End: SpawnPosition!.At,
Direction: spawnPosition!.Direction Direction: SpawnPosition!.Direction
} : null; } : null;
this.Type = type; this.Type = Type;
} }
public follow(path: Path): void { public follow(path: Path): void {
@ -62,16 +62,16 @@ export class Character {
export class PacMan extends Character { export class PacMan extends Character {
public constructor({colour, position, isEatable = true, spawnPosition, type = CharacterType.pacMan}: CharacterProps) { public constructor({Colour, Position, IsEatable = true, SpawnPosition, Type = CharacterType.pacMan}: CharacterProps) {
super({colour, position, isEatable, spawnPosition, type}); super({Colour: Colour, Position: Position, IsEatable: IsEatable, SpawnPosition: SpawnPosition, Type: Type});
} }
} }
export class Ghost extends Character { export class Ghost extends Character {
public constructor({colour, position, isEatable, spawnPosition, type = CharacterType.ghost}: CharacterProps) { public constructor({Colour, Position, IsEatable, SpawnPosition, Type = CharacterType.ghost}: CharacterProps) {
super({colour, position, isEatable, spawnPosition, type}); super({Colour: Colour, Position: Position, IsEatable: IsEatable, SpawnPosition: SpawnPosition, Type: Type});
} }
} }
@ -79,11 +79,11 @@ export class Dummy extends Character {
public constructor(position: Path) { // TODO see-through public constructor(position: Path) { // TODO see-through
super({ super({
colour: "grey", Colour: "grey",
position, Position: position,
isEatable: false, IsEatable: false,
spawnPosition: {At: {x: 0, y: 0}, Direction: Direction.up}, SpawnPosition: {At: {x: 0, y: 0}, Direction: Direction.up},
type: CharacterType.dummy, Type: CharacterType.dummy,
}); });
} }

View File

@ -8,12 +8,12 @@ export default class Player {
public readonly Box: Box; public readonly Box: Box;
constructor(props: PlayerProps) { constructor(props: PlayerProps) {
this.Name = props.name; this.Name = props.Name;
this.Colour = props.colour; this.Colour = props.Colour;
this.Box = new Box(props.box ?? {colour: props.colour}); this.Box = new Box(props.Box ?? {colour: props.Colour});
this.PacMan = new Character(props.pacMan ?? { this.PacMan = new Character(props.PacMan ?? {
colour: props.colour, Colour: props.Colour,
type: CharacterType.pacMan Type: CharacterType.pacMan
}); });
} }

View File

@ -12,11 +12,11 @@ interface ChildProps extends ComponentProps {
} }
interface CharacterProps { interface CharacterProps {
colour: Colour, Colour: Colour,
position?: Path | null, Position?: Path | null,
isEatable?: boolean, IsEatable?: boolean,
spawnPosition?: DirectionalPosition | null, SpawnPosition?: DirectionalPosition | null,
type?: import("../game/character").CharacterType, Type?: import("../game/character").CharacterType,
} }
interface BoxProps { interface BoxProps {
@ -25,8 +25,8 @@ interface BoxProps {
} }
interface PlayerProps { interface PlayerProps {
readonly name: string, readonly Name: string,
readonly pacMan?: CharacterProps, readonly PacMan?: CharacterProps,
readonly colour: Colour, readonly Colour: Colour,
readonly box?: BoxProps, readonly Box?: BoxProps,
} }

View File

@ -1 +1,9 @@
/// <reference types="vite/client" /> /// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_API: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}

View File

@ -8,7 +8,7 @@ let pacMan: Character;
beforeEach(() => { beforeEach(() => {
pacMan = new PacMan({ pacMan = new PacMan({
colour: "yellow", spawnPosition: {At: {x: 3, y: 3}, Direction: Direction.up} Colour: "yellow", SpawnPosition: {At: {x: 3, y: 3}, Direction: Direction.up}
}); });
}); });