HeavenStudioPlus/Assets/Scripts/GameManager.cs

97 lines
2.3 KiB
C#
Raw Normal View History

2021-12-19 04:10:43 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Starpelly;
using Newtonsoft.Json;
2021-12-21 01:10:49 +00:00
namespace RhythmHeavenMania
{
public class GameManager : MonoBehaviour
{
public static GameManager instance;
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
public List<Event> Events = new List<Event>();
2021-12-19 04:10:43 +00:00
public int currentEvent;
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
public TextAsset txt;
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
public float startOffset;
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
[Serializable]
public class Event : ICloneable
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
public float spawnTime;
public string eventName;
public object Clone()
{
return this.MemberwiseClone();
}
2021-12-19 04:10:43 +00:00
}
2021-12-21 01:10:49 +00:00
private void Awake()
{
instance = this;
}
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
private void Start()
{
SortEventsList();
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
string json = txt.text;
Events = JsonConvert.DeserializeObject<List<Event>>(json);
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
SortEventsList();
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
StartCoroutine(Begin());
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
GlobalGameManager.Init();
}
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
private IEnumerator Begin()
{
yield return new WaitForSeconds(startOffset);
Conductor.instance.musicSource.Play();
}
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
private void Update()
{
if (Events.Count < 1)
return;
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
List<float> floats = Events.Select(c => c.spawnTime).ToList();
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
if (currentEvent < Events.Count && currentEvent >= 0)
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
if (Conductor.instance.songPositionInBeats >= floats[currentEvent])
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
currentEvent++;
2021-12-19 04:10:43 +00:00
}
}
}
2021-12-21 01:10:49 +00:00
public void SortEventsList()
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
Events.Sort((x, y) => x.spawnTime.CompareTo(y.spawnTime));
2021-12-19 04:10:43 +00:00
}
2021-12-21 01:10:49 +00:00
public void SetCurrentEventToClosest()
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
if (Events.Count > 0)
{
List<float> floats = Events.Select(c => c.spawnTime).ToList();
currentEvent = floats.IndexOf(Mathp.GetClosestInList(floats, Conductor.instance.songPositionInBeats));
}
2021-12-19 04:10:43 +00:00
}
2021-12-21 01:10:49 +00:00
private void OnGUI()
{
// GUI.Box(new Rect(0, 0, 300, 50), $"SongPosInBeats: {Conductor.instance.songPositionInBeats}");
}
2021-12-19 04:10:43 +00:00
}
2021-12-21 01:10:49 +00:00
}