HeavenStudioPlus/Assets/Scripts/GameManager.cs

92 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-23 00:08:35 +00:00
private EventCaller eventCaller;
2021-12-19 04:10:43 +00:00
2021-12-23 00:08:35 +00:00
public Beatmap Beatmap;
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
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;
2021-12-23 00:08:35 +00:00
Beatmap.entities = JsonConvert.DeserializeObject<List<Beatmap.Entity>>(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-23 00:08:35 +00:00
eventCaller = GetComponent<EventCaller>();
eventCaller.Init();
2021-12-21 01:10:49 +00:00
}
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()
{
2021-12-23 00:08:35 +00:00
if (Beatmap.entities.Count < 1)
2021-12-21 01:10:49 +00:00
return;
2021-12-19 04:10:43 +00:00
2021-12-23 00:08:35 +00:00
List<float> entities = Beatmap.entities.Select(c => c.beat).ToList();
2021-12-19 04:10:43 +00:00
2021-12-23 00:08:35 +00:00
if (currentEvent < Beatmap.entities.Count && currentEvent >= 0)
2021-12-19 04:10:43 +00:00
{
2021-12-23 00:08:35 +00:00
if (Conductor.instance.songPositionInBeats >= entities[currentEvent])
2021-12-19 04:10:43 +00:00
{
2021-12-23 00:08:35 +00:00
eventCaller.CallEvent(Beatmap.entities[currentEvent].datamodel, Beatmap.entities[currentEvent].beat);
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-23 00:08:35 +00:00
Beatmap.entities.Sort((x, y) => x.beat.CompareTo(y.beat));
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-23 00:08:35 +00:00
if (Beatmap.entities.Count > 0)
2021-12-21 01:10:49 +00:00
{
2021-12-23 00:08:35 +00:00
List<float> entities = Beatmap.entities.Select(c => c.beat).ToList();
currentEvent = entities.IndexOf(Mathp.GetClosestInList(entities, Conductor.instance.songPositionInBeats));
2021-12-21 01:10:49 +00:00
}
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
}