Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wellsousaaa
GitHub Repository: wellsousaaa/Five-Nights-at-Freddys-Web
Path: blob/main/src/Controller.js
270 views
1
import React, { useState, useEffect } from "react";
2
import BlackoutSound from "./media/Sounds/powerdown.mp3";
3
import { connect } from "react-redux";
4
import Game from "./Game";
5
6
import StaticImage from "./media/Textures/Static-Cam.webp";
7
import StaticSound from "./media/Sounds/Dead.mp3";
8
import VictoryGIF from "./media/Textures/Victory.gif";
9
import VictorySound from "./media/Sounds/Clock.mp3";
10
11
///89000
12
const TIME_TO_CHANGE_HOUR = 89000;
13
14
let gameOverAudio = new Audio(StaticSound);
15
let hourInterval = null;
16
17
function Controller({
18
isPlaying,
19
hour,
20
time,
21
energy,
22
jumpscare,
23
setStart,
24
dispatch,
25
stages,
26
}) {
27
const [gameOver, setGameOver] = useState(false);
28
const [victory, setVictory] = useState(false);
29
30
31
useEffect(() => {
32
dispatch({ type: "CLEAR_DATA" });
33
changeEnergy();
34
35
return () => {
36
// clearInterval(hourInterval);
37
dispatch({ type: "CLEAR_DATA" });
38
gameOverAudio.pause();
39
};
40
}, []);
41
42
useEffect(() => {
43
setTimeout(() => {
44
if (hour === 5 && !gameOver) endGame(true);
45
else changeHour(hour);
46
}, TIME_TO_CHANGE_HOUR);
47
}, [hour])
48
49
useEffect(() => {
50
if (energy <= 0) {
51
setBlackout();
52
} else changeEnergy(energy);
53
}, [energy]);
54
55
async function changeHour(h) {
56
if (isPlaying && !jumpscare && !gameOver && h < 6) {
57
dispatch({ type: "CHANGE_HOUR" });
58
}
59
}
60
61
async function changeEnergy(e) {
62
if (isPlaying && !gameOver && e > 0) {
63
setTimeout(() => {
64
dispatch({ type: "CHANGE_ENERGY" });
65
}, time);
66
}
67
}
68
69
const setBlackout = () => {
70
new Audio(BlackoutSound).play();
71
72
dispatch({ type: "FORCE_CAMERA_CLOSE" });
73
dispatch({ type: "CHANGE_CAMERA_BUTTON" });
74
};
75
76
const endGame = (hasWon) => {
77
if (hasWon) {
78
setVictory(true);
79
let VictoryMusic = new Audio(VictorySound);
80
VictoryMusic.play();
81
82
const victories = JSON.parse(localStorage.getItem("victories")) || {};
83
if(stages.mode !== "CUSTOM") victories[stages.mode] = "★"
84
85
localStorage.setItem("victories", JSON.stringify(victories));
86
} else {
87
setGameOver(true);
88
gameOverAudio.currentTime = 0;
89
gameOverAudio.play();
90
}
91
dispatch({ type: "SET_GAME_OVER" });
92
setTimeout(() => {
93
setStart(false);
94
}, 10000);
95
};
96
97
return (
98
<>
99
{gameOver ? (
100
<img
101
alt="static"
102
src={StaticImage}
103
style={{ width: "100vw" }}
104
/>
105
) : null}
106
{victory ? (
107
<div
108
style={{
109
width: "100vw",
110
height: "100vh",
111
display: "flex",
112
justifyContent: "center",
113
alignItems: "center",
114
}}
115
>
116
<img alt="victory" src={VictoryGIF} />
117
</div>
118
) : null}
119
<Game stages={stages} gameOver={gameOver || victory} endGame={endGame} />
120
</>
121
);
122
}
123
124
const mapStateToProps = (state) => {
125
return {
126
time: state.configReducer.time,
127
hour: state.configReducer.hour,
128
isPlaying: state.configReducer.isPlaying,
129
jumpscare: state.configReducer.jumpscare,
130
energy: state.configReducer.energy,
131
animatronics: state.animatronicsReducer,
132
};
133
};
134
135
export default connect(mapStateToProps)(Controller);
136
137