Schlangen werden angezeigt aber sind noch nicht bewegbar

This commit is contained in:
2025-04-10 21:04:52 +02:00
parent 125639cfa1
commit d669420a4f
9 changed files with 250 additions and 24 deletions
@@ -3,19 +3,77 @@ const Playground = require("../Playground/Playground");
class Snake{
/** @param {SocketUser} player @param {Playground} playground */
constructor(player, playground, color, startTiles) {
constructor(player, playground, color, startTiles, startMovement) {
this.player = player;
this.playground = playground;
this.color = color;
this.tiles = [];
this.nextMovement = null;
this.nextMovement = startMovement;
this.directionDegree = new Map([
["right", 0],
["down", 90],
["left", 180],
["up", 270]
]);
this.player.socket.on("movement", (data) => { this.updateNextMovement(data) })
this.setup(startTiles);
}
setup(startTiles){
this.player.socket.emit("color", this.color);
const headX = startTiles.x;
const headY = startTiles.y;
let dx = 0;
let dy = 0;
switch (this.nextMovement) {
case "up": dy = 1; break;
case "down": dy = -1; break;
case "left": dx = 1; break;
case "right": dx = -1; break;
}
for (let i = 0; i < 3; i++) {
let type = null;
switch(i){
case 0:
type = "Head";
break;
case 2:
type = "End";
break;
default:
type = "Straight";
break;
}
this.tiles.push({
class: "Snake",
type: type,
color: this.color,
deg: this.directionDegree.get(this.nextMovement),
x: headX + i * dx,
y: headY + i * dy
});
}
this.drawTiles();
console.log(this.tiles);
}
drawTiles(){
this.tiles.forEach(tile => {
this.playground.setTile(tile.x, tile.y, tile);
})
}
updateNextMovement(data){
console.log(`${this.player.username} | ${this.nextMovement}`);
}
}