This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
340 lines (332 loc) · 12.3 KB
/
game.js
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
//THANK @la_grib (from http://discord.gg/code) for helping me
//find a great solution to the updating problem! :heart:
class Person {
constructor(name) {
this.jobStore = 0;
this.name = name || "";
this.alcoholicStore = false;
this.canGetJobStore = true;
this.jailStore = false;
this.daysTillOutStore = 0;
this.strikerStore = false;
this.strikebreakerStore = false;
this.druggieStore = false;
}
get job() {return this.jobStore;}
set job(value) {
this.jobStore = value;
updateFamily();
}
get alcoholic() {return this.alcoholicStore;}
set alcoholic(value) {
this.alcoholicStore = value;
updateFamily();
}
get canGetJob() {return this.canGetJobStore;}
set canGetJob(value) {
this.canGetJobStore = value;
if (!value) this.job = 0;
updateFamily();
}
get jail() {return this.jailStore;}
set jail(value) {
this.jailStore = value;
updateFamily();
}
get daysTillOut() {return this.daysTillOutStore;}
set daysTillOut(value) {
this.daysTillOutStore = value;
if (this.daysTillOutStore <= 0) {this.jail = false}
updateFamily();
}
get striker() {return this.strikerStore;}
set striker(value) {
this.strikerStore = value;
updateFamily();
}
get strikebreaker() {return this.strikebreakerStore}
set strikebreaker(value) {
this.strikebreakerStore = value;
updateFamily();
}
get druggie() {return this.druggieStore}
set druggie(value) {
this.druggieStore = value;
updateFamily();
}
}
//VARS
let membersStore = [];
let members = new Proxy(membersStore, {
apply: (target, thisArg, argumentList) => {
return thisArg[target].apply(this, argumentList);
},
deleteProperty: (target, property) => {
return true;
},
set: (target, property, value, receiver) => {
target[property] = value;
return true;
}
});
let moneyStore = 700;
Object.defineProperty(window, "money", {
get: () => moneyStore,
set: value => {
moneyStore = value;
updateMoney();
}
});
let babyStore = 0;
Object.defineProperty(window, "babies", {
get: () => babyStore,
set: value => {
babyStore = value;
updateFamily();
}
});
window.onload = () => {updateStatus("You come into America with your family and $700");};
let cycle = 0;
let dwelling = "homeless";
let house = 0;
let jobChooser = 0;
let cycleType = 0;//0 for first, 1 for second
//Concerning the family
function removeMember(button) {//Remove a member of the family
button.parentNode.remove();
}
function addMember() {//add a member of the family
let element = document.createElement("div");
element.setAttribute("class", "member");//create a new element, make it a member, and put the normal stuff inside it
element.innerHTML = "Name: <input/> <button onClick='removeMember(this)'>Remove</button>";
document.getElementById("family").appendChild(element);//add it to the DOM
}
function startGame() {//Start the game
let memberElements = Array.from(document.querySelectorAll(".member > input"));//get all of the member input elements
if (memberElements.length <= 0) {//alert if there aren't any
alert("You can't have a family with no members!");
return;//exit function
}
memberElements.forEach(memberElement => {
members.push(new Person(memberElement.value));//for each element, make a new person with their name
});
document.getElementById("dwelling").style.display = "block";
document.getElementById("family").style.display = "none";//hide the family menu and show the house one
updateFamily();
}
//Choosing a house
function chooseDwelling(chosenDwelling) {
let youHave = "";
if (chosenDwelling === "house") {
if (dwelling !== "house") {
if (money >= 300) {//make sure you have enough money
dwelling = "house";
house = 20;//this is a counting-down number---when it reaches 0 you stop paying
money -= 300;
updateStatus(`You pay $300 down for a new house!`);
youHave = "You already live in a house. Choose an option below to change your dwelling or press skip to continue ";
} else {
alert("You don't have enough money for that!");//if you don't...
return;
}
}
} else if (chosenDwelling === "rent") {
dwelling = "rent";
house = 0;//reset the counting-down if someone loses their house
youHave = "You currently pay rent each cycle. Choose an option below to change your dwelling or press skip to continue ";
} else {
dwelling = "homeless";
house = 0;//^^^
youHave = "You're homeless. Press skip to stay homeless, or choose one of the options below ";
}
youHave += "<button onClick='skipDwelling()'>Skip</button>";
document.getElementById("youHave").innerHTML = youHave;
if (cycle === 0) {
document.getElementById("job").style.display = "block";
document.getElementById("dwelling").style.display = "none";//go to the next screen
while(!members[jobChooser].canGetJob) jobChooser++;
document.getElementById("jobChooser").innerText = members[jobChooser].name;
} else {
document.getElementById("dwelling").style.display = "none";
document.getElementById("game").style.display = "block";
}
}
function skipDwelling() {
if (cycle === 0) {
document.getElementById("job").style.display = "block";
document.getElementById("dwelling").style.display = "none";//go to the next screen
while(!members[jobChooser].canGetJob) jobChooser++;
document.getElementById("jobChooser").innerText = members[jobChooser].name;
} else {
document.getElementById("dwelling").style.display = "none";
document.getElementById("game").style.display = "block";
}
}
function chooseJob(element, skip) {
if (skip) {
jobChooser++;//next jobChooser
if (!members[jobChooser]) {
document.getElementById("job").style.display = "none";
document.getElementById("game").style.display = "block";
startCycle();
return;
}
while(!members[jobChooser].canGetJob) {
jobChooser++;
if (!members[jobChooser]) {
document.getElementById("job").style.display = "none";
document.getElementById("game").style.display = "block";
startCycle();
return;
}
}
document.getElementById("jobChooser").innerText = members[jobChooser].name;//show their name
return;
};//if you're not getting a job or already have one, skip!
Array.from(document.getElementsByClassName("job")).forEach(el => {el.style.visibility = "hidden"});
element.style.visibility = "visible";//make sure you can only see the one you picked
element.style.background = "white";
let job = [0, 3, 5][Math.floor(Math.random()*3)];//see what job you get---completely random
if (job === 0) {
element.innerHTML = "<h1>You didn't get a job :(</h1><br>";//Here and below; show what job you got and your options
updateStatus(`"${members[jobChooser].name}" tried to get a job and failed`);
} else {
element.innerHTML = `<h1>You got a job that pays $${job}!</h1><br><span id="bribe"><button onClick="bribe()">Bribe the boss for double pay - $20</button></span><br><br>`;
updateStatus(`"${members[jobChooser].name}" got a job that pays $${job}!`);
}
element.innerHTML += "<button onClick='nextJobChoose(this)'>Next</button>";
element.removeAttribute("onClick");//make sure that it doesn't change when you click on it by taking that away
document.getElementById("skipButton").style.display = "none";//make the skip button invisible
members[jobChooser].job = job;//sets the job for real now
}
function bribe() {
if (money < 20) {
alert("You don't have enough money!");//yelling!
return;
}
money -= 20;//take away money
members[jobChooser].job *= 2;//double pay
document.getElementById("bribe").innerHTML = `Your job now earns you $${members[jobChooser].job}!`;//tell 'em
updateStatus(`"${members[jobChooser].name}" bribed the boss $20 to get double pay and earn $${members[jobChooser].job}!`);
}
function nextJobChoose(element) {
jobChooser++;//next jobChooser
if (!members[jobChooser]) {
document.getElementById("job").style.display = "none";
document.getElementById("game").style.display = "block";
startCycle();
return;
}
while(!members[jobChooser].canGetJob) {
jobChooser++;
if (!members[jobChooser]) {
document.getElementById("job").style.display = "none";
document.getElementById("game").style.display = "block";
cycleType = 0;
startCycle();
return;
}
}
document.getElementById("jobChooser").innerText = members[jobChooser].name;//show their name
Array.from(document.getElementsByClassName("job")).forEach(el => {el.style.visibility = "visible"});//make sure you can see all of the options
let parent = element.parentNode;
parent.style.background = "lightcyan";//make them light-cyan again
setTimeout(()=>{parent.setAttribute("onClick", "chooseJob(this)")}, 100);//some things I don't like, and this is one of them
parent.innerHTML = "";//take away the text
document.getElementById("skipButton").style.display = "";//Show the skip button again
}
//Game functions:
function startCycle() {
members.filter(member => member.job).forEach(member => {
money += member.job;
updateStatus(`You earned $${member.job} from "${member.name}"'s job.`);
});
if (house > 0) {
money -= 12;
updateStatus(`You spent $12 to pay off your house.`);
}
if (dwelling === "rent") {
money -= 9;
updateStatus(`You spent $9 on rent.`);
}
members.filter(member => member.alcoholic).forEach(member => {
money--;
updateStatus(`You spent $1 on alcohol for "${member.name}".`);
});
members.filter(member => member.druggie).forEach(member => {
money -= 2;
updateStatus(`You spent $2 on drugs for "${member.name}".`);
});
cycle++;
house--;
members.forEach(person => {
if (person.daysTillOut) person.daysTillOut--;
});
}
function chooseCycle(element) {
Array.from(document.getElementsByClassName("cycle")).forEach(el => {el.style.visibility = "hidden"});
element.style.visibility = "visible";
element.style.background = "white";
element.removeAttribute("onClick");
if (cycleType === 0) {
cycleType = 1;
chooseCycleOne(element);
} else if (cycleType === 1) {
cycleType = 2;
chooseCycleTwo(element);
} else if (cycleType === 2) {
cycleType = 3;
chooseCycleThree(element);
}
}
function nextCycleChoose(element) {
startCycle();
if (cycleType === 3) {
let string = `You pay $${members.length * 2} for groceries for the ${members.length} ${members.length === 1 ? "member" : "members"} in your family.<br>`;
if (babies > 0) string += `You pay $${babies * 4} for formula and food for your ${babies} ${babies === 1 ? "baby" : "babies"}.`;
updateStatus(string);
money -= ((members.length * 2) + (babies * 4));
document.getElementById("dwelling").style.display = "block";
document.getElementById("game").style.display = "none";
cycleType = 0;
}
Array.from(document.getElementsByClassName("cycle")).forEach(el => {el.style.visibility = "visible"});
let parent = element.parentNode;
parent.style.background = "lightcyan";
setTimeout(()=>{parent.setAttribute("onClick", "chooseCycle(this)")}, 100);
parent.innerHTML = "";
}
//DOM manipulation functions
function updateStatus(string) {
let status = document.getElementById("status");
status.innerHTML = `<p class="status">${string}</p><hr/>${status.innerHTML}`;
}
function updateFamily() {
let string = "";
members.forEach(person => {
string += `"${person.name}" - Job: $${person.job}`;
if (person.alcoholic) string += " - Alcoholic";
if (person.druggie) string += " - Addictied to drugs";
if (!person.canGetJob) string += " - Injured and can't get a job";
if (person.jail) string += ` - In jail, ${person.daysTillOut} ${person.daysTillOut === 1 ? "day" : "days"} until they're out.`;
if (person.striker) string += " - On strike";
if (person.strikebreaker) string += " - Breaking the strike";
string += "<br>"
});
if (babies) string += babies + (babies === 1 ? " baby" : " babies");
if (members.length === 0) {
Array.from(document.getElementsByClassName("hide")).forEach(el => {el.style.display = "none"});
document.getElementById("loseDie").style.display = "block";
setTimeout(() => {document.getElementById("laststatus").innerHTML = document.querySelector(".status").innerHTML}, 100);//quick and dirty fix
}
document.getElementById("familyDisplay").innerHTML = string;
document.getElementById("familyTitle").style.display = "block";
}
function updateMoney() {
if (money <= 0) {
Array.from(document.getElementsByClassName("hide")).forEach(el => {el.style.display = "none"});
document.getElementById("loseMoney").style.display = "block";
setTimeout(() => {document.getElementById("laststatustwo").innerHTML = document.querySelector(".status").innerHTML}, 100);//quick and dirty fix
}
document.getElementById("money").innerHTML = money;
}