72 lines
2.4 KiB
JavaScript
72 lines
2.4 KiB
JavaScript
import { updateResourceUI } from '../components/resource-header/resource-header.js';
|
|
|
|
// Wir lagern die Daten in ein reaktives Zustandsobjekt aus.
|
|
// Später wird dieses Objekt durch die echte Backend-API befüllt
|
|
// (z.B. über WebSockets oder Polling, ähnlich wie in der Lobby).
|
|
const sectorState = {
|
|
res_primary: 1000, cap_primary: 10000,
|
|
res_secondary: 500, cap_secondary: 10000,
|
|
res_fuel_base: 0,cap_fuel_base: 10000,
|
|
res_fuel_adv: 0, cap_fuel_adv: 10000,
|
|
|
|
energy: 0,
|
|
|
|
pop_idle: 500,
|
|
pop_work: 0,
|
|
pop_injured: 500,
|
|
pop_happiness: 50,
|
|
res_food_base: 10000,
|
|
res_food_adv: 0,
|
|
res_premium: 150,
|
|
res_premium_global: 100,
|
|
res_premium_local: 50
|
|
};
|
|
|
|
/**
|
|
* Hilfsfunktion für gedeckelte Ressourcen-Produktion.
|
|
* Erhöht den aktuellen Wert um 'amount', stoppt aber exakt am 'cap'.
|
|
* Ist der Wert bereits über dem Cap (z.B. durch Startwerte oder Boni),
|
|
* wird nichts abgezogen, aber die Produktion pausiert.
|
|
*
|
|
* @param {number} currentValue - Der derzeitige Bestand
|
|
* @param {number} productionAmount - Menge, die hinzugefügt wird
|
|
* @param {number} capacity - Das maximale Lagerlimit
|
|
*
|
|
* @returns {number} - Der neue berechnete Bestand
|
|
*/
|
|
function addResourceWithCap(currentValue, productionAmount, capacity) {
|
|
if (currentValue >= capacity) {
|
|
return currentValue;
|
|
}
|
|
|
|
const newValue = currentValue + productionAmount;
|
|
if (newValue > capacity) {
|
|
return capacity;
|
|
}
|
|
|
|
return newValue;
|
|
}
|
|
|
|
/**
|
|
* Startet die Simulation und den Interval-Timer für die Ressourcen-Produktion.
|
|
* Initiiert zudem das erste sofortige UI-Update.
|
|
*/
|
|
export function startSimulation() {
|
|
// 1. Initiales Update beim Start, um die Nullen im HUD zu überschreiben
|
|
updateResourceUI(sectorState);
|
|
|
|
// 2. Der simulierte Game-Loop (1 Tick pro Sekunde)
|
|
setInterval(() => {
|
|
// Ressourcen berechnen
|
|
sectorState.res_primary = addResourceWithCap(sectorState.res_primary, 15, sectorState.cap_primary);
|
|
sectorState.res_secondary = addResourceWithCap(sectorState.res_secondary, 5, sectorState.cap_secondary);
|
|
|
|
// Nahrung wird hier beispielhaft verbraucht
|
|
sectorState.res_food_base -= 10;
|
|
|
|
// 3. UI mit den neuen Werten aktualisieren
|
|
updateResourceUI(sectorState);
|
|
}, 1000);
|
|
|
|
console.log('[Simulation] Game-Loop gestartet.');
|
|
} |