Skip to content

Commit

Permalink
Merge branch 'develop' into 27-check
Browse files Browse the repository at this point in the history
  • Loading branch information
asiaziola authored Feb 14, 2021
2 parents 0c72494 + 80acb7a commit bd1150e
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 2 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 @@ -5,6 +5,7 @@ import { Piece } from '../services/game-logic/pieces/Piece';
import { BoardView } from '../views/BoardView';
import { SettingsControls } from '../views/SettingsControls';
import { OpeningView } from '../views/OpeningView';
import { Sound } from '../services/game-logic/Sound';

class GameController {
boardView: BoardView;
Expand All @@ -13,6 +14,7 @@ class GameController {
activeSquare: Square | null;
currentPlayer: Colors;
openingView: OpeningView;
sound: Sound;

constructor() {
this.activeSquare = null;
Expand All @@ -21,6 +23,7 @@ class GameController {
this.settingsView = new SettingsControls();
this.openingView = new OpeningView();
this.currentPlayer = Colors.WHITE;
this.sound = new Sound();
this.updateBoard();
}

Expand All @@ -32,6 +35,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 @@ -45,6 +49,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
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 };
Binary file added static/sounds/0.mp3
Binary file not shown.
Binary file added static/sounds/1.mp3
Binary file not shown.
Binary file added static/sounds/2.mp3
Binary file not shown.
Binary file added static/sounds/3.mp3
Binary file not shown.
Binary file added static/sounds/capture.mp3
Binary file not shown.
11 changes: 9 additions & 2 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 @@ -23,7 +24,7 @@ module.exports = {
]
},
{
test: /\.(png|jpe?g|svg)$/i,
test: /\.(png|jpe?g|svg|mp3)$/i,
use: 'file-loader'
}
]
Expand All @@ -35,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 bd1150e

Please sign in to comment.