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

Refactored code for better architecture #301

Merged
merged 5 commits into from
May 14, 2024
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
File renamed without changes.
2 changes: 1 addition & 1 deletion core/development/Game.ts → core/controller/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import { Logger } from './ui/logs/Logger';
import { initializeInventoryUI } from './ui/InventoriesUI';
import { PlayerColor } from './logic/players/types/PlayerColor';
import { PlayerInventory } from './logic/inventory/PlayerInventory';
import { BasePiece } from './logic/pieces/abstract/BasePiece';
import { ItemsShop } from './logic/shop/ItemsShop';
import { PlayersTurnSwitcher } from './logic/turn switcher/PlayersTurnSwitcher';
import {
renderInitialShopItemsElements,
updateShopButtonsState,
} from './ui/ShopUI';
import { BasePiece } from '../model/pieces/abstract/BasePiece';

export const shop = new ItemsShop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ import { Trap } from './logic/items/Trap';
import { HEAVEN_BOARD_ID, HELL_BOARD_ID } from './Constants';
import { showUpgradeablePiecesElements } from './ui/UpgradeUI';
import { PlayerColor } from './logic/players/types/PlayerColor';
import { BasePiece } from './logic/pieces/abstract/BasePiece';
import { Position } from './logic/pieces/types/Position';
import { Square } from './logic/pieces/types/Square';
import { Unicorn } from './logic/pieces/Unicorn';
import { KillPieceByFallingOffTheBoardAction } from './logic/actions/KillPieceByFallingOffTheBoardAction';
import { KillPieceByPieceAction } from './logic/actions/KillPieceByPieceAction';
import { ShopActionResult } from './logic/shop/types/ShopActionResult';
import { updateShopButtonsState } from './ui/ShopUI';
import { Position } from '../model/types/Position';
import { BasePiece } from '../model/pieces/abstract/BasePiece';
import { Square } from '../model/types/Square';

export function renderScreen() {
renderGameInformation();
Expand Down Expand Up @@ -163,7 +163,7 @@ export function onPieceSelected(pieceElement: HTMLElement, boardId: string) {
hideUnicornAttackButton();
}

showUpgradeablePiecesElements(piece, piece.upgrades);
showUpgradeablePiecesElements(piece, piece.modifiers.upgrades);
highlightLegalMoves(piece, boardId);
}

