IEnumerator UnlockInitialData() { while (state != Globals.MainLoadState.InitialUnlock) { yield return(null); } yield return(null); for (int xx = 0; xx < categories.Count; xx++) { int unlockCount = 0; if (categories [xx].type == Globals.Categories.Buildings || categories [xx].type == Globals.Categories.Scenery) { unlockCount = 5; } else if (categories [xx].type != Globals.Categories.Completed || categories [xx].type != Globals.Categories.Started) { unlockCount = 10; } unlockCount = Mathf.Clamp(unlockCount, 0, (categories [xx].buttons.Count)); for (int yy = 0; yy < unlockCount; yy++) { if (!gameData.unlocked.Contains(categories[xx].buttons[yy].data.myName)) { gameData.unlocked.Add(categories [xx].buttons [yy].data.myName); } } } state = Globals.MainLoadState.UnlockingSprites; }
IEnumerator SetLockedState() { while (state != Globals.MainLoadState.UnlockingSprites) { yield return(null); } yield return(null); for (int xx = 0; xx < categories.Count; xx++) { for (int yy = 0; yy < categories [xx].buttons.Count; yy++) { if (gameData.unlocked.Contains(categories [xx].buttons [yy].data.myName)) { categories [xx].buttons [yy].locked.enabled = false; } } } state = Globals.MainLoadState.Complete; }
void Start() { lastState = Globals.MainMenu.Categories; currState = Globals.MainMenu.Categories; bool initialData = false; if (File.Exists(Application.persistentDataPath + "/MainData/GameData.dat")) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/MainData/GameData.dat", FileMode.Open); GameData dat = (GameData)bf.Deserialize(file); file.Close(); gameData = dat; } else { GameData newDat = new GameData(); newDat.tokenCount = 50; System.DateTime currTime = System.DateTime.Now; newDat.dayStarted = currTime; newDat.lastCheck = currTime; newDat.unlocked = new List <string> (); gameData = newDat; initialData = true; } categories = new List <Globals.CategoryCollection> (); StartCoroutine(InitializeData()); if (initialData) { StartCoroutine(UnlockInitialData()); } else { state = Globals.MainLoadState.UnlockingSprites; } StartCoroutine(SetLockedState()); StartCoroutine(CheckTimeDiff()); }
IEnumerator InitializeData() { yield return(null); Object[] sprites = Resources.LoadAll("Sprite Objects", typeof(SpriteData)); List <SpriteButton> allButtons = new List <SpriteButton> (); for (int xx = 0; xx < sprites.Length; xx++) { SpriteButton newButton = Instantiate(spriteButton); newButton.data = sprites [xx] as SpriteData; allButtons.Add(newButton); if (File.Exists(Application.persistentDataPath + "/LevelData/" + newButton.data.myName + ".lev")) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/LevelData/" + newButton.data.myName + ".lev", FileMode.Open); ProgressData dat = (ProgressData)bf.Deserialize(file); file.Close(); newButton.progress = dat; } if (File.Exists(Application.persistentDataPath + "/Thumbs/" + newButton.data.myName + ".png")) { byte[] imageData = File.ReadAllBytes(Application.persistentDataPath + "/Thumbs/" + newButton.data.myName + ".png"); Texture2D newTex = new Texture2D(0, 0); newTex.LoadImage(imageData); newTex.filterMode = FilterMode.Point; newTex.wrapMode = TextureWrapMode.Clamp; newTex.Apply(); newButton.myCol.sprite = Sprite.Create(newTex, new Rect(0, 0, newTex.width, newTex.height), Vector2.zero, 100); } else { newButton.myCol.sprite = Sprite.Create(newButton.data.thumb, new Rect(0, 0, newButton.data.thumb.width, newButton.data.thumb.height), Vector2.zero, 100); } newButton.myButton.onClick.AddListener(delegate { LoadLevel(newButton); }); } System.Array typeNums = System.Enum.GetValues(typeof(Globals.Categories)); Globals.Categories[] types = new Globals.Categories[typeNums.Length]; System.Array.Copy(typeNums, types, typeNums.Length); for (int xx = 0; xx < types.Length; xx++) { Globals.CategoryCollection coll = new Globals.CategoryCollection(); coll.buttons = new List <SpriteButton> (); coll.type = types [xx]; for (int yy = 0; yy < allButtons.Count; yy++) { if (coll.type != Globals.Categories.Started && coll.type != Globals.Categories.Completed) { if (allButtons [yy].data.type == types [xx]) { if (allButtons [yy].progress == null) { coll.buttons.Add(allButtons [yy]); } else if (!allButtons [yy].progress.started && !allButtons [yy].progress.completed) { coll.buttons.Add(allButtons [yy]); } } } else { if (coll.type == Globals.Categories.Completed) { if (allButtons [yy].progress != null) { if (allButtons [yy].progress.completed) { coll.buttons.Add(allButtons [yy]); } } } else if (coll.type == Globals.Categories.Started) { if (allButtons [yy].progress != null) { if (!allButtons [yy].progress.completed) { if (allButtons [yy].progress.started) { coll.buttons.Add(allButtons [yy]); } } } } } } coll.buttons = SortByDifficulty(true, coll.buttons); categories.Add(coll); } LayoutRebuilder.MarkLayoutForRebuild(spritePanel.GetComponent <RectTransform> ()); initialized = true; state = Globals.MainLoadState.InitialUnlock; }