-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from Ido-Barnea/122-better-separation-of-conc…
…erns-code-organization Code Refactoring
- Loading branch information
Showing
44 changed files
with
3,617 additions
and
752 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,191 @@ | ||
import { Logger } from './ui/Logger'; | ||
import { | ||
initializeEventListeners, | ||
setOnAction, | ||
setOnFallOffTheBoard, | ||
setOnHighlight, | ||
} from './ui/Events'; | ||
import { highlightSquare, initializeBoards } from './ui/BoardManager'; | ||
import { BaseRule } from './logic/rules/BaseRule'; | ||
import { getCurrentPlayer, onAction, onFallOffTheBoard, players, roundCounter } from './logic/GameController'; | ||
import { renderScreen } from './LogicAdapter'; | ||
import { OVERWORLD_BOARD_ID } from './logic/Constants'; | ||
import { Player, PlayerColors } from './logic/Players'; | ||
import { Item } from './logic/items/Items'; | ||
import { Bishop } from './logic/pieces/Bishop'; | ||
import { King } from './logic/pieces/King'; | ||
import { Knight } from './logic/pieces/Knight'; | ||
import { Pawn } from './logic/pieces/Pawn'; | ||
import { Piece } from './logic/pieces/Pieces'; | ||
import { Queen } from './logic/pieces/Queen'; | ||
import { Rook } from './logic/pieces/Rook'; | ||
import { RulesManager } from './logic/rules/RulesManager'; | ||
|
||
const infoDisplay = document.getElementById('info-display'); | ||
const rulesContainer = document.getElementById('rules-container'); | ||
let rulesManager: RulesManager; | ||
const whitePlayer = new Player(PlayerColors.WHITE); | ||
const blackPlayer = new Player(PlayerColors.BLACK); | ||
const players: Array<Player> = [whitePlayer, blackPlayer]; | ||
let pieces: Array<Piece> = [ | ||
new Rook({ coordinates: [0, 0], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Knight({ coordinates: [1, 0], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Bishop({ coordinates: [2, 0], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Queen({ coordinates: [3, 0], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new King({ coordinates: [4, 0], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Bishop({ coordinates: [5, 0], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Knight({ coordinates: [6, 0], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Rook({ coordinates: [7, 0], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Pawn({ coordinates: [0, 1], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Pawn({ coordinates: [1, 1], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Pawn({ coordinates: [2, 1], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Pawn({ coordinates: [3, 1], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Pawn({ coordinates: [4, 1], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Pawn({ coordinates: [5, 1], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Pawn({ coordinates: [6, 1], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Pawn({ coordinates: [7, 1], boardId: OVERWORLD_BOARD_ID }, blackPlayer), | ||
new Pawn({ coordinates: [0, 6], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Pawn({ coordinates: [1, 6], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Pawn({ coordinates: [2, 6], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Pawn({ coordinates: [3, 6], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Pawn({ coordinates: [4, 6], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Pawn({ coordinates: [5, 6], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Pawn({ coordinates: [6, 6], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Pawn({ coordinates: [7, 6], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Rook({ coordinates: [0, 7], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Knight({ coordinates: [1, 7], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Bishop({ coordinates: [2, 7], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Queen({ coordinates: [3, 7], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new King({ coordinates: [4, 7], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Bishop({ coordinates: [5, 7], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Knight({ coordinates: [6, 7], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
new Rook({ coordinates: [7, 7], boardId: OVERWORLD_BOARD_ID }, whitePlayer), | ||
]; | ||
let items: Array<Item> = []; | ||
let currentPlayerIndex = 0; | ||
let turnCounter = 0; | ||
let roundCounter = 1; | ||
let deathCounter = 0; | ||
let isCastling = false; | ||
let isFriendlyFire = false; | ||
let isPieceKilled = false; | ||
let fellOffTheBoardPiece: Piece | undefined; | ||
|
||
export function updatePlayersInformation() { | ||
if (infoDisplay) infoDisplay.textContent = ''; | ||
|
||
const roundElement = document.createElement('p'); | ||
roundElement.innerHTML = `Round: ${roundCounter}`; | ||
infoDisplay?.appendChild(roundElement); | ||
function initializeGame() { | ||
rulesManager = new RulesManager(); | ||
} | ||
|
||
const playersElement = document.createElement('p'); | ||
playersElement.innerHTML = 'Players:'; | ||
function endTurn() { | ||
rulesManager.activeRules.forEach((rule) => { | ||
rule.trigger(); | ||
}); | ||
|
||
players.forEach((player) => { | ||
const playerInformationElement = document.createElement('div'); | ||
resetVariables(); | ||
|
||
const statusElement = document.createElement('p'); | ||
const isCurrentPlayer = getCurrentPlayer() === player; | ||
const title = `${isCurrentPlayer ? '> ' : ''} ${player.color} Player:`; | ||
const status = `${title} ${player.xp} XP; ${player.gold} Gold.`; | ||
statusElement.innerHTML = status; | ||
currentPlayerIndex = currentPlayerIndex + 1 < players.length ? currentPlayerIndex + 1 : 0; | ||
turnCounter++; | ||
if (turnCounter % players.length === 0) { | ||
turnCounter = 0; | ||
roundCounter++; | ||
} | ||
|
||
const inventoryElement = player.inventory.toHTMLElement(); | ||
renderScreen(); | ||
} | ||
|
||
playerInformationElement.appendChild(statusElement); | ||
playerInformationElement.appendChild(inventoryElement); | ||
function resetVariables() { | ||
isCastling = false; | ||
isFriendlyFire = false; | ||
isPieceKilled = false; | ||
fellOffTheBoardPiece = undefined; | ||
|
||
playersElement.appendChild(playerInformationElement); | ||
pieces.forEach((piece) => { | ||
if (piece.player !== getCurrentPlayer() && piece instanceof Pawn) { | ||
const pawn = piece; | ||
pawn.enPassant = false; | ||
pawn.enPassantPosition = undefined; | ||
} | ||
}); | ||
} | ||
|
||
infoDisplay?.appendChild(playersElement); | ||
function getCurrentPlayer() { | ||
return players[currentPlayerIndex]; | ||
} | ||
|
||
export function updateRules(rule: BaseRule) { | ||
const ruleElement = document.createElement('p'); | ||
ruleElement.innerHTML = `<b>${rule.id + 1}) ${rule.description}</b>`; | ||
rulesContainer?.appendChild(ruleElement); | ||
function getPlayers(): Array<Player> { | ||
return players; | ||
} | ||
|
||
function initializeGame() { | ||
Logger.logGeneral('Game started!'); | ||
initializeBoards(); | ||
initializeEventListeners(); | ||
updatePlayersInformation(); | ||
setOnAction(onAction); | ||
setOnFallOffTheBoard(onFallOffTheBoard); | ||
setOnHighlight(highlightSquare); | ||
function getPieces(): Array<Piece> { | ||
return pieces; | ||
} | ||
|
||
function setPieces(updatedPieces: Array<Piece>) { | ||
pieces = updatedPieces; | ||
} | ||
|
||
function getItems(): Array<Item> { | ||
return items; | ||
} | ||
|
||
function setItems(updatedItems: Array<Item>) { | ||
items = updatedItems; | ||
} | ||
|
||
function getRoundCounter(): number { | ||
return roundCounter; | ||
} | ||
|
||
function increaseRoundCounter() { | ||
roundCounter++; | ||
} | ||
|
||
function getDeathCounter(): number { | ||
return deathCounter; | ||
} | ||
|
||
function increaseDeathCounter() { | ||
deathCounter++; | ||
} | ||
|
||
function getIsCaslting(): boolean { | ||
return isCastling; | ||
} | ||
|
||
function switchIsCastling() { | ||
isCastling = !isCastling; | ||
} | ||
|
||
function getIsFriendlyFire(): boolean { | ||
return isFriendlyFire; | ||
} | ||
|
||
function setIsFriendlyFire(_isFriendlyFire: boolean) { | ||
isFriendlyFire = _isFriendlyFire; | ||
} | ||
|
||
function getIsPieceKilled(): boolean { | ||
return isPieceKilled; | ||
} | ||
|
||
function setIsPieceKilled(_isPieceKilled: boolean) { | ||
isPieceKilled = _isPieceKilled; | ||
} | ||
|
||
function getFellOffTheBoardPiece(): Piece | undefined { | ||
return fellOffTheBoardPiece; | ||
} | ||
|
||
function setFellOffTheBoardPiece(_fellOffTheBoardPiece: Piece | undefined) { | ||
fellOffTheBoardPiece = _fellOffTheBoardPiece; | ||
} | ||
|
||
initializeGame(); | ||
export const game = { | ||
initialize: initializeGame, | ||
endTurn, | ||
getCurrentPlayer, | ||
switchIsCastling, | ||
getPlayers, | ||
getPieces, | ||
setPieces, | ||
getItems, | ||
setItems, | ||
getRoundCounter, | ||
increaseRoundCounter, | ||
getDeathCounter, | ||
increaseDeathCounter, | ||
getIsCaslting, | ||
getIsFriendlyFire, | ||
setIsFriendlyFire, | ||
getIsPieceKilled, | ||
setIsPieceKilled, | ||
getFellOffTheBoardPiece, | ||
setFellOffTheBoardPiece, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { game } from './Game'; | ||
import { onActionTriggered, onFellOffTheBoardTriggered, renderScreen } from './LogicAdapter'; | ||
import { highlightSquare, initializeBoards } from './ui/BoardManager'; | ||
import { initializeEventListeners, setOnAction, setOnFellOffTheBoard, setOnHighlight } from './ui/Events'; | ||
import { Logger } from './ui/Logger'; | ||
|
||
function setGameEventHandlers() { | ||
setOnAction(onActionTriggered); | ||
setOnFellOffTheBoard(onFellOffTheBoardTriggered); | ||
setOnHighlight(highlightSquare); | ||
} | ||
|
||
function initializeUI() { | ||
initializeBoards(); | ||
initializeEventListeners(); | ||
renderScreen(); | ||
setGameEventHandlers(); | ||
} | ||
|
||
initializeUI(); | ||
game.initialize(); | ||
|
||
Logger.logGeneral('Game started!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { game } from './Game'; | ||
import { isAllowedToAct, onPieceFellOffTheBoard, onPlayerAction } from './logic/PieceLogic'; | ||
import { comparePositions, convertSquareIdToPosition } from './logic/Utilities'; | ||
import { Item } from './logic/items/Items'; | ||
import { Piece } from './logic/pieces/Pieces'; | ||
import { Position, Square } from './logic/pieces/PiecesUtilities'; | ||
import { BaseRule } from './logic/rules/BaseRule'; | ||
import { destroyElementOnBoard, moveElementOnBoard, spawnItemElementOnBoard, spawnPieceElementOnBoard } from './ui/BoardManager'; | ||
import { renderPlayersInformation, renderNewRule } from './ui/Screen'; | ||
|
||
export function renderScreen() { | ||
renderPlayersInformation(); | ||
} | ||
|
||
export function renderRules(newRule: BaseRule) { | ||
renderNewRule(newRule); | ||
} | ||
|
||
function findPieceAtPosition( | ||
position: Position, | ||
): Piece | undefined { | ||
return game.getPieces().find((piece) => comparePositions(piece.position, position)); | ||
} | ||
|
||
function getSquareIdFromElement(element: HTMLElement): string | undefined { | ||
while (element && !element.getAttribute('square-id')) { | ||
element = element.parentElement as HTMLElement; | ||
} | ||
return element?.getAttribute('square-id') || undefined; | ||
} | ||
|
||
function getPositionFromSquareId(squareId: string, boardId: string): Position { | ||
return { | ||
coordinates: convertSquareIdToPosition(squareId), | ||
boardId: boardId, | ||
}; | ||
} | ||
|
||
export function onActionTriggered( | ||
draggedElement: HTMLElement, | ||
targetElement: HTMLElement, | ||
boardId: string, | ||
) { | ||
const originSquareId = getSquareIdFromElement(draggedElement); | ||
if (!originSquareId) return; | ||
|
||
const draggedElementPosition = getPositionFromSquareId(originSquareId, boardId); | ||
const draggedPiece = findPieceAtPosition(draggedElementPosition); | ||
if (!draggedPiece) return; | ||
|
||
const targetSquareId = getSquareIdFromElement(targetElement); | ||
if (!targetSquareId) return; | ||
|
||
const targetElementPosition = getPositionFromSquareId(targetSquareId, boardId); | ||
|
||
if (targetElement.classList.contains('piece')) { | ||
const targetPiece = findPieceAtPosition(targetElementPosition); | ||
if (!targetPiece) return; | ||
|
||
onPlayerAction(draggedPiece, targetPiece); | ||
} else if (targetElement.classList.contains('item')) { | ||
game.getItems().forEach((item) => { | ||
if (comparePositions(item.position, targetElementPosition)) { | ||
onPlayerAction(draggedPiece, item); | ||
} | ||
}); | ||
} else { | ||
const targetSquare: Square = { | ||
position: targetElementPosition, | ||
}; | ||
onPlayerAction(draggedPiece, targetSquare); | ||
} | ||
} | ||
|
||
export function onFellOffTheBoardTriggered( | ||
draggedElement: HTMLElement, | ||
boardId: string, | ||
) { | ||
const squareId = getSquareIdFromElement(draggedElement); | ||
if (!squareId) return; | ||
|
||
const draggedElementPosition = getPositionFromSquareId(squareId, boardId); | ||
const draggedPiece = findPieceAtPosition(draggedElementPosition); | ||
if (!draggedPiece || !isAllowedToAct(draggedPiece)) return; | ||
|
||
onPieceFellOffTheBoard(draggedPiece); | ||
} | ||
|
||
export function movePieceOnBoard( | ||
draggedPiece: Piece, | ||
targetSquare: Square, | ||
) { | ||
const draggedPieceCoordinates = draggedPiece.position.coordinates; | ||
const originSquareId = draggedPieceCoordinates.join(','); | ||
|
||
const targetSquareId = targetSquare.position.coordinates.join(','); | ||
|
||
moveElementOnBoard(draggedPiece.position.boardId, originSquareId, targetSquareId); | ||
} | ||
|
||
export function destroyPieceOnBoard(piece: Piece) { | ||
const tpieceCoordinates = piece.position.coordinates; | ||
const squareId = tpieceCoordinates.join(','); | ||
|
||
destroyElementOnBoard(squareId, piece.position.boardId); | ||
} | ||
|
||
export function destroyItemOnBoard(item: Item) { | ||
const itemCoordinates = item.position.coordinates; | ||
const squareId = itemCoordinates.join(','); | ||
|
||
destroyElementOnBoard(squareId, item.position.boardId); | ||
} | ||
|
||
export function spawnPieceOnBoard(piece: Piece) { | ||
const pieceCoordinates = piece.position.coordinates; | ||
const squareId = pieceCoordinates.join(','); | ||
|
||
spawnPieceElementOnBoard(piece, squareId); | ||
} | ||
|
||
export function spawnItemOnBoard(item: Item) { | ||
const itemCoordinates = item.position.coordinates; | ||
const squareId = itemCoordinates.join(','); | ||
|
||
spawnItemElementOnBoard(item, squareId); | ||
} |
Oops, something went wrong.