public void LoadLevel(string name, long id) { LevelGenerator.levelName = name; LevelGenerator.levelId = id; levelText.text = name; try { if (levelMetaData == null) { throw new Exception(); } JSONScore score = JSONScoreActions.getJSONScore(id); if (score != null) { timeText.text = GetTimeScore(score.seconds); pointText.text = score.points.ToString(); } else { timeText.text = "--m--s"; pointText.text = "----"; } levelGenerator.InitializeLevelEditor(levelGenerator.ReadLevelJSON()); } catch (Exception e) { Debug.LogException(e, this); Debug.Log("Can't generate level"); } }
public void EndGame() { if (!hasWon) { hasWon = true; LevelEndingInterface.SetActive(true); TextMeshProUGUI[] textmeshes = LevelEndingInterface.GetComponentsInChildren <TextMeshProUGUI>(); textmeshes[0].SetText(nbScore.ToString()); textmeshes[1].SetText(GetTimeScore()); if (!editorMode) { JSONScore newScore = new JSONScore(); newScore.id = LevelGenerator.levelId; newScore.points = nbScore; newScore.seconds = Convert.ToInt64(Time.fixedTime - startingTime); List <JSONScore> list = new List <JSONScore>(); list.Add(newScore); if (JSONScoreActions.addScore(list)) { StartCoroutine(sendScores(newScore.points)); } ScoreUI.SetActive(false); } } }
static public JSONScore getJSONScore(long id) { try { string filePath = Path.Combine(Application.persistentDataPath, "Levels/scores"); if (File.Exists(filePath)) { string dataAsJson = File.ReadAllText(filePath); JSONScores loadedData = JsonUtility.FromJson <JSONScores>(dataAsJson); JSONScore cur = loadedData.scores.Find(x => x.id == id); Debug.Log(dataAsJson); Debug.Log(JsonUtility.ToJson(loadedData)); return(cur); } return(null); } catch (Exception e) { Debug.LogException(e); Debug.Log("Can't find files"); return(null); } }
static public bool addScore(List <JSONScore> jSONScores) { bool scored = false; try { JSONScores tmp = getJSONScores(); foreach (var scr in jSONScores) { JSONScore cur = tmp.scores.Find(x => x.id == scr.id); if (cur != null) { if (cur.points <= scr.points && cur.seconds >= scr.seconds) { cur.points = scr.points; cur.seconds = scr.seconds; scored = true; } } else { tmp.scores.Add(scr); scored = true; } } string filePath = Path.Combine(Application.persistentDataPath, "Levels/scores"); File.WriteAllText(filePath, JsonUtility.ToJson(tmp)); return(scored); } catch (Exception e) { Debug.LogException(e); Debug.Log("Can't find files"); return(false); } }