MVH/Scripts/Game/Game.cs

104 lines
1.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UIElements;
using System.Linq;
using UITypes;
using BangerTypes;
public class Game : MonoBehaviour
{
const float DELAY = 0.005f; // 200 TPS
public AudioManager manager;
private MainMenu menu;
private BassEffect effect;
string totalDuration;
bool repeatRoutine = true;
Song song = null;
Note[] activeNotes;
void Awake()
{
menu = GetComponent<MainMenu>();
effect = new(manager.source);
}
public void GameUpdate()
{
float[] specData = effect.UpdateSpectrumData();
float bassStrength = effect.GetBassStrength();
menu.GameElem.UpdateVisualizers(specData, bassStrength);
UpdateTimestamp();
GetNotes();
}
public void GetNotes()
{
float timestamp = manager.source.time;
activeNotes = song.Notes.Where(note => note.t >= timestamp && timestamp + 1f <= note.t).ToArray();
if(activeNotes.Length > 0)
{
Debug.Log("Found some notes");
}
menu.GameElem.UpdateNotes(activeNotes);
}
IEnumerator GameLoop()
{
while (repeatRoutine)
{
GameUpdate();
yield return new WaitForSeconds(DELAY);
}
}
public void LoadSong(SongEntry song)
{
this.song = song.Song;
totalDuration = song.Song.Duration;
manager.source.clip = song.Song.Clip;
menu.GameElem.SetColumnColor(song.Song.AverageColor);
}
void UpdateTimestamp()
{
int totalSeconds = (int)manager.source.time;
int h = totalSeconds / 3600;
int m = (totalSeconds % 3600) / 60;
int s = totalSeconds % 60;
string result = $"{h:D2}:{m:D2}:{s:D2} / {totalDuration}";
menu.GameElem.UpdateSongTime(result);
}
public void EscapeClicked()
{
repeatRoutine = false;
manager.Stop();
menu.Escape();
}
public void Play()
{
manager.Play();
repeatRoutine = true;
StartCoroutine(GameLoop());
}
}