From 72e5519c1f1a8f306c09790ed9c09ebab0b11ba8 Mon Sep 17 00:00:00 2001 From: Marcin Bator Date: Fri, 13 Dec 2024 19:05:00 +0100 Subject: [PATCH] feat: #197 classes init --- src/app/game/data/games.ts | 2 + src/app/game/game.page.component.ts | 11 +++- .../games/climbhill/climbhill.component.ts | 63 +++++++++++++++++++ .../games/climbhill/models/climbhill.class.ts | 36 +++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/app/game/games/climbhill/climbhill.component.ts create mode 100644 src/app/game/games/climbhill/models/climbhill.class.ts diff --git a/src/app/game/data/games.ts b/src/app/game/data/games.ts index 23e54d3..c12c455 100644 --- a/src/app/game/data/games.ts +++ b/src/app/game/data/games.ts @@ -3,9 +3,11 @@ import { Pong } from '../games/pong/models/pong.class'; import { FlappyBird } from '../games/flappybird/models/flappybird.class'; import { SkiJump } from '../games/skijump/models/skijump.class'; import { HappyJump } from '../games/happyjump/models/happyjump.class'; +import { ClimbHill } from '../games/climbhill/models/climbhill.class'; export const games: Record = { pong: new Pong(), skijump: new SkiJump(), flappybird: new FlappyBird(), happyjump: new HappyJump(), + climbhill: new ClimbHill(), }; diff --git a/src/app/game/game.page.component.ts b/src/app/game/game.page.component.ts index 310e44b..a7439ce 100644 --- a/src/app/game/game.page.component.ts +++ b/src/app/game/game.page.component.ts @@ -27,6 +27,7 @@ import { CantDisplayGameComponent } from './components/cant-display-game/cant-di import { FlappyBirdComponent } from './games/flappybird/flappybird.component'; import { SkiJumpGameWindowComponent } from './games/skijump/skijump.component'; import { HappyJumpComponent } from './games/happyjump/happyjump.component'; +import { ClimbHillComponent } from './games/climbhill/climbhill.component'; @Component({ selector: 'app-game', @@ -44,7 +45,6 @@ import { HappyJumpComponent } from './games/happyjump/happyjump.component'; SkiJumpGameWindowComponent, GameControlsComponent, FlappyBirdComponent, - HappyJumpComponent, ], template: `
@@ -115,6 +115,15 @@ import { HappyJumpComponent } from './games/happyjump/happyjump.component'; [gameRestart]="gameRestartSubject.asObservable()" [gamePause]="gamePauseSubject.asObservable()" /> } + @case ('climbhill') { + + } }
} diff --git a/src/app/game/games/climbhill/climbhill.component.ts b/src/app/game/games/climbhill/climbhill.component.ts new file mode 100644 index 0000000..ecb98a0 --- /dev/null +++ b/src/app/game/games/climbhill/climbhill.component.ts @@ -0,0 +1,63 @@ +/* eslint-disable max-lines */ +import { AfterViewInit, Component, OnDestroy, OnInit } from '@angular/core'; +import { CanvasComponent } from 'app/game/components/canvas/canvas.component'; +import { BaseGameWindowComponent } from '../base-game.component'; +import { ClimbHill, ClimbHillState } from './models/climbhill.class'; + +@Component({ + selector: 'app-climbhill', + standalone: true, + imports: [CanvasComponent], + template: `
+ score: {{ game.state.score }} +
+ + FPS: {{ fps }} `, +}) +export class ClimbHillComponent + extends BaseGameWindowComponent + implements OnInit, AfterViewInit, OnDestroy +{ + public override game!: ClimbHill; + + public override ngOnInit(): void { + super.ngOnInit(); + this.game = this.game as ClimbHill; + } + + public override ngAfterViewInit(): void { + super.ngAfterViewInit(); + this.render(); + } + + public override restart(): void { + this.game.state = new ClimbHillState(); + } + + protected override update(): void { + super.update(); + + //update + + this.render(); + } + + // + + private render(): void { + const context = this._canvas.getContext('2d'); + if (!context) return; + context.clearRect(0, 0, this._canvas.width, this._canvas.height); + } + + private random(min: number, max: number): number { + return Math.floor(Math.random() * (max - min + 1) + min); + } + + private round(value: number, decimals = 2): number { + return Math.round(value * Math.pow(10, decimals)) / Math.pow(10, decimals); + } +} diff --git a/src/app/game/games/climbhill/models/climbhill.class.ts b/src/app/game/games/climbhill/models/climbhill.class.ts new file mode 100644 index 0000000..275b4cf --- /dev/null +++ b/src/app/game/games/climbhill/models/climbhill.class.ts @@ -0,0 +1,36 @@ +import { TGameState } from '@gameModels/game-state.type'; +import { Game } from '@gameModels/game.class'; +import { Player } from '@gameModels/player.class'; + +export class ClimbHillState implements TGameState { + public score = 0; +} + +export class ClimbHill extends Game { + public override name = 'climbhill'; + public override state = new ClimbHillState(); + public override outputSpec = ``; + + public override players = [ + new Player( + 0, + true, + 'Player 1', + { gas: 0, brake: 0 }, + { + ArrowRight: { + variableName: 'gas', + pressedValue: 1, + releasedValue: 0, + }, + ArrowLeft: { + variableName: 'brake', + pressedValue: 1, + releasedValue: 0, + }, + }, + ', : value of {0, 1}, 0: gas/brake released, 1: gas/brake pressed', + { gas: '[ArrowRight]', brake: '[ArrowLeft]' } + ), + ]; +}