HeavenStudioPlus/Assets/Scripts/GameManager.cs

188 lines
6.2 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
using RhythmHeavenMania.Games.ForkLifter;
using RhythmHeavenMania.Util;
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-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
public List<Event> Events = new List<Event>();
public List<float> AutoPlay = new List<float>();
public List<Event> allPlayerActions = new List<Event>();
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
public int currentEvent, currentEventAutoplay, currentEventPlayer;
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 bool autoplay = false;
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
[Serializable]
public class Event : ICloneable
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
public float spawnTime;
public string eventName;
public object Clone()
{
return this.MemberwiseClone();
}
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;
Events = JsonConvert.DeserializeObject<List<Event>>(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
allPlayerActions = Events.FindAll(c => c.eventName != "gulp" && c.eventName != "sigh" && c.eventName != "prepare" && c.eventName != "end");
AutoPlay = allPlayerActions.Select(c => c.spawnTime + 2).ToList();
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
/*List<Event> temp = new List<Event>();
for (int i = 0; i < allPlayerActions.Count; i++)
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
if (i - 1 > 0)
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
if (Mathp.IsWithin(allPlayerActions[i - 1].spawnTime, allPlayerActions[i].spawnTime - 1f, allPlayerActions[i].spawnTime))
{
// do nothing lul
continue;
}
2021-12-19 04:10:43 +00:00
}
2021-12-21 01:10:49 +00:00
Event e = (Event)allPlayerActions[i].Clone();
e.spawnTime = allPlayerActions[i].spawnTime - 1;
e.eventName = "prepare";
temp.Add(e);
2021-12-19 04:10:43 +00:00
}
2021-12-21 01:10:49 +00:00
string s = JsonConvert.SerializeObject(temp);
print(s);*/
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-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();
GoForAPerfect.instance.Enable();
}
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
private void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
ForkLifter.instance.Flick(Conductor.instance.songPositionInBeats, 0);
if (Input.GetKeyDown(KeyCode.W))
ForkLifter.instance.Flick(Conductor.instance.songPositionInBeats, 1);
if (Input.GetKeyDown(KeyCode.E))
ForkLifter.instance.Flick(Conductor.instance.songPositionInBeats, 2);
if (Input.GetKeyDown(KeyCode.R))
ForkLifter.instance.Flick(Conductor.instance.songPositionInBeats, 3);
if (Events.Count < 1)
return;
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
List<float> floats = Events.Select(c => c.spawnTime).ToList();
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
if (currentEvent < Events.Count && currentEvent >= 0)
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
if (Conductor.instance.songPositionInBeats >= floats[currentEvent])
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
switch (Events[currentEvent].eventName)
{
case "pea":
currentEventPlayer++;
ForkLifter.instance.Flick(Events[currentEvent].spawnTime, 0);
break;
case "topbun":
currentEventPlayer++;
ForkLifter.instance.Flick(Events[currentEvent].spawnTime, 1);
break;
case "burger":
currentEventPlayer++;
ForkLifter.instance.Flick(Events[currentEvent].spawnTime, 2);
break;
case "bottombun":
currentEventPlayer++;
ForkLifter.instance.Flick(Events[currentEvent].spawnTime, 3);
break;
case "gulp":
ForkLifterPlayer.instance.Eat();
break;
case "sigh":
Jukebox.PlayOneShot("sigh");
break;
case "prepare":
ForkLifterHand.instance.Prepare();
break;
case "end":
GlobalGameManager.LoadScene(2, 0.45f);
break;
}
currentEvent++;
2021-12-19 04:10:43 +00:00
}
}
2021-12-21 01:10:49 +00:00
if (autoplay)
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
if (currentEventAutoplay < AutoPlay.Count && currentEventAutoplay >= 0)
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
if (Conductor.instance.songPositionInBeats >= AutoPlay[currentEventAutoplay])
{
ForkLifterPlayer.instance.Stab();
currentEventAutoplay++;
}
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-21 01:10:49 +00:00
Events.Sort((x, y) => x.spawnTime.CompareTo(y.spawnTime));
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-21 01:10:49 +00:00
if (Events.Count > 0)
{
List<float> floats = Events.Select(c => c.spawnTime).ToList();
currentEvent = floats.IndexOf(Mathp.GetClosestInList(floats, Conductor.instance.songPositionInBeats));
}
if (AutoPlay.Count > 0)
{
currentEvent = AutoPlay.IndexOf(Mathp.GetClosestInList(AutoPlay, Conductor.instance.songPositionInBeats));
currentEventPlayer = AutoPlay.IndexOf(Mathp.GetClosestInList(AutoPlay, Conductor.instance.songPositionInBeats));
}
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
}