/// <summary> /// Loads previously saved state of the game and replaces current one /// Uses 'slotToLoad' variable to identify the file from which to load /// </summary> public void LoadLevel(string path, int levelNumber) { LevelData ld = SaveUnit.LoadLevel(levelNumber, path); if (path == "/Levels/Editor/") { GameDataManager.instance.generalData.selectedRocket = -1; } for (int x = 0; x < gridSize.x; x++) { for (int y = 0; y < gridSize.y; y++) { if (!lu.grid[x, y].IsEmpty()) { gu.DestroyGem(x, y, lu.grid[x, y].Gem.Color, false); } } } gridSize.x = ld.gridSizeX; gridSize.y = ld.gridSizeY; randomizeColors = ld.randomizeColors; colorsAvailable = ld.colorVector.Length; permittedBonuses = ld.availableBonuses; ComputeGemColors(); ComputeGemSizes(); winCondition = ld.winCondition; timeAvailable = ld.timeAvailable; movesAvailable = ld.movesAvailable; scoreToWin = ld.scoreToWin; gu.UpdateDataAfterLoading(); lu.UpdateDataAfterLoading(); gu.RecreateGrid((int)gridSize.x, (int)gridSize.y); int[] gemColors = ld.gemColors; if (randomizeColors) { for (int i = 0; i < gemColors.Length; i++) { gemColors[i] = colorVector[IndexOf(ld.gemColors[i], ld.colorVector)]; } } lu.grid = new Cell[(int)gridSize.x, (int)gridSize.y]; for (int x = 0; x < (int)gridSize.x; x++) { for (int y = 0; y < (int)gridSize.y; y++) { if (ld.gemColors[x * (int)gridSize.y + y] != -1) { lu.grid[x, y] = new Cell(new Vector2(x, y)) { Gem = new Gem { Color = gemColors[x * (int)gridSize.y + y], Bonus = ld.gemBonuses[x * (int)gridSize.y + y] } }; gu.SpawnGem((int)lu.grid[x, y].Position.x, (int)lu.grid[x, y].Position.y, lu.grid[x, y].Gem.Color, lu.grid[x, y].Gem.Bonus); } else { lu.grid[x, y] = new Cell(new Vector2(x, y)); lu.grid[x, y].SetEmpty(); } } } sequenceSize = ld.sequenceSize; maximumEnergy = ld.maximumEnergy; int rocketId = GameDataManager.instance.generalData.selectedRocket; if (rocketId != -1) { maximumEnergy = GameDataManager.instance.rocketData[rocketId].maxEnergy; } lu.suboptimalMoves = maximumEnergy; gu.RecreateEnergyBar(maximumEnergy); spawnNewGems = ld.spawnNewGems; spawnEnergy = ld.spawnEnergy; winCondition = ld.winCondition; timeAvailable = ld.timeAvailable; scoreToWin = ld.scoreToWin; movesAvailable = ld.movesAvailable; lu.ComputeResourceColors(); }
/// <summary> /// Destroys gem at the given location /// and performs actions according to its bonus /// </summary> /// <param name="x">X-pos of the gem to be destroyed</param> /// <param name="y">Y-pos of the gem to be destroyed</param> public int DestroyGem(int x, int y) { bool needToDestroy = true; int localScore = 0; if (grid[x, y].Gem.IsBonus()) { switch (grid[x, y].Gem.Bonus) { case (int)ParamUnit.Bonus.METEOR: gu.ActivateMeteorBonus(x, y); localScore += pu.scoreUnit; break; case (int)ParamUnit.Bonus.SAME_COLOR: if (!bonusIsWorking) { bonusIsWorking = true; // Search for gems with the same color int color = grid[x, y].Gem.Color; for (int _x = 0; _x < gSizeX; _x++) { for (int _y = 0; _y < gSizeY; _y++) { if (!grid[_x, _y].IsEmpty() && grid[_x, _y].Gem.Color == color) { gu.DestroyGem(_x, _y, color); localScore += DestroyGem(_x, _y); } } } } localScore += pu.scoreUnit; bonusIsWorking = false; break; case (int)ParamUnit.Bonus.ENERGY: IncreaseSuboptimal(); localScore += pu.scoreUnit; break; case (int)ParamUnit.Bonus.ICE_1: needToDestroy = false; grid[x, y].Gem.Bonus = (int)ParamUnit.Bonus.ICE_2; gu.RespawnGem(x, y, grid[x, y].Gem.Color, grid[x, y].Gem.Bonus); localScore += pu.scoreUnit / 2; break; case (int)ParamUnit.Bonus.ICE_2: needToDestroy = false; grid[x, y].Gem.Bonus = (int)ParamUnit.Bonus.ICE_3; gu.RespawnGem(x, y, grid[x, y].Gem.Color, grid[x, y].Gem.Bonus); localScore += pu.scoreUnit / 2; break; case (int)ParamUnit.Bonus.ICE_3: needToDestroy = false; grid[x, y].Gem.Bonus = (int)ParamUnit.Bonus.NONE; gu.RespawnGem(x, y, grid[x, y].Gem.Color, grid[x, y].Gem.Bonus); localScore += pu.scoreUnit / 2; break; } } if (needToDestroy) { if (grid[x, y].Gem != null) { if (colorsMetal.Contains(grid[x, y].Gem.Color)) { collectedMetal++; } if (colorsFuel.Contains(grid[x, y].Gem.Color)) { collectedFuel++; } localScore += pu.scoreUnit; } grid[x, y].Gem = null; } needToCheck = true; return(localScore); }