-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattle_system.js
More file actions
139 lines (115 loc) · 5.87 KB
/
battle_system.js
File metadata and controls
139 lines (115 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { checkRectOverlap } from './shared/utils.js';
import { hitEnemy } from './shared/enemy.js';
import { RESPAWN_DELAY } from './shared/config.js';
export class BattleSystem {
constructor(room) {
this.room = room; // Ссылка на комнату для доступа к данным
}
update() {
const { bullets, enemies, players, teamManager } = this.room;
// Собираем цели (Враги + Живые Игроки)
// (Игроков берем из объекта values)
const activePlayers = Object.values(players).filter(p => !p.isDead && !p.isSpawning);
const targets = [...enemies, ...activePlayers];
bullets.forEach(b => {
if (b.isDead) return;
// 1. ПУЛЯ vs ПУЛЯ
for (const otherB of bullets) {
if (b !== otherB && !otherB.isDead && b.team !== otherB.team) {
if (checkRectOverlap(b, otherB)) {
b.isDead = true; otherB.isDead = true;
this.room.bulletEvents.push({ type: 'BULLET_CLASH', x: b.x, y: b.y });
}
}
}
if (b.isDead) return;
// 2. ПУЛЯ vs БАЗА
const bases = teamManager.getAllBases();
bases.forEach(base => {
if (base.isDead) return;
if (checkRectOverlap(b, base)) {
b.isDead = true;
if (b.ownerId >= 1000 && b.team === base.team) {
return; // Урон не наносим
}
teamManager.destroyBase(base.team);
this.room.bulletEvents.push({ type: 'BASE_DESTROY', x: base.x, y: base.y });
const winnerId = (base.team === 1) ? 2 : 1;
this.room.handleVictory(winnerId);
}
});
if (b.isDead) return;
// 3. ПУЛЯ vs ТАНК
for (const tank of targets) {
// Огонь по своим отключен (b.team !== tank.team)
// Огонь по себе отключен (b.ownerId !== tank.id)
// Добавляем friendlyFire из настроек, если нужно
if (b.team !== tank.team) {
if (checkRectOverlap(b, tank)) {
b.isDead = true;
// Щит
if (tank.shieldTimer > 0) {
this.room.bulletEvents.push({ type: 'SHIELD_HIT', x: tank.x, y: tank.y });
break;
}
let isDead = false;
let wasBonus = false;
// Логика получения урона
if (tank.inputs) {
// ЭТО ИГРОК
tank.hp--; // Снимаем жизнь
if (tank.hp <= 0) {
isDead = true; // Умер окончательно
const killer = this.getOwnerName(b.ownerId);
this.room.sendSystemMessage(`${tank.nickname} killed by ${killer}`);
} else {
// ВЫЖИЛ!
isDead = false;
// Играем звук брони (как у тяжелых ботов)
this.room.bulletEvents.push({ type: 'ARMOR_HIT', x: tank.x, y: tank.y });
}
} else {
// ЭТО БОТ
const idx = enemies.indexOf(tank);
if (idx !== -1) {
wasBonus = tank.isBonus;
// Передаем this.room в hitEnemy
const res = hitEnemy(this.room, idx);
isDead = res.isDead;
}
}
if (isDead) {
this.room.bulletEvents.push({ type: 'TANK_EXPLODE', x: tank.x, y: tank.y, isPlayer: !!tank.inputs });
// Если умер Игрок - ставим таймер
if (tank.inputs) {
tank.isDead = true;
tank.lives--;
tank.respawnTimer = RESPAWN_DELAY; // 1.5 сек (как мы договорились)
tank.x = -1000;
}
// Если умер бонусный танк (логика звука)
if (wasBonus) {
this.room.bulletEvents.push({ type: 'BONUS_SPAWN' });
}
} else {
// Броня
this.room.bulletEvents.push({ type: 'ARMOR_HIT', x: tank.x, y: tank.y });
}
break;
}
}
}
});
}
getOwnerName(ownerId) {
// Игрок?
const player = this.room.players[ownerId]; // ownerId может быть socketId_idx (строка)
if (player) return player.nickname || "Player";
// Бот?
if (typeof ownerId === 'number' && ownerId >= 1000) {
return `Bot #${ownerId}`;
}
if (this.room.players[ownerId]) return this.room.players[ownerId].nickname;
return "Unknown";
}
}