/
ivaninvv
/
minus-5
Обзор
Документация
Войти
/
ivaninvv
/
minus-5
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/core/GameState.ts
79 строк
2 KB
Vladimir Ivanin
v1
03 дек 2025, 00:59
03 дек 2025, 00:59
1659a8e
Код
Авторство
О чём код?
export interface PlayerState { id: string; name: string; score: number; eliminated: boolean; color: number; isBot?: boolean; } export class GameState { public players: PlayerState[] = []; public currentTurn: number = 0; public obligatorySpot: { x: number, y: number } | null = null; constructor(vsBot: boolean = false) { // Demo players this.players.push({ id: 'p1', name: 'Player 1', score: 0, eliminated: false, color: 0x00FF00 }); this.players.push({ id: 'p2', name: vsBot ? 'Bot' : 'Player 2', score: 0, eliminated: false, color: 0x0000FF, isBot: vsBot }); } public getCurrentPlayer(): PlayerState { return this.players[this.currentTurn]; } public nextTurn() { const startTurn = this.currentTurn; do { this.currentTurn = (this.currentTurn + 1) % this.players.length; } while (this.players[this.currentTurn].eliminated && this.currentTurn !== startTurn); // Check for winner (only 1 left) const active = this.players.filter(p => !p.eliminated); if (active.length === 1) { console.log(`Winner is ${active[0].name}`); // TODO: Game Over state } } // Returns true if turn changed public handleShotResult(hit: boolean, shotSpot: { x: number, y: number }): boolean { const currentPlayer = this.players[this.currentTurn]; if (this.obligatorySpot) { // Must hit if (hit) { // Success. New spot. this.obligatorySpot = shotSpot; this.nextTurn(); return true; } else { // Failed. Penalty. currentPlayer.score -= 1; console.log(`${currentPlayer.name} penalty! Score: ${currentPlayer.score}`); if (currentPlayer.score <= -5) { currentPlayer.eliminated = true; console.log(`${currentPlayer.name} eliminated!`); } // Chain broken. this.obligatorySpot = null; this.nextTurn(); return true; } } else { // Free shot if (hit) { // Set obligatory spot this.obligatorySpot = shotSpot; this.nextTurn(); return true; } else { // Missed free shot. Next turn. this.nextTurn(); return true; } } } }