Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wellsousaaa
GitHub Repository: wellsousaaa/Five-Nights-at-Freddys-Web
Path: blob/main/src/Game.js
270 views
1
import React, { useEffect } from "react";
2
import { connect } from "react-redux";
3
4
import Animatronic from "./components/Animatronic";
5
import Office from "./components/Office";
6
import Camera from "./components/Camera";
7
import Hud from "./components/Hud";
8
import Media from "./components/Media";
9
10
let isBlackout = false;
11
12
let { Ambience } = Media.Sounds;
13
Ambience.loop = true;
14
15
let officeProps = { leftDoor: false, rightDoor: false };
16
17
const Game = ({
18
office,
19
isCameraOpen,
20
energy,
21
gameOver,
22
stages,
23
endGame,
24
dispatch,
25
}) => {
26
useEffect(() => {
27
Ambience.currentTime = 0;
28
Ambience.play();
29
isBlackout = false;
30
officeProps = { leftDoor: false, rightDoor: false };
31
}, []);
32
33
useEffect(() => {
34
if (gameOver) Ambience.pause();
35
}, [gameOver]);
36
37
useEffect(() => {
38
if (energy <= 0) {
39
isBlackout = true;
40
Ambience.pause();
41
}
42
}, [energy]);
43
44
useEffect(() => {
45
let newTime = 6300;
46
if (office.leftDoor) newTime -= 1100;
47
if (office.rightDoor) newTime -= 1100;
48
if (office.leftLight) newTime -= 500;
49
if (office.rightLight) newTime -= 500;
50
if (isCameraOpen) newTime -= 1100;
51
52
dispatch({ type: "CHANGE_TIME", content: newTime });
53
officeProps = {
54
leftDoor: office.leftDoor,
55
rightDoor: office.rightDoor,
56
};
57
}, [
58
office.leftDoor,
59
office.rightDoor,
60
office.leftLight,
61
office.rightLight,
62
isCameraOpen,
63
]);
64
65
const handleJumpscare = (character) => {
66
if (isBlackout || gameOver) return;
67
dispatch({
68
type: "CHANGE_ANIMATRONIC",
69
animatronic: character,
70
animatronicState: {
71
door: null,
72
camera: null,
73
jumpscare: true,
74
},
75
});
76
77
dispatch({ type: "CHANGE_JUMPSCARE", animatronic: character });
78
if (character === "Foxy" || character === "Freddy")
79
dispatch({ type: "FORCE_CAMERA_CLOSE" });
80
setTimeout(() => {
81
if (!isCameraOpen) dispatch({ type: "FORCE_CAMERA_CLOSE" });
82
}, 10000);
83
};
84
85
async function isThisDoorOpen(door) {
86
const isDoorOpen = await officeProps[door];
87
return isDoorOpen;
88
}
89
90
return (
91
<>
92
<Animatronic
93
stages={stages}
94
handleJumpscare={handleJumpscare}
95
gameOver={gameOver}
96
isThisDoorOpen={isThisDoorOpen}
97
blackout={energy <= 0}
98
/>
99
100
{!gameOver ? (
101
<>
102
{energy <= 0 ? null : <Hud />}
103
<Camera handleJumpscare={handleJumpscare} />
104
{isCameraOpen ? null : (
105
<Office endGame={endGame} blackout={energy <= 0} />
106
)}
107
</>
108
) : null}
109
</>
110
);
111
};
112
113
const mapStateToProps = (state) => {
114
return {
115
animatronics: state.animatronicsReducer,
116
time: state.configReducer.time,
117
hour: state.configReducer.hour,
118
energy: state.configReducer.energy,
119
office: state.officeReducer,
120
camera: state.cameraReducer.camera,
121
isCameraOpen: state.cameraReducer.isCameraOpen,
122
};
123
};
124
125
export default connect(mapStateToProps)(Game);
126
127