HeavenStudioPlus/Assets/Scripts/GameManager.cs

172 lines
5.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;
public CircleCursor CircleCursor;
2021-12-24 03:36:16 +00:00
[Header("Games")]
Coroutine currentGameSwitchIE;
public string currentGame;
2021-12-19 04:10:43 +00:00
public float startBeat;
2021-12-21 01:10:49 +00:00
private void Awake()
{
instance = this;
}
2021-12-19 04:10:43 +00:00
// before Start() since this is very important
private void OnEnable()
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
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
// SetCurrentGame(eventCaller.GamesHolder.transform.GetComponentsInChildren<Transform>()[1].name);
if (Beatmap.entities.Count >= 1)
{
SetCurrentGame(Beatmap.entities[0].datamodel.Split('/')[0]);
}
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;
SetCurrentEventToClosest();
GetGame(currentGame).holder.GetComponent<Minigame>().OnTimeChange();
}
else if (Input.GetKeyDown(KeyCode.S))
{
Conductor.instance.musicSource.time -= 3;
SetCurrentEventToClosest();
GetGame(currentGame).holder.GetComponent<Minigame>().OnTimeChange();
}
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
{
// allows for multiple events on the same beat to be executed on the same frame, so no more 1-frame delay
List<Beatmap.Entity> entitesAtSameBeat = Beatmap.entities.FindAll(c => c.beat == Beatmap.entities[currentEvent].beat);
2021-12-23 00:08:35 +00:00
for (int i = 0; i < entitesAtSameBeat.Count; i++)
{
eventCaller.CallEvent(entitesAtSameBeat[i].datamodel);
currentEvent++;
}
// eventCaller.CallEvent(Beatmap.entities[currentEvent].datamodel);
// 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;
GetGame(currentGame).holder.GetComponent<Minigame>().OnGameSwitch();
GetGame(currentGame).holder.SetActive(false);
GetGame(game).holder.SetActive(true);
GameCamera.orthographic = true;
2021-12-24 03:36:16 +00:00
GetGame(game).holder.GetComponent<Minigame>().OnGameSwitch();
2021-12-24 03:36:16 +00:00
SetCurrentGame(game);
yield return new WaitForSeconds(0.1666f);
2021-12-24 03:36:16 +00:00
this.GetComponent<SpriteRenderer>().enabled = false;
}
2021-12-19 04:10:43 +00:00
public EventCaller.MiniGame GetGame(string name)
2021-12-21 01:10:49 +00:00
{
return eventCaller.minigames.Find(c => c.name == name);
}
// never gonna use this
public EventCaller.MiniGame GetCurrentGame()
{
return eventCaller.minigames.Find(c => c.name == transform.GetComponentsInChildren<Transform>()[1].name);
}
public void SetCurrentGame(string game)
{
currentGame = game;
CircleCursor.InnerCircle.GetComponent<SpriteRenderer>().color = Colors.Hex2RGB(GetGame(currentGame).color);
2021-12-21 01:10:49 +00:00
}
2021-12-19 04:10:43 +00:00
}
2021-12-21 01:10:49 +00:00
}