Class object now implements props

This commit is contained in:
Martin Berg Alstad 2023-07-22 20:56:30 +02:00
parent f4d91e43d8
commit d98293e3e7
4 changed files with 22 additions and 24 deletions

View File

@ -17,6 +17,7 @@ const wsService = new WebSocketService(import.meta.env.VITE_API_WS);
// TODO bug, when taking player on last dice, the currentPlayer changes and the wrong character gets to steal
// TODO bug, first player can sometimes roll dice twice
// TODO bug, when refreshing page, some data is missing until other clients make a move
// TODO bug, stolen pellets are only updated on the client that stole them
// TODO guest users
// TODO protected routes? checking if user is logged in

View File

@ -1,9 +1,8 @@
import Pellet from "./pellet";
import {Colour} from "./colour";
export default class Box {
public pellets: Pellet[];
public readonly colour: Colour;
export default class Box implements BoxProps {
public pellets;
public readonly colour;
public constructor({colour, pellets = []}: BoxProps) {
this.colour = colour;

View File

@ -7,12 +7,12 @@ export enum CharacterType {
dummy,
}
export class Character {
public readonly colour: Colour;
public position: Path | null;
public isEatable: boolean;
public readonly spawnPosition: DirectionalPosition | null;
public readonly type: CharacterType;
export class Character implements CharacterProps {
public readonly colour;
public position;
public isEatable;
public readonly spawnPosition;
public readonly type;
public constructor(
{
@ -66,7 +66,7 @@ export class Character {
}
}
export class PacMan extends Character {
export class PacMan extends Character implements CharacterProps {
public constructor({colour, position, isEatable = true, spawnPosition, type = CharacterType.pacMan}: CharacterProps) {
super({colour: colour, position: position, isEatable: isEatable, spawnPosition: spawnPosition, type: type});
@ -74,14 +74,14 @@ export class PacMan extends Character {
}
export class Ghost extends Character {
export class Ghost extends Character implements CharacterProps {
public constructor({colour, position, isEatable, spawnPosition, type = CharacterType.ghost}: CharacterProps) {
super({colour: colour, position: position, isEatable: isEatable, spawnPosition: spawnPosition, type: type});
}
}
export class Dummy extends Character {
export class Dummy extends Character implements CharacterProps {
public constructor(position: Path) { // TODO see-through
super({

View File

@ -1,6 +1,5 @@
import {Character, CharacterType} from "./character";
import Box from "./box";
import {Colour} from "./colour";
import {getDefaultStore} from "jotai";
import {currentPlayerNameAtom, playersAtom} from "../utils/state";
import Pellet from "./pellet";
@ -13,12 +12,13 @@ export enum State {
disconnected
}
export default class Player {
public readonly username: string;
public readonly pacMan: Character;
public readonly colour: Colour;
public readonly box: Box;
public state: State;
export default class Player implements PlayerProps {
private static store = getDefaultStore();
public readonly username;
public readonly pacMan;
public readonly colour;
public readonly box;
public state;
constructor(props: PlayerProps) {
this.username = props.username;
@ -32,8 +32,7 @@ export default class Player {
}
public isTurn(): boolean {
const store = getDefaultStore();
return store.get(currentPlayerNameAtom) === this.username;
return Player.store.get(currentPlayerNameAtom) === this.username;
}
public addPellet(pellet: Pellet): void {
@ -46,8 +45,7 @@ export default class Player {
if (pellet)
this.box.addPellet(pellet);
}
const store = getDefaultStore();
store.set(playersAtom, store.get(playersAtom).map(player => player));
Player.store.set(playersAtom, Player.store.get(playersAtom).map(player => player));
}
}