Path: blob/main/src/components/Office.js
270 views
import React, { useState, useEffect } from "react";1import { connect } from "react-redux";23import Default from "../media/Textures/Office/Default.webp";4import Media from "./Media";56import Blackout from "../media/Textures/Office/304.webp";7import MusicBox from "../media/Sounds/music box.mp3";8import FreddyBlackout from "../media/Textures/Freddy.webp";910import JumpscareMP3 from "../media/Sounds/jumpscare.mp3";11import BonnieJumpscare from "../media/Textures/Bonnie-Jumpscare.webp";12import ChicaJumpscare from "../media/Textures/Chica-Jumpscare.webp";13import FreddyJumpscare1 from "../media/Textures/Freddy-Jumpscare1.gif";14import FreddyJumpscare2 from "../media/Textures/Freddy-Jumpscare.webp";15import FoxyJumpscare from "../media/Textures/Foxy-Jumpscare.gif";1617import LD from "../media/Textures/Office/LD.webp";18import RD from "../media/Textures/Office/RD.webp";19import RD_LD from "../media/Textures/Office/RD_LD.webp";20import LD_RL from "../media/Textures/Office/LD_RL.webp";21import RD_LL from "../media/Textures/Office/RD_LL.webp";2223import RD_LL_BONNIE from "../media/Textures/Office/RD_LL_BONNIE.webp";24import LD_RL_CHICA from "../media/Textures/Office/LD_RL_CHICA.webp";25import RL from "../media/Textures/Office/RL.webp";2627import RL_LL_BONNIE from "../media/Textures/Office/RL_LL_BONNIE.webp";28import LL from "../media/Textures/Office/LL.webp";29import LL_BONNIE from "../media/Textures/Office/LL_BONNIE.webp";30import RL_LL from "../media/Textures/Office/RL_LL.webp";31import RL_CHICA from "../media/Textures/Office/RL_CHICA.webp";32import RL_LL_CHICA from "../media/Textures/Office/RL_LL_CHICA.webp";33import RL_LL_BONNIE_CHICA from "../media/Textures/Office/RL_LL_BONNIE_CHICA.webp";3435let canJumpscare = true;3637const officeImages = {38LD,39RD,40RD_LD,41LD_RL,42RD_LL,43RD_LL_BONNIE,44LD_RL_CHICA,45RL,46RL_LL_BONNIE,47LL,48LL_BONNIE,49RL_CHICA,50RL_LL,51RL_LL_CHICA,52RL_LL_BONNIE_CHICA,53};5455let musicBox = new Audio(MusicBox);56let jumpscareSound = new Audio(JumpscareMP3);57musicBox.loop = "true";5859function Office({60blackout,61animatronics,62officeConfig,63jumpscare,64isCameraOpen,65endGame,66dispatch,67}) {68const [isJumpscare, setIsJumpscare] = useState(null);69const [background, setBackground] = useState(Default);70const [blackoutBackground, setBlackoutBackground] = useState(Blackout);7172useEffect(() => {73checkBackground();74}, [75officeConfig.leftDoor,76officeConfig.leftLight,77officeConfig.rightDoor,78officeConfig.rightLight,79]);8081useEffect(() => {82if (isCameraOpen) return;83checkBackground();8485if (jumpscare) {86dispatch({ type: "CHANGE_CAMERA_BUTTON" });87setIsJumpscare(jumpscare);88jumpscareSound.play();89setTimeout(() => {90endGame(false);91}, 5000);92}93}, [isCameraOpen]);9495useEffect(() => {96if (animatronics.Foxy.jumpscare) {97dispatch({ type: "CHANGE_CAMERA_BUTTON" });98setIsJumpscare("Foxy");99jumpscareSound.play();100setTimeout(() => {101endGame(false);102}, 5000);103}104}, [animatronics.Foxy.jumpscare]);105106useEffect(() => {107if (animatronics.Freddy.jumpscare) {108dispatch({ type: "CHANGE_CAMERA_BUTTON" });109setIsJumpscare("Freddy");110jumpscareSound.play();111setTimeout(() => {112endGame(false);113}, 5000);114}115}, [animatronics.Freddy.jumpscare]);116117async function checkBackground() {118const { leftDoor, rightDoor, leftLight, rightLight } = officeConfig;119const { Bonnie, Chica } = animatronics;120121function getBackground() {122const result = [];123124if (rightDoor) result.push("RD");125if (leftDoor) result.push("LD");126127if (rightLight && !rightDoor) result.push("RL");128if (leftLight && !leftDoor) result.push("LL");129if (Bonnie.door && leftLight) result.push("BONNIE");130if (Chica.door && rightLight) result.push("CHICA");131132return result;133}134135const result = await getBackground();136if (result.length === 0) setBackground(Default);137else setBackground(officeImages[result.join("_")]);138}139140const checkDoorSounds = (light) => {141const { leftDoor, rightDoor } = officeConfig;142const { Bonnie, Chica } = animatronics;143144let condition1 = light === "leftLight" && !leftDoor && Bonnie.door;145let condition2 = light === "rightLight" && !rightDoor && Chica.door;146147if (condition1 || condition2) Media.Sounds.Surprise.play();148};149150useEffect(() => {151if (blackout) FreddyJumpscare();152}, [blackout]);153154useEffect(() => {155canJumpscare = true;156return () => {157canJumpscare = false;158musicBox.pause();159};160}, []);161162const FreddyJumpscare = () => {163const musicBoxInterval = setInterval(() => {164let musicBoxNumber = Math.floor(Math.random() * 9);165if (!canJumpscare) clearInterval(musicBoxInterval);166if (musicBoxNumber < 3 && canJumpscare) {167musicBox.currentTime = 0;168musicBox.play();169setBlackoutBackground(FreddyBlackout);170clearInterval(musicBoxInterval);171172const pauseMusicInterval = setInterval(() => {173let pauseNumber = Math.floor(Math.random() * 3);174if (!canJumpscare) clearInterval(pauseMusicInterval);175if (pauseNumber == 0 && canJumpscare) {176setBlackoutBackground(null);177musicBox.pause();178clearInterval(pauseMusicInterval);179180const freddyInterval = setInterval(() => {181let freddyNumber = Math.floor(Math.random() * 9);182if (!canJumpscare) clearInterval(freddyInterval);183if (freddyNumber == 0 && canJumpscare) {184const JumpscareImage = FreddyJumpscare1;185setBlackoutBackground(JumpscareImage);186jumpscareSound.play();187dispatch({188type: "CHANGE_JUMPSCARE",189animatronic: true,190});191clearInterval(freddyInterval);192setTimeout(() => {193endGame(false);194setBlackoutBackground(Blackout);195}, 5000);196}197}, 3000);198}199}, 5000);200}201}, 3000);202};203204if (isJumpscare === "Bonnie" && !isCameraOpen)205return (206<div className="office-container">207<img208alt="Office"209draggable="false"210style={{ width: "100vw" }}211src={BonnieJumpscare}212className="office-img"213useMap="#office"214/>215</div>216);217else if (isJumpscare === "Foxy")218return (219<div className="office-container">220<img221alt="Office"222draggable="false"223style={{ width: "100vw" }}224src={FoxyJumpscare}225className="office-img"226useMap="#office"227/>228</div>229);230else if (isJumpscare === "Freddy")231return (232<div className="office-container">233<img234alt="Office"235draggable="false"236style={{ width: "100vw" }}237src={FreddyJumpscare2}238className="office-img"239useMap="#office"240/>241</div>242);243else if (isJumpscare === "Chica" && !isCameraOpen)244return (245<div className="office-container">246<img247alt="Office"248draggable="false"249style={{ width: "100vw" }}250src={ChicaJumpscare}251className="office-img"252useMap="#office"253/>254</div>255);256257return (258<>259{!blackout ? (260<div className="office-container">261<div style={{ width: "fit-content" }}>262<img263alt="Office"264draggable="false"265style={{ width: "100vw" }}266src={background}267className="office-img"268useMap="#office"269/>270<a271href=""272title="leftDoor"273style={{274position: "absolute",275left: "1.63%",276top: "46.53%",277width: "3.13%",278height: "10.56%",279zIndex: 2,280}}281onClick={(e) => {282e.preventDefault();283dispatch({284type: "CHANGE_OFFICE_CONFIG",285obj: "leftDoor",286});287checkBackground();288Media.Sounds.Door.play();289}}290></a>291<a292href=""293title="leftLight"294style={{295position: "absolute",296left: "1.75%",297top: "58.06%",298width: "3.13%",299height: "10.56%",300zIndex: 2,301}}302onClick={(e) => {303e.preventDefault();304dispatch({305type: "CHANGE_OFFICE_CONFIG",306obj: "leftLight",307});308checkBackground();309checkDoorSounds("leftLight");310}}311></a>312313<a314href=""315title="rightDoor"316style={{317position: "absolute",318left: "94.25%",319top: "46.94%",320width: "3.13%",321height: "10.56%",322zIndex: 2,323}}324onClick={(e) => {325e.preventDefault();326dispatch({327type: "CHANGE_OFFICE_CONFIG",328obj: "rightDoor",329});330checkBackground();331Media.Sounds.Door.play();332}}333></a>334335<a336href=""337title="rightLight"338style={{339position: "absolute",340left: "94.44%",341top: "58.47%",342width: "3.13%",343height: "10.56%",344zIndex: 2,345}}346onClick={(e) => {347e.preventDefault();348dispatch({349type: "CHANGE_OFFICE_CONFIG",350obj: "rightLight",351});352checkBackground();353checkDoorSounds("rightLight");354}}355></a>356</div>357</div>358) : (359<div className="office-container">360<img361alt="Office"362draggable="false"363style={{ width: "100vw" }}364src={blackoutBackground}365className="office-img"366/>367</div>368)}369</>370);371}372373const mapStateToProps = (state) => {374return {375officeConfig: state.officeReducer,376animatronics: state.animatronicsReducer,377isCameraOpen: state.cameraReducer.isCameraOpen,378jumpscare: state.configReducer.jumpscare,379};380};381382export default connect(mapStateToProps)(Office);383384385