//SAVING 2 ENTITIES WITHOUT READING/WRITING TWICE
    public static void Save2LevelDesigns(LevelDesignInfo _thisLevelDesign, LevelDesignInfo _nextLevelDesign)
    {
        //first load all data
        string   jsonString  = GetJSONFileString();
        JsonData jsonDataOld = JsonMapper.ToObject(jsonString);
        //load all into list
        List <LevelDesignInfo> list_levelDesign = new List <LevelDesignInfo>();

        for (int i = 0; i < jsonDataOld.Count; ++i)
        {
            int category = int.Parse(jsonDataOld[i]["categoryID"].ToString());
            int level    = int.Parse(jsonDataOld[i]["levelID"].ToString());
            list_levelDesign.Add(LevelDesignInfo.LoadLevelDesign(category, level));
        }

        //add new or object to list OR update existing object(remove all other with same level and cat ID, then add new one that will be unique)
        if (LevelDesignInfo.DoesLevelDesignExists(_thisLevelDesign.categoryID, _thisLevelDesign.levelID))
        {
            for (int i = list_levelDesign.Count - 1; i >= 0; --i)
            {
                if (list_levelDesign[i].categoryID == _thisLevelDesign.categoryID && list_levelDesign[i].levelID == _thisLevelDesign.levelID)
                {
                    list_levelDesign.RemoveAt(i);
                }
            }
        }
        list_levelDesign.Add(_thisLevelDesign);

        if (LevelDesignInfo.DoesLevelDesignExists(_nextLevelDesign.categoryID, _nextLevelDesign.levelID))
        {
            for (int i = list_levelDesign.Count - 1; i >= 0; --i)
            {
                if (list_levelDesign[i].categoryID == _nextLevelDesign.categoryID && list_levelDesign[i].levelID == _nextLevelDesign.levelID)
                {
                    list_levelDesign.RemoveAt(i);
                }
            }
        }
        list_levelDesign.Add(_nextLevelDesign);

        //save with new at the end
        JsonData jsonDataNew = JsonMapper.ToJson(list_levelDesign);

        File.WriteAllText(GetJSONFilePath(), jsonDataNew.ToString());
    }
