public void LoadLevel(string levelName) { // Read the json from the file into a string LevelJson loadedData = new LevelJson(); try{ string jsonString = File.ReadAllText(Application.streamingAssetsPath + "/level_" + levelName + ".json"); // Debug.Log(jsonString); loadedData = JsonUtility.FromJson <LevelJson>(jsonString); }catch { Debug.Log("No se encontro el archivo"); MKGame.Instance.GetFlowManager().NoMoreLevels(); // Nomore } //Los muebles los intancia lin e ignora los playesr MKGame.Instance.GetFurnitureManager().CreateFurniture(loadedData.cellDataList); foreach (MKCellData mKCellData in loadedData.cellDataList) { // Debug.Log(mKCellData.Type); //SI es jugador lo instancio if (mKCellData.Type == EMKCellType.Player1 || mKCellData.Type == EMKCellType.Player2) { // if(true){ if (MKGame.Instance.GetGameManager().PlaceInCell(mKCellData.PosX, mKCellData.PosY, mKCellData.Type, mKCellData.Color)) { GameObject characterInstance = Instantiate(characterPrefab, levelContainer); characterInstance.transform.position = MKGame.Instance.GetGameManager().GetWorldPosition(mKCellData.PosX, mKCellData.PosY); MKCharacterController characterController = characterInstance.GetComponent <MKCharacterController>(); if (mKCellData.Type == EMKCellType.Player1) { characterController.m_PlayerNumber = EMKPlayerNumber.Player1; characterController.playerPower = PlayerPower.VERTICAL; MKGame.Instance.GetCharacterManager().player1 = characterController; } else { characterController.m_PlayerNumber = EMKPlayerNumber.Player2; characterController.playerPower = PlayerPower.HORIZONTAL; MKGame.Instance.GetCharacterManager().player2 = characterController; } MKGame.Instance.GetCharacterManager().SetPlayerActiveStatus(false); characterController.m_CharacterIndexPositionX = mKCellData.PosX; characterController.m_CharacterIndexPositionY = mKCellData.PosY; } } } //TODO nomore levels MKGame.Instance.GetFlowManager().NextLevelLoaded(); MKGame.Instance.GetUIManager().ResetUI(); }
public void SaveJson() { currentLevelJson = new LevelJson(); SetBordersAsDecoration(); foreach (Transform item in cellParent) { item.GetComponent <CellGenerator>().SetFurniture(); currentLevelJson.cellDataList.Add(item.GetComponent <CellGenerator>().mKCellData); } foreach (MKCellData item in currentLevelJson.cellDataList) { Debug.Log(item.Type); } File.WriteAllText(Application.streamingAssetsPath + "/level_" + levelNumber + "_" + System.DateTime.Now.Minute + ".json", JsonUtility.ToJson(currentLevelJson)); }
/* * * Loads a level from the level editor json * @params filename the name of the file * */ public void LoadLevel(int levelNum) { level.levelTitle.text = "Level " + levelNum; //string path = "Assets/Levels/" + filename + ".json"; TextAsset jsonTextAsset = jsonTextAssets[levelNum - 1]; //Check if file exists at filepath if (jsonTextAsset != null) { //Extracts file into LevelJson Object LevelJson jsonFile = JsonUtility.FromJson <LevelJson>(jsonTextAsset.text); level.setSquare(jsonFile.height, jsonFile.width); hover.InstantiateHoverGrid(); // Game board population level.BoardData = new GameObject[level.squaresX, level.squaresY]; int count = 0; for (int j = 0; j < level.squaresY; j++) { for (int i = 0; i < level.squaresX; i++) { switch (jsonFile.gameBoard[count]) { case 1: level.BoardData[i, j] = null; //Empty Space break; case 2: level.BoardData[i, j] = level.Gem; //Blue Gem break; case 3: level.BoardData[i, j] = level.DeathRock; //Death Rock break; case 4: level.BoardData[i, j] = level.Sinkhole; //Death Rock break; default: level.BoardData[i, j] = level.Rock; //Default Rock break; } count++; } } //Inventory population ArrayList bombs = new ArrayList(); if (jsonFile.B1x3 > 0) { bombs.Add(new Bomb("1x3", jsonFile.B1x3)); } if (jsonFile.B1x5 > 0) { bombs.Add(new Bomb("1x5", jsonFile.B1x5)); } if (jsonFile.B1xINF > 0) { bombs.Add(new Bomb("infinite_horizontal", jsonFile.B1xINF)); } if (jsonFile.B3x1 > 0) { bombs.Add(new Bomb("3x1", jsonFile.B3x1)); } if (jsonFile.B3x3Cross > 0) { bombs.Add(new Bomb("3x3_cross", jsonFile.B3x3Cross)); } if (jsonFile.B3x3Square > 0) { bombs.Add(new Bomb("3x3_square", jsonFile.B3x3Square)); } if (jsonFile.B5x1 > 0) { bombs.Add(new Bomb("5x1", jsonFile.B5x1)); } if (jsonFile.B5x5Cross > 0) { bombs.Add(new Bomb("5x5_cross", jsonFile.B5x5Cross)); } if (jsonFile.B5x5Square > 0) { bombs.Add(new Bomb("5x5_square", jsonFile.B5x5Square)); } if (jsonFile.BINFx1 > 0) { bombs.Add(new Bomb("infinite_vertical", jsonFile.BINFx1)); } if (jsonFile.BINFxINFCross > 0) { bombs.Add(new Bomb("infinite_cross", jsonFile.BINFxINFCross)); } if (jsonFile.BUpFirework > 0) { bombs.Add(new Bomb("up_fireworks", jsonFile.BUpFirework)); } if (jsonFile.BDownFirework > 0) { bombs.Add(new Bomb("down_fireworks", jsonFile.BDownFirework)); } if (jsonFile.BLeftFirework > 0) { bombs.Add(new Bomb("left_fireworks", jsonFile.BLeftFirework)); } if (jsonFile.BRightFirework > 0) { bombs.Add(new Bomb("right_fireworks", jsonFile.BRightFirework)); } level.InventoryArray = (Bomb[])bombs.ToArray(typeof(Bomb)); level.invSize = level.InventoryArray.Length; level.id = jsonFile.id; /* For level id * int x = 0; * int c = 0; * * foreach (int b in jsonFile.gameBoard) * { * x += (int) Mathf.Pow(c, b); * c += 1; * } * * x += jsonFile.width * 12735 + jsonFile.height * 23213; * * Debug.Log(levelNum + ": " + x); */ } else { throw new IOException("File Not Found"); } }