Example #1
0
    IEnumerator InstantiateButtons()
    {
        yield return(new WaitForSeconds(0.4f));

        int number = 1;

        for (int i = 0; i < 4; i++)
        {
            Vector3 pos = new Vector3(-6.4f, 8.8f - (3.3f * i), 0f);

            for (int j = 0; j < 5; j++)
            {
                GameObject obj = Instantiate(module2MapButton);
                spawnedMapButtons.Add(obj);
                obj.transform.SetParent(module2Levels.transform);
                obj.transform.localPosition = pos;

                obj.name = "PreLevel" + number.ToString();

                RankingManager.Record?bestResult = RankingManager.GetTheBestRecord("PreLevel" + number.ToString());

                if (bestResult != null)
                {
                    obj.transform.GetChild(0).GetComponent <TextMeshPro>().text += $" <size=\"1\"><b>{((RankingManager.Record)bestResult).points.ToString()}</b></size>";
                }
                else
                {
                    obj.transform.GetChild(0).GetComponent <TextMeshPro>().text += " <size=\"1\"><b>0</b></size>";
                }

                Button3D btn = obj.GetComponent <Button3D>();

                string iconPath = $"Maps/{Path.GetFileNameWithoutExtension($"{obj.name}_icon.jpg")}";

                MeshRenderer renderer = obj.GetComponent <MeshRenderer>();
                Material     mat      = new Material(Shader.Find("Legacy Shaders/Diffuse"));
                Texture2D    texture  = Resources.Load <Texture2D>(iconPath);
                print(iconPath);

                if (texture != null)
                {
                    mat.mainTexture   = texture;
                    renderer.material = mat;
                }

                btn.OnClick.AddListener((sender) =>
                {
                    MapSerializer serializer = new MapSerializer(MapSerializer.MapsPath + "/" + sender.name);
                    Map deserializedMap      = serializer.Deserialize(true);

                    module2Levels.SetActive(false);
                    module2Info.SetActive(true);

                    currentLevelModule2 = sender.name;

                    RankingManager.Record[] records = RankingManager.GetRecords(currentLevelModule2);
                    TextMeshPro text = module2Info.transform.GetChild(2).GetComponent <TextMeshPro>();
                    text.text        = "";

                    SetMapIcon(MapSerializer.MapsPath + "/" + sender.name + ".xml", module2Info.transform.GetChild(0).gameObject);

                    foreach (RankingManager.Record r in records)
                    {
                        text.text += $"Points: <b>{r.points.ToString()} | </b>Count of moves: <b>{r.moves.ToString()}</b> | Date: <b>{r.date.ToShortDateString()} {r.date.ToShortTimeString()}</b>\n";
                    }

                    Button3D playBtn      = module2Info.transform.GetChild(4).GetComponent <Button3D>();
                    Button3D playSavedBtn = module2Info.transform.GetChild(5).GetComponent <Button3D>();

                    playSavedBtn.isClickable = SaveLoadManager.SaveExists(deserializedMap);

                    playSavedBtn.OnClick.RemoveAllListeners();

                    if (playSavedBtn.isClickable)
                    {
                        playSavedBtn.OnClick.AddListener(s =>
                        {
                            Map progressedMap = SaveLoadManager.LoadLevelProgress(deserializedMap, out int movesCount);

                            StartCoroutine(Coroutine());

                            IEnumerator Coroutine()
                            {
                                camAnim.SetInteger("view", LEVEL);
                                camAnim.SetTrigger("switch");

                                yield return(new WaitForSeconds(0.5f));

                                if (spawnModule2ButtonsCor != null)
                                {
                                    StopCoroutine(spawnModule2ButtonsCor);
                                    spawnModule2ButtonsCor = null;
                                }

                                foreach (GameObject o in spawnedMapButtons)
                                {
                                    Destroy(o);
                                }

                                spawnedMapButtons.Clear();

                                module2.SetActive(false);
                                module2Info.SetActive(false);

                                camAnim.enabled = false;

                                LevelManager.CurrentManager.SetBackgroundColor(progressedMap.biomeType);
                                LevelManager.CurrentManager.LoadLevel(progressedMap, movesCount);
                            }
                        });
                    }

                    playBtn.OnClick.RemoveAllListeners();
                    playBtn.OnClick.AddListener(s =>
                    {
                        SaveLoadManager.ClearSave(deserializedMap);

                        StartCoroutine(Coroutine());

                        IEnumerator Coroutine()
                        {
                            camAnim.SetInteger("view", LEVEL);
                            camAnim.SetTrigger("switch");

                            yield return(new WaitForSeconds(0.5f));

                            if (spawnModule2ButtonsCor != null)
                            {
                                StopCoroutine(spawnModule2ButtonsCor);
                                spawnModule2ButtonsCor = null;
                            }

                            foreach (GameObject o in spawnedMapButtons)
                            {
                                Destroy(o);
                            }

                            spawnedMapButtons.Clear();

                            module2.SetActive(false);
                            module2Info.SetActive(false);

                            camAnim.enabled = false;

                            LevelManager.CurrentManager.SetBackgroundColor(deserializedMap.biomeType);
                            LevelManager.CurrentManager.LoadLevel(deserializedMap);
                        }
                    });
                });

                number++;
                pos += new Vector3(5.3f, 0f, 0f);
                yield return(new WaitForSeconds(0.075f));
            }
        }

        spawnModule2ButtonsCor = null;
    }