-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
332 lines (257 loc) · 7.17 KB
/
main.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
const FONT_FACE = 'PICO-8'
const FONT_SIZE = 16
const FONT = FONT_SIZE + 'px ' + FONT_FACE
const BAR_SIZE = 1000
const GRAPH_FREQ = 2
const BARS = {
profits : { value : 0, rate : 1, multipliers : [] },
family : { value : 800, rate : -1, multipliers : [] },
reputation : { value : 800, rate : -1, multipliers : [] },
productivity : { value : 800, rate : -1, multipliers : [] },
}
const barsArray = Object.keys(BARS)
const DAYS = 5
const INFINITY = 999999
const ACTIONS = {
'day off' : [
{ bar : 'profits', value : -100 },
{ bar : 'family', value : 10 },
{ bar : 'reputation', value : -10 },
{ bar : 'productivity', value : -5 },
],
'leave early' : [
{ bar : 'profits', value : -25 },
{ bar : 'family', value : 5 },
{ bar : 'reputation', value : -15 },
{ bar : 'productivity', value : -2 },
],
'family vacation' : [
{ bar : 'profits', multiplier : { value : -2, duration : 7 * DAYS } },
{ bar : 'family', value : 25 },
{ bar : 'reputation', value : -10 },
{ bar : 'productivity', value : 20 },
],
'employee raises' : [
{ bar : 'profits', multiplier : { value : -1.5, duration : INFINITY * DAYS } },
{ bar : 'family', value : 10 },
{ bar : 'reputation', value : 25 },
{ bar : 'productivity', value : 5 },
],
'volunteer work' : [
{ bar : 'family', value : 10 },
{ bar : 'reputation', value : 10 },
{ bar : 'productivity', value : -5 },
],
'employee recognition' : [
{ bar : 'profits', value : -200 },
{ bar : 'reputation', value : 15 },
{ bar : 'productivity', value : -5 },
],
'layoffs' : [
{ bar : 'profits', multiplier : { value : 2.5, duration : INFINITY * DAYS } },
{ bar : 'family', value : -10 },
{ bar : 'reputation', value : -25 },
{ bar : 'productivity', value : -5 },
],
'price increase' : [
{ bar : 'profits', multiplier : { value : 1.5, duration : INFINITY * DAYS } },
{ bar : 'family', value : -5 },
{ bar : 'reputation', value : -15 },
],
'overtime' : [
{ bar : 'profits', multiplier : { value : 2, duration : 2 * DAYS } },
{ bar : 'family', value : -40 },
{ bar : 'reputation', value : 5 },
{ bar : 'productivity', value : 5 },
],
}
const actionsArray = Object.keys(ACTIONS)
let selectedActionIndex = 0
const graph = { values : [], accruedTime : 0, nextValue : 0 }
let cooldown = 0
let time = 0
let gameOver = null
let lastBarMult = 1
function getAndUpdateMultiplier(bar, dt) {
let negative = -1
let positive = 1
if (BARS[bar].multipliers.length === 0)
return 1
for (const multiplier of BARS[bar].multipliers) {
if (multiplier.value > 0)
positive *= multiplier.value
else
negative *= Math.abs(multiplier.value)
// TODO: delete expired multiplier
if (multiplier.duration <= 0)
multiplier.value = 1
else
multiplier.duration -= dt
}
let result = positive
if (negative !== -1)
result = negative + positive
return result
}
function drawProfitGraph(x, y, w, h) {
PG.layer.strokeStyle('#666')
PG.layer.strokeRect(x, y, w, h)
PG.layer.strokeRect(x, y + h / 2, w, 1)
print('0', x - 16, y + h / 2 - 8, '#666')
for (let i = 0; i < graph.values.length; i++) {
const value = graph.values[i]
const gv = value / 100 * h / 2
if (gv > 0)
PG.layer.fillStyle('#0f0')
else
PG.layer.fillStyle('#f00')
PG.layer.fillRect(x + i * GRAPH_FREQ, y + h / 2 - gv, GRAPH_FREQ, gv)
}
}
function drawProfitTotal(x, y, w, h) {
print('0', x - 16, y + h / 2 - 8, '#666')
print('total profits', x - 70, y + h + 10, '#666')
const value = BARS['profits'].value
const gv = value / 1000 * h / 2
if (gv > 0)
PG.layer.fillStyle('#0f0')
else
PG.layer.fillStyle('#f00')
PG.layer.fillRect(x, y + h / 2 - gv, w, gv)
PG.layer.strokeStyle('#666')
PG.layer.strokeRect(x, y, w, h)
PG.layer.strokeRect(x, y + h / 2, w, 1)
}
const PG = playground({
width : 1280,
height : 720,
smoothing : false,
create : function() {
PG.loadImage('background')
PG.loadData('cards')
PG_FONT = PG.loadFont(FONT_FACE)
},
ready : function() {
},
step : function(dt) {
if (gameOver)
return
time += dt
if (time / DAYS >= 111)
gameOver = 'lose'
cooldown -= dt
if (cooldown < 0)
cooldown = 0
let posBarMult = 1
let negBarMult = -1
for(const bar of Object.keys(BARS)) {
let mult = getAndUpdateMultiplier(bar, dt)
if (bar === 'profits') {
mult += lastBarMult
} else {
const barMult = ((BARS[bar].value / 1000) * 6) - 3
if (barMult < 0)
negBarMult *= Math.abs(barMult)
else
posBarMult *= barMult
}
let rate = BARS[bar].rate * mult * dt * (1 + Math.random()*5)
if (rate > 50 * dt)
rate = 50 * dt
if (rate < -50 * dt)
rate = -50 * dt
BARS[bar].value += rate
if (BARS[bar].value > 1000) {
BARS[bar].value = 1000
if (bar === 'profits')
gameOver = 'win'
}
if (bar === 'profits' && BARS[bar].value < -1000) {
BARS[bar].value = -1000
gameOver = 'lose'
}
if (bar !== 'profits' && BARS[bar].value < 0) {
BARS[bar].value = 0
}
if (bar === 'profits') {
graph.accruedTime += dt
graph.nextValue += rate
if (graph.accruedTime > GRAPH_FREQ) {
graph.values.push(graph.nextValue)
graph.accruedTime = 0
graph.nextValue = 0
}
}
}
lastBarMult = posBarMult + negBarMult
},
render : function() {
PG.layer.drawImage(PG.images.background, 0, 0)
print(`day ${1 + Math.floor(time / DAYS)}`, 350, 88, '#666')
if (gameOver) {
print(`you ${gameOver}!`, 570, 250, '#666')
return
}
for (let i = 0; i < barsArray.length; i++) {
const bar = barsArray[i];
if (bar === 'profits')
continue
let colour
if (BARS[bar].value > 699)
colour = '#0f0'
else if (BARS[bar].value > 300)
colour = '#ff0'
else
colour = '#f00'
print(`${bar}`, 735, 88 + i * 35, colour)
PG.layer.strokeStyle(colour)
PG.layer.strokeRect(350, 95 + i * 35, 350, 10)
PG.layer.fillStyle(colour)
PG.layer.fillRect(350, 95 + i * 35, BARS[bar].value / BAR_SIZE * 350, 10)
}
drawProfitGraph(350, 230, 550, 75)
drawProfitTotal(790, 330, 50, 150)
if (cooldown > 0)
return
for (let i = 0; i < actionsArray.length; i++) {
const action = actionsArray[i]
let colour = '#666'
if (selectedActionIndex === i)
colour = '#ff0'
print(action, 350, 320 + i * 20, colour)
}
},
keyup : function(event) {
if (cooldown > 0)
return
if (event.key === 'up') {
selectedActionIndex -= 1
if (selectedActionIndex < 0)
selectedActionIndex = 0
}
if (event.key === 'down') {
selectedActionIndex += 1
if (selectedActionIndex > actionsArray.length - 1)
selectedActionIndex = actionsArray.length - 1
}
if (event.key === 'space' || event.key === 'enter') {
const action = ACTIONS[actionsArray[selectedActionIndex]]
// apply action
for (let i = 0; i < action.length; i++) {
const effect = action[i]
if (effect.value)
BARS[effect.bar].value += effect.value
if (effect.multiplier) {
const newMult = { value : effect.multiplier.value, duration : effect.multiplier.duration}
BARS[effect.bar].multipliers.push(newMult)
}
}
cooldown = 5
}
}
})
function print(text, x, y, colour) {
PG.layer.fillStyle(colour)
PG.layer.font(FONT)
PG.layer.fillText(text, x, y)
}