-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/#56-banks-board
- Loading branch information
Showing
13 changed files
with
547 additions
and
76 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,65 +1,77 @@ | ||
// import { time } from 'console'; | ||
import { AnimalNames } from '../Enums/AnimalNamesEnum'; | ||
import { Player } from '../Player'; | ||
import { BreedProcessor } from './BreedProcessor'; | ||
import { Bank } from './logic/Bank'; | ||
import { Timer } from './Timer'; | ||
import { GameProcessor } from './logic/GameProcessor'; | ||
import { View } from './View'; | ||
import { Game } from './logic/Game'; | ||
import { defaultGameConfiguration } from './logic/defaultGameConfiguration'; | ||
|
||
export class GameController { | ||
private currentPlayer: Player | undefined; | ||
private player: Player | undefined; | ||
private timer: Timer; | ||
private breedProcessor: BreedProcessor; | ||
private game: Game; | ||
private gameProcessor: GameProcessor; | ||
constructor(private view: View) { | ||
this.timer = new Timer(); | ||
this.breedProcessor = new BreedProcessor(new Bank()); | ||
this.game = new Game(defaultGameConfiguration); | ||
this.gameProcessor = new GameProcessor(this.game); | ||
} | ||
|
||
get theCurrentPlayer(): Player { | ||
return this.currentPlayer as Player; | ||
get theGame(): Game { | ||
return this.game; | ||
} | ||
|
||
get theTimer(): Timer { | ||
return this.timer; | ||
/** | ||
* Starts the turn for the current player. | ||
* Displays alert when the time is over. | ||
* Updates the remaining time on the View. | ||
*/ | ||
startTurn(): void { | ||
this.gameProcessor.startTurn( | ||
(currentPlayer) => { | ||
this.view.displayAlert(currentPlayer?.theName as string); | ||
}, | ||
(remainingTime) => { | ||
this.view.updateRemainingTime(remainingTime); | ||
}, | ||
); | ||
} | ||
|
||
startTurn(): void { | ||
this.timer.countdown(); | ||
const turnTimer = setInterval(() => { | ||
if (!this.timer.running) { | ||
clearInterval(turnTimer); | ||
if (Math.ceil(this.timer.theTurnTimeLeft) === 0) { | ||
this.view.displayAlert( | ||
this.currentPlayer?.theName as string, | ||
); | ||
this.nextPlayer(); | ||
this.startTurn(); | ||
} | ||
} | ||
this.view.updateRemainingTime( | ||
Math.round(this.timer.theTurnTimeLeft), | ||
); | ||
}, 10); | ||
stopTurn(): void { | ||
this.gameProcessor.stopTurn(); | ||
} | ||
|
||
breed(): [AnimalNames, AnimalNames] | undefined { | ||
if (this.timer.theTurnTimeLeft === 0) { | ||
return; | ||
/** | ||
* Executes trade proposed by the player and checks win condition. | ||
* If player wins the game after the trade, stops the timer and tells the View to display the WinModal. | ||
* @param offer made by the player | ||
* @param target desired by the player | ||
* @returns true if the trade was sucessful, false otherwise | ||
*/ | ||
trade( | ||
offer: [AnimalNames, number], | ||
target: [AnimalNames, number], | ||
): boolean { | ||
const tradeResult = this.gameProcessor.trade(offer, target); | ||
if (this.gameProcessor.checkWin()) { | ||
this.gameProcessor.stopTurn(); | ||
this.view.displayWinModal(this.game.theCurrentPlayer); | ||
} | ||
const rollResult = this.breedProcessor.processBreedPhase( | ||
this.currentPlayer as Player, | ||
); | ||
return rollResult; | ||
return tradeResult; | ||
} | ||
|
||
stopTurn(): void { | ||
this.timer.resetTurn(); | ||
/** | ||
* Rolls the dice for the player and updates their Herd. | ||
* If player wins the game after the breed, stops the timer and tells the View to display the WinModal. | ||
*/ | ||
breed(): [AnimalNames, AnimalNames] | undefined { | ||
const diceResult = this.gameProcessor.breed(); | ||
if (this.gameProcessor.checkWin()) { | ||
this.gameProcessor.stopTurn(); | ||
this.view.displayWinModal(this.game.theCurrentPlayer); | ||
} | ||
return diceResult; | ||
} | ||
|
||
/** | ||
* Sets the current player to the next player in order. | ||
*/ | ||
nextPlayer(): void { | ||
//TODO multiplayer logic | ||
this.currentPlayer = this.player; | ||
this.timer.resetTurn(); | ||
this.gameProcessor.nextPlayer(); | ||
} | ||
} |
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
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
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,31 @@ | ||
import { Render } from '../utils/Render'; | ||
import { pull } from 'lodash'; | ||
|
||
export abstract class EmptyModal { | ||
modal: HTMLElement; | ||
modalContainer: HTMLElement; | ||
constructor() { | ||
this.modalContainer = Render.elementFactory('div', { | ||
className: 'modal__container', | ||
}); | ||
this.modal = Render.elementFactory( | ||
'div', | ||
{ className: 'modal' }, | ||
this.modalContainer, | ||
); | ||
} | ||
|
||
/** | ||
* Hides modal - adds class with display:none | ||
*/ | ||
protected hideModal(): void { | ||
this.modal.classList.add('modal--hidden'); | ||
} | ||
|
||
/** | ||
* Shows modal - removes class with display:none | ||
*/ | ||
protected showModal(): void { | ||
this.modal.classList.remove('modal--hidden'); | ||
} | ||
} |
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
Oops, something went wrong.