forked from AsphaltWorld/Fnaf-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.js
More file actions
135 lines (116 loc) · 3.64 KB
/
Controller.js
File metadata and controls
135 lines (116 loc) · 3.64 KB
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
import React, { useState, useEffect } from "react";
import BlackoutSound from "./media/Sounds/powerdown.mp3";
import { connect } from "react-redux";
import Game from "./Game";
import StaticImage from "./media/Textures/Static-Cam.webp";
import StaticSound from "./media/Sounds/Dead.mp3";
import VictoryGIF from "./media/Textures/Victory.gif";
import VictorySound from "./media/Sounds/Clock.mp3";
///89000
const TIME_TO_CHANGE_HOUR = 89000;
let gameOverAudio = new Audio(StaticSound);
let hourInterval = null;
function Controller({
isPlaying,
hour,
time,
energy,
jumpscare,
setStart,
dispatch,
stages,
}) {
const [gameOver, setGameOver] = useState(false);
const [victory, setVictory] = useState(false);
useEffect(() => {
dispatch({ type: "CLEAR_DATA" });
changeEnergy();
return () => {
// clearInterval(hourInterval);
dispatch({ type: "CLEAR_DATA" });
gameOverAudio.pause();
};
}, []);
useEffect(() => {
setTimeout(() => {
if (hour === 5 && !gameOver) endGame(true);
else changeHour(hour);
}, TIME_TO_CHANGE_HOUR);
}, [hour])
useEffect(() => {
if (energy <= 0) {
setBlackout();
} else changeEnergy(energy);
}, [energy]);
async function changeHour(h) {
if (isPlaying && !jumpscare && !gameOver && h < 6) {
dispatch({ type: "CHANGE_HOUR" });
}
}
async function changeEnergy(e) {
if (isPlaying && !gameOver && e > 0) {
setTimeout(() => {
dispatch({ type: "CHANGE_ENERGY" });
}, time);
}
}
const setBlackout = () => {
new Audio(BlackoutSound).play();
dispatch({ type: "FORCE_CAMERA_CLOSE" });
dispatch({ type: "CHANGE_CAMERA_BUTTON" });
};
const endGame = (hasWon) => {
if (hasWon) {
setVictory(true);
let VictoryMusic = new Audio(VictorySound);
VictoryMusic.play();
const victories = JSON.parse(localStorage.getItem("victories")) || {};
if(stages.mode !== "CUSTOM") victories[stages.mode] = "★"
localStorage.setItem("victories", JSON.stringify(victories));
} else {
setGameOver(true);
gameOverAudio.currentTime = 0;
gameOverAudio.play();
}
dispatch({ type: "SET_GAME_OVER" });
setTimeout(() => {
setStart(false);
}, 10000);
};
return (
<>
{gameOver ? (
<img
alt="static"
src={StaticImage}
style={{ width: "100vw" }}
/>
) : null}
{victory ? (
<div
style={{
width: "100vw",
height: "100vh",
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
>
<img alt="victory" src={VictoryGIF} />
</div>
) : null}
<Game stages={stages} gameOver={gameOver || victory} endGame={endGame} />
</>
);
}
const mapStateToProps = (state) => {
return {
time: state.configReducer.time,
hour: state.configReducer.hour,
isPlaying: state.configReducer.isPlaying,
jumpscare: state.configReducer.jumpscare,
energy: state.configReducer.energy,
animatronics: state.animatronicsReducer,
};
};
export default connect(mapStateToProps)(Controller);