HeavenStudioPlus/Assets/Scripts/GameManager.cs

386 lines
13 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
{
2022-01-21 01:24:30 +00:00
[Header("Lists")]
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>();
2022-01-21 01:24:30 +00:00
private List<GameObject> preloadedGames = new List<GameObject>();
public List<GameObject> SoundObjects = new List<GameObject>();
2021-12-19 04:10:43 +00:00
2022-01-21 01:24:30 +00:00
[Header("Components")]
2021-12-21 01:10:49 +00:00
public TextAsset txt;
public Camera GameCamera, CursorCam;
public CircleCursor CircleCursor;
2022-01-21 01:24:30 +00:00
[HideInInspector] public GameObject GamesHolder;
2021-12-24 03:36:16 +00:00
[Header("Games")]
public string currentGame;
2022-01-21 01:24:30 +00:00
Coroutine currentGameSwitchIE;
2021-12-19 04:10:43 +00:00
2022-01-21 01:24:30 +00:00
[Header("Properties")]
public int currentEvent, currentTempoEvent;
2022-01-21 01:24:30 +00:00
public float startOffset;
public bool playOnStart;
public float startBeat;
2021-12-31 14:46:11 +00:00
private GameObject currentGameO;
2022-01-23 03:40:53 +00:00
public bool autoplay;
2022-01-30 23:40:12 +00:00
public int BeatmapEntities()
{
return Beatmap.entities.Count + Beatmap.tempoChanges.Count;
}
2021-12-31 14:46:11 +00:00
2022-01-21 01:24:30 +00:00
public static GameManager instance { get; private set; }
private EventCaller eventCaller;
2022-01-06 00:11:33 +00:00
2021-12-21 01:10:49 +00:00
private void Awake()
{
autoplay = true;
2021-12-21 01:10:49 +00:00
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
{
this.transform.localScale = new Vector3(30000000, 30000000);
2022-01-03 22:42:43 +00:00
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
if (txt != null)
{
string json = txt.text;
Beatmap = JsonConvert.DeserializeObject<Beatmap>(json);
}
else
{
Beatmap = new Beatmap();
Beatmap.bpm = 120f;
}
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-15 22:52:53 +00:00
if (playOnStart)
{
Play(startBeat);
}
2021-12-24 03:36:16 +00:00
// SetCurrentGame(eventCaller.GamesHolder.transform.GetComponentsInChildren<Transform>()[1].name);
if (Beatmap.entities.Count >= 1)
{
2022-01-29 23:30:29 +00:00
SetCurrentGame(Beatmap.entities[0].datamodel.Split(0));
SetGame(Beatmap.entities[0].datamodel.Split(0));
}
else
{
SetGame("noGame");
}
2021-12-21 01:10:49 +00:00
}
2021-12-19 04:10:43 +00:00
2022-01-30 12:03:37 +00:00
public void LoadRemix(string json)
{
SortEventsList();
Beatmap = JsonConvert.DeserializeObject<Beatmap>(json);
Conductor.instance.SetBpm(Beatmap.bpm);
Stop(0);
SetCurrentEventToClosest(0);
2022-01-30 12:03:37 +00:00
if (Beatmap.entities.Count >= 1)
{
SetCurrentGame(Beatmap.entities[0].datamodel.Split(0));
SetGame(Beatmap.entities[0].datamodel.Split(0));
}
else
{
SetGame("noGame");
}
}
// LateUpdate works a bit better but causes a bit of bugs, so remind me to fix those eventually
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;
if (!Conductor.instance.isPlaying)
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();
List<float> tempoChanges = Beatmap.tempoChanges.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-16 19:23:46 +00:00
if (Conductor.instance.songPositionInBeats >= entities[currentEvent] /*&& SongPosLessThanClipLength(Conductor.instance.songPositionInBeats)*/)
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
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-15 17:45:08 +00:00
eventCaller.CallEvent(gameManagerEntities[i].datamodel);
2022-01-31 05:02:36 +00:00
currentEvent++;
2021-12-31 14:46:11 +00:00
}
2021-12-23 00:08:35 +00:00
for (int i = 0; i < entitesAtSameBeat.Count; i++)
{
var entity = entitesAtSameBeat[i];
2022-01-15 17:45:08 +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])))
2021-12-31 14:46:11 +00:00
{
2022-01-15 17:45:08 +00:00
PreloadGame(entitesAtSameBeat[i].datamodel.Split('/')[0]);
2021-12-31 14:46:11 +00:00
}
2022-01-15 17:45:08 +00:00
eventCaller.CallEvent(entitesAtSameBeat[i].datamodel);
// Thank you to @shshwdr for bring this to my attention
currentEvent++;
}
2022-01-31 05:02:36 +00:00
// currentEvent += gameManagerEntities.Count;
2021-12-19 04:10:43 +00:00
}
}
if (currentTempoEvent < Beatmap.tempoChanges.Count && currentTempoEvent >= 0)
{
if (Conductor.instance.songPositionInBeats >= tempoChanges[currentTempoEvent])
{
Conductor.instance.songBpm = Beatmap.tempoChanges[currentTempoEvent].tempo;
Conductor.instance.timeSinceLastTempoChange = Time.time;
currentTempoEvent++;
}
}
2021-12-19 04:10:43 +00:00
}
#region Play Events
public void Play(float beat)
{
StartCoroutine(PlayCo(beat));
}
private IEnumerator PlayCo(float beat)
{
yield return null;
bool paused = Conductor.instance.isPaused;
Conductor.instance.SetBpm(Beatmap.bpm);
Conductor.instance.Play(beat);
if (!paused)
{
SetCurrentEventToClosest(beat);
}
2022-01-21 01:24:30 +00:00
for (int i = 0; i < SoundObjects.Count; i++) Destroy(SoundObjects[i].gameObject);
}
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));
Beatmap.tempoChanges.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();
2021-12-31 14:46:11 +00:00
currentEvent = entities.IndexOf(Mathp.GetClosestInList(entities, beat));
2021-12-31 14:46:11 +00:00
2022-01-15 18:46:50 +00:00
var gameSwitchs = Beatmap.entities.FindAll(c => c.datamodel.Split(1) == "switchGame");
2022-01-15 17:45:08 +00:00
string newGame = Beatmap.entities[currentEvent].datamodel.Split(0);
2022-01-15 17:45:08 +00:00
if (gameSwitchs.Count > 0)
2021-12-31 14:46:11 +00:00
{
2022-01-15 18:46:50 +00:00
int index = gameSwitchs.FindIndex(c => c.beat == Mathp.GetClosestInList(gameSwitchs.Select(c => c.beat).ToList(), beat));
var closestGameSwitch = gameSwitchs[index];
if (closestGameSwitch.beat <= beat)
{
newGame = closestGameSwitch.datamodel.Split(2);
}
else if (closestGameSwitch.beat > beat)
{
if (index == 0)
2022-01-15 18:46:50 +00:00
{
newGame = Beatmap.entities[0].datamodel.Split(0);
2022-01-15 18:46:50 +00:00
}
else
{
if (index - 1 >= 0)
{
newGame = gameSwitchs[index - 1].datamodel.Split(2);
}
else
{
newGame = Beatmap.entities[Beatmap.entities.IndexOf(closestGameSwitch) - 1].datamodel.Split(0);
}
2022-01-15 18:46:50 +00:00
}
}
// newGame = gameSwitchs[gameSwitchs.IndexOf(gameSwitchs.Find(c => c.beat == Mathp.GetClosestInList(gameSwitchs.Select(c => c.beat).ToList(), beat)))].datamodel.Split(2);
2022-01-15 17:45:08 +00:00
}
2022-01-15 18:46:50 +00:00
SetGame(newGame);
2021-12-21 01:10:49 +00:00
}
else
{
SetGame("noGame");
}
if (Beatmap.tempoChanges.Count > 0)
{
List<float> tempoChanges = Beatmap.tempoChanges.Select(c => c.beat).ToList();
currentTempoEvent = tempoChanges.IndexOf(Mathp.GetClosestInList(tempoChanges, beat));
}
2021-12-19 04:10:43 +00:00
}
#endregion
2021-12-24 03:36:16 +00:00
public void SwitchGame(string game)
{
2022-01-15 17:45:08 +00:00
if (game != currentGame)
{
if (currentGameSwitchIE != null)
StopCoroutine(currentGameSwitchIE);
currentGameSwitchIE = StartCoroutine(SwitchGameIE(game));
}
2021-12-24 03:36:16 +00:00
}
IEnumerator SwitchGameIE(string game)
{
this.GetComponent<SpriteRenderer>().enabled = true;
2021-12-29 06:52:48 +00:00
SetGame(game);
2022-01-15 17:45:08 +00:00
yield return new WaitForSeconds(0.1f);
2021-12-29 06:52:48 +00:00
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++)
{
if (preloadedGames[i].gameObject != null)
2021-12-31 14:46:11 +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-31 14:46:11 +00:00
if (instantiate)
{
currentGameO = Instantiate(GetGame(game));
2021-12-31 14:46:11 +00:00
currentGameO.transform.parent = eventCaller.GamesHolder.transform;
currentGameO.name = game;
}
/*if (onGameSwitch)
2021-12-29 06:52:48 +00:00
{
if (GetGame(currentGame).GetComponent<Minigame>() != null)
GetGame(game).GetComponent<Minigame>().OnGameSwitch();
}*/
2021-12-24 03:36:16 +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));
2021-12-31 14:46:11 +00:00
g.transform.parent = eventCaller.GamesHolder.transform;
g.SetActive(false);
g.name = game;
preloadedGames.Add(g);
}
public GameObject GetGame(string name)
2021-12-21 01:10:49 +00:00
{
2022-01-29 23:30:29 +00:00
if (name == "gameManager")
{
name = Beatmap.entities.FindAll(c => c.datamodel.Split(0) != "gameManager").ToList()[0].datamodel.Split(0);
}
return Resources.Load<GameObject>($"Games/{name}");
}
public Minigames.Minigame GetGameInfo(string name)
{
return EventCaller.instance.minigames.Find(c => c.name == name);
}
// never gonna use this
2022-01-17 19:23:18 +00:00
public Minigames.Minigame GetCurrentGame()
{
return eventCaller.minigames.Find(c => c.name == transform.GetComponentsInChildren<Transform>()[1].name);
}
public void SetCurrentGame(string game)
{
currentGame = game;
if (GetGameInfo(currentGame) != null) CircleCursor.InnerCircle.GetComponent<SpriteRenderer>().color = Colors.Hex2RGB(GetGameInfo(currentGame).color);
else
CircleCursor.InnerCircle.GetComponent<SpriteRenderer>().color = Color.white;
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
}