-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
582 lines (505 loc) · 22.2 KB
/
Copy pathscript.js
File metadata and controls
582 lines (505 loc) · 22.2 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/**
* LifeStream Simulator - Core Game Engine
* Object-Oriented Architecture for handling game loop, state, UI, and event dispatching.
*/
// --- DATA MANAGERS & UTILS ---
class SVGAvatarGenerator {
static generate(gender, seed = 0) {
const hairColors = ['#2c1b18', '#4a3728', '#b55239', '#d69d2a', '#e6cea8'];
const skinColors = ['#f8d5c2', '#e0ac69', '#c68642', '#8d5524', '#3a2211'];
const hairColor = hairColors[seed % hairColors.length];
const skinColor = skinColors[(seed * 3) % skinColors.length];
return `
<svg viewBox="0 0 100 100">
<circle cx="50" cy="50" r="45" fill="${skinColor}" />
<circle cx="35" cy="40" r="5" fill="#333" />
<circle cx="65" cy="40" r="5" fill="#333" />
<path d="M 35 65 Q 50 80 65 65" stroke="#333" stroke-width="3" fill="none" />
<path d="M 20 30 Q 50 ${gender === 'Female' ? '10' : '20'} 80 30" stroke="${hairColor}" stroke-width="12" fill="none" />
</svg>`;
}
}
class GameData {
constructor() {
this.events = [];
this.careers = [];
this.countries = [];
this.achievements = [];
}
async loadAll() {
try {
const [evRes, carRes, couRes, achRes] = await Promise.all([
fetch('events.json'),
fetch('careers.json'),
fetch('countries.json'),
fetch('achievements.json')
]);
this.events = await evRes.json();
this.careers = await carRes.json();
this.countries = await couRes.json();
this.achievements = await achRes.json();
} catch (err) {
console.error("Failed loading JSON game data:", err);
}
}
}
// --- CHARACTER MODEL ---
class Character {
constructor(data = {}) {
this.firstName = data.firstName || "Alex";
this.lastName = data.lastName || "Smith";
this.gender = data.gender || "Male";
this.country = data.country || "USA";
this.age = data.age || 0;
this.money = data.money || 0;
// Base Stats (0 - 100)
this.happiness = data.happiness ?? 80;
this.health = data.health ?? 90;
this.intelligence = data.intelligence ?? Math.floor(Math.random() * 60 + 20);
this.looks = data.looks ?? Math.floor(Math.random() * 60 + 20);
this.fame = data.fame ?? 0;
this.karma = data.karma ?? 50;
this.stress = data.stress ?? 0;
this.energy = data.energy ?? 100;
// Relationships & Status
this.relationships = data.relationships || [];
this.job = data.job || null;
this.education = data.education || "None";
this.inventory = data.inventory || [];
this.unlockedAchievements = data.unlockedAchievements || [];
this.isAlive = data.isAlive ?? true;
this.seed = data.seed || Math.floor(Math.random() * 1000);
}
clampStats() {
const stats = ['happiness', 'health', 'intelligence', 'looks', 'fame', 'karma', 'stress', 'energy'];
stats.forEach(s => {
this[s] = Math.max(0, Math.min(100, this[s]));
});
}
applyEffects(effects = {}) {
for (let key in effects) {
if (key in this) {
this[key] += effects[key];
}
}
this.clampStats();
}
}
// --- MAIN ENGINE CLASS ---
class LifeEngine {
constructor() {
this.data = new GameData();
this.character = null;
this.difficulty = "Normal";
this.initUI();
}
async init() {
await this.data.loadAll();
if (!this.loadGame()) {
this.createNewLife();
} else {
this.updateUI();
}
}
createNewLife() {
const randomCountry = this.data.countries[Math.floor(Math.random() * this.data.countries.length)] || { name: "USA", maleNames: ["John"], femaleNames: ["Jane"], lastNames: ["Doe"] };
const gender = Math.random() > 0.5 ? "Male" : "Female";
const firstList = gender === "Male" ? randomCountry.maleNames : randomCountry.femaleNames;
const firstName = firstList[Math.floor(Math.random() * firstList.length)];
const lastName = randomCountry.lastNames[Math.floor(Math.random() * randomCountry.lastNames.length)];
this.character = new Character({
firstName,
lastName,
gender,
country: randomCountry.name
});
// Add initial parents
this.character.relationships.push({
name: `Mother (${lastName})`,
type: "Parent",
relationship: 80
});
this.character.relationships.push({
name: `Father (${lastName})`,
type: "Parent",
relationship: 75
});
const feed = document.getElementById("eventFeed");
feed.innerHTML = "";
this.logEvent(`You were born in ${randomCountry.name} as ${firstName} ${lastName}.`);
this.saveGame();
this.updateUI();
}
ageUp() {
if (!this.character || !this.character.isAlive) return;
this.character.age++;
// Primary Life Progression Events
if (this.character.age === 5) this.character.education = "Elementary School";
if (this.character.age === 11) this.character.education = "Middle School";
if (this.character.age === 14) this.character.education = "High School";
if (this.character.age === 18) this.character.education = "High School Graduate";
// Natural Stat Shifts
this.character.health -= Math.floor(Math.random() * (this.character.age > 60 ? 4 : 2));
this.character.energy = 100;
this.character.clampStats();
this.logEvent(`You turned ${this.character.age} years old.`);
// Death Check
if (this.character.health <= 0 || (this.character.age > 75 && Math.random() < (this.character.age - 70) * 0.03)) {
this.handleDeath();
return;
}
// Trigger Random Event
this.triggerRandomEvent();
this.checkAchievements();
this.saveGame();
this.updateUI();
}
triggerRandomEvent() {
const eligibleEvents = this.data.events.filter(e => (!e.minAge || this.character.age >= e.minAge) && (!e.maxAge || this.character.age <= e.maxAge));
if (eligibleEvents.length === 0) return;
const event = eligibleEvents[Math.floor(Math.random() * eligibleEvents.length)];
this.showInteraction(event);
}
showInteraction(eventData) {
const overlay = document.getElementById("interactionOverlay");
const title = document.getElementById("eventTitle");
const desc = document.getElementById("eventDescription");
const choicesContainer = document.getElementById("eventChoices");
title.textContent = eventData.title;
desc.textContent = eventData.description;
choicesContainer.innerHTML = "";
eventData.choices.forEach(choice => {
const btn = document.createElement("button");
btn.className = "btn btn-primary";
btn.textContent = choice.text;
btn.onclick = () => {
if (choice.effects) {
this.character.applyEffects(choice.effects);
}
this.logEvent(`[Decision] ${choice.text}`);
overlay.classList.add("hidden");
this.saveGame();
this.updateUI();
};
choicesContainer.appendChild(btn);
});
overlay.classList.remove("hidden");
}
handleDeath() {
this.character.isAlive = false;
this.logEvent(`☠️ You died peacefully at age ${this.character.age}.`);
alert(`Life Over! You lived for ${this.character.age} years.`);
this.saveGame();
this.updateUI();
}
logEvent(text) {
const feed = document.getElementById("eventFeed");
const item = document.createElement("div");
item.className = "feed-item";
item.innerHTML = `<span class="age-badge">Age ${this.character.age}:</span> ${text}`;
feed.prepend(item);
}
checkAchievements() {
this.data.achievements.forEach(ach => {
if (!this.character.unlockedAchievements.includes(ach.id)) {
let unlocked = false;
if (ach.condition === "age" && this.character.age >= ach.value) unlocked = true;
if (ach.condition === "money" && this.character.money >= ach.value) unlocked = true;
if (unlocked) {
this.character.unlockedAchievements.push(ach.id);
this.logEvent(`🏆 Achievement Unlocked: ${ach.title}`);
if (typeof confetti === 'function') confetti();
}
}
});
}
saveGame() {
localStorage.setItem("lifestream_save", JSON.stringify(this.character));
}
loadGame() {
const saved = localStorage.getItem("lifestream_save");
if (saved) {
this.character = new Character(JSON.parse(saved));
return true;
}
return false;
}
initUI() {
document.getElementById("btnAgeUp").addEventListener("click", () => this.ageUp());
document.getElementById("btnNewLife").addEventListener("click", () => {
if (confirm("Are you sure you want to start a new life? Current progress will be lost.")) {
this.createNewLife();
}
});
document.getElementById("modalClose").addEventListener("click", () => {
document.getElementById("modal").classList.add("hidden");
});
document.getElementById("btnCareers").addEventListener("click", () => this.openCareersModal());
document.getElementById("btnActivities").addEventListener("click", () => this.openActivitiesModal());
}
updateUI() {
if (!this.character) return;
const c = this.character;
// Info
document.getElementById("charName").textContent = `${c.firstName} ${c.lastName}`;
document.getElementById("charTitle").textContent = c.job ? c.job.title : c.education;
document.getElementById("charDetails").textContent = `${c.age} yrs old • ${c.country}`;
document.getElementById("avatarContainer").innerHTML = SVGAvatarGenerator.generate(c.gender, c.seed);
// Stats
const stats = ['Happiness', 'Health', 'Intelligence', 'Looks', 'Fame', 'Karma', 'Stress', 'Energy'];
stats.forEach(s => {
const key = s.toLowerCase();
const val = c[key];
document.getElementById(`val${s}`).textContent = `${val}%`;
document.getElementById(`bar${s}`).style.width = `${val}%`;
});
// Summary
document.getElementById("valMoney").textContent = `$${c.money.toLocaleString()}`;
document.getElementById("valJob").textContent = c.job ? c.job.title : "Unemployed";
document.getElementById("valEducation").textContent = c.education;
// Relationships
const relContainer = document.getElementById("relationshipList");
relContainer.innerHTML = "";
c.relationships.forEach(r => {
const div = document.createElement("div");
div.className = "list-item";
div.innerHTML = `<span>${r.name} (${r.type})</span><span>${r.relationship}%</span>`;
relContainer.appendChild(div);
});
// Achievements Preview
const achContainer = document.getElementById("achieveList");
achContainer.innerHTML = "";
document.getElementById("achieveCount").textContent = c.unlockedAchievements.length;
c.unlockedAchievements.forEach(achId => {
const ach = this.data.achievements.find(a => a.id === achId);
if (ach) {
const div = document.createElement("div");
div.className = "list-item";
div.textContent = `🏆 ${ach.title}`;
achContainer.appendChild(div);
}
});
// Age Up Button state
document.getElementById("btnAgeUp").disabled = !c.isAlive;
}
openCareersModal() {
const modal = document.getElementById("modal");
const title = document.getElementById("modalTitle");
const body = document.getElementById("modalBody");
title.textContent = "Job Market";
body.innerHTML = "";
this.data.careers.forEach(job => {
const div = document.createElement("div");
div.className = "list-item";
div.style.marginBottom = "8px";
div.innerHTML = `
<div>
<strong>${job.title}</strong> - $${job.salary}/yr
<br><small>Req: Intel ${job.reqIntel}%</small>
</div>
`;
const applyBtn = document.createElement("button");
applyBtn.className = "btn btn-primary";
applyBtn.textContent = "Apply";
applyBtn.onclick = () => {
if (this.character.intelligence >= job.reqIntel) {
this.character.job = job;
this.logEvent(`You were hired as a ${job.title}!`);
modal.classList.add("hidden");
this.updateUI();
} else {
alert("You were rejected due to qualifications.");
}
};
div.appendChild(applyBtn);
body.appendChild(div);
});
modal.classList.remove("hidden");
}
openActivitiesModal() {
const modal = document.getElementById("modal");
const title = document.getElementById("modalTitle");
const body = document.getElementById("modalBody");
title.textContent = "Lifestyle & Activities";
body.innerHTML = `
<div class="menu-grid">
<button id="actGym" class="btn btn-menu">🏋️ Go to Gym (+Health)</button>
<button id="actRead" class="btn btn-menu">📚 Read Book (+Intel)</button>
<button id="actMeditate" class="btn btn-menu">🧘 Meditate (+Happiness)</button>
</div>
`;
modal.classList.remove("hidden");
document.getElementById("actGym").onclick = () => {
this.character.applyEffects({ health: 5, happiness: 2, energy: -10 });
this.logEvent("You worked out at the gym.");
modal.classList.add("hidden");
this.updateUI();
};
document.getElementById("actRead").onclick = () => {
this.character.applyEffects({ intelligence: 5, stress: -2 });
this.logEvent("You read an informative book.");
modal.classList.add("hidden");
this.updateUI();
};
document.getElementById("actMeditate").onclick = () => {
this.character.applyEffects({ happiness: 5, stress: -10 });
this.logEvent("You meditated peacefully.");
modal.classList.add("hidden");
this.updateUI();
};
}
}
// Global Initialization
window.addEventListener("DOMContentLoaded", () => {
window.game = new LifeEngine();
window.game.init();
});
// --- PARENT MODEL CLASS ---
class Parent {
constructor(data = {}) {
this.firstName = data.firstName || "Jane";
this.lastName = data.lastName || "Doe";
this.gender = data.gender || "Female";
this.age = data.age || 30;
this.occupation = data.occupation || "Unemployed";
this.salary = data.salary || 0;
this.education = data.education || "High School";
this.happiness = data.happiness ?? 75;
this.health = data.health ?? 85;
this.intelligence = data.intelligence ?? 50;
this.relationship = data.relationship ?? 80;
this.isAlive = data.isAlive ?? true;
this.jobData = data.jobData || null; // Stores promotion track info
}
// Clamp stats between 0 and 100
clampStats() {
this.happiness = Math.max(0, Math.min(100, this.happiness));
this.health = Math.max(0, Math.min(100, this.health));
this.intelligence = Math.max(0, Math.min(100, this.intelligence));
this.relationship = Math.max(0, Math.min(100, this.relationship));
}
}
// --- FAMILY SYSTEM CONTROLLER ---
class FamilySystem {
constructor(gameData) {
this.gameData = gameData;
this.mother = null;
this.father = null;
}
// Generate random realistic parents on new life
generateParents(lastName, countryData) {
const educations = ["None", "High School", "Trade School", "College", "University"];
// Helper to select random job matching education tier
const getRandomJob = (edu) => {
const jobs = this.gameData.familyData.parentJobs;
let available = jobs.filter(j => j.reqEducation === edu);
if (available.length === 0) available = jobs;
return available[Math.floor(Math.random() * available.length)];
};
// Generate Mother
const momEdu = educations[Math.floor(Math.random() * educations.length)];
const momJob = getRandomJob(momEdu);
this.mother = new Parent({
firstName: countryData.femaleNames[Math.floor(Math.random() * countryData.femaleNames.length)],
lastName: lastName,
gender: "Female",
age: Math.floor(Math.random() * 15) + 22, // 22 - 36 years old
occupation: momJob.title,
salary: momJob.salary,
education: momEdu,
jobData: momJob
});
// Generate Father
const dadEdu = educations[Math.floor(Math.random() * educations.length)];
const dadJob = getRandomJob(dadEdu);
this.father = new Parent({
firstName: countryData.maleNames[Math.floor(Math.random() * countryData.maleNames.length)],
lastName: lastName,
gender: "Male",
age: Math.floor(Math.random() * 15) + 24, // 24 - 38 years old
occupation: dadJob.title,
salary: dadJob.salary,
education: dadEdu,
jobData: dadJob
});
}
// Calculate household bracket
getFamilyIncomeTier() {
const totalIncome = (this.mother.isAlive ? this.mother.salary : 0) +
(this.father.isAlive ? this.father.salary : 0);
const tiers = this.gameData.familyData.socioeconomicTiers;
for (let t of tiers) {
if (totalIncome >= t.minIncome && totalIncome <= t.maxIncome) {
return { tier: t.tier, totalIncome };
}
}
return { tier: "Millionaire", totalIncome };
}
// Dynamic annual simulation tick for parents
ageUpParents(logCallback) {
[this.mother, this.father].forEach(parent => {
if (!parent || !parent.isAlive) return;
parent.age++;
// Age health drop
if (parent.age > 50) {
parent.health -= Math.floor(Math.random() * 3) + 1;
}
// Check parent death
if (parent.health <= 0 || (parent.age > 75 && Math.random() < (parent.age - 70) * 0.04)) {
parent.isAlive = false;
parent.occupation = "Deceased";
parent.salary = 0;
logCallback(`💔 Your ${parent.gender === "Female" ? "mother" : "father"}, ${parent.firstName}, has passed away at age ${parent.age}.`);
return;
}
// Retirement Check
if (parent.age >= 65 && parent.occupation !== "Retired") {
parent.occupation = "Retired";
parent.salary = Math.floor(parent.salary * 0.6); // Pension
logCallback(`👴 Your ${parent.gender === "Female" ? "mother" : "father"} retired at age ${parent.age}.`);
return;
}
// Skip event rolls if retired
if (parent.occupation === "Retired") return;
// Random Life Event Roll (25% chance per parent per year)
const eventRoll = Math.random();
if (eventRoll < 0.05) { // Promotion
if (parent.jobData && parent.jobData.promotions && parent.jobData.promotions.length > 0) {
const nextJob = parent.jobData.promotions.shift();
parent.occupation = nextJob;
parent.salary = Math.floor(parent.salary * 1.25);
logCallback(`📈 Your ${parent.gender === "Female" ? "mother" : "father"} got promoted to ${nextJob}!`);
}
} else if (eventRoll < 0.08) { // Pay Raise
const raise = Math.floor(parent.salary * 0.08);
parent.salary += raise;
logCallback(`💵 Your ${parent.gender === "Female" ? "mother" : "father"} got a salary raise!`);
} else if (eventRoll < 0.11) { // Job Loss
parent.occupation = "Unemployed";
parent.salary = 0;
logCallback(`⚠️ Your ${parent.gender === "Female" ? "mother" : "father"} lost their job.`);
} else if (eventRoll < 0.14) { // Found New Job
if (parent.occupation === "Unemployed") {
const jobs = this.gameData.familyData.parentJobs;
const newJob = jobs[Math.floor(Math.random() * jobs.length)];
parent.occupation = newJob.title;
parent.salary = newJob.salary;
parent.jobData = newJob;
logCallback(`💼 Your ${parent.gender === "Female" ? "mother" : "father"} started working as a ${newJob.title}.`);
}
} else if (eventRoll < 0.16) { // Illness
parent.health -= 20;
logCallback(`🤒 Your ${parent.gender === "Female" ? "mother" : "father"} fell ill.`);
} else if (eventRoll < 0.17) { // Lottery Win
parent.salary += 50000;
logCallback(`🎉 Your parents won a small lottery prize!`);
}
parent.clampStats();
});
// House upgrade event (2% chance if middle class or above)
const incomeInfo = this.getFamilyIncomeTier();
if (incomeInfo.totalIncome > 60000 && Math.random() < 0.02) {
logCallback(`🏡 Your parents bought a new house!`);
}
}
}