Initial commit
This commit is contained in:
Executable
+268
@@ -0,0 +1,268 @@
|
||||
// ─── Combat Store ─────────────────────────────────────────────────────────────
|
||||
// Handles floors, spells, guardians, combat, and casting
|
||||
|
||||
import { create } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
import { SPELLS_DEF, GUARDIANS, HOURS_PER_TICK } from '../constants';
|
||||
import type { GameAction, SpellState } from '../types';
|
||||
import { getFloorMaxHP, getFloorElement, calcDamage } from '../utils';
|
||||
import { usePrestigeStore } from './prestigeStore';
|
||||
|
||||
export interface CombatState {
|
||||
// Floor state
|
||||
currentFloor: number;
|
||||
floorHP: number;
|
||||
floorMaxHP: number;
|
||||
maxFloorReached: number;
|
||||
|
||||
// Action state
|
||||
activeSpell: string;
|
||||
currentAction: GameAction;
|
||||
castProgress: number;
|
||||
|
||||
// Spells
|
||||
spells: Record<string, SpellState>;
|
||||
|
||||
// Actions
|
||||
setCurrentFloor: (floor: number) => void;
|
||||
advanceFloor: () => void;
|
||||
setFloorHP: (hp: number) => void;
|
||||
setMaxFloorReached: (floor: number) => void;
|
||||
|
||||
setAction: (action: GameAction) => void;
|
||||
setSpell: (spellId: string) => void;
|
||||
setCastProgress: (progress: number) => void;
|
||||
|
||||
// Spells
|
||||
learnSpell: (spellId: string) => void;
|
||||
setSpellState: (spellId: string, state: Partial<SpellState>) => void;
|
||||
|
||||
// Combat tick
|
||||
processCombatTick: (
|
||||
skills: Record<string, number>,
|
||||
rawMana: number,
|
||||
elements: Record<string, { current: number; max: number; unlocked: boolean }>,
|
||||
maxMana: number,
|
||||
attackSpeedMult: number,
|
||||
onFloorCleared: (floor: number, wasGuardian: boolean) => void,
|
||||
onDamageDealt: (damage: number) => { rawMana: number; elements: Record<string, { current: number; max: number; unlocked: boolean }> },
|
||||
) => { rawMana: number; elements: Record<string, { current: number; max: number; unlocked: boolean }>; logMessages: string[] };
|
||||
|
||||
// Reset
|
||||
resetCombat: (startFloor: number, spellsToKeep?: string[]) => void;
|
||||
}
|
||||
|
||||
export const useCombatStore = create<CombatState>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
currentFloor: 1,
|
||||
floorHP: getFloorMaxHP(1),
|
||||
floorMaxHP: getFloorMaxHP(1),
|
||||
maxFloorReached: 1,
|
||||
activeSpell: 'manaBolt',
|
||||
currentAction: 'meditate',
|
||||
castProgress: 0,
|
||||
spells: {
|
||||
manaBolt: { learned: true, level: 1, studyProgress: 0 },
|
||||
},
|
||||
|
||||
setCurrentFloor: (floor: number) => {
|
||||
set({
|
||||
currentFloor: floor,
|
||||
floorHP: getFloorMaxHP(floor),
|
||||
floorMaxHP: getFloorMaxHP(floor),
|
||||
});
|
||||
},
|
||||
|
||||
advanceFloor: () => {
|
||||
set((state) => {
|
||||
const newFloor = Math.min(state.currentFloor + 1, 100);
|
||||
return {
|
||||
currentFloor: newFloor,
|
||||
floorHP: getFloorMaxHP(newFloor),
|
||||
floorMaxHP: getFloorMaxHP(newFloor),
|
||||
maxFloorReached: Math.max(state.maxFloorReached, newFloor),
|
||||
castProgress: 0,
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
setFloorHP: (hp: number) => {
|
||||
set({ floorHP: Math.max(0, hp) });
|
||||
},
|
||||
|
||||
setMaxFloorReached: (floor: number) => {
|
||||
set((state) => ({
|
||||
maxFloorReached: Math.max(state.maxFloorReached, floor),
|
||||
}));
|
||||
},
|
||||
|
||||
setAction: (action: GameAction) => {
|
||||
set({ currentAction: action });
|
||||
},
|
||||
|
||||
setSpell: (spellId: string) => {
|
||||
const state = get();
|
||||
if (state.spells[spellId]?.learned) {
|
||||
set({ activeSpell: spellId });
|
||||
}
|
||||
},
|
||||
|
||||
setCastProgress: (progress: number) => {
|
||||
set({ castProgress: progress });
|
||||
},
|
||||
|
||||
learnSpell: (spellId: string) => {
|
||||
set((state) => ({
|
||||
spells: {
|
||||
...state.spells,
|
||||
[spellId]: { learned: true, level: 1, studyProgress: 0 },
|
||||
},
|
||||
}));
|
||||
},
|
||||
|
||||
setSpellState: (spellId: string, spellState: Partial<SpellState>) => {
|
||||
set((state) => ({
|
||||
spells: {
|
||||
...state.spells,
|
||||
[spellId]: { ...(state.spells[spellId] || { learned: false, level: 0, studyProgress: 0 }), ...spellState },
|
||||
},
|
||||
}));
|
||||
},
|
||||
|
||||
processCombatTick: (
|
||||
skills: Record<string, number>,
|
||||
rawMana: number,
|
||||
elements: Record<string, { current: number; max: number; unlocked: boolean }>,
|
||||
maxMana: number,
|
||||
attackSpeedMult: number,
|
||||
onFloorCleared: (floor: number, wasGuardian: boolean) => void,
|
||||
onDamageDealt: (damage: number) => { rawMana: number; elements: Record<string, { current: number; max: number; unlocked: boolean }> },
|
||||
) => {
|
||||
const state = get();
|
||||
const logMessages: string[] = [];
|
||||
|
||||
if (state.currentAction !== 'climb') {
|
||||
return { rawMana, elements, logMessages };
|
||||
}
|
||||
|
||||
const spellId = state.activeSpell;
|
||||
const spellDef = SPELLS_DEF[spellId];
|
||||
if (!spellDef) {
|
||||
return { rawMana, elements, logMessages };
|
||||
}
|
||||
|
||||
// Calculate cast speed
|
||||
const baseAttackSpeed = 1 + (skills.quickCast || 0) * 0.05;
|
||||
const totalAttackSpeed = baseAttackSpeed * attackSpeedMult;
|
||||
const spellCastSpeed = spellDef.castSpeed || 1;
|
||||
const progressPerTick = HOURS_PER_TICK * spellCastSpeed * totalAttackSpeed;
|
||||
|
||||
let castProgress = (state.castProgress || 0) + progressPerTick;
|
||||
let floorHP = state.floorHP;
|
||||
let currentFloor = state.currentFloor;
|
||||
let floorMaxHP = state.floorMaxHP;
|
||||
|
||||
// Process complete casts
|
||||
while (castProgress >= 1) {
|
||||
// Check if we can afford the spell
|
||||
const cost = spellDef.cost;
|
||||
let canCast = false;
|
||||
|
||||
if (cost.type === 'raw') {
|
||||
canCast = rawMana >= cost.amount;
|
||||
if (canCast) rawMana -= cost.amount;
|
||||
} else if (cost.element) {
|
||||
const elem = elements[cost.element];
|
||||
canCast = elem && elem.unlocked && elem.current >= cost.amount;
|
||||
if (canCast) {
|
||||
elements = {
|
||||
...elements,
|
||||
[cost.element]: { ...elem, current: elem.current - cost.amount },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (!canCast) break;
|
||||
|
||||
// Calculate damage
|
||||
const floorElement = getFloorElement(currentFloor);
|
||||
const damage = calcDamage(
|
||||
{ skills, signedPacts: usePrestigeStore.getState().signedPacts },
|
||||
spellId,
|
||||
floorElement
|
||||
);
|
||||
|
||||
// Apply damage
|
||||
floorHP = Math.max(0, floorHP - damage);
|
||||
castProgress -= 1;
|
||||
|
||||
// Check if floor is cleared
|
||||
if (floorHP <= 0) {
|
||||
const wasGuardian = GUARDIANS[currentFloor];
|
||||
onFloorCleared(currentFloor, !!wasGuardian);
|
||||
|
||||
currentFloor = Math.min(currentFloor + 1, 100);
|
||||
floorMaxHP = getFloorMaxHP(currentFloor);
|
||||
floorHP = floorMaxHP;
|
||||
castProgress = 0;
|
||||
|
||||
if (wasGuardian) {
|
||||
logMessages.push(`⚔️ ${wasGuardian.name} defeated!`);
|
||||
} else if (currentFloor % 5 === 0) {
|
||||
logMessages.push(`🏰 Floor ${currentFloor - 1} cleared!`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
set({
|
||||
currentFloor,
|
||||
floorHP,
|
||||
floorMaxHP,
|
||||
maxFloorReached: Math.max(state.maxFloorReached, currentFloor),
|
||||
castProgress,
|
||||
});
|
||||
|
||||
return { rawMana, elements, logMessages };
|
||||
},
|
||||
|
||||
resetCombat: (startFloor: number, spellsToKeep: string[] = []) => {
|
||||
const startSpells = makeInitialSpells(spellsToKeep);
|
||||
|
||||
set({
|
||||
currentFloor: startFloor,
|
||||
floorHP: getFloorMaxHP(startFloor),
|
||||
floorMaxHP: getFloorMaxHP(startFloor),
|
||||
maxFloorReached: startFloor,
|
||||
activeSpell: 'manaBolt',
|
||||
currentAction: 'meditate',
|
||||
castProgress: 0,
|
||||
spells: startSpells,
|
||||
});
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: 'mana-loop-combat',
|
||||
partialize: (state) => ({
|
||||
currentFloor: state.currentFloor,
|
||||
maxFloorReached: state.maxFloorReached,
|
||||
spells: state.spells,
|
||||
activeSpell: state.activeSpell,
|
||||
}),
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// Helper function to create initial spells
|
||||
export function makeInitialSpells(spellsToKeep: string[] = []): Record<string, SpellState> {
|
||||
const startSpells: Record<string, SpellState> = {
|
||||
manaBolt: { learned: true, level: 1, studyProgress: 0 },
|
||||
};
|
||||
|
||||
// Add kept spells
|
||||
for (const spellId of spellsToKeep) {
|
||||
startSpells[spellId] = { learned: true, level: 1, studyProgress: 0 };
|
||||
}
|
||||
|
||||
return startSpells;
|
||||
}
|
||||
Reference in New Issue
Block a user