diff --git a/development/code/Game.ts b/development/code/Game.ts index bc1d0415..1b17780b 100644 --- a/development/code/Game.ts +++ b/development/code/Game.ts @@ -10,6 +10,7 @@ import { Piece } from './logic/pieces/Pieces'; import { Queen } from './logic/pieces/Queen'; import { Rook } from './logic/pieces/Rook'; import { RulesManager } from './logic/rules/RulesManager'; +import { showWinningAlert } from './ui/Screen'; let rulesManager: RulesManager; const whitePlayer = new Player(PlayerColors.WHITE); @@ -58,6 +59,7 @@ let isCastling = false; let isFriendlyFire = false; let isPieceKilled = false; let fellOffTheBoardPiece: Piece | undefined; +let winner: Player | undefined = undefined; function initializeGame() { rulesManager = new RulesManager(); @@ -79,6 +81,16 @@ function endTurn() { } renderScreen(); + + // element.remove() is scheduled to run in the next event sycle while alert() runs immedietely. + // To make sure the element is removed before displaying the winning alert, we need to add + // a small delay before displaying the alert. + setTimeout(() => { + if (winner !== undefined) { + showWinningAlert(winner.color); + window.location.reload(); + } + }, 10); } function resetVariables() { @@ -180,6 +192,10 @@ function setFellOffTheBoardPiece(_fellOffTheBoardPiece: Piece | undefined) { fellOffTheBoardPiece = _fellOffTheBoardPiece; } +function setWinner(_winner: Player) { + winner = _winner; +} + export const game = { initialize: initializeGame, endTurn, @@ -201,4 +217,5 @@ export const game = { setIsPieceKilled, getFellOffTheBoardPiece, setFellOffTheBoardPiece, + setWinner, }; diff --git a/development/code/LogicAdapter.ts b/development/code/LogicAdapter.ts index 8d5a1c54..ae89055a 100644 --- a/development/code/LogicAdapter.ts +++ b/development/code/LogicAdapter.ts @@ -1,6 +1,6 @@ import { game } from './Game'; import { isPlayerAllowedToAct, onPieceFellOffTheBoard, onPlayerAction } from './logic/PieceLogic'; -import { PlayerColors } from './logic/Players'; +import { Player, PlayerColors } from './logic/Players'; import { comparePositions, convertSquareIdToPosition } from './logic/Utilities'; import { Item } from './logic/items/Items'; import { Piece } from './logic/pieces/Pieces'; @@ -189,3 +189,7 @@ export function changePieceToAnotherPlayer(piece: Piece) { piece.player = game.getPlayers().filter(_player => _player !== piece.player)[0]; } + +export function winGame(winnerPlayer: Player){ + game.setWinner(winnerPlayer); +} diff --git a/development/code/logic/PieceLogic.ts b/development/code/logic/PieceLogic.ts index 5c1ebfa0..6996da99 100644 --- a/development/code/logic/PieceLogic.ts +++ b/development/code/logic/PieceLogic.ts @@ -1,5 +1,5 @@ import { game } from '../Game'; -import { destroyItemOnBoard, destroyPieceOnBoard, movePieceOnBoard, spawnPieceOnBoard } from '../LogicAdapter'; +import { destroyItemOnBoard, destroyPieceOnBoard, movePieceOnBoard, spawnPieceOnBoard, winGame } from '../LogicAdapter'; import { Logger } from '../ui/Logger'; import { HEAVEN_BOARD_ID, @@ -226,7 +226,6 @@ function killPiece( targetPosition = targetPiece.position, ) { draggedPiece.hasKilled = true; - commonKillPieceActions(targetPiece); logKillMessages(targetPiece, draggedPiece); @@ -263,6 +262,7 @@ function handleOverworldKill( targetPiece: Piece, targetPosition: Position, ) { + commonKillPieceActions(targetPiece); targetPiece.position = targetPosition; if (targetPiece.hasKilled || targetPiece instanceof King) { @@ -278,6 +278,10 @@ export function permanentlyKillPiece(targetPiece: Piece, draggedPiece: Piece) { logKillMessages(targetPiece, draggedPiece, true); game.setPieces(game.getPieces().filter((piece) => piece !== targetPiece)); commonKillPieceActions(targetPiece); + + if (targetPiece instanceof King){ + winGame(draggedPiece.player); + } } function onActionPieceToItem(piece: Piece, item: Item) { diff --git a/development/code/logic/rules/CoupRule.ts b/development/code/logic/rules/CoupRule.ts index 7c74c3ca..ba6f46a4 100644 --- a/development/code/logic/rules/CoupRule.ts +++ b/development/code/logic/rules/CoupRule.ts @@ -41,4 +41,4 @@ export class CoupRule extends BaseRule { super(description, isRevealed, condition, onTrigger); } -} \ No newline at end of file +} diff --git a/development/code/logic/rules/EmptyPocketsRule.ts b/development/code/logic/rules/EmptyPocketsRule.ts index f46ba5e7..24dad598 100644 --- a/development/code/logic/rules/EmptyPocketsRule.ts +++ b/development/code/logic/rules/EmptyPocketsRule.ts @@ -26,4 +26,4 @@ export class EmptyPocketsRule extends BaseRule { super(description, isRevealed, condition, onTrigger); } -} \ No newline at end of file +} diff --git a/development/code/logic/rules/ExperienceOnKillRule.ts b/development/code/logic/rules/ExperienceOnKillRule.ts index 59c64e6d..4459f636 100644 --- a/development/code/logic/rules/ExperienceOnKillRule.ts +++ b/development/code/logic/rules/ExperienceOnKillRule.ts @@ -14,4 +14,4 @@ export class ExperienceOnKillRule extends BaseRule { super(description, isRevealed, condition, onTrigger); } -} \ No newline at end of file +} diff --git a/development/code/logic/rules/FirstBloodRule.ts b/development/code/logic/rules/FirstBloodRule.ts index c1842079..331b2eae 100644 --- a/development/code/logic/rules/FirstBloodRule.ts +++ b/development/code/logic/rules/FirstBloodRule.ts @@ -14,4 +14,4 @@ export class FirstBloodRule extends BaseRule { super(description, isRevealed, condition, onTrigger); } -} \ No newline at end of file +} diff --git a/development/code/logic/rules/FriendlyFireRule.ts b/development/code/logic/rules/FriendlyFireRule.ts index c790677f..29289827 100644 --- a/development/code/logic/rules/FriendlyFireRule.ts +++ b/development/code/logic/rules/FriendlyFireRule.ts @@ -14,4 +14,4 @@ export class FriendlyFireRule extends BaseRule { super(description, isRevealed, condition, onTrigger); } -} \ No newline at end of file +} diff --git a/development/code/logic/rules/PiecesCanFallOffTheBoardRule.ts b/development/code/logic/rules/PiecesCanFallOffTheBoardRule.ts index 2340fde0..2404e360 100644 --- a/development/code/logic/rules/PiecesCanFallOffTheBoardRule.ts +++ b/development/code/logic/rules/PiecesCanFallOffTheBoardRule.ts @@ -14,4 +14,4 @@ export class PiecesCanFallOffTheBoardRule extends BaseRule { super(description, isRevealed, condition, onTrigger); } -} \ No newline at end of file +} diff --git a/development/code/logic/rules/WithAgeComesWisdomRule.ts b/development/code/logic/rules/WithAgeComesWisdomRule.ts index a3fbcc2f..5f6b545e 100644 --- a/development/code/logic/rules/WithAgeComesWisdomRule.ts +++ b/development/code/logic/rules/WithAgeComesWisdomRule.ts @@ -16,4 +16,4 @@ export class WithAgeComesWisdomRule extends BaseRule { super(description, isRevealed, condition, onTrigger); } -} \ No newline at end of file +} diff --git a/development/code/ui/Screen.ts b/development/code/ui/Screen.ts index 1be36c3d..74cb5db8 100644 --- a/development/code/ui/Screen.ts +++ b/development/code/ui/Screen.ts @@ -39,3 +39,7 @@ export function renderNewRule(rule: BaseRule) { ruleElement.innerHTML = `- ${rule.description}`; rulesContainer?.appendChild(ruleElement); } + +export function showWinningAlert(winnerColor: string) { + alert(winnerColor + ' wins!'); +}