void HandleLevelDoneState() { if (IsBallTouchingFinish() && !isLevelFinished) { isLevelFinished = true; } if (!isLevelFinished) { return; } GlobalSettings.finishTime = timePlaying; //keep ball on the same position if level is finished ball.GetComponent <Rigidbody2D>().isKinematic = true; ball.GetComponent <Ball>().isAtFinishLine = true; afterFinishedTimer.Tick(Time.deltaTime); if (afterFinishedTimer.IsFinished()) { LevelDesignInfo.SetTimeChangesOnLevelComplete(world, level, timePlaying); ScreenPrefabHolder prefabHolder = GameObject.Find("ScreenPrefabHolder").GetComponent <ScreenPrefabHolder>(); Instantiate(prefabHolder.GetPrefab(ScreenPrefabHolder.e_screenID.AFTER_LEVEL_FINISHED), new Vector2(0, 0), Quaternion.identity); DestroyLevelObjects(); Destroy(gameObject); } }
//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()); }
public static LevelDesignInfo LoadLevelDesign(int _category, int _level) { //read all from json string jsonString = GetJSONFileString(); JsonData jsonData = JsonMapper.ToObject(jsonString); //create level by reading values from json ony for given categoryID and levelID LevelDesignInfo levelDesign = new LevelDesignInfo(); for (int i = 0; i < jsonData.Count; ++i) { if (int.Parse(jsonData[i]["categoryID"].ToString()) == _category && int.Parse(jsonData[i]["levelID"].ToString()) == _level) { double time = double.Parse(jsonData[i]["time"].ToString()); double startX = double.Parse(jsonData[i]["startX"].ToString()); double startY = double.Parse(jsonData[i]["startY"].ToString()); double endX = double.Parse(jsonData[i]["endX"].ToString()); double endY = double.Parse(jsonData[i]["endY"].ToString()); List <PlatformEnvironmentInfo> platformEnvironment = new List <PlatformEnvironmentInfo>(); for (int j = 0; j < jsonData[i]["platEnvironmentInfos"].Count; ++j) { double posX = double.Parse(jsonData[i]["platEnvironmentInfos"][j]["posX"].ToString()); double posY = double.Parse(jsonData[i]["platEnvironmentInfos"][j]["posY"].ToString()); double rotZ = double.Parse(jsonData[i]["platEnvironmentInfos"][j]["rotZ"].ToString()); int shape = int.Parse(jsonData[i]["platEnvironmentInfos"][j]["shape"].ToString()); int surface = int.Parse(jsonData[i]["platEnvironmentInfos"][j]["surface"].ToString()); platformEnvironment.Add(new PlatformEnvironmentInfo(posX, posY, rotZ, shape, surface)); } List <PlatformMovableInfo> platformMovable = new List <PlatformMovableInfo>(); for (int j = 0; j < jsonData[i]["platMovableInfos"].Count; ++j) { int shape = int.Parse(jsonData[i]["platMovableInfos"][j]["shape"].ToString()); int surface = int.Parse(jsonData[i]["platMovableInfos"][j]["surface"].ToString()); platformMovable.Add(new PlatformMovableInfo(shape, surface)); } //set level design with proper values levelDesign.Init(_category, _level, time, startX, startY, endX, endY, platformEnvironment, platformMovable); } } return(levelDesign); }
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); } }
//GUI STUFF::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: void SetSaveButtonListener() { saveButton.GetComponent <Button>().onClick.AddListener(delegate { int levelID = 0; int categoryID = 0; bool isLevelReadable = int.TryParse(levelIDInput.GetComponent <Text>().text, out levelID); bool isCategoryReadable = int.TryParse(worldIDInput.GetComponent <Text>().text, out categoryID); if (!isLevelReadable || !isCategoryReadable) { onSaveErrorText.GetComponent <Text>().text = "ERROR ON CATEGORY/LEVEL INPUT"; return; } //Check if level or world already exists and give proper message if (LevelDesignInfo.DoesLevelDesignExists(categoryID, levelID)) { onSaveErrorText.GetComponent <Text>().text = "ALREADY EXISTS"; return; } else if (start_line == null || end_line == null) { onSaveErrorText.GetComponent <Text>().text = "START OR END LINE MISSING"; return; } else { onSaveErrorText.GetComponent <Text>().text = "SUSCESSFULLY CHECKED"; } //set-up all info that will be written in JSON file and save it List <PlatformMovableInfo> movableInfo = GetMovablePlatInfoFromInput(); List <PlatformEnvironmentInfo> environmentInfo = GetEnvironmentPlatInfo(); double startX = start_line.transform.position.x; double startY = start_line.transform.position.y; double endX = end_line.transform.position.x; double endY = end_line.transform.position.y; //apply info to object that is used for JSON and save it LevelDesignInfo levelDesign = new LevelDesignInfo(categoryID, levelID, 777, startX, startY, endX, endY, environmentInfo, movableInfo); LevelDesignInfo.SaveLevelDesign(levelDesign); }); }
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); }
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; } } } }
void Awake() { LevelDesignInfo.InitJSONFileOnDeviceMemoryIfNeeded(); }