-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* changedd how when do i load the service from game scene to main menu scene * finalize the game over scene
- Loading branch information
Showing
9 changed files
with
250 additions
and
29 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { IServiceSceneManager } from './SceneManager'; | ||
import { ServiceLocator } from './ServiceLocator'; | ||
import { IServiceWaveManager } from './WaveManager/WaveManager'; | ||
|
||
export interface IGameStatManager { | ||
GetTimePlayerHasSurvived(): string; | ||
HasPlayerWon(): { hasWon: boolean; round: number }; | ||
} | ||
|
||
// Manages the gme stats related to if the player wins or loses the game | ||
// how many rounds the player survived, how long the player survived, etc. | ||
export class GameStatManager implements IGameStatManager { | ||
private timePlayerSurvived: number; | ||
private winningRound: number; | ||
|
||
constructor() { | ||
this.timePlayerSurvived = 0; | ||
this.winningRound = 100; | ||
ServiceLocator.AddService('GameStatManager', this); | ||
} | ||
|
||
public Update(dt: number) { | ||
this.timePlayerSurvived = Math.round((this.timePlayerSurvived + dt) * 100) / 100; | ||
const waveManager = ServiceLocator.GetService<IServiceWaveManager>('WaveManager'); | ||
// if the player has survived 100 rounds (thus reached 101 round), then the player has won the game | ||
if (waveManager.Round > this.winningRound) { | ||
ServiceLocator.GetService<IServiceSceneManager>('SceneManager').PlayMainScene('GameOver'); | ||
} | ||
} | ||
|
||
public GetTimePlayerHasSurvived(): string { | ||
return this.formatTime({ seconds: this.timePlayerSurvived }); | ||
} | ||
|
||
public HasPlayerWon(): { hasWon: boolean; round: number } { | ||
const waveManager = ServiceLocator.GetService<IServiceWaveManager>('WaveManager'); | ||
const round = waveManager.Round; | ||
if (round > this.winningRound) { | ||
return { hasWon: true, round }; | ||
} | ||
return { hasWon: false, round }; | ||
} | ||
|
||
private formatTime(parameters: { seconds: number }): string { | ||
const { seconds } = parameters; | ||
const date = new Date(0); | ||
date.setSeconds(seconds); | ||
|
||
const hours = String(date.getUTCHours()).padStart(2, '0'); | ||
const minutes = String(date.getUTCMinutes()).padStart(2, '0'); | ||
const secs = String(date.getUTCSeconds()).padStart(2, '0'); | ||
|
||
return `${hours}:${minutes}:${secs}`; | ||
} | ||
} | ||
|
||
let gameStatManager: GameStatManager; | ||
|
||
export function LoadGameStatManager() { | ||
gameStatManager = new GameStatManager(); | ||
} | ||
|
||
export function UpdateGameStatManager(dt: number) { | ||
gameStatManager.Update(dt); | ||
} |
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,115 @@ | ||
import { IGameStatManager } from '../GameStatManager'; | ||
import { IScene, IServiceSceneManager } from '../SceneManager'; | ||
import { CANVA_SCALEX, CANVA_SCALEY } from '../ScreenConstant'; | ||
import { ServiceLocator } from '../ServiceLocator'; | ||
import { FieldWithText } from './BaseUserInterface/FieldWithText'; | ||
import { UIManager } from './BaseUserInterface/UIManager'; | ||
|
||
export class GameOverScene implements IScene { | ||
private gameOverUiManager: UIManager; | ||
Load(): void { | ||
this.loadUI(); | ||
} | ||
Update(dt: number): void { | ||
this.gameOverUiManager.Update(dt); | ||
} | ||
Draw(ctx: CanvasRenderingContext2D): void { | ||
this.gameOverUiManager.Draw(ctx); | ||
} | ||
Unload(): void {} | ||
|
||
private loadUI() { | ||
this.gameOverUiManager = new UIManager(); | ||
|
||
/* Return to Main-Menu Button*/ | ||
const widthReturnButton = 62 * CANVA_SCALEX; | ||
const heightReturnButton = 10 * CANVA_SCALEY; | ||
const xReturnButton = 235 * CANVA_SCALEX; | ||
const yReturnButton = 165 * CANVA_SCALEY; | ||
const gameOverReturnButton = new FieldWithText({ | ||
x: xReturnButton, | ||
y: yReturnButton, | ||
width: widthReturnButton, | ||
height: heightReturnButton, | ||
text: 'MAIN-MENU', | ||
fontSize: UIManager.Typography.button.fontSize, | ||
fontFamily: UIManager.Typography.button.fontFamily, | ||
HasHovered: true, | ||
onClick: () => { | ||
const SceneManager = ServiceLocator.GetService<IServiceSceneManager>('SceneManager'); | ||
SceneManager.PlayMainScene('MainMenu'); | ||
}, | ||
}); | ||
this.gameOverUiManager.AddComponent(gameOverReturnButton); | ||
|
||
/* Text that show the time the player finished 100 rounds */ | ||
const gameStatManager = ServiceLocator.GetService<IGameStatManager>('GameStatManager'); | ||
const { hasWon, round } = gameStatManager.HasPlayerWon(); | ||
const timePlayerSurvived = gameStatManager.GetTimePlayerHasSurvived(); | ||
if (hasWon) { | ||
this.UIToShowIfPlayerWin({ uiManager: this.gameOverUiManager, round, timePlayerSurvived }); | ||
} else { | ||
this.UIToShowIfPlayerLose({ uiManager: this.gameOverUiManager, round, timePlayerSurvived }); | ||
} | ||
} | ||
|
||
private UIToShowIfPlayerWin(parameters: { uiManager: UIManager; round: number; timePlayerSurvived: string }) { | ||
/* Text that show the time the player finished 100 rounds */ | ||
const widthTimeText = 255 * CANVA_SCALEX; | ||
const heightTimeText = 6 * CANVA_SCALEY; | ||
const xTimeText = 21 * CANVA_SCALEX; | ||
const yTimeText = 19 * CANVA_SCALEY; | ||
const gameOverTimeText = new FieldWithText({ | ||
x: xTimeText, | ||
y: yTimeText, | ||
width: widthTimeText, | ||
height: heightTimeText, | ||
text: `YOU WON, YOU DID A 100 ROUNDS IN: ${parameters.timePlayerSurvived}`, | ||
fontSize: UIManager.Typography.button.fontSize, | ||
fontFamily: UIManager.Typography.button.fontFamily, | ||
leftAlign: true, | ||
}); | ||
gameOverTimeText.HasBorderOnAllSide = false; | ||
parameters.uiManager.AddComponent(gameOverTimeText); | ||
} | ||
|
||
private UIToShowIfPlayerLose(parameters: { uiManager: UIManager; round: number; timePlayerSurvived: string }) { | ||
/* Text that show how many rounds the player survived */ | ||
const widthTimeText = 215 * CANVA_SCALEX; | ||
const heightTimeText = 6 * CANVA_SCALEY; | ||
const xTimeText = 21 * CANVA_SCALEX; | ||
const yTimeText = 19 * CANVA_SCALEY; | ||
const gameOverTimeText = new FieldWithText({ | ||
x: xTimeText, | ||
y: yTimeText, | ||
width: widthTimeText, | ||
height: heightTimeText, | ||
text: `YOU LOST, YOU REACHED ROUND: ${parameters.round}`, | ||
fontSize: UIManager.Typography.button.fontSize, | ||
fontFamily: UIManager.Typography.button.fontFamily, | ||
leftAlign: true, | ||
}); | ||
gameOverTimeText.HasBorderOnAllSide = false; | ||
parameters.uiManager.AddComponent(gameOverTimeText); | ||
|
||
/* Text that shows how long the player survived */ | ||
const widthSurvivalTimeText = 215 * CANVA_SCALEX; | ||
const heightSurvivalTimeText = 6 * CANVA_SCALEY; | ||
const xSurvivalTimeText = 21 * CANVA_SCALEX; | ||
const ySurvivalTimeText = 29 * CANVA_SCALEY; | ||
|
||
const gameOverSurvivalTimeText = new FieldWithText({ | ||
x: xSurvivalTimeText, | ||
y: ySurvivalTimeText, | ||
width: widthSurvivalTimeText, | ||
height: heightSurvivalTimeText, | ||
text: `YOU SURVIVED FOR: ${parameters.timePlayerSurvived}`, | ||
fontSize: UIManager.Typography.button.fontSize, | ||
fontFamily: UIManager.Typography.button.fontFamily, | ||
leftAlign: true, | ||
}); | ||
|
||
gameOverSurvivalTimeText.HasBorderOnAllSide = false; | ||
parameters.uiManager.AddComponent(gameOverSurvivalTimeText); | ||
} | ||
} |
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
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.