Path: blob/main/src/Controller.js
270 views
import React, { useState, useEffect } from "react";1import BlackoutSound from "./media/Sounds/powerdown.mp3";2import { connect } from "react-redux";3import Game from "./Game";45import StaticImage from "./media/Textures/Static-Cam.webp";6import StaticSound from "./media/Sounds/Dead.mp3";7import VictoryGIF from "./media/Textures/Victory.gif";8import VictorySound from "./media/Sounds/Clock.mp3";910///8900011const TIME_TO_CHANGE_HOUR = 89000;1213let gameOverAudio = new Audio(StaticSound);14let hourInterval = null;1516function Controller({17isPlaying,18hour,19time,20energy,21jumpscare,22setStart,23dispatch,24stages,25}) {26const [gameOver, setGameOver] = useState(false);27const [victory, setVictory] = useState(false);282930useEffect(() => {31dispatch({ type: "CLEAR_DATA" });32changeEnergy();3334return () => {35// clearInterval(hourInterval);36dispatch({ type: "CLEAR_DATA" });37gameOverAudio.pause();38};39}, []);4041useEffect(() => {42setTimeout(() => {43if (hour === 5 && !gameOver) endGame(true);44else changeHour(hour);45}, TIME_TO_CHANGE_HOUR);46}, [hour])4748useEffect(() => {49if (energy <= 0) {50setBlackout();51} else changeEnergy(energy);52}, [energy]);5354async function changeHour(h) {55if (isPlaying && !jumpscare && !gameOver && h < 6) {56dispatch({ type: "CHANGE_HOUR" });57}58}5960async function changeEnergy(e) {61if (isPlaying && !gameOver && e > 0) {62setTimeout(() => {63dispatch({ type: "CHANGE_ENERGY" });64}, time);65}66}6768const setBlackout = () => {69new Audio(BlackoutSound).play();7071dispatch({ type: "FORCE_CAMERA_CLOSE" });72dispatch({ type: "CHANGE_CAMERA_BUTTON" });73};7475const endGame = (hasWon) => {76if (hasWon) {77setVictory(true);78let VictoryMusic = new Audio(VictorySound);79VictoryMusic.play();8081const victories = JSON.parse(localStorage.getItem("victories")) || {};82if(stages.mode !== "CUSTOM") victories[stages.mode] = "★"8384localStorage.setItem("victories", JSON.stringify(victories));85} else {86setGameOver(true);87gameOverAudio.currentTime = 0;88gameOverAudio.play();89}90dispatch({ type: "SET_GAME_OVER" });91setTimeout(() => {92setStart(false);93}, 10000);94};9596return (97<>98{gameOver ? (99<img100alt="static"101src={StaticImage}102style={{ width: "100vw" }}103/>104) : null}105{victory ? (106<div107style={{108width: "100vw",109height: "100vh",110display: "flex",111justifyContent: "center",112alignItems: "center",113}}114>115<img alt="victory" src={VictoryGIF} />116</div>117) : null}118<Game stages={stages} gameOver={gameOver || victory} endGame={endGame} />119</>120);121}122123const mapStateToProps = (state) => {124return {125time: state.configReducer.time,126hour: state.configReducer.hour,127isPlaying: state.configReducer.isPlaying,128jumpscare: state.configReducer.jumpscare,129energy: state.configReducer.energy,130animatronics: state.animatronicsReducer,131};132};133134export default connect(mapStateToProps)(Controller);135136137