MVH/Scripts/Game/Game.cs

104 lines
1.8 KiB
C#
Raw Normal View History

2024-06-07 00:47:07 +02:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UIElements;
2024-06-13 14:46:43 +02:00
using System.Linq;
2024-06-07 00:47:07 +02:00
using UITypes;
2024-06-13 14:46:43 +02:00
using BangerTypes;
2024-06-07 00:47:07 +02:00
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;
2024-06-13 14:46:43 +02:00
Song song = null;
Note[] activeNotes;
2024-06-07 00:47:07 +02:00
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();
2024-06-13 14:46:43 +02:00
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);
2024-06-07 00:47:07 +02:00
}
IEnumerator GameLoop()
{
while (repeatRoutine)
{
GameUpdate();
yield return new WaitForSeconds(DELAY);
}
}
public void LoadSong(SongEntry song)
{
2024-06-13 14:46:43 +02:00
this.song = song.Song;
2024-06-07 00:47:07 +02:00
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());
}
}