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
|
|
|
|
2022-01-08 16:42:48 +00:00
|
|
|
public Beatmap Beatmap = new Beatmap();
|
2022-01-03 22:42:43 +00:00
|
|
|
[HideInInspector] public List<Beatmap.Entity> playerEntities = new List<Beatmap.Entity>();
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-23 01:49:16 +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
|
|
|
|
2021-12-25 13:32:52 +00:00
|
|
|
public Camera GameCamera, CursorCam;
|
2021-12-26 01:04:23 +00:00
|
|
|
public CircleCursor CircleCursor;
|
2021-12-25 12:16:40 +00:00
|
|
|
|
2021-12-24 03:36:16 +00:00
|
|
|
[Header("Games")]
|
|
|
|
Coroutine currentGameSwitchIE;
|
2021-12-26 01:04:23 +00:00
|
|
|
public string currentGame;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-28 02:36:27 +00:00
|
|
|
public float startBeat;
|
2021-12-25 12:16:40 +00:00
|
|
|
|
2021-12-31 14:46:11 +00:00
|
|
|
private GameObject currentGameO;
|
|
|
|
|
|
|
|
private List<GameObject> preloadedGames = new List<GameObject>();
|
|
|
|
|
2022-01-03 22:42:43 +00:00
|
|
|
[HideInInspector] public GameObject GamesHolder;
|
|
|
|
|
2022-01-06 00:11:33 +00:00
|
|
|
public bool playOnStart;
|
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
instance = this;
|
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2022-01-08 16:42:48 +00:00
|
|
|
public void Init()
|
2021-12-21 01:10:49 +00:00
|
|
|
{
|
2022-01-03 22:42:43 +00:00
|
|
|
this.transform.localScale = new Vector3(3000, 3000);
|
|
|
|
SpriteRenderer sp = this.gameObject.AddComponent<SpriteRenderer>();
|
|
|
|
sp.enabled = false;
|
|
|
|
sp.color = Color.black;
|
|
|
|
sp.sprite = Resources.Load<Sprite>("Sprites/GeneralPurpose/Square");
|
|
|
|
sp.sortingOrder = 30000;
|
|
|
|
this.gameObject.layer = 3;
|
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
|
|
|
|
2022-01-03 22:42:43 +00:00
|
|
|
eventCaller = this.gameObject.AddComponent<EventCaller>();
|
|
|
|
eventCaller.GamesHolder = GamesHolder.transform;
|
2021-12-23 00:08:35 +00:00
|
|
|
eventCaller.Init();
|
2021-12-23 02:28:05 +00:00
|
|
|
Conductor.instance.SetBpm(Beatmap.bpm);
|
|
|
|
|
2022-01-06 00:11:33 +00:00
|
|
|
if (playOnStart) StartCoroutine(Begin());
|
2021-12-24 03:36:16 +00:00
|
|
|
|
2021-12-28 02:36:27 +00:00
|
|
|
// SetCurrentGame(eventCaller.GamesHolder.transform.GetComponentsInChildren<Transform>()[1].name);
|
|
|
|
|
|
|
|
if (Beatmap.entities.Count >= 1)
|
|
|
|
{
|
|
|
|
SetCurrentGame(Beatmap.entities[0].datamodel.Split('/')[0]);
|
2021-12-29 06:52:48 +00:00
|
|
|
SetGame(Beatmap.entities[0].datamodel.Split('/')[0]);
|
2021-12-28 02:36:27 +00:00
|
|
|
}
|
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);
|
2022-01-07 11:36:23 +00:00
|
|
|
// Conductor.instance.Play(startBeat);
|
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 Update()
|
|
|
|
{
|
2021-12-23 00:08:35 +00:00
|
|
|
if (Beatmap.entities.Count < 1)
|
2021-12-21 01:10:49 +00:00
|
|
|
return;
|
2022-01-07 11:36:23 +00:00
|
|
|
if (!Conductor.instance.isPlaying)
|
2021-12-30 12:17:22 +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
|
|
|
{
|
2022-01-09 23:35:55 +00:00
|
|
|
if (Conductor.instance.songPositionInBeats >= entities[currentEvent] && SongPosLessThanClipLength(Conductor.instance.songPositionInBeats))
|
2021-12-19 04:10:43 +00:00
|
|
|
{
|
2021-12-26 05:11:54 +00:00
|
|
|
// allows for multiple events on the same beat to be executed on the same frame, so no more 1-frame delay
|
2021-12-31 14:46:11 +00:00
|
|
|
var entitesAtSameBeat = Beatmap.entities.FindAll(c => c.beat == Beatmap.entities[currentEvent].beat && c.datamodel.Split('/')[0] != "gameManager");
|
|
|
|
var gameManagerEntities = Beatmap.entities.FindAll(c => c.beat == Beatmap.entities[currentEvent].beat && c.datamodel.Split('/')[0] == "gameManager");
|
|
|
|
|
|
|
|
// GameManager entities should ALWAYS execute before gameplay entities
|
|
|
|
for (int i = 0; i < gameManagerEntities.Count; i++)
|
|
|
|
{
|
2022-01-14 00:35:41 +00:00
|
|
|
var gameManagerEntity = gameManagerEntities[i];
|
|
|
|
if ((gameManagerEntity.beat + eventCaller.GetGameAction(eventCaller.GetMinigame(gameManagerEntity.datamodel.Split(0)), gameManagerEntity.datamodel.Split(1)).defaultLength) > Conductor.instance.songPositionInBeats)
|
|
|
|
{
|
|
|
|
eventCaller.CallEvent(gameManagerEntities[i].datamodel);
|
|
|
|
}
|
2021-12-31 14:46:11 +00:00
|
|
|
}
|
2021-12-23 00:08:35 +00:00
|
|
|
|
2021-12-26 05:11:54 +00:00
|
|
|
for (int i = 0; i < entitesAtSameBeat.Count; i++)
|
|
|
|
{
|
2022-01-14 00:35:41 +00:00
|
|
|
var entity = entitesAtSameBeat[i];
|
|
|
|
if ((entity.beat + eventCaller.GetGameAction(eventCaller.GetMinigame(entity.datamodel.Split(0)), entity.datamodel.Split(1)).defaultLength) > Conductor.instance.songPositionInBeats)
|
2021-12-31 14:46:11 +00:00
|
|
|
{
|
2022-01-14 00:35:41 +00:00
|
|
|
// if game isn't loaded, preload game so whatever event that would be called will still run outside if needed
|
|
|
|
if (entitesAtSameBeat[i].datamodel.Split('/')[0] != currentGame && !preloadedGames.Contains(preloadedGames.Find(c => c.name == entitesAtSameBeat[i].datamodel.Split('/')[0])))
|
|
|
|
{
|
|
|
|
PreloadGame(entitesAtSameBeat[i].datamodel.Split('/')[0]);
|
|
|
|
}
|
|
|
|
eventCaller.CallEvent(entitesAtSameBeat[i].datamodel);
|
2021-12-31 14:46:11 +00:00
|
|
|
}
|
2021-12-26 05:11:54 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 14:46:11 +00:00
|
|
|
currentEvent += entitesAtSameBeat.Count + gameManagerEntities.Count;
|
2021-12-19 04:10:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 00:35:41 +00:00
|
|
|
#region Play Events
|
|
|
|
|
|
|
|
public void Play(float beat)
|
|
|
|
{
|
|
|
|
StartCoroutine(PlayCo(beat));
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerator PlayCo(float beat)
|
|
|
|
{
|
|
|
|
yield return null;
|
|
|
|
Conductor.instance.Play(beat);
|
|
|
|
SetCurrentEventToClosest(beat);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Pause()
|
|
|
|
{
|
|
|
|
Conductor.instance.Pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Stop(float beat)
|
|
|
|
{
|
|
|
|
Conductor.instance.Stop(beat);
|
|
|
|
SetCurrentEventToClosest(beat);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region List Functions
|
|
|
|
|
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
|
|
|
|
2021-12-31 14:46:11 +00:00
|
|
|
public void SetCurrentEventToClosest(float beat)
|
2021-12-19 04:10:43 +00:00
|
|
|
{
|
2022-01-09 23:35:55 +00:00
|
|
|
SortEventsList();
|
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();
|
2022-01-03 22:42:43 +00:00
|
|
|
if (playerEntities != null)
|
|
|
|
{
|
|
|
|
List<float> entities_p = playerEntities.Select(c => c.beat).ToList();
|
|
|
|
currentPlayerEvent = entities_p.IndexOf(Mathp.GetClosestInList(entities_p, beat));
|
|
|
|
}
|
2021-12-31 14:46:11 +00:00
|
|
|
|
2022-01-07 23:51:08 +00:00
|
|
|
currentEvent = entities.IndexOf(Mathp.GetClosestInList(entities, beat));
|
2021-12-31 14:46:11 +00:00
|
|
|
|
|
|
|
string newGame = Beatmap.entities[currentEvent].datamodel.Split('/')[0];
|
2022-01-07 23:51:08 +00:00
|
|
|
|
|
|
|
if (Beatmap.entities[currentEvent].datamodel.Split('/')[1] != "switchGame")
|
2021-12-31 14:46:11 +00:00
|
|
|
{
|
2022-01-07 23:51:08 +00:00
|
|
|
if (newGame == "gameManager")
|
|
|
|
{
|
|
|
|
// holy shit
|
|
|
|
newGame = Beatmap.entities[entities.IndexOf(Mathp.GetClosestInList(Beatmap.entities.FindAll(c => c.datamodel != "gameManager" && c.beat < Conductor.instance.songPositionInBeats).ToList().Select(c => c.beat).ToList(), beat))].datamodel.Split('/')[0];
|
|
|
|
}
|
|
|
|
|
2022-01-14 00:35:41 +00:00
|
|
|
SetGame(newGame);
|
2021-12-31 14:46:11 +00:00
|
|
|
}
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 00:35:41 +00:00
|
|
|
#endregion
|
|
|
|
|
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;
|
|
|
|
|
2021-12-29 06:52:48 +00:00
|
|
|
SetGame(game);
|
|
|
|
|
|
|
|
yield return new WaitForSeconds(0.1666f);
|
|
|
|
|
|
|
|
this.GetComponent<SpriteRenderer>().enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetGame(string game, bool onGameSwitch = true)
|
|
|
|
{
|
2021-12-31 14:46:11 +00:00
|
|
|
Destroy(currentGameO);
|
|
|
|
|
|
|
|
var instantiate = true;
|
|
|
|
|
|
|
|
if (preloadedGames.Count > 0)
|
2021-12-29 06:52:48 +00:00
|
|
|
{
|
2021-12-31 14:46:11 +00:00
|
|
|
for (int i = 0; i < preloadedGames.Count; i++)
|
|
|
|
{
|
2022-01-07 23:51:08 +00:00
|
|
|
if (preloadedGames[i].gameObject != null)
|
2021-12-31 14:46:11 +00:00
|
|
|
{
|
2022-01-07 23:51:08 +00:00
|
|
|
if (preloadedGames[i].gameObject.name == game)
|
|
|
|
{
|
|
|
|
preloadedGames[i].SetActive(true);
|
|
|
|
currentGameO = preloadedGames[i];
|
|
|
|
preloadedGames.Remove(preloadedGames[i]);
|
|
|
|
instantiate = false;
|
|
|
|
}
|
2021-12-31 14:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-29 06:52:48 +00:00
|
|
|
}
|
2021-12-26 01:04:23 +00:00
|
|
|
|
2021-12-31 14:46:11 +00:00
|
|
|
if (instantiate)
|
|
|
|
{
|
|
|
|
currentGameO = Instantiate(GetGame(game).holder);
|
|
|
|
currentGameO.transform.parent = eventCaller.GamesHolder.transform;
|
|
|
|
currentGameO.name = game;
|
|
|
|
}
|
2021-12-26 01:04:23 +00:00
|
|
|
|
2021-12-25 12:16:40 +00:00
|
|
|
GameCamera.orthographic = true;
|
2021-12-24 03:36:16 +00:00
|
|
|
|
2021-12-29 06:52:48 +00:00
|
|
|
if (onGameSwitch)
|
|
|
|
{
|
|
|
|
if (GetGame(currentGame).holder.GetComponent<Minigame>() != null)
|
|
|
|
GetGame(game).holder.GetComponent<Minigame>().OnGameSwitch();
|
|
|
|
}
|
2021-12-24 03:36:16 +00:00
|
|
|
|
2021-12-26 01:04:23 +00:00
|
|
|
SetCurrentGame(game);
|
2021-12-24 03:36:16 +00:00
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-31 14:46:11 +00:00
|
|
|
private void PreloadGame(string game)
|
|
|
|
{
|
|
|
|
if (preloadedGames.Contains(preloadedGames.Find(c => c.name == game)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
var g = Instantiate(GetGame(game).holder);
|
|
|
|
g.transform.parent = eventCaller.GamesHolder.transform;
|
|
|
|
g.SetActive(false);
|
|
|
|
g.name = game;
|
|
|
|
preloadedGames.Add(g);
|
|
|
|
}
|
|
|
|
|
2021-12-26 01:04:23 +00:00
|
|
|
public EventCaller.MiniGame GetGame(string name)
|
2021-12-21 01:10:49 +00:00
|
|
|
{
|
2021-12-26 01:04:23 +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
|
|
|
}
|
2022-01-09 23:35:55 +00:00
|
|
|
|
|
|
|
private bool SongPosLessThanClipLength(float t)
|
|
|
|
{
|
|
|
|
if (Conductor.instance.musicSource.clip != null)
|
|
|
|
return Conductor.instance.GetSongPosFromBeat(t) < Conductor.instance.musicSource.clip.length;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
}
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|