fix: correct collision message and improve asset loading

- Fix typo in collision message from "Ihr seit koolidiert!" to "Es gab eine Kollision!"
- Reorder TODO comment in endGame method
- Rename "Früchte" folder to "Fruits" for consistency
- Add TileLoader class to preload and cache game assets
- Refactor Overlay class to use TileLoader for improved image handling
This commit is contained in:
2025-04-28 14:59:18 +02:00
parent f450526375
commit 76392172b5
9 changed files with 47 additions and 12 deletions

View File

@@ -192,7 +192,7 @@ class Snake{
} }
if(exsitingtile?.class === "Snake"){ if(exsitingtile?.class === "Snake"){
// Eng Game weil schon belegt mit anderer oder eigender Schlange // Eng Game weil schon belegt mit anderer oder eigender Schlange
this.game.endGame(`Ihr seit koolidiert!`) this.game.endGame(`Es gab eine Kollision!`)
} }
this.playground.setTile(tile.x, tile.y, tile); this.playground.setTile(tile.x, tile.y, tile);

View File

@@ -55,12 +55,13 @@ class Game{
} }
endGame(msg){ endGame(msg){
// TODO: Spielende in die Datenbank eintragen
this.io.to(`game-${this.code}`).emit("gameEnd", { this.io.to(`game-${this.code}`).emit("gameEnd", {
msg: msg, msg: msg,
score: this.score, score: this.score,
}); });
// TODO: Spielende in die Datenbank eintragen
} }
waitingForPlayers(changeTime = false){ waitingForPlayers(changeTime = false){

View File

Before

Width:  |  Height:  |  Size: 90 KiB

After

Width:  |  Height:  |  Size: 90 KiB

View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 34 KiB

View File

@@ -1,11 +1,14 @@
import TileLoader from "./TileLoader.js";
class Overlay{ class Overlay{
constructor(src = null, deg = 0){ /** @param {TileLoader} tileLoader */
this.src = src; constructor(tileLoader, imgSrc = null, deg = 0){
this.tileLoader = tileLoader;
this.deg = deg; this.deg = deg;
this.src = imgSrc;
if(!this.src) return; if(!this.src) return;
this.image = new Image(); this.image = this.tileLoader.tileMap.get(this.src);
this.image.src = src;
} }
} }

View File

@@ -56,9 +56,9 @@ class Playground{
// Falls overlay vorhanden darauf rendern // Falls overlay vorhanden darauf rendern
const overlay = this.overlays[y][x]; const overlay = this.overlays[y][x];
if(!overlay.src) continue; if(!overlay.src) continue;
this.ctx.save();
// Rotation anwenden | Bisschen Komplexes Mathe Funzt :) // Rotation anwenden | Bisschen Komplexes Mathe Funzt :)
this.ctx.save();
const centerX = posX + this.tileSize / 2; const centerX = posX + this.tileSize / 2;
const centerY = posY + this.tileSize / 2; const centerY = posY + this.tileSize / 2;
this.ctx.translate(centerX, centerY); this.ctx.translate(centerX, centerY);
@@ -74,6 +74,7 @@ class Playground{
this.overlays.forEach(row => { this.overlays.forEach(row => {
row.forEach(overlay => { row.forEach(overlay => {
overlay.src = null; overlay.src = null;
overlay.image = null;
overlay.deg = 0; overlay.deg = 0;
}) })
}) })

View File

@@ -0,0 +1,26 @@
class TileLoader{
constructor(){
this.allSrc = [
"Fruits/Apfel.png",
"Fruits/Blaubeere.png",
"Snakes/blue/End.png",
"Snakes/blue/Head.png",
"Snakes/blue/Straight.png",
"Snakes/blue/Turn.png",
"Snakes/red/End.png",
"Snakes/red/Head.png",
"Snakes/red/Straight.png",
"Snakes/red/Turn.png",
];
this.tileMap = new Map();
this.allSrc.forEach(src => {
const img = new Image();
img.src = `./assets/${src}`;
this.tileMap.set(src, img);
})
}
}
export default TileLoader;

View File

@@ -1,4 +1,5 @@
import Overlay from "./Elements/Overlay.js"; import Overlay from "./Elements/Overlay.js";
import TileLoader from "./Elements/TileLoader.js";
import Game from "./Game.js" import Game from "./Game.js"
class Loop{ class Loop{
@@ -6,6 +7,7 @@ class Loop{
constructor(socket, game){ constructor(socket, game){
this.socket = socket; this.socket = socket;
this.game = game; this.game = game;
this.tileLoader = new TileLoader();
this.socket.on("loop", (data) => { this.loop(data) }); this.socket.on("loop", (data) => { this.loop(data) });
} }
@@ -14,13 +16,14 @@ class Loop{
this.game.playGround.resetOverlay(); this.game.playGround.resetOverlay();
const tiles = data.playground.tiles; const tiles = data.playground.tiles;
// Spielfeld Zeichenen TODO: Image Laden Bug Fixen // Spielfeld Zeichenen
tiles.forEach((row, x) => { tiles.forEach((row, x) => {
row.forEach((tile, y) => { row.forEach((tile, y) => {
if(!tile) return; if(!tile) return;
if(tile.class === "Snake"){ if(tile.class === "Snake"){
const overlay = new Overlay( const overlay = new Overlay(
`./assets/Snakes/${tile.color}/${tile.type}.png`, this.tileLoader,
`Snakes/${tile.color}/${tile.type}.png`,
tile.deg tile.deg
) )
@@ -28,7 +31,8 @@ class Loop{
} }
else if(tile.class === "Fruit"){ else if(tile.class === "Fruit"){
const overlay = new Overlay( const overlay = new Overlay(
`./assets/Früchte/${tile.type}.png` this.tileLoader,
`Fruits/${tile.type}.png`
) )
this.game.playGround.setOverlay(x, y, overlay); this.game.playGround.setOverlay(x, y, overlay);