MVH/Scripts/Game/Game.cs

113 lines
2.1 KiB
C#
Raw Permalink 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;
using Unity.VisualScripting;
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;
List<Note> allNotes = new();
List<Note> activeNotes = new();
2024-06-07 00:47:07 +02:00
2024-06-17 19:54:50 +02:00
public int Score { get; set; } = 0;
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;
List<Note> newNotes = allNotes.Where(note => note.t >= timestamp && timestamp + 1f <= note.t).ToList();
2024-06-16 22:07:02 +02:00
foreach (Note note in newNotes)
{
2024-06-17 19:54:50 +02:00
allNotes.Remove(note);
2024-06-16 22:07:02 +02:00
}
2024-06-13 14:46:43 +02:00
menu.GameElem.UpdateNotes(newNotes);
2024-06-16 22:07:02 +02:00
menu.GameElem.CheckNoteLifetime(timestamp);
menu.GameElem.UpdateMargin(timestamp);
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;
allNotes = this.song.Notes.ToList();
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;
2024-06-16 22:07:02 +02:00
int m = totalSeconds % 3600 / 60;
2024-06-07 00:47:07 +02:00
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());
}
}