Exemple #2
0
    void LoadMovablePlatforms(int _category, int _level)
    {
        LevelDesignInfo levelDesign = LevelDesignInfo.LoadLevelDesign(_category, _level);

        if (levelDesign == null || levelDesign.platEnvironmentInfos == null || levelDesign.platMovableInfos == null)
        {
            return;
        }

        float distanceBtwObjects = 0.07f;
        float xSpaceTaken        = 0;

        //calculate x length that all movable objects will take + distance between every one
        foreach (PlatformMovableInfo platInfo in levelDesign.platMovableInfos)
        {
            Platform.e_platformShape   shape   = (Platform.e_platformShape)platInfo.shape;
            Platform.e_platformSurface surface = (Platform.e_platformSurface)platInfo.surface;
            float objLength = LevelObjectsPrefabHolder.s_instance.GetPlatformPrefab(shape, surface).GetComponent <SpriteRenderer>().bounds.size.x;
            xSpaceTaken += objLength + distanceBtwObjects;
        }

        Vector2 spawnPoint   = new Vector2(box.transform.position.x - xSpaceTaken / 2, box.transform.position.y);
        float   prevObjWidth = 0f;

        //create movable platforms
        foreach (PlatformMovableInfo platInfo in levelDesign.platMovableInfos)
        {
            Platform.e_platformShape   shape   = (Platform.e_platformShape)platInfo.shape;
            Platform.e_platformSurface surface = (Platform.e_platformSurface)platInfo.surface;
            GameObject platformPrefab          = LevelObjectsPrefabHolder.s_instance.GetPlatformPrefab(shape, surface);

            float thisObjectWidth = platformPrefab.GetComponent <SpriteRenderer>().bounds.size.x;
            spawnPoint = new Vector2(spawnPoint.x + thisObjectWidth / 2 + prevObjWidth / 2 + distanceBtwObjects, spawnPoint.y);
            GameObject platformInstance = (GameObject)Instantiate(platformPrefab, spawnPoint, Quaternion.identity);
            prevObjWidth = platformPrefab.GetComponent <SpriteRenderer>().bounds.size.x;

            platformInstance.GetComponent <TransformPositionInGame>().SetMovableObjectSettings();
            platformInstance.GetComponent <TransformRotationInGame>().SetMovableObjectSettings();
            list_movablePlatforms.Add(platformInstance);
        }
    }
    public static void SetTimeChangesOnLevelComplete(int _completedCat, int __completedLvl, float _time)
    {
        //set medal for this level
        LevelDesignInfo thisInfo = LevelDesignInfo.LoadLevelDesign(_completedCat, __completedLvl);

        if (_time < thisInfo.time && _time >= 0 && _time <= 15)
        {
            thisInfo.time = _time;
        }

        //unlock next level if it's not already unlocked
        int nextLevel = __completedLvl;
        int nextWorld = _completedCat;

        if (__completedLvl != 16)
        {
            nextLevel++;
        }
        else if (__completedLvl == 16 && _completedCat != GlobalSettings.WORLD_COUNT)
        {
            nextLevel = 1;
            nextWorld++;
        }

        if (nextWorld != GlobalSettings.WORLD_COUNT && nextLevel != GlobalSettings.LEVEL_COUNT)
        {
            LevelDesignInfo nextInfo = LevelDesignInfo.LoadLevelDesign(nextWorld, nextLevel);
            if (nextInfo.time == 777.0f)
            {
                nextInfo.time = 888.0f;
            }

            LevelDesignInfo.Save2LevelDesigns(thisInfo, nextInfo);
        }
        else
        {
            LevelDesignInfo.SaveLevelDesign(thisInfo);
        }
    }
    //WHEN IT IS SAVED HIS OLDER 'COPIES' MUST BE DELETED OR IT WILL HAVE BUNCH OF DIFFERENT
    //(category:x,level:y) INFOS AND IT WILL CHOOSE ONE OF THEM(WHO KNOWS WHICH ONE)

    public static void SaveLevelDesign(LevelDesignInfo _levelDesign)
    {
        //first load all data
        string   jsonString  = GetJSONFileString();
        JsonData jsonDataOld = JsonMapper.ToObject(jsonString);
        //load all into list
        List <LevelDesignInfo> list_levelDesign = new List <LevelDesignInfo>();

        for (int i = 0; i < jsonDataOld.Count; ++i)
        {
            int category = int.Parse(jsonDataOld[i]["categoryID"].ToString());
            int level    = int.Parse(jsonDataOld[i]["levelID"].ToString());
            list_levelDesign.Add(LevelDesignInfo.LoadLevelDesign(category, level));
        }
        //add new or object to list OR update existing object(remove all other with same level and cat ID, then add new one that will be unique)
        if (LevelDesignInfo.DoesLevelDesignExists(_levelDesign.categoryID, _levelDesign.levelID))
        {
            for (int i = list_levelDesign.Count - 1; i >= 0; --i)
            {
                if (list_levelDesign[i].categoryID == _levelDesign.categoryID && list_levelDesign[i].levelID == _levelDesign.levelID)
                {
                    list_levelDesign.RemoveAt(i);
                }
            }
        }
        list_levelDesign.Add(_levelDesign);

        //foreach (LevelDesignInfo design in list_levelDesign)
        //Debug.Log(design.categoryID + " " + design.levelID + " " + design.time);

        //save with new at the end
        JsonData jsonDataNew = JsonMapper.ToJson(list_levelDesign);

        //File.WriteAllText(Application.dataPath + "/Resources/JSON/LevelDesignInfo.json", jsonDataNew.ToString());
        File.WriteAllText(GetJSONFilePath(), jsonDataNew.ToString());

        Debug.Log("NEW TIME OF (" + _levelDesign.categoryID + "," + _levelDesign.levelID + ")= " + LevelDesignInfo.LoadLevelDesign(_levelDesign.categoryID, _levelDesign.levelID).time);
    }