Expand All @@ -172,8 +172,8 @@ export function upgradePiece(
upgradedPiece: BasePiece,
) {
const currentPlayer = game.getPlayersTurnSwitcher().getCurrentPlayer();
if (currentPlayer.xp < upgradedPiece.price) return;
currentPlayer.xp -= upgradedPiece.price;
if (currentPlayer.xp < upgradedPiece.stats.price) return;
currentPlayer.xp -= upgradedPiece.stats.price;

// Destroy piece
destroyPieceOnBoard(upgradeablePiece);
Expand Down Expand Up @@ -260,12 +260,12 @@ export function destroyItemOnBoard(item: BaseItem) {
}

export function destroyItemOnPiece(piece: BasePiece) {
if (!piece.position || !piece.isEquipedItem) return;
if (!piece.position || !piece.stats.isEquippedItem) return;

const pieceCoordinates = piece.position.coordinates;
const squareId = pieceCoordinates.join(',');

piece.isEquipedItem = false;
piece.stats.isEquippedItem = false;
destroyElementOnPiece(squareId, piece.position.boardId);
}

Expand Down Expand Up @@ -353,7 +353,7 @@ export function canPlaceItemOnBoard(
const targetPiece = getPieceByPosition(squarePosition);
if (!targetPiece) return false;

targetPiece.isEquipedItem = true;
targetPiece.stats.isEquippedItem = true;
new Shield().use(squarePosition);

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { movePieceOnBoard } from '../LogicAdapter';
import { BaseItem } from './items/abstract/Item';
import { Player } from './players/Player';
import { MovementLog } from '../ui/logs/Log';
import { BasePiece } from './pieces/abstract/BasePiece';
import { Square } from './pieces/types/Square';
import { Position } from './pieces/types/Position';
import { PlayerMoveValidator } from './validators/PlayerMoveValidator';
import { PieceMovementSimulationValidator } from './validators/PieceMovementSimulationValidator';
import { AttackPieceAction } from './actions/AttackPieceAction';
import { MovePieceAction } from './actions/MovePieceAction';
import { RevertPieceMovementAction } from './actions/RevertPieceMovementAction';
import { BasePiece } from '../../model/pieces/abstract/BasePiece';
import { Square } from '../../model/types/Square';
import { Position } from '../../model/types/Position';

export function onPlayerAction(
draggedPiece: BasePiece,
Expand All @@ -23,7 +23,7 @@ export function onPlayerAction(
}

if (game.getMovesLeft() === 0) {
game.setMovesLeft(draggedPiece.moves);
game.setMovesLeft(draggedPiece.stats.moves);
}

const pieceMovementSimulationValidator = new PieceMovementSimulationValidator(
Expand Down Expand Up @@ -62,6 +62,6 @@ export function move(
boardId: draggedPiece.position.boardId,
};

draggedPiece.hasMoved = true;
draggedPiece.modifiers.hasMoved = true;
if (shouldEndTurn) game.endMove();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BasePiece } from '../../model/pieces/abstract/BasePiece';
import { Position } from '../../model/types/Position';
import { game } from '../Game';
import { BasePiece } from './pieces/abstract/BasePiece';
import { Position } from './pieces/types/Position';

export function comparePositions(
firstPosition: Position | undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Position } from '../../../model/types/Position';
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { Queen } from '../pieces/Queen';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { AttackPieceAction } from './AttackPieceAction';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BasePiece } from '../../../model/pieces/abstract/BasePiece';
import { Position } from '../../../model/types/Position';
import { Square } from '../../../model/types/Square';
import { game } from '../../Game';
import { move } from '../PieceLogic';
import { BasePiece } from '../pieces/abstract/BasePiece';
import { Position } from '../pieces/types/Position';
import { Square } from '../pieces/types/Square';
import { KillPieceByPieceAction } from './KillPieceByPieceAction';
import { GameAction } from './abstract/GameAction';
import { ActionResult } from './types/ActionResult';
Expand All @@ -18,7 +18,10 @@ export class AttackPieceAction implements GameAction {

execute(): ActionResult {
if (!this.target.position) return ActionResult.FAILURE;
const targetPosition = this.target.copyPosition() as Position;
const targetPosition: Position = {
coordinates: this.target.position.coordinates,
boardId: this.target.position.boardId,
};

game.setIsFriendlyFire(this.target.player === this.piece.player);
const killPieceByPieceResult = new KillPieceByPieceAction(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Position } from '../../../model/types/Position';
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { King } from '../pieces/King';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { CastleAction } from './CastleAction';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Position } from '../../../model/types/Position';
import { Square } from '../../../model/types/Square';
import { Log } from '../../ui/logs/Log';
import { move } from '../PieceLogic';
import { King } from '../pieces/King';
import { Position } from '../pieces/types/Position';
import { Square } from '../pieces/types/Square';
import { GameAction } from './abstract/GameAction';
import { ActionResult } from './types/ActionResult';

Expand Down Expand Up @@ -46,7 +46,7 @@ export class CastleAction implements GameAction {
move(rookPiece, rookPieceTargetSquare.position, false);

new Log(
`${this.piece.pieceIcon} ${this.piece.player.color} castled.`,
`${this.piece.resource.pieceIcon} ${this.piece.player.color} castled.`,
).addToQueue();
return ActionResult.SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Position } from '../../../model/types/Position';
import {
HEAVEN_BOARD_ID,
HELL_BOARD_ID,
Expand All @@ -7,7 +8,6 @@ import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { King } from '../pieces/King';
import { Pawn } from '../pieces/Pawn';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { KillPieceAction } from './KillPieceAction';
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('KillPieceAction', () => {
boardId: OVERWORLD_BOARD_ID,
};
const killedPiece = new Pawn(whitePlayer, initialPosition);
killedPiece.killCount = 1;
killedPiece.modifiers.killCount = 1;
const killPieceAction = new KillPieceAction(
killedPiece,
OVERWORLD_BOARD_ID,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {
HEAVEN_BOARD_ID,
HELL_BOARD_ID,
OVERWORLD_BOARD_ID,
} from '../../Constants';
import { BasePiece } from '../../../model/pieces/abstract/BasePiece';
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { destroyPieceOnBoard } from '../../LogicAdapter';
import { King } from '../pieces/King';
import { BasePiece } from '../pieces/abstract/BasePiece';
import { PermanentlyKillPieceAction } from './PermanentlyKillPieceAction';
import { SpawnPieceInHeavenAction } from './SpawnPieceInHeavenAction';
import { SpawnPieceInHellAction } from './SpawnPieceInHellAction';
Expand All @@ -26,7 +22,8 @@ export class KillPieceAction implements GameAction {
if (this.killedPiece.position?.boardId === OVERWORLD_BOARD_ID) {
game.increaseDeathCounter();

const hasPieceKilledOtherPieces = this.killedPiece.killCount > 0;
const hasPieceKilledOtherPieces =
this.killedPiece.modifiers.killCount > 0;
const isKilledPieceKing = this.killedPiece instanceof King;

const pieceOriginBoardId = this.killedPiece.position.boardId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BasePiece } from '../pieces/abstract/BasePiece';
import { ActionResult } from './types/ActionResult';
import { KillPieceAction } from './KillPieceAction';
import { KillLog } from '../../ui/logs/Log';
import { game } from '../../Game';
import { BasePiece } from '../../../model/pieces/abstract/BasePiece';

export class KillPieceByEnvironmentAction extends KillPieceAction {
private killingSource: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { OVERWORLD_BOARD_ID, VOID_BOARD_ID } from '../../Constants';
import { BasePiece } from '../../../model/pieces/abstract/BasePiece';
import { Position } from '../../../model/types/Position';
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { Pawn } from '../pieces/Pawn';
import { BasePiece } from '../pieces/abstract/BasePiece';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { KillPieceByEnvironmentAction } from './KillPieceByEnvironmentAction';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BasePiece } from '../../../model/pieces/abstract/BasePiece';
import { VOID_BOARD_ID } from '../../Constants';
import { BasePiece } from '../pieces/abstract/BasePiece';
import { KillPieceByEnvironmentAction } from './KillPieceByEnvironmentAction';
import { ActionResult } from './types/ActionResult';
export class KillPieceByFallingOffTheBoardAction extends KillPieceByEnvironmentAction {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Position } from '../../../model/types/Position';
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { Pawn } from '../pieces/Pawn';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { KillPieceByPieceAction } from './KillPieceByPieceAction';
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('KillPieceByPieceAction', () => {
};
const killedPiece = new Pawn(whitePlayer, initialPosition);
const initialPieceHealth = 6;
killedPiece.health = initialPieceHealth;
killedPiece.stats.health = initialPieceHealth;
const killPieceByPieceAction = new KillPieceByPieceAction(
killedPiece,
killedPiece,
Expand All @@ -61,7 +61,7 @@ describe('KillPieceByPieceAction', () => {

// Assert
expect(actionResult).toEqual(ActionResult.FAILURE);
expect(killedPiece.health).toEqual(initialPieceHealth - 1);
expect(killedPiece.stats.health).toEqual(initialPieceHealth - 1);
});

test('should return SUCCESS if killedPiece.health == 1', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BasePiece } from '../pieces/abstract/BasePiece';
import { ActionResult } from './types/ActionResult';
import { KillLog } from '../../ui/logs/Log';
import { game } from '../../Game';
import { MIN_KILLINGS_FOR_BOUNTY } from '../../Constants';
import { destroyItemOnPiece, movePieceOnBoard } from '../../LogicAdapter';
import { Position } from '../pieces/types/Position';
import { KillPieceAction } from './KillPieceAction';
import { BasePiece } from '../../../model/pieces/abstract/BasePiece';
import { Position } from '../../../model/types/Position';
export class KillPieceByPieceAction extends KillPieceAction {
private killerPiece: BasePiece;

Expand All @@ -15,15 +15,15 @@ export class KillPieceByPieceAction extends KillPieceAction {
}

execute(): ActionResult {
this.killedPiece.health--;
if (this.killedPiece.health > 0) {
this.killedPiece.stats.health--;
if (this.killedPiece.stats.health > 0) {
this.failToKillPiece();
return ActionResult.FAILURE;
}

this.killerPiece.killCount++;
if (this.killedPiece.killCount >= MIN_KILLINGS_FOR_BOUNTY) {
this.killerPiece.player.gold += this.killedPiece.killCount;
this.killerPiece.modifiers.killCount++;
if (this.killedPiece.modifiers.killCount >= MIN_KILLINGS_FOR_BOUNTY) {
this.killerPiece.player.gold += this.killedPiece.modifiers.killCount;
}

game.setKillerPiece(this.killerPiece);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Position } from '../../../model/types/Position';
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { King } from '../pieces/King';
import { Rook } from '../pieces/Rook';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { MovePieceAction } from './MovePieceAction';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BasePiece } from '../../../model/pieces/abstract/BasePiece';
import { Position } from '../../../model/types/Position';
import { game } from '../../Game';
import { move } from '../PieceLogic';
import { comparePositions } from '../Utilities';
import { King } from '../pieces/King';
import { Pawn } from '../pieces/Pawn';
import { BasePiece } from '../pieces/abstract/BasePiece';
import { Position } from '../pieces/types/Position';
import { CastleAction } from './CastleAction';
import { KillPieceByPieceAction } from './KillPieceByPieceAction';
import { GameAction } from './abstract/GameAction';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Position } from '../../../model/types/Position';
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { King } from '../pieces/King';
import { Pawn } from '../pieces/Pawn';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { PermanentlyKillPieceAction } from './PermanentlyKillPieceAction';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BasePiece } from '../../../model/pieces/abstract/BasePiece';
import { game } from '../../Game';
import { destroyPieceOnBoard } from '../../LogicAdapter';
import { King } from '../pieces/King';
import { BasePiece } from '../pieces/abstract/BasePiece';
import { GameAction } from './abstract/GameAction';
import { ActionResult } from './types/ActionResult';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Position } from '../../../model/types/Position';
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { Pawn } from '../pieces/Pawn';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { RevertPieceMovementAction } from './RevertPieceMovementAction';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BasePiece } from '../../../model/pieces/abstract/BasePiece';
import { movePieceOnBoard } from '../../LogicAdapter';
import { BasePiece } from '../pieces/abstract/BasePiece';
import { GameAction } from './abstract/GameAction';
import { ActionResult } from './types/ActionResult';

Expand Down
Loading