Files
blackhole-escape/Assets/Scripts/Other/MainMenuEngine.cs
iDunnoDev fb3415c7b2 Added new sounds needed for game
Added blackhole swirl texture and materials
Added Magnet, Hunter and Drop core powerups and script overhaul for powerups in general
Added Scenes and Cinematic changes for the game, win and death screens
Added a score
Changed the way enemy spawns work
Removed unused scripts
2022-07-14 17:31:10 +01:00

155 lines
5.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
public class MainMenuEngine : MonoBehaviour
{
public GameObject hudGO;
public GameObject joystickGO;
public GameObject yolkAGO;
public GameObject yolkBGO;
public TMPro.TextMeshProUGUI versionText;
public BackgroundMusicManager currentBGMManager;
public MenuAudioManager currentMenuAudioManager;
public float joystickJiggleSpeed;
public float flashFrequencyTime;
private TimerHelper _flashTimer;
private MeshRenderer _meshRenderer;
private Color _defaultEmissionColor;
private GameObject _lastControlSelected;
private void Awake()
{
GameManager.Instance.currentGameState = GameState.MAINMENU;
}
// Start is called before the first frame update
void Start()
{
// If the BGM manager is present, queue up and play the given track index
if (currentBGMManager)
{
currentBGMManager.StartAudioQueueAndPlay(0);
}
_flashTimer = new TimerHelper(flashFrequencyTime);
_meshRenderer = hudGO.GetComponent<MeshRenderer>();
_defaultEmissionColor = _meshRenderer.materials[0].GetColor("_EmissionColor");
_lastControlSelected = EventSystem.current.firstSelectedGameObject;
versionText.text = versionText.text.Replace("{versionNo}", GameManager.Instance.CurrentVersion);
}
public void SetupVolumeOptions()
{
// Gets the volume settings from the player profile and sets it in the game
float currentMasterVol = PlayerPrefs.GetFloat("currentMasterVol");
GameObject masterVolSliderGo = GameObject.Find("MainVolSlider");
float currentMusicVol = PlayerPrefs.GetFloat("currentMusicVol");
GameObject musicVolSliderGo = GameObject.Find("MusicVolSlider");
float currentSFXVol = PlayerPrefs.GetFloat("currentSFXVol");
GameObject SFXVolSliderGo = GameObject.Find("SfxVolSlider");
masterVolSliderGo.GetComponent<Slider>().value = currentMasterVol;
musicVolSliderGo.GetComponent<Slider>().value = currentMusicVol;
SFXVolSliderGo.GetComponent<Slider>().value = currentSFXVol;
}
public void PlayMenuClick()
{
if (currentMenuAudioManager)
{
currentMenuAudioManager.PlayMenuClick();
}
}
public void StartGame()
{
// Play the menu click sound if the audio manager is present
if (currentMenuAudioManager)
{
currentMenuAudioManager.PlayMenuClick();
}
SceneManager.LoadSceneAsync("Rules");
}
public void Quit()
{
// Play the menu click sound if the audio manager is present
if (currentMenuAudioManager)
{
currentMenuAudioManager.PlayMenuClick();
}
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
public void SetMasterVolume(float value)
{
// Converts the master volume into decibels and stores it in the player profile
GameManager.Instance.currentAudioMixer.SetFloat("masterVol", SharedMethods.ConvertToDecibels(value));
PlayerPrefs.SetFloat("currentMasterVol", value);
}
public void SetMusicVolume(float value)
{
// Converts the music volume into decibels and stores it in the player profile
GameManager.Instance.currentAudioMixer.SetFloat("musicVol", SharedMethods.ConvertToDecibels(value));
PlayerPrefs.SetFloat("currentMusicVol", value);
}
public void SetSFXVolume(float value)
{
// Converts the sfx volume into decibels and stores it in the player profile
GameManager.Instance.currentAudioMixer.SetFloat("sfxVol", SharedMethods.ConvertToDecibels(value));
PlayerPrefs.SetFloat("currentSFXVol", value);
}
// Update is called once per frame
void Update()
{
if (_flashTimer.HasTicked(Time.deltaTime))
{
float randomNo = Random.Range(0.0f, 1.0f);
_meshRenderer.materials[0].SetColor("_EmissionColor", Color.Lerp(Color.black, _defaultEmissionColor, randomNo));
}
float horzIn = Input.GetAxis("Horizontal");
float vertIn = Input.GetAxis("Vertical");
float horzLRot = -horzIn * joystickJiggleSpeed;
float vertLRot = -45.0f + -vertIn * joystickJiggleSpeed;
joystickGO.transform.eulerAngles = new Vector3(vertLRot, 0.0f, horzLRot);
if (horzIn != 0 || vertIn != 0)
{
if (EventSystem.current.currentSelectedGameObject == null)
{
EventSystem.current.SetSelectedGameObject(_lastControlSelected);
}
else
{
_lastControlSelected = EventSystem.current.currentSelectedGameObject;
}
}
float horzRRot = -25 + -Input.GetAxis("Mouse X") * joystickJiggleSpeed;
float vertRRot = -25 + -Input.GetAxis("Mouse Y") * joystickJiggleSpeed;
yolkAGO.transform.eulerAngles = new Vector3(vertRRot, 0.0f, 0.0f);
yolkBGO.transform.eulerAngles = new Vector3(horzRRot, 0.0f, 0.0f);
}
}