forked from wellsousaaa/Five-Nights-at-Freddys-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
125 lines (109 loc) · 2.9 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
import React, { useEffect } from "react";
import { connect } from "react-redux";
import Animatronic from "./components/Animatronic";
import Office from "./components/Office";
import Camera from "./components/Camera";
import Hud from "./components/Hud";
import Media from "./components/Media";
let isBlackout = false;
let { Ambience } = Media.Sounds;
Ambience.loop = true;
let officeProps = { leftDoor: false, rightDoor: false };
const Game = ({
office,
isCameraOpen,
energy,
gameOver,
stages,
endGame,
dispatch,
}) => {
useEffect(() => {
Ambience.currentTime = 0;
Ambience.play();
isBlackout = false;
officeProps = { leftDoor: false, rightDoor: false };
}, []);
useEffect(() => {
if (gameOver) Ambience.pause();
}, [gameOver]);
useEffect(() => {
if (energy <= 0) {
isBlackout = true;
Ambience.pause();
}
}, [energy]);
useEffect(() => {
let newTime = 6300;
if (office.leftDoor) newTime -= 1100;
if (office.rightDoor) newTime -= 1100;
if (office.leftLight) newTime -= 500;
if (office.rightLight) newTime -= 500;
if (isCameraOpen) newTime -= 1100;
dispatch({ type: "CHANGE_TIME", content: newTime });
officeProps = {
leftDoor: office.leftDoor,
rightDoor: office.rightDoor,
};
}, [
office.leftDoor,
office.rightDoor,
office.leftLight,
office.rightLight,
isCameraOpen,
]);
const handleJumpscare = (character) => {
if (isBlackout || gameOver) return;
dispatch({
type: "CHANGE_ANIMATRONIC",
animatronic: character,
animatronicState: {
door: null,
camera: null,
jumpscare: true,
},
});
dispatch({ type: "CHANGE_JUMPSCARE", animatronic: character });
if (character === "Foxy" || character === "Freddy")
dispatch({ type: "FORCE_CAMERA_CLOSE" });
setTimeout(() => {
if (!isCameraOpen) dispatch({ type: "FORCE_CAMERA_CLOSE" });
}, 10000);
};
async function isThisDoorOpen(door) {
const isDoorOpen = await officeProps[door];
return isDoorOpen;
}
return (
<>
<Animatronic
stages={stages}
handleJumpscare={handleJumpscare}
gameOver={gameOver}
isThisDoorOpen={isThisDoorOpen}
blackout={energy <= 0}
/>
{!gameOver ? (
<>
{energy <= 0 ? null : <Hud />}
<Camera handleJumpscare={handleJumpscare} />
{isCameraOpen ? null : (
<Office endGame={endGame} blackout={energy <= 0} />
)}
</>
) : null}
</>
);
};
const mapStateToProps = (state) => {
return {
animatronics: state.animatronicsReducer,
time: state.configReducer.time,
hour: state.configReducer.hour,
energy: state.configReducer.energy,
office: state.officeReducer,
camera: state.cameraReducer.camera,
isCameraOpen: state.cameraReducer.isCameraOpen,
};
};
export default connect(mapStateToProps)(Game);