communication to the backend works

This commit is contained in:
miwr 2025-04-07 14:02:55 +02:00
parent 33dc8b423a
commit f54c02b8c2
4 changed files with 36 additions and 3 deletions

View file

@ -1,8 +1,9 @@
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideHttpClient } from '@angular/common/http'
import { provideRouter } from '@angular/router'; import { provideRouter } from '@angular/router';
import { routes } from './app.routes'; import { routes } from './app.routes';
export const appConfig: ApplicationConfig = { export const appConfig: ApplicationConfig = {
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes)] providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideHttpClient()]
}; };

13
src/app/jump.service.ts Normal file
View file

@ -0,0 +1,13 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({ providedIn: 'root' })
export class JumpService {
private apiUrl = 'http://localhost:8080/game/jump';
constructor(private http: HttpClient) {}
registerJump() {
return this.http.get<{ totalJumps: number }>(this.apiUrl, {});
}
}

View file

@ -1,4 +1,5 @@
import { Component, HostListener, OnInit } from '@angular/core'; import { Component, HostListener, OnInit } from '@angular/core';
import { JumpService } from '../jump.service';
@Component({ @Component({
selector: 'app-mario', selector: 'app-mario',
@ -59,6 +60,10 @@ import { Component, HostListener, OnInit } from '@angular/core';
this.loop(); this.loop();
} }
totalJumps = 0;
constructor(private jumpService: JumpService) {}
@HostListener('window:keydown', ['$event']) @HostListener('window:keydown', ['$event'])
handleKeyDown(event: KeyboardEvent) { handleKeyDown(event: KeyboardEvent) {
if (event.key === 'ArrowRight') this.mario.dx = this.mario.speed; if (event.key === 'ArrowRight') this.mario.dx = this.mario.speed;
@ -66,6 +71,10 @@ import { Component, HostListener, OnInit } from '@angular/core';
if (event.key === ' ' && this.mario.onGround) { if (event.key === ' ' && this.mario.onGround) {
this.mario.dy = -this.mario.jumpStrength; this.mario.dy = -this.mario.jumpStrength;
this.mario.onGround = false; this.mario.onGround = false;
this.jumpService.registerJump().subscribe({
next: (res) => this.totalJumps = res.totalJumps,
error: (err) => console.error('Jump API error:', err)
});
} }
} }
@ -144,6 +153,10 @@ import { Component, HostListener, OnInit } from '@angular/core';
this.ctx.fillRect(this.canvasWidth - 60, this.canvas.height - 160, 20, 100); this.ctx.fillRect(this.canvasWidth - 60, this.canvas.height - 160, 20, 100);
this.ctx.fillStyle = 'black'; this.ctx.fillStyle = 'black';
this.ctx.fillText('🏁', this.canvasWidth - 60, this.canvas.height - 170); this.ctx.fillText('🏁', this.canvasWidth - 60, this.canvas.height - 170);
this.ctx.fillStyle = 'black';
this.ctx.font = '20px Arial';
this.ctx.fillText(`Jumps: ${this.totalJumps}`, this.canvas.width - 150, 30);
} }
private scrollToMario() { private scrollToMario() {

View file

@ -1,7 +1,13 @@
import { bootstrapApplication } from '@angular/platform-browser'; import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config'; import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component'; import { AppComponent } from './app/app.component';
import { MarioComponent } from './app/mario/mario.component'; import { provideHttpClient } from '@angular/common/http';
bootstrapApplication(AppComponent, appConfig) bootstrapApplication(AppComponent, appConfig)
.catch((err) => console.error(err)); .catch((err) => console.error(err));
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient()
]
});