-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombat manager
More file actions
349 lines (280 loc) · 12.6 KB
/
Copy pathcombat manager
File metadata and controls
349 lines (280 loc) · 12.6 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { Spells } from "./spells.js";
import { saveManager } from "./saveManager.js";
import { bossManager } from "./bossManager.js";
import { audio } from "./audioManager.js";
class CombatManager {
constructor() {
this.playerHp = 100;
this.playerMaxHp = 100;
this.playerMana = 100;
this.playerMaxMana = 100;
this.playerLevel = 1;
this.playerXp = 0;
this.playerXpNeeded = 100;
this.combo = 0;
this.streak = 0;
this.highestComboThisDuel = 0;
this.spellsCompleted = 0;
this.targetSpells = 15;
this.damageTakenThisDuel = 0;
this.currentRequestedSpell = null;
// Callbacks to UI and Animations
this.onCombatUpdateCallback = null;
this.onSpellCompletedCallback = null;
this.onPlayerHitCallback = null;
this.onPlayerHealedCallback = null;
this.onLevelUpCallback = null;
this.onRuneUnlockCallback = null;
}
setupDuel(bossConfig, saveState, callbacks) {
this.playerHp = 100;
this.playerMaxHp = 100 + (saveState.level - 1) * 10; // health scales with level
this.playerMana = 100;
this.playerMaxMana = 100;
this.playerLevel = saveState.level;
this.playerXp = saveState.xp;
this.playerXpNeeded = saveState.xpNeeded;
this.combo = 0;
this.streak = 0;
this.highestComboThisDuel = 0;
this.spellsCompleted = 0;
this.targetSpells = bossConfig.targetSpells;
this.damageTakenThisDuel = 0;
// Register UI & Animation Callbacks
this.onCombatUpdateCallback = callbacks.onUpdate;
this.onSpellCompletedCallback = callbacks.onSpellCompleted;
this.onPlayerHitCallback = callbacks.onPlayerHit;
this.onPlayerHealedCallback = callbacks.onPlayerHealed;
this.onLevelUpCallback = callbacks.onLevelUp;
this.onRuneUnlockCallback = callbacks.onRuneUnlock;
// Pick the first requested spell
this.requestNextSpell(bossConfig.spellsAllowed);
if (this.onCombatUpdateCallback) {
this.onCombatUpdateCallback();
}
}
requestNextSpell(allowedSpells) {
if (!allowedSpells || allowedSpells.length === 0) return;
// Filter down to make sure we don't request the exact same spell twice in a row if there are multiple options
let pool = allowedSpells;
if (allowedSpells.length > 1 && this.currentRequestedSpell) {
pool = allowedSpells.filter(s => s !== this.currentRequestedSpell);
}
const randomIndex = Math.floor(Math.random() * pool.length);
this.currentRequestedSpell = pool[randomIndex];
if (this.onCombatUpdateCallback) {
this.onCombatUpdateCallback();
}
}
// Called when a simulated pose is completed
handlePoseInput(posePacket, allowedSpells) {
// Check if the completed spell is the one requested
if (posePacket.spell === this.currentRequestedSpell) {
this.handleSpellSuccess(posePacket, allowedSpells);
} else {
this.handleSpellMismatch(posePacket.spell);
}
}
handleSpellSuccess(posePacket, allowedSpells) {
const spellData = Spells[posePacket.spell];
if (!spellData) return;
this.spellsCompleted++;
this.combo++;
this.streak++;
if (this.combo > this.highestComboThisDuel) {
this.highestComboThisDuel = this.combo;
}
// Mana reward
this.playerMana = Math.min(this.playerMaxMana, this.playerMana + spellData.manaReward);
// Task 5: XP progression based on streak combo
let xpMultiplier = 1.0;
if (this.streak >= 15) xpMultiplier = 3.0;
else if (this.streak >= 10) xpMultiplier = 2.0;
else if (this.streak >= 5) xpMultiplier = 1.5;
let xpReward = 10 * xpMultiplier;
// Task 3: 15 streak double XP reward
if (this.streak >= 15) {
xpReward = xpReward * 2;
}
xpReward = Math.round(xpReward);
const leveledUp = saveManager.addXP(xpReward);
this.playerXp = saveManager.currentData.xp;
this.playerXpNeeded = saveManager.currentData.xpNeeded;
this.lastXpAwarded = xpReward; // cache for diagnostics
if (leveledUp) {
this.playerLevel = saveManager.currentData.level;
audio.playHealAura();
if (this.onLevelUpCallback) {
this.onLevelUpCallback(this.playerLevel);
}
}
// Task 3: Streak heal rewards
let healAmount = 0;
if (this.streak === 5) healAmount = 10;
else if (this.streak === 10) healAmount = 25;
if (healAmount > 0) {
this.playerHp = Math.min(this.playerMaxHp, this.playerHp + healAmount);
audio.playHealAura();
if (this.onPlayerHealedCallback) {
this.onPlayerHealedCallback(healAmount);
}
}
// Task 3: 15 streak stagger boss
if (this.streak === 15) {
bossManager.stagger(3000);
}
// Play casting whoosh SFX immediately
audio.playSpellCast(posePacket.spell);
// Interrupt boss immediately & reset boss attack timer (Task 1 Root Cause Fix)
bossManager.interrupt();
// Increase boss rage slightly on player success (Task 2)
bossManager.rage = Math.min(100, bossManager.rage + 5);
// Request next spell immediately to keep input HUD snappy
this.requestNextSpell(allowedSpells);
// Task 3: Damage bonus based on streak
let damageMultiplier = 1.0;
if (this.streak >= 10) damageMultiplier = 1.5; // +50% damage
else if (this.streak >= 5) damageMultiplier = 1.2; // +20% damage
else if (this.streak >= 3) damageMultiplier = 1.1; // +10% damage
const baseDamage = spellData.damage + (this.playerLevel - 1) * 2;
const finalDamage = Math.round(baseDamage * damageMultiplier);
// Notify game.js — this triggers the cast animation AND schedules damage at impact frame
if (this.onSpellCompletedCallback) {
this.onSpellCompletedCallback(posePacket.spell, finalDamage);
}
if (this.onCombatUpdateCallback) {
this.onCombatUpdateCallback();
}
}
applyPlayerSpellDamage(spellName, damage) {
const playerHpBefore = this.playerHp;
// Apply damage to boss HP and Armor at impact frame
const damageInfo = bossManager.takeDamage(damage);
// Check if boss defeated (session exercises completed OR boss HP <= 0)
if (bossManager.hp <= 0 || this.spellsCompleted >= this.targetSpells) {
bossManager.hp = 0; // Ensure boss is dead
bossManager.state = 'death';
bossManager.hitbox = null; // Remove boss hitbox
}
// Every successful player attack must log:
console.log(`BOSS HP BEFORE: ${Math.round(damageInfo.hpBefore)}`);
console.log(`DAMAGE: ${Math.round(damageInfo.amount)}`);
console.log(`ARMOR ABSORBED: ${Math.round(damageInfo.armorAbsorbed)}`);
console.log(`FINAL DAMAGE: ${Math.round(damageInfo.actualDamage)}`);
console.log(`BOSS HP AFTER: ${Math.round(damageInfo.hpAfter)}`);
// Task 6: Combat Diagnostics
this.logCombatDiagnostics("SUCCESS", damage, 0, playerHpBefore, damageInfo.hpBefore, this.lastXpAwarded || 10);
if (this.onCombatUpdateCallback) {
this.onCombatUpdateCallback();
}
}
applyBossAttackDamage(attackType, damage) {
const playerHpBefore = this.playerHp;
const bossHpBefore = bossManager.hp;
let finalDamage = damage;
// If player level is high, slightly mitigate damage
const levelDefenseBonus = (this.playerLevel - 1) * 1;
finalDamage = Math.max(5, finalDamage - levelDefenseBonus);
this.playerHp -= finalDamage;
this.damageTakenThisDuel += finalDamage;
// Task 2: Break combo and streak on failed spell / boss hit
this.combo = 0;
this.streak = 0;
audio.playHitImpact();
if (this.playerHp <= 0) {
this.playerHp = 0;
}
// Trigger callback to game.js (blood burst, hit stop, camera shake)
if (this.onPlayerHitCallback) {
this.onPlayerHitCallback(finalDamage);
}
// Task 6: Combat Diagnostics
this.logCombatDiagnostics("FAILURE", 0, finalDamage, playerHpBefore, bossHpBefore, 0);
if (this.onCombatUpdateCallback) {
this.onCombatUpdateCallback();
}
}
handleSpellMismatch(spellCast) {
const playerHpBefore = this.playerHp;
const bossHpBefore = bossManager.hp;
// Task 2: Reset combo and streak on mismatch
this.combo = 0;
this.streak = 0;
// Make a minor error sound (wrong spell penalty tone)
audio.init();
audio.resume();
if (audio.ctx) {
const osc = audio.ctx.createOscillator();
const gainNode = audio.ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(180, audio.ctx.currentTime);
gainNode.gain.setValueAtTime(0.15, audio.ctx.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.0001, audio.ctx.currentTime + 0.2);
osc.connect(gainNode);
gainNode.connect(audio.sfxGain);
osc.start();
osc.stop(audio.ctx.currentTime + 0.2);
}
// Task 2: Force boss to execute an immediate attack
bossManager.forceAttack();
if (this.onCombatUpdateCallback) {
this.onCombatUpdateCallback();
}
}
// Task 6: Custom logger formatting diagnostics
logCombatDiagnostics(result, damageDealt, damageReceived, playerHpBefore, bossHpBefore, xpAwarded) {
console.log(`
=========================================
🛡️ COMBAT DIAGNOSTICS: ${result}
=========================================
Result: ${result}
Player Damage Dealt: ${damageDealt}
Player Damage Received: ${damageReceived}
Boss HP: ${Math.round(bossHpBefore)} ➔ ${Math.round(bossManager.hp)} (Max: ${bossManager.currentBoss?.maxHp})
Player HP: ${Math.round(playerHpBefore)} ➔ ${Math.round(this.playerHp)} (Max: ${this.playerMaxHp})
Current Streak: ${this.streak}
Current Combo: ${this.combo}
XP Awarded: ${xpAwarded}
=========================================
`);
}
// End of battle metrics
getVictoryStats(bossId) {
const accuracy = Math.round((this.spellsCompleted / (this.spellsCompleted + (this.damageTakenThisDuel > 0 ? 1 : 0))) * 100);
const calories = this.spellsCompleted * 1.5; // roughly 1.5 kcal per exercise hold
// Calculate Rank
let rank = "C";
if (this.damageTakenThisDuel === 0) rank = "SS";
else if (this.highestComboThisDuel >= 12 && this.damageTakenThisDuel < 20) rank = "S";
else if (this.highestComboThisDuel >= 8 && this.damageTakenThisDuel < 40) rank = "A";
else if (this.highestComboThisDuel >= 5 && this.damageTakenThisDuel < 60) rank = "B";
// Update local storage save statistics
saveManager.updateStats({
exercises: this.spellsCompleted,
damageTaken: this.damageTakenThisDuel,
calories: calories,
highestCombo: this.highestComboThisDuel
});
// Save unlocked state
saveManager.completeLevel(bossId);
// Task 5: Victory XP bonus (+100 XP)
const baseVictoryXp = this.spellsCompleted * 15;
const finalVictoryXp = baseVictoryXp + 100;
saveManager.addXP(finalVictoryXp);
// Sync state variables with save file
this.playerLevel = saveManager.currentData.level;
this.playerXp = saveManager.currentData.xp;
this.playerXpNeeded = saveManager.currentData.xpNeeded;
return {
xpGained: finalVictoryXp,
accuracy: accuracy,
spellsCompleted: this.spellsCompleted,
damageTaken: this.damageTakenThisDuel,
highestStreak: this.highestComboThisDuel,
calories: calories,
rank: rank
};
}
}
export const combatManager = new CombatManager();
export default combatManager;