HeavenStudioPlus/Assets/Scripts/GameManager.cs

136 lines
4 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-24 03:36:16 +00:00
using RhythmHeavenMania.Games;
2021-12-19 04:10:43 +00:00
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;
[HideInInspector] public List<Beatmap.Entity> playerEntities;
2021-12-19 04:10:43 +00:00
public int currentEvent, currentPlayerEvent;
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
public Camera GameCamera, CursorCam;
2021-12-24 03:36:16 +00:00
[Header("Games")]
Coroutine currentGameSwitchIE;
private string currentGame;
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 02:28:05 +00:00
Beatmap = JsonConvert.DeserializeObject<Beatmap>(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
GlobalGameManager.Init();
2021-12-23 00:08:35 +00:00
eventCaller = GetComponent<EventCaller>();
eventCaller.Init();
2021-12-23 02:28:05 +00:00
Conductor.instance.SetBpm(Beatmap.bpm);
StartCoroutine(Begin());
2021-12-24 03:36:16 +00:00
currentGame = eventCaller.GamesHolder.GetComponentsInChildren<Transform>()[1].name;
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
if (Input.GetKeyDown(KeyCode.A))
{
Conductor.instance.musicSource.time += 3;
}
else if (Input.GetKeyDown(KeyCode.S))
{
Conductor.instance.musicSource.time -= 3;
GameManager.instance.SetCurrentEventToClosest();
}
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
{
eventCaller.CallEvent(Beatmap.entities[currentEvent].datamodel);
2021-12-23 00:08:35 +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-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();
List<float> entities_p = playerEntities.Select(c => c.beat).ToList();
2021-12-23 00:08:35 +00:00
currentEvent = entities.IndexOf(Mathp.GetClosestInList(entities, Conductor.instance.songPositionInBeats));
currentPlayerEvent = entities_p.IndexOf(Mathp.GetClosestInList(entities_p, Conductor.instance.songPositionInBeats));
2021-12-21 01:10:49 +00:00
}
2021-12-19 04:10:43 +00:00
}
2021-12-24 03:36:16 +00:00
public void SwitchGame(string game)
{
if (currentGameSwitchIE != null) StopCoroutine(currentGameSwitchIE);
currentGameSwitchIE = StartCoroutine(SwitchGameIE(game));
}
IEnumerator SwitchGameIE(string game)
{
this.GetComponent<SpriteRenderer>().enabled = true;
eventCaller.minigames.Find(c => c.name == currentGame).holder.SetActive(false);
eventCaller.minigames.Find(c => c.name == game).holder.SetActive(true);
GameCamera.orthographic = true;
2021-12-24 03:36:16 +00:00
eventCaller.minigames.Find(c => c.name == game).holder.GetComponent<Minigame>().OnGameSwitch();
currentGame = game;
yield return new WaitForSeconds(0.1666f);
this.GetComponent<SpriteRenderer>().enabled = false;
}
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
}