diff --git a/Assets/Scripts/GameObjectExtensions.cs b/Assets/Scripts/GameObjectExtensions.cs index f036f1387e87ca1dd4ea92bd2eae198004b0897b..89c88fe135b5f542225cdf4feb63d8e82b9be238 100644 --- a/Assets/Scripts/GameObjectExtensions.cs +++ b/Assets/Scripts/GameObjectExtensions.cs @@ -22,7 +22,7 @@ public static void DestroyAllChildren(this GameObject root) GameObject.Destroy(root.transform.GetChild(i).gameObject); } - public static GameObject GetNthChild(this GameObject root, List<int> pos) + public static GameObject GetNthChild(this GameObject root, IEnumerable<int> pos) { GameObject ret = root; foreach (var i in pos) diff --git a/Assets/Scripts/Loading/Stage.cs b/Assets/Scripts/Loading/Stage.cs index cfd8d1f036c7a159fe7c5f2b57be6b4e9088c222..6f2848ae3151092c6b937691a60d5de207b2688a 100644 --- a/Assets/Scripts/Loading/Stage.cs +++ b/Assets/Scripts/Loading/Stage.cs @@ -185,7 +185,7 @@ public bool set_record(PlayerRecord record) public void push_record(double seconds_s = -1, bool force_push = false) { - if(!force_push && StageStatic.devel && creatorMode) + if(!force_push && StageStatic.mode == StageStatic.Mode.Create && creatorMode) // store solution space { SetMode(false); diff --git a/Assets/Scripts/StageBehaviour.cs b/Assets/Scripts/StageBehaviour.cs index 4985a8cae3e2134e08ca426df1eb9f5d5e9d9ea1..0689d4c023133bff25bb0baf43f8a292bda24b97 100644 --- a/Assets/Scripts/StageBehaviour.cs +++ b/Assets/Scripts/StageBehaviour.cs @@ -12,17 +12,12 @@ void Start() private void OnDestroy() { - StageStatic.devel = false; + StageStatic.SetMode(Mode.Play); // no Mode.Create StageStatic.stage.solution.hardreset(); StageStatic.stage.factState.hardreset(); } - public void SetDevel(bool devel) - { - StageStatic.devel = devel; - gameObject.UpdateTagActive("DevelopingMode", devel); - } - + // needed as endpoint for unity buttons public void SetMode(bool create) { SetMode(create ? Mode.Create : Mode.Play); diff --git a/Assets/Scripts/StageStatic.cs b/Assets/Scripts/StageStatic.cs index 38a1c88297aef5eadfa6e99e2bf3df29591b4c4c..39bbbc3069e9fefe88711f7a3ac66f3758e3003f 100644 --- a/Assets/Scripts/StageStatic.cs +++ b/Assets/Scripts/StageStatic.cs @@ -4,26 +4,56 @@ using System; using UnityEngine; +/// <summary> +/// Keeps track of all available and current <see cref="Stage"/> +/// </summary> public static class StageStatic { + /// <summary> + /// - <c>Key</c>: stage name + /// - <c>Value</c>: stages created by KWARC + /// </summary> public static Dictionary<string, Stage> StageOfficial; + + /// <summary> + /// - <c>Key</c>: stage name + /// - <c>Value</c>: stages created by local user + /// </summary> public static Dictionary<string, Stage> StageLocal; + + /// <summary> + /// Used to map <see cref="StageOfficial"/> <see cref="Stage.category">categories</see> into a ordered list for the StageMenue. + /// </summary> public static Dictionary<string, int> Category = new Dictionary<string, int> { { "", -1 }, { "Demo Category", 0 }, }; + /// <summary> + /// <see cref="Stage.name"/> of current <see cref="stage"/> or one to be loaded. + /// <seealso cref="LoadInitStage(bool, GameObject)"/> + /// </summary> public static string current_name; - public static bool local_stage; - public static bool devel = false; + + /// <summary> + /// !<see cref="Stage.use_install_folder"/> of current <see cref="stage"/> or one to be loaded. + /// <seealso cref="LoadInitStage(bool, GameObject)"/> + /// </summary> + public static bool local_stage; + + /// <summary> + /// Current <see cref="Mode"/> + /// </summary> public static Mode mode; - public static List<string> Worlds - { - get { return _Worlds ?? GenerateWorldList(); } - } - private static List<string> _Worlds = null; + /// <summary> + /// Loadable world scenes + /// </summary> + public static readonly List<string> Worlds = GenerateWorldList(); + /// <summary> + /// Current <see cref="Stage"/> + /// </summary> public static Stage stage { get { return (local_stage ? StageLocal : StageOfficial)[current_name]; @@ -39,19 +69,25 @@ public static Stage stage { } } - // TODO: set when encountering an error - public static StageErrorStruct error { + /// <summary> + /// TODO: set when encountering an error + /// </summary> + public static StageErrorStruct last_error { get; private set; } // TODO! generate at buildtime + /// <summary> + /// Extracts all loadable scenes for <see cref="Worlds"/>. + /// </summary> + /// <returns><see cref="Worlds"/></returns> private static List<string> GenerateWorldList() { #if UNITY_EDITOR - _Worlds = new List<string>(); + List<string> _Worlds = new List<string>(); string world = "World"; string ending = ".unity"; @@ -69,31 +105,57 @@ private static List<string> GenerateWorldList() } } #else - _Worlds = new List<string> {"TreeWorld", "RiverWorld"}; + List<string> _Worlds = new List<string> {"TreeWorld", "RiverWorld"}; Debug.Log("WorldList might be incomplete or incorrect!"); #endif return _Worlds; } + /// <summary> + /// Available Modes a <see cref="Stage"/> to be selected and/ or loaded in. + /// </summary> public enum Mode { Play, Create, } + /// <summary> + /// Created when an error (may) occures while a <see cref="Stage"/> is being created, because of incompatible variables. + /// </summary> public struct StageErrorStruct { + /// <summary> set iff <see cref="Stage.category"/> is incompatible </summary> public bool category { get { return state[0]; } set { state[0] = value; } } + + /// <summary> set iff <see cref="Stage.number"/> is incompatible </summary> public bool id { get { return state[1]; } set { state[1] = value; } } + + /// <summary> set iff <see cref="Stage.name"/> is incompatible </summary> public bool name { get { return state[2]; } set { state[2] = value; } } + + /// <summary> set iff <see cref="Stage.description"/> is incompatible </summary> public bool description { get { return state[3]; } set { state[3] = value; } } + + /// <summary> set iff <see cref="Stage.scene"/> is incompatible </summary> public bool scene { get { return state[4]; } set { state[4] = value; } } + + /// <summary> set iff !<see cref="Stage.use_install_folder"/> is incompatible </summary> public bool local { get { return state[5]; } set { state[5] = value; } } + + /// <summary> set iff <see cref="Stage.path"/> was not found </summary> public bool load { get { return state[6]; } set { state[6] = value; } } + + /// <summary> + /// stores all boolish members, to iterate over + /// </summary> private bool[] state; + /// <summary> + /// <see langword="true"/> iff no error occures. + /// </summary> public bool pass { get { return state.Aggregate(true, (last, next) => last && !next); } @@ -103,6 +165,10 @@ public readonly static StageErrorStruct InvalidFolder = new StageErrorStruct(false, false, false, false, false, true, false), NotLoadable = new StageErrorStruct(false, false, false, false, false, false, true); + /// <summary> + /// Initiator <br/> + /// canonical + /// </summary> public StageErrorStruct(bool category, bool id, bool name, bool description, bool scene, bool local, bool load) { state = new bool[7]; @@ -117,6 +183,11 @@ public StageErrorStruct(bool category, bool id, bool name, bool description, boo } } + /// <summary> + /// sets <see cref="mode"/> and en-/ disables children of <paramref name="gameObject"/> with certain Tags, only available in certain <see cref="Mode">Modes</see> (e.g. in Def_Stage) + /// </summary> + /// <param name="mode"><see cref="Mode"/> to set</param> + /// <param name="gameObject"> which children will be checked</param> public static void SetMode(Mode mode, GameObject gameObject = null) { gameObject ??= new GameObject(); @@ -137,7 +208,8 @@ public static void SetMode(Mode mode, GameObject gameObject = null) { case Mode.Play: case Mode.Create: - stage.SetMode(mode == Mode.Create); + if (ContainsKey(current_name, local_stage)) + stage.SetMode(mode == Mode.Create); break; } @@ -170,15 +242,24 @@ public static StageErrorStruct LoadNewStage(string category, int id, string name return ret; } + /// <summary> + /// Load current <see cref="stage"/> in <see cref="Mode.Create"/> + /// </summary> public static void LoadCreate() { - devel = true; + SetMode(Mode.Create); Loader.LoadScene(stage.scene); } - public static int NextNumber(bool local) + /// <summary> + /// Finds first unused <see cref="Stage.number"/> in a certain <paramref name="category"/>. + /// </summary> + /// <param name="local">which kind of stage we are looking at</param> + /// <param name="category">the category in question</param> + /// <returns>first unused <see cref="Stage.number"/> in a certain <paramref name="category"/></returns> + public static int NextNumber(bool local, string category) { - var numbers = (local ? StageLocal : StageOfficial).Values.Select(s => s.number).ToList(); + var numbers = (local ? StageLocal : StageOfficial).Values.Where(s => s.category == category).Select(s => s.number).ToList(); if (0 == numbers.Count) return 1; @@ -260,11 +341,11 @@ public static bool LoadInitStage(bool restore_session, GameObject gameObject = n else { stage.ResetPlay(); - if(devel) // block saving "player" progress + if(mode == Mode.Create) // block saving "player" progress stage.player_record.seconds = -1; } - gameObject.UpdateTagActive("DevelopingMode", devel); + gameObject.UpdateTagActive("DevelopingMode", mode == Mode.Create); SetMode(stage.creatorMode ? Mode.Create : Mode.Play); return true; } @@ -273,7 +354,7 @@ public static bool ContainsKey(string key) { return ContainsKey(key, local_stage); } - public static bool ContainsKey(string key, bool local = false) + public static bool ContainsKey(string key, bool local) { return (local ? StageLocal : StageOfficial).ContainsKey(key); } diff --git a/Assets/Scripts/UI/MainMenue/CollapsableStage/CollapsableStage.cs b/Assets/Scripts/UI/MainMenue/CollapsableStage/CollapsableStage.cs index 8d1b62000c431741a8be7677d197172a8c5ecda8..1c0e3a31acb6104f97300fb033e4b03aa69142e1 100644 --- a/Assets/Scripts/UI/MainMenue/CollapsableStage/CollapsableStage.cs +++ b/Assets/Scripts/UI/MainMenue/CollapsableStage/CollapsableStage.cs @@ -26,7 +26,7 @@ public void Init() // set implicit load button (whole header) header.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(delegate { - StageStatic.devel = false; + StageStatic.SetMode(StageStatic.Mode.Play); // TODO: handle unable to load Loader.LoadStage(stage.name, !stage.use_install_folder, true); }); @@ -76,7 +76,7 @@ public void DrawChildren() this.Init(); return; } - StageStatic.devel = false; + StageStatic.SetMode(StageStatic.Mode.Play); // TODO: handle unable to load Loader.LoadStage(stage.name, !stage.use_install_folder, true); }); diff --git a/Assets/Scripts/UI/MainMenue/PageLoader/CreateLoader.cs b/Assets/Scripts/UI/MainMenue/PageLoader/CreateLoader.cs index 61b5cd7c03a8940cbe20673743c1f23b97f28b99..3341ff9f1174316b4da28e9dd09f34404910a7ce 100644 --- a/Assets/Scripts/UI/MainMenue/PageLoader/CreateLoader.cs +++ b/Assets/Scripts/UI/MainMenue/PageLoader/CreateLoader.cs @@ -18,7 +18,7 @@ protected string category set { Category.text = value; } } protected int id { - get { return Id.text.Length == 0 ? StageStatic.NextNumber(true) : int.Parse(Id.text); } + get { return Id.text.Length == 0 ? StageStatic.NextNumber(true, category) : int.Parse(Id.text); } set { Id.text = value.ToString(); } } protected new string name {