Skip to content

Commit 15b70c3

Browse files
committed
Added Name and Time
1 parent 77c2091 commit 15b70c3

File tree

12 files changed

+130
-15
lines changed

12 files changed

+130
-15
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"build": "ng build",
88
"watch": "ng build --watch --configuration development",
99
"test": "ng test",
10-
"createComponent": "ng generate component components/game",
11-
"createService": "ng generate service services/home"
10+
"createComponent": "ng generate component components/intro",
11+
"createService": "ng generate service services/leaderboard"
1212
},
1313
"private": true,
1414
"dependencies": {

src/app/app.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {RouterModule} from "@angular/router";
88
import {routes} from "./modules/route/app.routes";
99
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
1010
import {GameComponent} from './components/game/game.component';
11+
import { IntroComponent } from './components/intro/intro.component';
12+
import {LeaderboardService} from "./services/leaderboard.service";
1113

1214

1315
@NgModule({
@@ -16,6 +18,7 @@ import {GameComponent} from './components/game/game.component';
1618

1719
ErrorComponent,
1820
GameComponent,
21+
IntroComponent,
1922
],
2023
imports: [
2124
BrowserModule,
@@ -25,7 +28,7 @@ import {GameComponent} from './components/game/game.component';
2528
FormsModule
2629

2730
],
28-
providers: [],
31+
providers: [LeaderboardService],
2932
bootstrap: [AppComponent]
3033
})
3134
export class AppModule {

src/app/classes/entitiy/player/player.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class Player extends Sprite {
3535
private readonly hitbox: Hitbox;
3636
private potionEffects: PotionEffect[] = [];
3737
private collisionDone = new Set<Hitbox>;
38+
private name: string = 'Anonymous';
3839

3940
/**
4041
* Create a new player
@@ -86,6 +87,7 @@ export class Player extends Sprite {
8687
loop: false,
8788
imageSrc: '../../../assets/sprites/player/animation/enterDoor.png',
8889
onComplete: () => {
90+
8991
GameComponent.levelChange();
9092
GameComponent.player.preventInput = false;
9193

@@ -179,6 +181,14 @@ export class Player extends Sprite {
179181
return this.potionEffects;
180182
}
181183

184+
public setName(name: string) {
185+
this.name = name;
186+
}
187+
188+
public getName() {
189+
return this.name;
190+
}
191+
182192
public switchSprite(name: string, destinationDoor?: Door) {
183193
if (this.image === this.animations[name].image) return;
184194
this.currentFrame = 0;

src/app/classes/level/level.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ export class Level extends Sprite {
310310
GameComponent.player.getVelocity().setX(0);
311311
GameComponent.player.preventInput = true;
312312
GameComponent.player.switchSprite('enterDoor');
313+
if(GameComponent.getCurrentLevel() === GameComponent.levels[GameComponent.levels.length - 1]) {
314+
GameComponent.isFinished = true;
315+
}
313316

314317
}
315318

src/app/components/game/game.component.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {TitleScreen} from "../../classes/gui/window/title";
88
import {initializeSounds} from "../../classes/audio/audio";
99
import {Mobile} from "../../classes/gui/window/mobile";
1010
import {PotionEffectType} from "../../classes/collectibles/potion/potioneffect";
11+
import {LeaderboardService} from "../../services/leaderboard.service";
1112

1213
@Component({
1314
selector: 'app-game',
@@ -24,7 +25,9 @@ export class GameComponent implements AfterViewInit {
2425
public static isTitleScreen: boolean = true;
2526
public static hasInteracted: boolean = false;
2627
public static isFlashLightShaderOn: boolean = true;
27-
private static currentLevel = level1;
28+
public static levels = [level4];
29+
30+
private static currentLevel = level1 ?? GameComponent.levels[0];
2831
@ViewChild('canvas', {static: true})
2932
public canvas: ElementRef<HTMLCanvasElement> | undefined;
3033
@ViewChild('cameraCanvas', {static: true})
@@ -35,8 +38,10 @@ export class GameComponent implements AfterViewInit {
3538
public volume: number = localStorage.getItem('volume') ? parseFloat(localStorage.getItem('volume')!) : 1.0;
3639
private oldFrameTime: number = 1;
3740

41+
public static isFinished: boolean = false;
42+
3843

39-
constructor() {
44+
constructor(public readonly leaderboard: LeaderboardService) {
4045
GameComponent.player = new Player();
4146
initializeSounds();
4247
this.registerGuiListener();
@@ -62,9 +67,9 @@ export class GameComponent implements AfterViewInit {
6267
}
6368

6469
public static levelChange(): void {
65-
const levels = [level4];
66-
const index = levels.indexOf(GameComponent.getCurrentLevel());
67-
GameComponent.setCurrentLevel(levels[(index + 1) % levels.length]);
70+
71+
const index = this.levels.indexOf(GameComponent.getCurrentLevel());
72+
GameComponent.setCurrentLevel(this.levels[(index + 1) % this.levels.length]);
6873

6974

7075
}
@@ -197,6 +202,8 @@ export class GameComponent implements AfterViewInit {
197202
this.oldFrameTime = performance.now();
198203

199204

205+
206+
200207
GameComponent.getCurrentLevel().draw(this.context!, delta);
201208
GameComponent.player.update(this.context!, delta);
202209
GameComponent.player.drawSprite(this.context!, delta);
@@ -231,6 +238,8 @@ export class GameComponent implements AfterViewInit {
231238
return;
232239
}
233240

241+
if(!GameComponent.isFinished)
242+
this.leaderboard.time += delta;
234243

235244
}
236245

@@ -263,30 +272,35 @@ export class GameComponent implements AfterViewInit {
263272
0, 0, cameraWidth, cameraHeight
264273
);
265274

275+
//Draw Name onto Camera Canvas
276+
this.cameraContext!.fillStyle = 'white';
277+
this.cameraContext!.font = '20px Arial';
278+
this.cameraContext!.fillText(`Name: ${this.leaderboard.name} Time: ${Math.round(this.leaderboard.time* 100) / 100}`, 10, 30);
279+
266280
//Draw Coin Counter onto Camera Canvas
267281
this.cameraContext!.fillStyle = 'white';
268282
this.cameraContext!.font = '20px Arial';
269-
this.cameraContext!.fillText(`Coins: ${GameComponent.player.collectedCoins}`, 10, 30);
283+
this.cameraContext!.fillText(`Coins: ${GameComponent.player.collectedCoins}`, 10, 60);
270284

271285
//Draw Shine Counter onto Camera Canvas
272286
this.cameraContext!.fillStyle = 'white';
273287
this.cameraContext!.font = '20px Arial';
274-
this.cameraContext!.fillText(`Shines: ${GameComponent.player.collectedShines}`, 10, 60);
288+
this.cameraContext!.fillText(`Shines: ${GameComponent.player.collectedShines}`, 10, 90);
275289

276290
//Draw Key Counter onto Camera Canvas
277291
this.cameraContext!.fillStyle = 'white';
278292
this.cameraContext!.font = '20px Arial';
279-
this.cameraContext!.fillText(`Keys: ${GameComponent.player.collectedKeys}`, 10, 90);
293+
this.cameraContext!.fillText(`Keys: ${GameComponent.player.collectedKeys}`, 10, 120);
280294

281295
//Draw Active Potion Effects
282296
this.cameraContext!.fillStyle = 'white';
283297
this.cameraContext!.font = '20px Arial';
284298

285-
for(let i = 0; i < GameComponent.player.getPotionEffects().length; i++) {
299+
for (let i = 0; i < GameComponent.player.getPotionEffects().length; i++) {
286300
let duration = GameComponent.player.getPotionEffects()[i].duration;
287301
//Round to 2 decimal places
288302
duration = Math.round(duration * 100) / 100;
289-
this.cameraContext!.fillText(PotionEffectType[GameComponent.player.getPotionEffects()[i].type] + ": " + duration, 10, 120 + i * 30);
303+
this.cameraContext!.fillText(PotionEffectType[GameComponent.player.getPotionEffects()[i].type] + ": " + duration, 10, 150 + i * 30);
290304
}
291305

292306
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<input [(ngModel)]="leaderboardService.name" placeholder="Enter Name">
2+
<button (click)="redirect()">Start Playing</button>

src/app/components/intro/intro.component.scss

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { IntroComponent } from './intro.component';
4+
5+
describe('IntroComponent', () => {
6+
let component: IntroComponent;
7+
let fixture: ComponentFixture<IntroComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [IntroComponent]
12+
})
13+
.compileComponents();
14+
15+
fixture = TestBed.createComponent(IntroComponent);
16+
component = fixture.componentInstance;
17+
fixture.detectChanges();
18+
});
19+
20+
it('should create', () => {
21+
expect(component).toBeTruthy();
22+
});
23+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {Component, signal} from '@angular/core';
2+
import {LeaderboardService} from "../../services/leaderboard.service";
3+
import {Router} from "@angular/router";
4+
import {distinctUntilChanged} from "rxjs";
5+
import {FormControl} from "@angular/forms";
6+
import {GameComponent} from "../game/game.component";
7+
8+
@Component({
9+
selector: 'app-intro',
10+
templateUrl: './intro.component.html',
11+
styleUrl: './intro.component.scss'
12+
})
13+
export class IntroComponent {
14+
15+
public name: FormControl<string | null> = new FormControl('');
16+
17+
18+
constructor(public readonly leaderboardService: LeaderboardService,
19+
public readonly router: Router) {
20+
21+
22+
}
23+
24+
public redirect(): void {
25+
this.router.navigate(['/game']).then(r => console.log(r));
26+
}
27+
28+
29+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {Routes} from '@angular/router';
22
import {GameComponent} from "../../components/game/game.component";
3+
import {IntroComponent} from "../../components/intro/intro.component";
34

45
export const routes: Routes = [
5-
6-
{path: '**', component: GameComponent}
6+
{path: '', component: IntroComponent},
7+
{path: 'game', component: GameComponent}
78
];

0 commit comments

Comments
 (0)