event caller refactoring

change operation order of minigame loading
This commit is contained in:
minenice55 2024-01-25 12:29:05 -05:00
parent 2d24e5000d
commit 6bb135a091
8 changed files with 94 additions and 106 deletions

View file

@ -8,6 +8,7 @@ namespace HeavenStudio
{ {
public class EventCaller : MonoBehaviour public class EventCaller : MonoBehaviour
{ {
public GameManager gameManager { get; private set; }
public Transform GamesHolder; public Transform GamesHolder;
public RiqEntity currentEntity = new RiqEntity(); public RiqEntity currentEntity = new RiqEntity();
public string currentSwitchGame; public string currentSwitchGame;
@ -18,6 +19,7 @@ namespace HeavenStudio
public Dictionary<string, Minigames.Minigame> minigames = new(); public Dictionary<string, Minigames.Minigame> minigames = new();
public Minigames.Minigame GetMinigame(string gameName) public Minigames.Minigame GetMinigame(string gameName)
{ {
if (!minigames.ContainsKey(gameName)) if (!minigames.ContainsKey(gameName))
@ -55,34 +57,14 @@ namespace HeavenStudio
return GetGameAction(gameName, action).parameters.Find(c => c.propertyName == param); return GetGameAction(gameName, action).parameters.Find(c => c.propertyName == param);
} }
public void Init() public void Init(GameManager mgr)
{ {
gameManager = mgr;
instance = this; instance = this;
currentEntity = new RiqEntity(); currentEntity = new RiqEntity();
Minigames.Init(this); Minigames.Init(this);
List<Minigames.Minigame> minigamesInBeatmap = new List<Minigames.Minigame>();
for (int i = 0; i < GameManager.instance.Beatmap.Entities.Count; i++)
{
//go through every entity in the timeline and add the game that they're from to the minigamesInBeatmap list (ignore entities from FX only categories, i.e. Game Manager and Count-Ins)
Minigames.Minigame game = GetMinigame(GameManager.instance.Beatmap.Entities[i].datamodel.Split('/')[0]);
if (!minigamesInBeatmap.Contains(game) && !FXOnlyGames().Contains(game))
{
minigamesInBeatmap.Add(game);
}
}
for (int i = 0; i < minigamesInBeatmap.Count; i++)
{
// minigames[minigames.FindIndex(c => c.name == minigamesInBeatmap[i].name)].holder = Resources.Load<GameObject>($"Games/{minigamesInBeatmap[i].name}");
}
}
private void Update()
{
} }
public void CallEvent(RiqEntity entity, bool gameActive) public void CallEvent(RiqEntity entity, bool gameActive)
@ -128,30 +110,9 @@ namespace HeavenStudio
} }
} }
static bool StringStartsWith(string a, string b)
{
int aLen = a.Length;
int bLen = b.Length;
int ap = 0; int bp = 0;
while (ap < aLen && bp < bLen && a [ap] == b [bp])
{
ap++;
bp++;
}
return (bp == bLen);
}
public static bool IsGameSwitch(RiqEntity entity)
{
return StringStartsWith(entity.datamodel, "gameManager/switchGame");
}
public static List<RiqEntity> GetAllInGameManagerList(string gameName, string[] include) public static List<RiqEntity> GetAllInGameManagerList(string gameName, string[] include)
{ {
List<RiqEntity> temp1 = GameManager.instance.Beatmap.Entities.FindAll(c => c.datamodel.Split('/')[0] == gameName); List<RiqEntity> temp1 = instance.gameManager.Beatmap.Entities.FindAll(c => c.datamodel.Split('/')[0] == gameName);
List<RiqEntity> temp2 = new List<RiqEntity>(); List<RiqEntity> temp2 = new List<RiqEntity>();
foreach (string s in include) foreach (string s in include)
{ {
@ -162,7 +123,7 @@ namespace HeavenStudio
public static List<RiqEntity> GetAllInGameManagerListExclude(string gameName, string[] exclude) public static List<RiqEntity> GetAllInGameManagerListExclude(string gameName, string[] exclude)
{ {
List<RiqEntity> temp1 = GameManager.instance.Beatmap.Entities.FindAll(c => c.datamodel.Split('/')[0] == gameName); List<RiqEntity> temp1 = instance.gameManager.Beatmap.Entities.FindAll(c => c.datamodel.Split('/')[0] == gameName);
List<RiqEntity> temp2 = new List<RiqEntity>(); List<RiqEntity> temp2 = new List<RiqEntity>();
foreach (string s in exclude) foreach (string s in exclude)
{ {

View file

@ -68,7 +68,7 @@ namespace HeavenStudio
GameObject Games = new GameObject(); GameObject Games = new GameObject();
Games.name = "Games"; Games.name = "Games";
gameManager.playMode = playOnStart; gameManager.TogglePlayMode(playOnStart);
gameManager.GamesHolder = Games; gameManager.GamesHolder = Games;
gameManager.CircleCursor = Cursor.transform.GetChild(0).GetComponent<CircleCursor>(); gameManager.CircleCursor = Cursor.transform.GetChild(0).GetComponent<CircleCursor>();

View file

@ -32,30 +32,31 @@ namespace HeavenStudio
[NonSerialized] public Games.Global.Filter filter; [NonSerialized] public Games.Global.Filter filter;
[Header("Games")] [Header("Games")]
[NonSerialized] public string currentGame;
Coroutine currentGameSwitchIE; Coroutine currentGameSwitchIE;
[Header("Properties")]
[NonSerialized] public string txt = null;
[NonSerialized] public string ext = null;
[NonSerialized] [NonSerialized]
public int currentEvent, currentTempoEvent, currentVolumeEvent, currentSectionEvent, public int currentEvent, currentTempoEvent, currentVolumeEvent, currentSectionEvent,
currentPreEvent, currentPreSwitch, currentPreSequence; currentPreEvent, currentPreSwitch, currentPreSequence;
[NonSerialized] public double endBeat;
[NonSerialized] public float startOffset; public string currentGame { get; private set; }
[NonSerialized] public bool playMode; public GameObject minigameObj { get; private set; }
[NonSerialized] public double startBeat; public Minigame minigame { get; private set; }
[NonSerialized] public GameObject currentGameO; public RiqEntity lastSection { get; private set; }
private Minigame _currentMinigame; public RiqEntity currentSection { get; private set; }
[NonSerialized] public bool autoplay;
[NonSerialized] public bool canInput = true; public double endBeat { get; private set; }
[NonSerialized] public RiqEntity lastSection, currentSection; public double startBeat { get; private set; }
[NonSerialized] public double nextSectionBeat;
public double nextSectionBeat { get; private set; }
public double SectionProgress { get; private set; } public double SectionProgress { get; private set; }
public float MarkerWeight { get; private set; } public float MarkerWeight { get; private set; }
public int MarkerCategory { get; private set; } public int MarkerCategory { get; private set; }
public bool playMode { get; private set; }
public bool autoplay { get; private set; }
public bool canInput { get; private set; }
public bool GameHasSplitColours public bool GameHasSplitColours
{ {
get get
@ -138,7 +139,9 @@ namespace HeavenStudio
eventCaller = this.gameObject.AddComponent<EventCaller>(); eventCaller = this.gameObject.AddComponent<EventCaller>();
eventCaller.GamesHolder = GamesHolder.transform; eventCaller.GamesHolder = GamesHolder.transform;
eventCaller.Init(); eventCaller.Init(this);
canInput = true;
// note: serialize this shit in the inspector // // note: serialize this shit in the inspector //
GameObject textbox = Instantiate(Resources.Load<GameObject>("Prefabs/Common/Textbox")); GameObject textbox = Instantiate(Resources.Load<GameObject>("Prefabs/Common/Textbox"));
@ -428,10 +431,11 @@ namespace HeavenStudio
public void SeekAheadAndDoPreEvent(double start) public void SeekAheadAndDoPreEvent(double start)
{ {
if (currentPreSequence < Beatmap.Entities.Count && currentPreSequence >= 0) if (currentPreSequence < Beatmap.Entities.Count && currentPreSequence >= 0)
{
if (start >= preSequenceBeats[currentPreSequence])
{ {
List<RiqEntity> entitiesInRange = ListPool<RiqEntity>.Get(); List<RiqEntity> entitiesInRange = ListPool<RiqEntity>.Get();
while (currentPreSequence < preSequenceBeats.Count && start >= preSequenceBeats[currentPreSequence])
{
entitiesInRange.Clear();
foreach (RiqEntity entity in Beatmap.Entities) foreach (RiqEntity entity in Beatmap.Entities)
{ {
string[] entityDatamodel = entity.datamodel.Split('/'); string[] entityDatamodel = entity.datamodel.Split('/');
@ -449,14 +453,14 @@ namespace HeavenStudio
var inf = GetGameInfo(gameName); var inf = GetGameInfo(gameName);
if (inf != null && inf.usesAssetBundle && inf.AssetsLoaded && !inf.SequencesPreloaded) if (inf != null && inf.usesAssetBundle && inf.AssetsLoaded && !inf.SequencesPreloaded)
{ {
Debug.Log($"Preloading game {gameName}"); Debug.Log($"Preparing game {gameName}");
PreloadGameSequences(gameName); PreloadGameSequences(gameName);
} }
eventCaller.CallPreEvent(entity); eventCaller.CallPreEvent(entity);
currentPreSequence++; currentPreSequence++;
} }
ListPool<RiqEntity>.Release(entitiesInRange);
} }
ListPool<RiqEntity>.Release(entitiesInRange);
} }
} }
@ -533,7 +537,7 @@ namespace HeavenStudio
if (cond.songPositionInBeatsAsDouble >= Math.Ceiling(_playStartBeat) + _pulseTally) if (cond.songPositionInBeatsAsDouble >= Math.Ceiling(_playStartBeat) + _pulseTally)
{ {
if (_currentMinigame != null) _currentMinigame.OnBeatPulse(Math.Ceiling(_playStartBeat) + _pulseTally); if (minigame != null) minigame.OnBeatPulse(Math.Ceiling(_playStartBeat) + _pulseTally);
onBeatPulse?.Invoke(Math.Ceiling(_playStartBeat) + _pulseTally); onBeatPulse?.Invoke(Math.Ceiling(_playStartBeat) + _pulseTally);
_pulseTally++; _pulseTally++;
} }
@ -544,13 +548,14 @@ namespace HeavenStudio
SeekAheadAndDoPreEvent(clampedBeat); SeekAheadAndDoPreEvent(clampedBeat);
if (currentEvent < Beatmap.Entities.Count && currentEvent >= 0) if (currentEvent < Beatmap.Entities.Count && currentEvent >= 0)
{
if (clampedBeat >= eventBeats[currentEvent])
{ {
List<RiqEntity> entitiesInRange = ListPool<RiqEntity>.Get(); List<RiqEntity> entitiesInRange = ListPool<RiqEntity>.Get();
List<RiqEntity> fxEntities = ListPool<RiqEntity>.Get(); List<RiqEntity> fxEntities = ListPool<RiqEntity>.Get();
// allows for multiple events on the same beat to be executed on the same frame, so no more 1-frame delay // allows for multiple events on the same beat to be executed on the same frame, so no more 1-frame delay
while (currentEvent < eventBeats.Count && clampedBeat >= eventBeats[currentEvent] && Conductor.instance.isPlaying)
{
fxEntities.Clear();
entitiesInRange.Clear();
using (PooledObject<List<RiqEntity>> pool = ListPool<RiqEntity>.Get(out List<RiqEntity> currentBeatEntities)) using (PooledObject<List<RiqEntity>> pool = ListPool<RiqEntity>.Get(out List<RiqEntity> currentBeatEntities))
{ {
currentBeatEntities = Beatmap.Entities.FindAll(c => c.beat == eventBeats[currentEvent]); currentBeatEntities = Beatmap.Entities.FindAll(c => c.beat == eventBeats[currentEvent]);
@ -592,11 +597,10 @@ namespace HeavenStudio
// Thank you to @shshwdr for bring this to my attention // Thank you to @shshwdr for bring this to my attention
currentEvent++; currentEvent++;
} }
}
ListPool<RiqEntity>.Release(entitiesInRange); ListPool<RiqEntity>.Release(entitiesInRange);
ListPool<RiqEntity>.Release(fxEntities); ListPool<RiqEntity>.Release(fxEntities);
} }
}
if (currentSection == null) if (currentSection == null)
{ {
@ -619,7 +623,7 @@ namespace HeavenStudio
if (Conductor.instance.songPositionInBeatsAsDouble >= Math.Ceiling(_playStartBeat) + _latePulseTally) if (Conductor.instance.songPositionInBeatsAsDouble >= Math.Ceiling(_playStartBeat) + _latePulseTally)
{ {
if (_currentMinigame != null) _currentMinigame.OnLateBeatPulse(Math.Ceiling(_playStartBeat) + _latePulseTally); if (minigame != null) minigame.OnLateBeatPulse(Math.Ceiling(_playStartBeat) + _latePulseTally);
onBeatPulse?.Invoke(Math.Ceiling(_playStartBeat) + _latePulseTally); onBeatPulse?.Invoke(Math.Ceiling(_playStartBeat) + _latePulseTally);
_latePulseTally++; _latePulseTally++;
} }
@ -630,6 +634,16 @@ namespace HeavenStudio
canInput = inputs; canInput = inputs;
} }
public void ToggleAutoplay(bool auto)
{
autoplay = auto;
}
public void TogglePlayMode(bool mode)
{
playMode = mode;
}
#region Play Events #region Play Events
private double _playStartBeat = 0; private double _playStartBeat = 0;
@ -701,7 +715,7 @@ namespace HeavenStudio
{ {
Conductor.instance.PlaySetup(beat); Conductor.instance.PlaySetup(beat);
Minigame miniGame = null; Minigame miniGame = null;
if (currentGameO != null && currentGameO.TryGetComponent<Minigame>(out miniGame)) if (minigameObj != null && minigameObj.TryGetComponent<Minigame>(out miniGame))
{ {
if (miniGame != null) if (miniGame != null)
{ {
@ -761,7 +775,7 @@ namespace HeavenStudio
} }
Minigame miniGame; Minigame miniGame;
if (currentGameO != null && currentGameO.TryGetComponent<Minigame>(out miniGame)) if (minigameObj != null && minigameObj.TryGetComponent<Minigame>(out miniGame))
{ {
if (miniGame != null) if (miniGame != null)
{ {
@ -1065,7 +1079,7 @@ namespace HeavenStudio
SetGame(game, false); SetGame(game, false);
Minigame miniGame; Minigame miniGame;
if (currentGameO != null && currentGameO.TryGetComponent<Minigame>(out miniGame)) if (minigameObj != null && minigameObj.TryGetComponent<Minigame>(out miniGame))
{ {
if (miniGame != null) if (miniGame != null)
{ {
@ -1095,19 +1109,19 @@ namespace HeavenStudio
GameObject prefab = GetGame(game); GameObject prefab = GetGame(game);
if (prefab == null) return; if (prefab == null) return;
Destroy(currentGameO); Destroy(minigameObj);
currentGameO = Instantiate(prefab); minigameObj = Instantiate(prefab);
if (currentGameO.TryGetComponent<Minigame>(out var minigame)) if (minigameObj.TryGetComponent<Minigame>(out var minigame))
{ {
_currentMinigame = minigame; this.minigame = minigame;
minigame.minigameName = game; minigame.minigameName = game;
minigame.gameManager = this; minigame.gameManager = this;
minigame.conductor = Conductor.instance; minigame.conductor = Conductor.instance;
} }
Vector3 originalScale = currentGameO.transform.localScale; Vector3 originalScale = minigameObj.transform.localScale;
currentGameO.transform.parent = eventCaller.GamesHolder.transform; minigameObj.transform.parent = eventCaller.GamesHolder.transform;
currentGameO.transform.localScale = originalScale; minigameObj.transform.localScale = originalScale;
currentGameO.name = game; minigameObj.name = game;
SetCurrentGame(game, useMinigameColor); SetCurrentGame(game, useMinigameColor);
} }

View file

@ -29,8 +29,9 @@ namespace HeavenStudio.Games.Loaders
{ {
preFunction = delegate preFunction = delegate
{ {
var e = eventCaller.currentEntity; EventCaller ec = eventCaller;
TrickClass.PreTossObject(e.beat, (int)e["obj"], e["nx"]); var e = ec.currentEntity;
TrickClass.PreTossObject(ec.gameManager, e.beat, (int)e["obj"], e["nx"]);
}, },
defaultLength = 2, defaultLength = 2,
parameters = new List<Param>() parameters = new List<Param>()
@ -46,7 +47,9 @@ namespace HeavenStudio.Games.Loaders
{ {
preFunction = delegate preFunction = delegate
{ {
TrickClass.PreTossObject(eventCaller.currentEntity.beat, (int)TrickClass.TrickObjType.Plane); EventCaller ec = eventCaller;
var e = ec.currentEntity;
TrickClass.PreTossObject(ec.gameManager, e.beat, (int)TrickClass.TrickObjType.Plane);
}, },
defaultLength = 3, defaultLength = 3,
}, },
@ -62,7 +65,9 @@ namespace HeavenStudio.Games.Loaders
{ {
preFunction = delegate preFunction = delegate
{ {
TrickClass.PreTossObject(eventCaller.currentEntity.beat, (int)TrickClass.TrickObjType.Chair); EventCaller ec = eventCaller;
var e = ec.currentEntity;
TrickClass.PreTossObject(ec.gameManager, e.beat, (int)TrickClass.TrickObjType.Chair);
}, },
defaultLength = 2, defaultLength = 2,
hidden = true, hidden = true,
@ -71,7 +76,9 @@ namespace HeavenStudio.Games.Loaders
{ {
preFunction = delegate preFunction = delegate
{ {
TrickClass.PreTossObject(eventCaller.currentEntity.beat, (int)TrickClass.TrickObjType.Phone, eventCaller.currentEntity["nx"]); EventCaller ec = eventCaller;
var e = ec.currentEntity;
TrickClass.PreTossObject(ec.gameManager, e.beat, (int)TrickClass.TrickObjType.Phone, eventCaller.currentEntity["nx"]);
}, },
defaultLength = 2, defaultLength = 2,
hidden = true, hidden = true,
@ -84,7 +91,9 @@ namespace HeavenStudio.Games.Loaders
{ {
preFunction = delegate preFunction = delegate
{ {
TrickClass.PreTossObject(eventCaller.currentEntity.beat, (int)TrickClass.TrickObjType.Shock); EventCaller ec = eventCaller;
var e = ec.currentEntity;
TrickClass.PreTossObject(ec.gameManager, e.beat, (int)TrickClass.TrickObjType.Shock);
}, },
defaultLength = 2, defaultLength = 2,
hidden = true, hidden = true,
@ -283,23 +292,23 @@ namespace HeavenStudio.Games
instance.showBubble = !instance.showBubble; instance.showBubble = !instance.showBubble;
} }
public static void PreTossObject(double beat, int type, bool variant = false) public static void PreTossObject(GameManager gm, double beat, int type, bool variant = false)
{ {
if (GameManager.instance.currentGame == "trickClass") if (gm.currentGame == "trickClass" && gm.minigameObj.TryGetComponent(out TrickClass tc))
{ {
BeatAction.New(instance, new List<BeatAction.Action>() BeatAction.New(tc, new List<BeatAction.Action>()
{ {
new BeatAction.Action(beat - 1, delegate new BeatAction.Action(beat - 1, delegate
{ {
if (instance.showBubble == true) if (tc.showBubble == true)
{ {
instance.warnAnim.Play(variant ? instance.objWarnAnimVariant[type] : instance.objWarnAnim[type], 0, 0); tc.warnAnim.Play(variant ? tc.objWarnAnimVariant[type] : tc.objWarnAnim[type], 0, 0);
} }
}), }),
new BeatAction.Action(beat, delegate new BeatAction.Action(beat, delegate
{ {
instance.warnAnim.Play("NoPose", 0, 0); tc.warnAnim.Play("NoPose", 0, 0);
instance.TossObject(beat, type, variant); tc.TossObject(beat, type, variant);
}) })
}); });
} }

View file

@ -522,7 +522,7 @@ namespace HeavenStudio.Editor
public void ToggleDebugCam() public void ToggleDebugCam()
{ {
var game = GameManager.instance.currentGameO; var game = GameManager.instance.minigameObj;
if (game != null) if (game != null)
{ {

View file

@ -353,12 +353,12 @@ namespace HeavenStudio.Editor.Track
if (!GameManager.instance.autoplay) if (!GameManager.instance.autoplay)
{ {
AutoplayBTN.GetComponent<Animator>().Play("Idle", 0, 0); AutoplayBTN.GetComponent<Animator>().Play("Idle", 0, 0);
GameManager.instance.autoplay = true; GameManager.instance.ToggleAutoplay(true);
} }
else else
{ {
AutoplayBTN.GetComponent<Animator>().Play("Disabled", 0, 0); AutoplayBTN.GetComponent<Animator>().Play("Disabled", 0, 0);
GameManager.instance.autoplay = false; GameManager.instance.ToggleAutoplay(false);
} }
} }

View file

@ -497,7 +497,7 @@ namespace HeavenStudio
if (!usesAssetBundle) return; if (!usesAssetBundle) return;
if (bundleCommon != null) return; if (bundleCommon != null) return;
AssetBundle bundle = await AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/common")).ToUniTask(); AssetBundle bundle = await AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/common")).ToUniTask(timing: PlayerLoopTiming.PreLateUpdate);
bundleCommon = bundle; bundleCommon = bundle;
commonLoaded = true; commonLoaded = true;
@ -519,7 +519,7 @@ namespace HeavenStudio
if (!usesAssetBundle) return; if (!usesAssetBundle) return;
if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) return; if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) return;
AssetBundle bundle = await AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/locale." + defaultLocale)).ToUniTask(); AssetBundle bundle = await AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/locale." + defaultLocale)).ToUniTask(timing: PlayerLoopTiming.PreLateUpdate);
if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) return; if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) return;
bundleLocalized = bundle; bundleLocalized = bundle;
@ -533,7 +533,7 @@ namespace HeavenStudio
if (!commonLoaded) return; if (!commonLoaded) return;
if (bundleCommon == null) return; if (bundleCommon == null) return;
UnityEngine.Object asset = await bundleCommon.LoadAssetAsync<GameObject>(name).ToUniTask(); UnityEngine.Object asset = await bundleCommon.LoadAssetAsync<GameObject>(name).ToUniTask(timing: PlayerLoopTiming.PreLateUpdate);
loadedPrefab = asset as GameObject; loadedPrefab = asset as GameObject;
// load sound sequences here for now // load sound sequences here for now

View file

@ -51,5 +51,9 @@ MonoBehaviour:
- Assets/Scripts/UI/PauseMenu.cs - Assets/Scripts/UI/PauseMenu.cs
- Assets/Scripts/Util/Sound.cs - Assets/Scripts/Util/Sound.cs
- Assets/Scripts/Conductor.cs - Assets/Scripts/Conductor.cs
- Assets/Scripts/EventCaller.cs
- Assets/Scripts/Games/TrickClass/TrickClass.cs
- Assets/Scripts/GameInitializer.cs
- Assets/Scripts/LevelEditor/Timeline/Timeline.cs
PathsToSkipImportEvent: [] PathsToSkipImportEvent: []
PathsToIgnoreOverwriteSettingOnAttribute: [] PathsToIgnoreOverwriteSettingOnAttribute: []