feat: buy x1/x5/x10/xMax + production preview per generator
All checks were successful
CI/CD — Build & Deploy / Build & Deploy (push) Successful in 21s
All checks were successful
CI/CD — Build & Deploy / Build & Deploy (push) Successful in 21s
- buyGenerator() supports quantity param (multi-buy loop) - maxAffordable() / bulkCost() — compute max purchasable + total cost - GeneratorShop: mode selector (x1/x5/x10/MAX) - Each generator shows +X/s in amber — what the next purchase adds - Button shows total cost + quantity (e.g. "1.5k (x5)")
This commit is contained in:
@@ -661,22 +661,62 @@ export function applyClick(state: GameState, rng: number = Math.random()): Click
|
||||
}
|
||||
|
||||
// Achat d'un générateur (retourne null si fonds insuffisants)
|
||||
export function buyGenerator(state: GameState, genId: string): GameState | null {
|
||||
export function buyGenerator(state: GameState, genId: string, quantity = 1): GameState | null {
|
||||
const genIndex = state.generators.findIndex((g) => g.id === genId);
|
||||
if (genIndex === -1) return null;
|
||||
|
||||
const gen = state.generators[genIndex];
|
||||
const cost = generatorCost(gen, state.evolutionTree);
|
||||
if (state.resources < cost) return null;
|
||||
let gen = { ...state.generators[genIndex] };
|
||||
let resources = state.resources;
|
||||
|
||||
let bought = 0;
|
||||
for (let i = 0; i < quantity; i++) {
|
||||
const cost = generatorCost(gen, state.evolutionTree);
|
||||
if (resources < cost) break;
|
||||
resources -= cost;
|
||||
gen = { ...gen, owned: gen.owned + 1 };
|
||||
bought++;
|
||||
}
|
||||
|
||||
if (bought === 0) return null;
|
||||
|
||||
const updatedGenerators = [...state.generators];
|
||||
updatedGenerators[genIndex] = { ...gen, owned: gen.owned + 1 };
|
||||
updatedGenerators[genIndex] = gen;
|
||||
|
||||
return {
|
||||
...state,
|
||||
resources: state.resources - cost,
|
||||
generators: updatedGenerators,
|
||||
};
|
||||
return { ...state, resources, generators: updatedGenerators };
|
||||
}
|
||||
|
||||
// Calcule combien d'unités on peut acheter avec les ressources actuelles
|
||||
export function maxAffordable(state: GameState, genId: string): number {
|
||||
const genIndex = state.generators.findIndex((g) => g.id === genId);
|
||||
if (genIndex === -1) return 0;
|
||||
|
||||
let gen = { ...state.generators[genIndex] };
|
||||
let resources = state.resources;
|
||||
let count = 0;
|
||||
|
||||
while (true) {
|
||||
const cost = generatorCost(gen, state.evolutionTree);
|
||||
if (resources < cost) break;
|
||||
resources -= cost;
|
||||
gen = { ...gen, owned: gen.owned + 1 };
|
||||
count++;
|
||||
if (count > 1000) break; // safety
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// Cout total pour acheter N unités
|
||||
export function bulkCost(state: GameState, genId: string, quantity: number): number {
|
||||
const genIndex = state.generators.findIndex((g) => g.id === genId);
|
||||
if (genIndex === -1) return Infinity;
|
||||
|
||||
let gen = { ...state.generators[genIndex] };
|
||||
let total = 0;
|
||||
for (let i = 0; i < quantity; i++) {
|
||||
total += generatorCost(gen, state.evolutionTree);
|
||||
gen = { ...gen, owned: gen.owned + 1 };
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
// Prestige : reset run, gain ADN, arbre persiste
|
||||
|
||||
Reference in New Issue
Block a user