From 254e725b6fb41f45c8071592d90a8ba85b1e5019 Mon Sep 17 00:00:00 2001 From: minenice55 Date: Mon, 8 Jan 2024 01:38:05 -0500 Subject: [PATCH] Put Minigame Definitions in a Dictionary (#614) * put minigame definitions in a dictionary why haven't we done this two years ago * paranoia --- Assets/Scripts/EventCaller.cs | 34 +++++++++++++++---- Assets/Scripts/GameManager.cs | 11 +++--- Assets/Scripts/LevelEditor/Editor.cs | 2 +- .../EventSelector/EventParameterManager.cs | 5 +-- .../Scripts/LevelEditor/Timeline/Timeline.cs | 8 ++--- .../LevelEditor/Timeline/TimelineEventObj.cs | 31 ++++++++--------- Assets/Scripts/Minigames.cs | 27 ++++++++++----- 7 files changed, 72 insertions(+), 46 deletions(-) diff --git a/Assets/Scripts/EventCaller.cs b/Assets/Scripts/EventCaller.cs index ad111faa..8460cf56 100644 --- a/Assets/Scripts/EventCaller.cs +++ b/Assets/Scripts/EventCaller.cs @@ -2,9 +2,7 @@ using System; using System.Linq; using System.Collections.Generic; using UnityEngine; -using UnityEngine.Events; using Jukebox; -using Jukebox.Legacy; namespace HeavenStudio { @@ -18,11 +16,15 @@ namespace HeavenStudio public static EventCaller instance { get; private set; } - public List minigames = new List(); + public Dictionary minigames = new(); public Minigames.Minigame GetMinigame(string gameName) { - return minigames.Find(c => c.name == gameName); + if (!minigames.ContainsKey(gameName)) + { + return null; + } + return minigames[gameName]; } public Minigames.GameAction GetGameAction(Minigames.Minigame game, string action) @@ -30,11 +32,29 @@ namespace HeavenStudio return game.actions.Find(c => c.actionName == action); } + public Minigames.GameAction GetGameAction(string gameName, string action) + { + if (minigames.ContainsKey(gameName)) + { + return minigames[gameName].actions.Find(c => c.actionName == action); + } + else + { + Debug.LogWarning($"Game {gameName} not found!"); + return null; + } + } + public Minigames.Param GetGameParam(Minigames.Minigame game, string action, string param) { return GetGameAction(game, action).parameters.Find(c => c.propertyName == param); } + public Minigames.Param GetGameParam(string gameName, string action, string param) + { + return GetGameAction(gameName, action).parameters.Find(c => c.propertyName == param); + } + public void Init() { instance = this; @@ -68,7 +88,7 @@ namespace HeavenStudio public void CallEvent(RiqEntity entity, bool gameActive) { string[] details = entity.datamodel.Split('/'); - Minigames.Minigame game = minigames.Find(c => c.name == details[0]); + Minigames.Minigame game = minigames[details[0]]; try { currentEntity = entity; @@ -94,7 +114,7 @@ namespace HeavenStudio public void CallPreEvent(RiqEntity entity) { string[] details = entity.datamodel.Split('/'); - Minigames.Minigame game = minigames.Find(c => c.name == details[0]); + Minigames.Minigame game = minigames[details[0]]; try { currentEntity = entity; @@ -153,7 +173,7 @@ namespace HeavenStudio public static List FXOnlyGames() { - return instance.minigames.FindAll(c => c.fxOnly == true).ToList(); + return instance.minigames.Values.ToList().FindAll(c => c.fxOnly); } } } \ No newline at end of file diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 77a7f179..2070a11c 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -469,8 +469,7 @@ namespace HeavenStudio string[] seekEntityDatamodel = seekEntity.datamodel.Split('/'); - float seekTime = eventCaller.GetGameAction( - eventCaller.GetMinigame(seekEntityDatamodel[0]), seekEntityDatamodel[1]).preFunctionLength; + float seekTime = eventCaller.GetGameAction(seekEntityDatamodel[0], seekEntityDatamodel[1]).preFunctionLength; if (start + seekTime >= eventBeats[currentPreSequence]) { @@ -904,10 +903,8 @@ namespace HeavenStudio xDatamodel = x.datamodel.Split('/'); yDatamodel = y.datamodel.Split('/'); - Minigames.Minigame xGame = eventCaller.GetMinigame(xDatamodel[0]); - Minigames.GameAction xAction = eventCaller.GetGameAction(xGame, xDatamodel[1]); - Minigames.Minigame yGame = eventCaller.GetMinigame(yDatamodel[0]); - Minigames.GameAction yAction = eventCaller.GetGameAction(yGame, yDatamodel[1]); + Minigames.GameAction xAction = eventCaller.GetGameAction(xDatamodel[0], xDatamodel[1]); + Minigames.GameAction yAction = eventCaller.GetGameAction(yDatamodel[0], yDatamodel[1]); return yAction.priority.CompareTo(xAction.priority); }); @@ -1196,7 +1193,7 @@ namespace HeavenStudio public Minigames.Minigame GetGameInfo(string name) { - return eventCaller.minigames.Find(c => c.name == name); + return eventCaller.GetMinigame(name); } Color colMain; diff --git a/Assets/Scripts/LevelEditor/Editor.cs b/Assets/Scripts/LevelEditor/Editor.cs index 66125067..0f2de366 100644 --- a/Assets/Scripts/LevelEditor/Editor.cs +++ b/Assets/Scripts/LevelEditor/Editor.cs @@ -104,7 +104,7 @@ namespace HeavenStudio.Editor GameManager.instance.Init(); Timeline.Init(); - foreach (var minigame in EventCaller.instance.minigames) + foreach (var minigame in EventCaller.instance.minigames.Values) AddIcon(minigame); UpdateEditorStatus(true); diff --git a/Assets/Scripts/LevelEditor/EventSelector/EventParameterManager.cs b/Assets/Scripts/LevelEditor/EventSelector/EventParameterManager.cs index be47e122..deb5ca4c 100644 --- a/Assets/Scripts/LevelEditor/EventSelector/EventParameterManager.cs +++ b/Assets/Scripts/LevelEditor/EventSelector/EventParameterManager.cs @@ -81,8 +81,9 @@ namespace HeavenStudio.Editor private void AddParams(RiqEntity entity) { - var minigame = EventCaller.instance.GetMinigame(entity.datamodel.Split(0)); - int actionIndex = minigame.actions.IndexOf(minigame.actions.Find(c => c.actionName == entity.datamodel.Split(1))); + string[] split = entity.datamodel.Split('/'); + var minigame = EventCaller.instance.GetMinigame(split[0]); + int actionIndex = minigame.actions.IndexOf(minigame.actions.Find(c => c.actionName == split[1])); Minigames.GameAction action = minigame.actions[actionIndex]; if (action.parameters != null) diff --git a/Assets/Scripts/LevelEditor/Timeline/Timeline.cs b/Assets/Scripts/LevelEditor/Timeline/Timeline.cs index aa1ba0e9..96e24d0b 100644 --- a/Assets/Scripts/LevelEditor/Timeline/Timeline.cs +++ b/Assets/Scripts/LevelEditor/Timeline/Timeline.cs @@ -873,10 +873,8 @@ namespace HeavenStudio.Editor.Track public TimelineEventObj AddEventObject(string eventName, bool dragNDrop = false, Vector3 pos = new Vector3(), RiqEntity entity = null, bool addEvent = false) { - var game = EventCaller.instance.GetMinigame(eventName.Split(0)); - var action = EventCaller.instance.GetGameAction(game, eventName.Split(1)); - - var gameAction = EventCaller.instance.GetGameAction(EventCaller.instance.GetMinigame(eventName.Split(0)), eventName.Split(1)); + string[] split = eventName.Split('/'); + var action = EventCaller.instance.GetGameAction(split[0], split[1]); if (addEvent) { @@ -884,7 +882,7 @@ namespace HeavenStudio.Editor.Track if (entity == null) { - RiqEntity en = GameManager.instance.Beatmap.AddNewEntity(eventName, 0, gameAction.defaultLength); + RiqEntity en = GameManager.instance.Beatmap.AddNewEntity(eventName, 0, action.defaultLength); tempEntity = en; diff --git a/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs b/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs index e39e3664..4f10bfe5 100644 --- a/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs +++ b/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs @@ -87,32 +87,31 @@ namespace HeavenStudio.Editor.Track var eventName = entity.datamodel; - var game = EventCaller.instance.GetMinigame(eventName.Split(0)); - var action = EventCaller.instance.GetGameAction(game, eventName.Split(1)); - var gameAction = EventCaller.instance.GetGameAction(EventCaller.instance.GetMinigame(eventName.Split(0)), eventName.Split(1)); + string[] split = eventName.Split('/'); + var action = EventCaller.instance.GetGameAction(split[0], split[1]); - if (eventName.Split(1) == "switchGame") - Icon.sprite = Editor.GameIcon(eventName.Split(2)); + if (split[1] == "switchGame") + Icon.sprite = Editor.GameIcon(split[2]); else - Icon.sprite = Editor.GameIcon(eventName.Split(0)); + Icon.sprite = Editor.GameIcon(split[0]); - if (gameAction != null) + if (action != null) { - this.resizable = gameAction.resizable; - if (gameAction.resizable == false) + this.resizable = action.resizable; + if (action.resizable == false) { - rectTransform.sizeDelta = new Vector2(gameAction.defaultLength * Timeline.instance.PixelsPerBeat, Timeline.instance.LayerHeight()); - this.length = gameAction.defaultLength; + rectTransform.sizeDelta = new Vector2(action.defaultLength * Timeline.instance.PixelsPerBeat, Timeline.instance.LayerHeight()); + this.length = action.defaultLength; } else { - if (entity != null && gameAction.defaultLength != entity.length) + if (entity != null && action.defaultLength != entity.length) { rectTransform.sizeDelta = new Vector2(entity.length * Timeline.instance.PixelsPerBeat, Timeline.instance.LayerHeight()); } else { - rectTransform.sizeDelta = new Vector2(gameAction.defaultLength * Timeline.instance.PixelsPerBeat, Timeline.instance.LayerHeight()); + rectTransform.sizeDelta = new Vector2(action.defaultLength * Timeline.instance.PixelsPerBeat, Timeline.instance.LayerHeight()); } } } @@ -383,13 +382,13 @@ namespace HeavenStudio.Editor.Track } else if (Input.GetMouseButton(2)) { - var mgs = EventCaller.instance.minigames; + // var mgs = EventCaller.instance.minigames; string[] datamodels = entity.datamodel.Split('/'); Debug.Log("Selected entity's datamodel : " + entity.datamodel); bool isSwitchGame = datamodels[1] == "switchGame"; - int gameIndex = mgs.FindIndex(c => c.name == datamodels[isSwitchGame ? 2 : 0]); - int block = isSwitchGame ? 0 : mgs[gameIndex].actions.FindIndex(c => c.actionName == datamodels[1]) + 1; + // int gameIndex = mgs.FindIndex(c => c.name == datamodels[isSwitchGame ? 2 : 0]); + int block = isSwitchGame ? 0 : EventCaller.instance.minigames[datamodels[isSwitchGame ? 2 : 0]].actions.FindIndex(c => c.actionName == datamodels[1]) + 1; if (!isSwitchGame) { diff --git a/Assets/Scripts/Minigames.cs b/Assets/Scripts/Minigames.cs index 13ce8878..31412c9b 100644 --- a/Assets/Scripts/Minigames.cs +++ b/Assets/Scripts/Minigames.cs @@ -192,16 +192,18 @@ namespace HeavenStudio System.Type type, pType; if (EventCaller.instance != null) { + string[] split; foreach (var e in data.entities) { - var gameName = e.datamodel.Split(0); - var actionName = e.datamodel.Split(1); + split = e.datamodel.Split('/'); + var gameName = split[0]; + var actionName = split[1]; game = EventCaller.instance.GetMinigame(gameName); if (game == null) { Debug.LogWarning($"Unknown game {gameName} found in remix.json! Adding game..."); game = new Minigames.Minigame(gameName, gameName.DisplayName() + " \n[inferred from remix.json]", "", false, false, new List(), inferred: true); - EventCaller.instance.minigames.Add(game); + EventCaller.instance.minigames.Add(gameName, game); if (Editor.Editor.instance != null) Editor.Editor.instance.AddIcon(game); } @@ -646,9 +648,6 @@ namespace HeavenStudio this.preFunction = prescheduleFunction ?? delegate { }; this.priority = priority; this.preFunctionLength = preFunctionLength; - - - //todo: converting to new versions of GameActions } /// @@ -728,7 +727,7 @@ namespace HeavenStudio public static void Init(EventCaller eventCaller) { - eventCaller.minigames = new List() + List defaultGames = new() { new Minigame("gameManager", "Game Manager", "", false, true, new List() { @@ -1175,11 +1174,23 @@ namespace HeavenStudio }), }; + foreach (var game in defaultGames) + { + eventCaller.minigames.Add(game.name, game); + } + BuildLoadRunnerList(); + Debug.Log($"Running {loadRunners.Count} game loaders..."); foreach (var load in loadRunners) { Debug.Log("Running game loader " + RuntimeReflectionExtensions.GetMethodInfo(load).DeclaringType.Name); - eventCaller.minigames.Add(load(eventCaller)); + Minigame game = load(eventCaller); + if (game == null) + { + Debug.LogError("Game loader " + RuntimeReflectionExtensions.GetMethodInfo(load).DeclaringType.Name + " failed!"); + continue; + } + eventCaller.minigames.Add(game.name, game); } } }