Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#90] Got rid of repetitive switch statements #96

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions development/src/ui/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ import { Item } from '../logic/items';
import { pieces } from '../logic/logic';
import { Piece, Square } from '../logic/pieces';

interface ChessBoardType {
boardElement: HTMLElement;
boardButton?: HTMLElement;
lightSquareColor: string;
darkSquareColor: string;
}
export class ChessBoard implements ChessBoardType {

export class ChessBoard {
boardElement: HTMLElement;
boardButtonElement: HTMLElement;
lightSquareColor: string;
darkSquareColor: string;

constructor(
boardContainer: HTMLElement,
boardElement: HTMLElement,
boardButtonElement: HTMLElement,
lightSquareColor: string,
darkSquareColor: string,
) {
this.boardElement = boardContainer;
this.boardElement = boardElement;
this.boardButtonElement = boardButtonElement;
this.darkSquareColor = darkSquareColor;
this.lightSquareColor = lightSquareColor;

Expand Down
84 changes: 29 additions & 55 deletions development/src/ui/boards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
LIGHT_OVERWORLD_SQUARE_COLOR,
NOTATIONS_LETTERS,
NOTATIONS_NUMBERS,
OVERWORLD_BOARD_BUTTON_ID,
OVERWORLD_BOARD_ID,
} from '../logic/constants';
import { Item } from '../logic/items';
Expand All @@ -40,6 +41,9 @@ export const BOTTOM_NOTATION_CONTAINER = document.getElementById(
export const LEFT_NOTATION_CONTAINER = document.getElementById(
LEFT_NOTATION_ID,
) as HTMLElement;
const OVERWORLD_BOARD_BUTTON = document.getElementById(
OVERWORLD_BOARD_BUTTON_ID,
) as HTMLElement;
const HELL_BOARD_BUTTON = document.getElementById(
HELL_BOARD_BUTTON_ID,
) as HTMLElement;
Expand All @@ -50,16 +54,19 @@ const HEAVEN_BOARD_BUTTON = document.getElementById(
export function initializeBoards() {
overworld = new ChessBoard(
OVERWORLD_BOARD,
OVERWORLD_BOARD_BUTTON,
LIGHT_OVERWORLD_SQUARE_COLOR,
DARK_OVERWORLD_SQUARE_COLOR,
);
hell = new ChessBoard(
HELL_BOARD,
HELL_BOARD_BUTTON,
LIGHT_HELL_SQUARE_COLOR,
DARK_HELL_SQUARE_COLOR,
);
heaven = new ChessBoard(
HEAVEN_BOARD,
HEAVEN_BOARD_BUTTON,
LIGHT_HEAVEN_SQUARE_COLOR,
DARK_HEAVEN_SQUARE_COLOR,
);
Expand All @@ -73,6 +80,16 @@ export function generateNotations(){
}
}

function getBoardbyId(boardId: string): ChessBoard {
switch (boardId) {
case HELL_BOARD_ID:
return hell;
case HEAVEN_BOARD_ID:
return heaven;
default:
return overworld;
}
}

export function createNotationGraphics(notation: string) {
const notationElement = document.createElement('p');
Expand All @@ -88,73 +105,30 @@ export function createNotationGraphics(notation: string) {
}

export function movePieceOnBoard(draggedPiece: Piece, targetSquare: Square) {
switch (draggedPiece.position.board) {
case OVERWORLD_BOARD_ID:
overworld.movePieceOnBoard(draggedPiece, targetSquare);
break;
case HELL_BOARD_ID:
hell.movePieceOnBoard(draggedPiece, targetSquare);
break;
case HEAVEN_BOARD_ID:
heaven.movePieceOnBoard(draggedPiece, targetSquare);
}
const board = getBoardbyId(draggedPiece.position.board);
board.movePieceOnBoard(draggedPiece, targetSquare);
}

export function destroyPieceOnBoard(targetPiece: Piece) {
switch (targetPiece.position.board) {
case OVERWORLD_BOARD_ID:
overworld.destroyPieceOnBoard(targetPiece);
break;
case HELL_BOARD_ID:
hell.destroyPieceOnBoard(targetPiece);
break;
case HEAVEN_BOARD_ID:
heaven.destroyPieceOnBoard(targetPiece);
}
const board = getBoardbyId(targetPiece.position.board);
board.destroyPieceOnBoard(targetPiece);
}

export function destroyItemOnBoard(targetItem: Item) {
switch (targetItem.position.board) {
case OVERWORLD_BOARD_ID:
overworld.destroyItemOnBoard(targetItem);
break;
case HELL_BOARD_ID:
hell.destroyItemOnBoard(targetItem);
break;
case HEAVEN_BOARD_ID:
heaven.destroyItemOnBoard(targetItem);
}
const board = getBoardbyId(targetItem.position.board);
board.destroyItemOnBoard(targetItem);

}

export function spawnPieceOnBoard(piece: Piece) {
switch (piece.position.board) {
case HELL_BOARD_ID:
hell.spawnPieceOnBoard(piece);
HELL_BOARD_BUTTON.classList.remove('collapsed');
break;
case HEAVEN_BOARD_ID:
heaven.spawnPieceOnBoard(piece);
HEAVEN_BOARD_BUTTON.classList.remove('collapsed');
break;
default:
return;
}
const board = getBoardbyId(piece.position.board);
board.spawnPieceOnBoard(piece);
board.boardButtonElement.classList.remove('collapsed');
}

export function spawnItemOnBoard(item: Item) {
switch (item.position.board) {
case OVERWORLD_BOARD_ID:
overworld.spawnItemOnBoard(item);
break;
case HELL_BOARD_ID:
hell.spawnItemOnBoard(item);
break;
case HEAVEN_BOARD_ID:
heaven.spawnItemOnBoard(item);
break;
default:
return;
}
const board = getBoardbyId(item.position.board);
board.spawnItemOnBoard(item);
}

export function highlightSquare(target: HTMLElement, shouldHighlight: boolean) {
Expand Down
Loading