Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wellsousaaa
GitHub Repository: wellsousaaa/Five-Nights-at-Freddys-Web
Path: blob/main/src/components/Office.js
270 views
1
import React, { useState, useEffect } from "react";
2
import { connect } from "react-redux";
3
4
import Default from "../media/Textures/Office/Default.webp";
5
import Media from "./Media";
6
7
import Blackout from "../media/Textures/Office/304.webp";
8
import MusicBox from "../media/Sounds/music box.mp3";
9
import FreddyBlackout from "../media/Textures/Freddy.webp";
10
11
import JumpscareMP3 from "../media/Sounds/jumpscare.mp3";
12
import BonnieJumpscare from "../media/Textures/Bonnie-Jumpscare.webp";
13
import ChicaJumpscare from "../media/Textures/Chica-Jumpscare.webp";
14
import FreddyJumpscare1 from "../media/Textures/Freddy-Jumpscare1.gif";
15
import FreddyJumpscare2 from "../media/Textures/Freddy-Jumpscare.webp";
16
import FoxyJumpscare from "../media/Textures/Foxy-Jumpscare.gif";
17
18
import LD from "../media/Textures/Office/LD.webp";
19
import RD from "../media/Textures/Office/RD.webp";
20
import RD_LD from "../media/Textures/Office/RD_LD.webp";
21
import LD_RL from "../media/Textures/Office/LD_RL.webp";
22
import RD_LL from "../media/Textures/Office/RD_LL.webp";
23
24
import RD_LL_BONNIE from "../media/Textures/Office/RD_LL_BONNIE.webp";
25
import LD_RL_CHICA from "../media/Textures/Office/LD_RL_CHICA.webp";
26
import RL from "../media/Textures/Office/RL.webp";
27
28
import RL_LL_BONNIE from "../media/Textures/Office/RL_LL_BONNIE.webp";
29
import LL from "../media/Textures/Office/LL.webp";
30
import LL_BONNIE from "../media/Textures/Office/LL_BONNIE.webp";
31
import RL_LL from "../media/Textures/Office/RL_LL.webp";
32
import RL_CHICA from "../media/Textures/Office/RL_CHICA.webp";
33
import RL_LL_CHICA from "../media/Textures/Office/RL_LL_CHICA.webp";
34
import RL_LL_BONNIE_CHICA from "../media/Textures/Office/RL_LL_BONNIE_CHICA.webp";
35
36
let canJumpscare = true;
37
38
const officeImages = {
39
LD,
40
RD,
41
RD_LD,
42
LD_RL,
43
RD_LL,
44
RD_LL_BONNIE,
45
LD_RL_CHICA,
46
RL,
47
RL_LL_BONNIE,
48
LL,
49
LL_BONNIE,
50
RL_CHICA,
51
RL_LL,
52
RL_LL_CHICA,
53
RL_LL_BONNIE_CHICA,
54
};
55
56
let musicBox = new Audio(MusicBox);
57
let jumpscareSound = new Audio(JumpscareMP3);
58
musicBox.loop = "true";
59
60
function Office({
61
blackout,
62
animatronics,
63
officeConfig,
64
jumpscare,
65
isCameraOpen,
66
endGame,
67
dispatch,
68
}) {
69
const [isJumpscare, setIsJumpscare] = useState(null);
70
const [background, setBackground] = useState(Default);
71
const [blackoutBackground, setBlackoutBackground] = useState(Blackout);
72
73
useEffect(() => {
74
checkBackground();
75
}, [
76
officeConfig.leftDoor,
77
officeConfig.leftLight,
78
officeConfig.rightDoor,
79
officeConfig.rightLight,
80
]);
81
82
useEffect(() => {
83
if (isCameraOpen) return;
84
checkBackground();
85
86
if (jumpscare) {
87
dispatch({ type: "CHANGE_CAMERA_BUTTON" });
88
setIsJumpscare(jumpscare);
89
jumpscareSound.play();
90
setTimeout(() => {
91
endGame(false);
92
}, 5000);
93
}
94
}, [isCameraOpen]);
95
96
useEffect(() => {
97
if (animatronics.Foxy.jumpscare) {
98
dispatch({ type: "CHANGE_CAMERA_BUTTON" });
99
setIsJumpscare("Foxy");
100
jumpscareSound.play();
101
setTimeout(() => {
102
endGame(false);
103
}, 5000);
104
}
105
}, [animatronics.Foxy.jumpscare]);
106
107
useEffect(() => {
108
if (animatronics.Freddy.jumpscare) {
109
dispatch({ type: "CHANGE_CAMERA_BUTTON" });
110
setIsJumpscare("Freddy");
111
jumpscareSound.play();
112
setTimeout(() => {
113
endGame(false);
114
}, 5000);
115
}
116
}, [animatronics.Freddy.jumpscare]);
117
118
async function checkBackground() {
119
const { leftDoor, rightDoor, leftLight, rightLight } = officeConfig;
120
const { Bonnie, Chica } = animatronics;
121
122
function getBackground() {
123
const result = [];
124
125
if (rightDoor) result.push("RD");
126
if (leftDoor) result.push("LD");
127
128
if (rightLight && !rightDoor) result.push("RL");
129
if (leftLight && !leftDoor) result.push("LL");
130
if (Bonnie.door && leftLight) result.push("BONNIE");
131
if (Chica.door && rightLight) result.push("CHICA");
132
133
return result;
134
}
135
136
const result = await getBackground();
137
if (result.length === 0) setBackground(Default);
138
else setBackground(officeImages[result.join("_")]);
139
}
140
141
const checkDoorSounds = (light) => {
142
const { leftDoor, rightDoor } = officeConfig;
143
const { Bonnie, Chica } = animatronics;
144
145
let condition1 = light === "leftLight" && !leftDoor && Bonnie.door;
146
let condition2 = light === "rightLight" && !rightDoor && Chica.door;
147
148
if (condition1 || condition2) Media.Sounds.Surprise.play();
149
};
150
151
useEffect(() => {
152
if (blackout) FreddyJumpscare();
153
}, [blackout]);
154
155
useEffect(() => {
156
canJumpscare = true;
157
return () => {
158
canJumpscare = false;
159
musicBox.pause();
160
};
161
}, []);
162
163
const FreddyJumpscare = () => {
164
const musicBoxInterval = setInterval(() => {
165
let musicBoxNumber = Math.floor(Math.random() * 9);
166
if (!canJumpscare) clearInterval(musicBoxInterval);
167
if (musicBoxNumber < 3 && canJumpscare) {
168
musicBox.currentTime = 0;
169
musicBox.play();
170
setBlackoutBackground(FreddyBlackout);
171
clearInterval(musicBoxInterval);
172
173
const pauseMusicInterval = setInterval(() => {
174
let pauseNumber = Math.floor(Math.random() * 3);
175
if (!canJumpscare) clearInterval(pauseMusicInterval);
176
if (pauseNumber == 0 && canJumpscare) {
177
setBlackoutBackground(null);
178
musicBox.pause();
179
clearInterval(pauseMusicInterval);
180
181
const freddyInterval = setInterval(() => {
182
let freddyNumber = Math.floor(Math.random() * 9);
183
if (!canJumpscare) clearInterval(freddyInterval);
184
if (freddyNumber == 0 && canJumpscare) {
185
const JumpscareImage = FreddyJumpscare1;
186
setBlackoutBackground(JumpscareImage);
187
jumpscareSound.play();
188
dispatch({
189
type: "CHANGE_JUMPSCARE",
190
animatronic: true,
191
});
192
clearInterval(freddyInterval);
193
setTimeout(() => {
194
endGame(false);
195
setBlackoutBackground(Blackout);
196
}, 5000);
197
}
198
}, 3000);
199
}
200
}, 5000);
201
}
202
}, 3000);
203
};
204
205
if (isJumpscare === "Bonnie" && !isCameraOpen)
206
return (
207
<div className="office-container">
208
<img
209
alt="Office"
210
draggable="false"
211
style={{ width: "100vw" }}
212
src={BonnieJumpscare}
213
className="office-img"
214
useMap="#office"
215
/>
216
</div>
217
);
218
else if (isJumpscare === "Foxy")
219
return (
220
<div className="office-container">
221
<img
222
alt="Office"
223
draggable="false"
224
style={{ width: "100vw" }}
225
src={FoxyJumpscare}
226
className="office-img"
227
useMap="#office"
228
/>
229
</div>
230
);
231
else if (isJumpscare === "Freddy")
232
return (
233
<div className="office-container">
234
<img
235
alt="Office"
236
draggable="false"
237
style={{ width: "100vw" }}
238
src={FreddyJumpscare2}
239
className="office-img"
240
useMap="#office"
241
/>
242
</div>
243
);
244
else if (isJumpscare === "Chica" && !isCameraOpen)
245
return (
246
<div className="office-container">
247
<img
248
alt="Office"
249
draggable="false"
250
style={{ width: "100vw" }}
251
src={ChicaJumpscare}
252
className="office-img"
253
useMap="#office"
254
/>
255
</div>
256
);
257
258
return (
259
<>
260
{!blackout ? (
261
<div className="office-container">
262
<div style={{ width: "fit-content" }}>
263
<img
264
alt="Office"
265
draggable="false"
266
style={{ width: "100vw" }}
267
src={background}
268
className="office-img"
269
useMap="#office"
270
/>
271
<a
272
href=""
273
title="leftDoor"
274
style={{
275
position: "absolute",
276
left: "1.63%",
277
top: "46.53%",
278
width: "3.13%",
279
height: "10.56%",
280
zIndex: 2,
281
}}
282
onClick={(e) => {
283
e.preventDefault();
284
dispatch({
285
type: "CHANGE_OFFICE_CONFIG",
286
obj: "leftDoor",
287
});
288
checkBackground();
289
Media.Sounds.Door.play();
290
}}
291
></a>
292
<a
293
href=""
294
title="leftLight"
295
style={{
296
position: "absolute",
297
left: "1.75%",
298
top: "58.06%",
299
width: "3.13%",
300
height: "10.56%",
301
zIndex: 2,
302
}}
303
onClick={(e) => {
304
e.preventDefault();
305
dispatch({
306
type: "CHANGE_OFFICE_CONFIG",
307
obj: "leftLight",
308
});
309
checkBackground();
310
checkDoorSounds("leftLight");
311
}}
312
></a>
313
314
<a
315
href=""
316
title="rightDoor"
317
style={{
318
position: "absolute",
319
left: "94.25%",
320
top: "46.94%",
321
width: "3.13%",
322
height: "10.56%",
323
zIndex: 2,
324
}}
325
onClick={(e) => {
326
e.preventDefault();
327
dispatch({
328
type: "CHANGE_OFFICE_CONFIG",
329
obj: "rightDoor",
330
});
331
checkBackground();
332
Media.Sounds.Door.play();
333
}}
334
></a>
335
336
<a
337
href=""
338
title="rightLight"
339
style={{
340
position: "absolute",
341
left: "94.44%",
342
top: "58.47%",
343
width: "3.13%",
344
height: "10.56%",
345
zIndex: 2,
346
}}
347
onClick={(e) => {
348
e.preventDefault();
349
dispatch({
350
type: "CHANGE_OFFICE_CONFIG",
351
obj: "rightLight",
352
});
353
checkBackground();
354
checkDoorSounds("rightLight");
355
}}
356
></a>
357
</div>
358
</div>
359
) : (
360
<div className="office-container">
361
<img
362
alt="Office"
363
draggable="false"
364
style={{ width: "100vw" }}
365
src={blackoutBackground}
366
className="office-img"
367
/>
368
</div>
369
)}
370
</>
371
);
372
}
373
374
const mapStateToProps = (state) => {
375
return {
376
officeConfig: state.officeReducer,
377
animatronics: state.animatronicsReducer,
378
isCameraOpen: state.cameraReducer.isCameraOpen,
379
jumpscare: state.configReducer.jumpscare,
380
};
381
};
382
383
export default connect(mapStateToProps)(Office);
384
385