Skip to content

Commit

Permalink
[#53] Fixes after review 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrydrychowicz committed Feb 12, 2021
1 parent baf5e1a commit 742975f
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 70 deletions.
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"babel-jest": "^26.6.3",
"babel-plugin-transform-class-properties": "^6.24.1",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^7.0.0",
"css-loader": "^5.0.1",
"eslint": "^7.18.0",
"eslint-config-prettier": "^7.2.0",
Expand Down
15 changes: 15 additions & 0 deletions src/controllers/GameController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import { Square } from '../models/Square';
import { GameEngine } from '../services/game-logic/GameEngine';
import { Piece } from '../services/game-logic/pieces/Piece';
import { BoardView } from '../views/BoardView';
import { Sound } from '../services/game-logic/Sound';

class GameController {
boardView: BoardView;
gameEngine: GameEngine;
activeSquare: Square | null;
currentPlayer: Colors;
sound: Sound;

constructor() {
this.activeSquare = null;
this.gameEngine = new GameEngine();
this.boardView = new BoardView(this.handleUserClick);
this.currentPlayer = Colors.WHITE;
this.sound = new Sound();
this.updateBoard();
}

Expand All @@ -26,6 +29,7 @@ class GameController {
const isLegalMove = legalMoves.some((move) => square?.row === move.row && square?.column === move.column);

if (isLegalMove) {
this.playSound(this.activeSquare, square);
this.gameEngine.movePiece(this.activeSquare, square);
this.boardView.render(this.gameEngine.board);
this.changePlayer();
Expand All @@ -39,6 +43,17 @@ class GameController {
}
};

private playSound(location: Square, destination: Square): void {
const locationPiece = this.gameEngine.board.getPiece(location);
const destinationPiece = this.gameEngine.board.getPiece(destination);

if (locationPiece && destinationPiece) {
this.sound.playCapturingMoveSound();
} else {
this.sound.playNormalMoveSound();
}
}

private isCurrentPlayer(selectedPiece: Piece | null): boolean {
return this.currentPlayer === selectedPiece?.color;
}
Expand Down
43 changes: 0 additions & 43 deletions src/controllers/SoundController.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/enums/SoundTypes.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './PieceNames';
export * from './Colors';
export * from './Constants';
export * from './SoundTypes';
17 changes: 1 addition & 16 deletions src/services/game-logic/GameEngine.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { PieceNames, Constants, SoundTypes } from '../../enums';
import { PieceNames, Constants } from '../../enums';
import { Square } from '../../models/Square';
import { Board } from './Board';
import { Piece } from './pieces/Piece';
import { SoundController } from '../../controllers/SoundController';

class GameEngine {
board: Board;
soundController: SoundController;

constructor() {
this.board = new Board();
this.soundController = new SoundController();
}

getLegalMoves = (square: Square): Square[] => {
Expand All @@ -27,24 +24,12 @@ class GameEngine {
public movePiece(location: Square, destination: Square): void {
//can be useful when saving moves
const piece = this.board.getPiece(location);
this.playSound(location, destination);
this.board.movePiece(location, destination);
if (piece?.name === PieceNames.KING) {
this.performCastling(location, destination);
}
}

private playSound(location: Square, destination: Square): void {
const locationPiece = this.board.getPiece(location);
const destinationPiece = this.board.getPiece(destination);

if (locationPiece && destinationPiece) {
this.soundController.makeSound(SoundTypes.CAPTURING_MOVE);
} else {
this.soundController.makeSound(SoundTypes.NORMAL_MOVE);
}
}

private performCastling(location: Square, destination: Square): void {
if (destination.column - location.column === Constants.KINGSIDE_CASTLING) {
this.board.movePiece(
Expand Down
24 changes: 24 additions & 0 deletions src/services/game-logic/Sound.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Sound {
private lastPlayedMoveSound = 0;
private AMOUNT_OF_NORMAL_MOVE_SOUNDS = 3;

public playCapturingMoveSound(): void {
const sound = new Audio('/static/sounds/capture.mp3');
sound.play();
}

public playNormalMoveSound(): void {
const generateRandomNumber = () => Math.floor(Math.random() * this.AMOUNT_OF_NORMAL_MOVE_SOUNDS);
let randomNumber = generateRandomNumber();

// avoid playing same sound as before
while (this.lastPlayedMoveSound === randomNumber) {
randomNumber = generateRandomNumber();
}
const sound = new Audio(`/static/sounds/${randomNumber}.mp3`);
sound.play();
this.lastPlayedMoveSound = randomNumber;
}
}

export { Sound };
2 changes: 1 addition & 1 deletion src/services/game-logic/pieces/Pawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Pawn extends Piece {
return moves;
}

private prepareMove(rowStep: number, colStep: number = 0): Square | null {
private prepareMove(rowStep: number, colStep = 0): Square | null {
const move = new Square(this.position.row + rowStep, this.position.column + colStep);

// Check if out of boundaries
Expand Down
14 changes: 9 additions & 5 deletions webpack.common.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const path = require('path');

module.exports = {
Expand All @@ -24,10 +25,7 @@ module.exports = {
},
{
test: /\.(png|jpe?g|svg|mp3)$/i,
loader: 'file-loader',
options: {
name: '[path][name].[ext]'
}
use: 'file-loader'
}
]
},
Expand All @@ -38,5 +36,11 @@ module.exports = {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
plugins: [new CleanWebpackPlugin(), new HtmlWebpackPlugin({ template: './src/index.html', scriptLoading: 'defer' })]
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({ template: './src/index.html', scriptLoading: 'defer' }),
new CopyPlugin({
patterns: [{ from: 'static', to: 'static' }]
})
]
};

0 comments on commit 742975f

Please sign in to comment.