Repository navigation
Expand file tree
/
Copy pathrebirthSystem.js
More file actions
212 lines (187 loc) · 7.14 KB
/
Copy pathrebirthSystem.js
File metadata and controls
212 lines (187 loc) · 7.14 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
// rebirthSystem.js
export function initRebirthSystem({
els,
state,
keys,
save,
renderMain,
renderStore = () => {},
formatCompact
}) {
const STORAGE_KEY = "rebirthCount";
const BOOST_RATE = 1.10; // +10% par Rebirt
const costTable = [];
let unit = 1e6; // commence à 1 million
const stepsPerUnit = [1, 100, 500];
const unitCount = 16; // nombre de paliers
for (let i = 0; i < unitCount; i++) {
for (const step of stepsPerUnit) {
costTable.push(step * unit);
}
unit *= 1000; // passe à l’unité suivante
}
// Charger le compteur de Rebirths
state.rebirths = parseInt(localStorage.getItem(STORAGE_KEY) || "0", 10);
// Calcul du coût précédent / prochain
function getNextCost(n = state.rebirths) {
if (costTable[n] !== undefined) {
return costTable[n];
}
// au-delà de costTable, on continue en appliquant BOOST_RATE
const lastBase = costTable[costTable.length - 1];
const extra = n - (costTable.length - 1);
return Math.floor(lastBase * Math.pow(BOOST_RATE, extra));
}
function getPrevCost() {
return state.rebirths > 0
? getNextCost(state.rebirths - 1)
: 0;
}
// Boost global
function getTotalBoostFactor(n = state.rebirths) {
return Math.pow(BOOST_RATE, n);
}
// Création de la modale Rebirth (HTML & styles)
let modal = document.getElementById("rebirthModal");
if (!modal) {
modal = document.createElement("div");
modal.id = "rebirthModal";
modal.setAttribute("aria-hidden", "true");
Object.assign(modal.style, {
position: "fixed",
inset: "0",
display: "grid",
placeItems:"center",
background:"rgba(0,0,0,0.5)",
zIndex: "1000",
opacity: "0",
pointerEvents: "none",
transition: "opacity .15s ease"
});
modal.innerHTML = `
<div class="rebirth-card" role="dialog" aria-labelledby="rebirthTitle" style="
width:min(520px,92vw);
background:var(--panel,#141414);
border:1px solid rgba(255,255,255,.08);
border-radius:12px;
box-shadow:0 6px 30px rgba(0,0,0,.35);
overflow:hidden;
">
<header style="display:flex;align-items:center;justify-content:space-between;padding:12px 14px;background:rgba(255,255,255,.04);">
<h2 id="rebirthTitle" style="margin:0;font-size:1.05rem;">🌱 Rebirth</h2>
<button id="rbCloseBtn" aria-label="Fermer" style="
background:none;border:none;color:#bbb;font-size:1.1rem;cursor:pointer;padding:6px;
">✕</button>
</header>
<div style="padding:14px;display:grid;gap:12px;">
<div id="rbTopLine" style="font-size:.95rem;color:#ddd;"></div>
<div>
<div style="display:flex;justify-content:space-between;font-size:.85rem;color:#aaa;margin-bottom:6px;">
<span>Progression vers le prochain rebirth</span>
<span id="rbProgLabelRight">0%</span>
</div>
<div style="width:100%;height:12px;background:rgba(255,255,255,.08);border-radius:999px;overflow:hidden;">
<div id="rbProgress" style="height:100%;width:0%;background:linear-gradient(90deg,#27ae60,#2ecc71);"></div>
</div>
</div>
<div id="rbCosts" style="display:grid;grid-template-columns:1fr 1fr;gap:10px;font-size:.9rem;color:#ccc;">
<div style="background:rgba(255,255,255,.04);border:1px solid rgba(255,255,255,.06);border-radius:8px;padding:10px;">
<div style="opacity:.75;font-size:.8rem;">Coût précédent</div>
<div id="rbPrevCost" style="font-weight:600;">—</div>
</div>
<div style="background:rgba(255,255,255,.04);border:1px solid rgba(255,255,255,.06);border-radius:8px;padding:10px;">
<div style="opacity:.75;font-size:.8rem;">Coût prochain</div>
<div id="rbNextCost" style="font-weight:600;">—</div>
</div>
</div>
<div style="display:flex;gap:10px;justify-content:flex-end;margin-top:4px;">
<button id="rbDoBtn" class="btn btn-warning" style="padding:10px 14px;border-radius:8px;border:none;cursor:pointer;">
🌱 Rebirth
</button>
</div>
<div style="font-size:.85rem;color:#9aa;">
• Reset complet des points, upgrades, machines et boosts achetés • Boost permanent +10% par niveau
</div>
</div>
</div>`;
document.body.appendChild(modal);
}
// Sélecteurs internes
const rbCloseBtn = modal.querySelector("#rbCloseBtn");
const rbDoBtn = modal.querySelector("#rbDoBtn");
const rbTopLine = modal.querySelector("#rbTopLine");
const rbPrevCost = modal.querySelector("#rbPrevCost");
const rbNextCost = modal.querySelector("#rbNextCost");
const rbProgRight= modal.querySelector("#rbProgLabelRight");
const rbProgress = modal.querySelector("#rbProgress");
let rafId = null;
let open = false;
function openModal() {
modal.setAttribute("aria-hidden", "false");
modal.style.opacity = "1";
modal.style.pointerEvents = "auto";
open = true;
loop();
}
function closeModal() {
modal.setAttribute("aria-hidden", "true");
modal.style.opacity = "0";
modal.style.pointerEvents = "none";
open = false;
if (rafId) cancelAnimationFrame(rafId);
}
function updateUI() {
const cur = state.rebirths;
const next = cur + 1;
const curPct = Math.floor((getTotalBoostFactor(cur) - 1) * 100);
const nxtPct = Math.floor((getTotalBoostFactor(next) - 1) * 100);
rbTopLine.textContent =
`Rebirth actuel : ${cur} (+${curPct} %) → Prochain : ${next} (+${nxtPct} %)`;
const prevCost = getPrevCost();
const nextCost = getNextCost();
rbPrevCost.textContent = prevCost > 0 ? formatCompact(prevCost) : "—";
rbNextCost.textContent = formatCompact(nextCost);
const ratio = nextCost > 0 ? Math.min(1, state.points / nextCost) : 0;
const pct = Math.floor(ratio * 100);
rbProgRight.textContent = `${pct}%`;
rbProgress.style.width = `${pct}%`;
const can = state.points >= nextCost;
rbDoBtn.disabled = !can;
rbDoBtn.style.opacity = can ? "1" : ".6";
rbDoBtn.style.cursor = can ? "pointer" : "not-allowed";
}
function loop() {
if (!open) return;
updateUI();
rafId = requestAnimationFrame(loop);
}
// Listeners ouverture/fermeture
els.rebirthBtn.addEventListener("click", openModal);
rbCloseBtn.addEventListener("click", closeModal);
modal.addEventListener("click", e => {
if (e.target === modal) closeModal();
});
// **Action Rebirth**
rbDoBtn.addEventListener("click", () => {
const cost = getNextCost();
if (state.points < cost) return;
// 1) dépense et reset complet
state.points = 0;
state.pointsPerClick = 1;
keys.forEach(k => {
if (k !== "shopBoost" && k !== "points" && k !== "pointsPerClick") {
state[k] = 0;
}
});
// 2) incrémenter Rebirth et persister
state.rebirths += 1;
localStorage.setItem(STORAGE_KEY, String(state.rebirths));
// 3) Sauvegarde + mise à jour UI
save();
renderMain();
renderStore();
updateUI();
});
// UI initiale
updateUI();
}