Exemple #5
0
    void InitLevel(int _category, int _level)
    {
        if (!LevelDesignInfo.DoesLevelDesignExists(_category, _level))
        {
            Debug.Log("level does not exist in json");
            return;
        }
        //if exists get it...
        LevelDesignInfo levelDesign = LevelDesignInfo.LoadLevelDesign(_category, _level);

        //set current category and level
        world = levelDesign.categoryID;
        level = levelDesign.levelID;

        //set environment platforms
        foreach (PlatformEnvironmentInfo platInfo in levelDesign.platEnvironmentInfos)
        {
            Platform.e_platformShape   shape   = (Platform.e_platformShape)platInfo.shape;
            Platform.e_platformSurface surface = (Platform.e_platformSurface)platInfo.surface;
            GameObject platformPrefab          = LevelObjectsPrefabHolder.s_instance.GetPlatformPrefab(shape, surface);
            GameObject platformInstance        = (GameObject)Instantiate(platformPrefab, new Vector2((float)platInfo.posX, (float)platInfo.posY), Quaternion.identity);

            platformInstance.GetComponent <TransformPositionInGame>().SetEnvironmentObjectSettings();
            platformInstance.GetComponent <TransformRotationInGame>().SetEnvironmentObjectSettings();

            platformInstance.transform.eulerAngles = new Vector3(0, 0, (float)platInfo.rotZ);
            list_environmentPlatforms.Add(platformInstance);
        }

        //start/end line
        Vector2 startLinePos = new Vector2((float)levelDesign.startX, (float)levelDesign.startY);
        Vector2 endLinePos   = new Vector2((float)levelDesign.endX, (float)levelDesign.endY);

        startLine = (GameObject)Instantiate(LevelObjectsPrefabHolder.s_instance.GetStartLinePrefab(), startLinePos, Quaternion.identity);
        endLine   = (GameObject)Instantiate(LevelObjectsPrefabHolder.s_instance.GetEndLinePrefab(), endLinePos, Quaternion.identity);
        startLine.GetComponent <TransformPositionInGame>().SetEnvironmentObjectSettings();
        endLine.GetComponent <TransformPositionInGame>().SetEnvironmentObjectSettings();
    }
    public static void RemoveLevelDesign(int _category, int _level)
    {
        //first load all data
        string   jsonString = GetJSONFileString();
        JsonData jsonData   = JsonMapper.ToObject(jsonString);
        //load all into list except levelDesign with specific categoryID and levelID
        List <LevelDesignInfo> list_levelDesign = new List <LevelDesignInfo>();

        for (int i = 0; i < jsonData.Count; ++i)
        {
            int category = int.Parse(jsonData[i]["categoryID"].ToString());
            int level    = int.Parse(jsonData[i]["levelID"].ToString());
            if (category != _category && level != _level)
            {
                list_levelDesign.Add(LevelDesignInfo.LoadLevelDesign(category, level));
            }
        }

        //save with new at the end
        JsonData jsonDataNew = JsonMapper.ToJson(list_levelDesign);

        File.WriteAllText(Application.dataPath + "/Resources/JSON/LevelDesignInfo.json", jsonDataNew.ToString());
    }
    //-------------------------------------------
    //SET LEVEL INFO FOR GIVEN WORLD TO GUI SLOTS
    //-------------------------------------------

    void SetLevelSlots(int worldID)
    {
        //call LoadLevelDesign for all 16 levels for given world(worldID)
        List <LevelDesignInfo> levelDesignInfo = new List <LevelDesignInfo>();

        for (int currLvlID = 1; currLvlID < GlobalSettings.LEVEL_COUNT + 1; ++currLvlID)
        {
            if (LevelDesignInfo.DoesLevelDesignExists(worldID, currLvlID))
            {
                levelDesignInfo.Add(LevelDesignInfo.LoadLevelDesign(worldID, currLvlID));
            }
        }

        if (levelDesignInfo.Count == 0)
        {
            Debug.Log(levelDesignInfo.Count);
            return;
        }

        foreach (LevelDesignInfo info in levelDesignInfo)
        {
            Image slotImg  = slotList[info.levelID - 1].GetComponent <Image>();
            Image medalImg = slotList[info.levelID - 1].transform.FindChild("medal").GetComponent <Image>();
            Text  slotText = slotList[info.levelID - 1].transform.FindChild("Text").GetComponent <Text>();

            slotText.text = info.levelID.ToString();
            //-->if time = 777 make slot gray an without medal(create lock instead)(can be played unitl prev level is finished)
            //-->else if time is 888 make slot colored but without any icon(can be played but was never finished)
            //-->else if time is > 0 && < 15 make slot colored and give it a proper medal(played and medal achieved)
            if (info.time == 777.0f)
            {
                medalImg.sprite = locked;
                slotText.color  = new Color32(75, 75, 75, 255);
                slotText.color  = new Color32(0, 0, 0, 0);
                slotImg.sprite  = lockedSlot;
            }
            else if (info.time == 888.0f)
            {
                slotText.color  = new Color32(122, 68, 0, 255);
                medalImg.sprite = emptySprite;
                slotImg.sprite  = normalSlot;
            }
            else
            {
                slotImg.sprite = normalSlot;
                slotText.color = new Color32(122, 68, 0, 255);
                if (info.time > 0 && info.time <= 5)
                {
                    medalImg.sprite = goldMedal;
                }
                else if (info.time > 5 && info.time <= 10)
                {
                    medalImg.sprite = silverMedal;
                }
                else if (info.time > 10 && info.time <= 15)
                {
                    medalImg.sprite = bronceMedal;
                }
            }
        }
    }