Converted server to Typescript

This commit is contained in:
Thomas
2021-01-28 17:49:49 +01:00
parent 1149f17b20
commit cc6a474a88
29 changed files with 655 additions and 1830 deletions

75
server/dist/models/Player.js vendored Normal file
View File

@@ -0,0 +1,75 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Player = void 0;
const Wall_1 = require("./Wall");
const GameSettings_1 = require("./GameSettings");
class Player {
constructor(id, name) {
this.id = id;
this.name = name;
this.gameSettings = new GameSettings_1.GameSettings();
this.bestScore = 0;
this.angle = 0;
this.score = 0;
this.color = '#' + (0x1000000 + (Math.random()) * 0xffffff).toString(16).substr(1, 6);
this.x = this.gameSettings.arenaSize * (0.25 + Math.random() * 0.5);
this.y = this.gameSettings.arenaSize * (0.25 + Math.random() * 0.5);
this.walls = [];
this.lastWall = 0;
this.state = 'ALIVE';
this.reset();
}
reset() {
this.score = 0;
this.color = '#' + (0x1000000 + (Math.random()) * 0xffffff).toString(16).substr(1, 6);
this.x = this.gameSettings.arenaSize * (0.25 + Math.random() * 0.5);
this.y = this.gameSettings.arenaSize * (0.25 + Math.random() * 0.5);
this.walls = [];
this.lastWall = 0;
this.state = 'ALIVE';
}
kill() {
this.state = 'DEAD';
if (this.bestScore < this.score)
this.bestScore = this.score;
}
isOutOfBorders() {
return this.x - this.gameSettings.playerSize < 0 ||
this.x + this.gameSettings.playerSize > this.gameSettings.arenaSize ||
this.y - this.gameSettings.playerSize < 0 ||
this.y + this.gameSettings.playerSize > this.gameSettings.arenaSize;
}
isCloseToWall(wallA, wallB) {
const xar = Math.min(wallA.x, wallB.x) - this.gameSettings.playerSize;
const yar = Math.min(wallA.y, wallB.y) - this.gameSettings.playerSize;
const xbr = Math.min(wallA.x, wallB.x) + this.gameSettings.playerSize;
const ybr = Math.min(wallA.y, wallB.y) + this.gameSettings.playerSize;
return ((this.x >= xar && this.x <= xbr) && (this.y >= yar && this.y <= ybr));
}
isCrossingLine(wallA, wallB) {
const xa = wallA.x;
const ya = wallA.y;
const xb = wallB.x;
const yb = wallB.y;
const xc = this.x;
const yc = this.y;
const radius = this.gameSettings.playerSize;
return Math.abs((yb - ya) * xc - (xb - xa) * yc + xb * ya - yb * xa) / Math.sqrt(Math.pow(xb - xa, 2) + Math.pow(yb - ya, 2)) < radius;
}
step(tickToSimulate) {
if (this.state === 'DEAD')
return;
for (let i = 0; i < tickToSimulate; i++) {
this.lastWall++;
if (this.lastWall > this.gameSettings.wallUpdate) {
this.walls.push(new Wall_1.Wall(this.x, this.y));
this.score++;
this.lastWall = 0;
}
this.x = this.x + this.gameSettings.playerSpeed * Math.cos(this.angle);
this.y = this.y + this.gameSettings.playerSpeed * Math.sin(this.angle);
}
}
}
exports.Player = Player;
//# sourceMappingURL=Player.js.map