public static int gamerGold = 1000000; // For Shell // public int maxOfMissedCreepsForComputer0; // public int missedCreepsForComputer0; // public int maxOfMissedCreepsForPlayer1; // public int missedCreepsForPlayer1; // GAME INTERFACE ZONE2 // LineRenderer lineRenderer; // Use this for initialization void Start() { Debug.Log("GameField::Start(); -- Start!"); waveManager = new WaveManager(); creepsManager = new CreepsManager(); towersManager = new TowersManager(); factionsManager = new FactionsManager(1f); factionsManager.loadFactions(); gamefield = GameObject.Find("GameField"); Map map = new MapLoader().loadMap(mapPath); sizeFieldX = int.Parse(map.properties ["width"]); sizeFieldZ = int.Parse(map.properties ["height"]); // lineRenderer = gameObject.AddComponent<LineRenderer>(); createField(sizeFieldX, sizeFieldZ, map.mapLayers); Creeps = new GameObject("Creeps"); Creeps.transform.position = new Vector3(0, 10, 0); GameObject NavMesh = new GameObject("NavMesh"); NavMesh.AddComponent <NavMeshSurface>(); // var geo = NavMesh.GetComponent<NavMeshSurface>(); // geo.overrideTileSize = true; // geo.tileSize = 64; //geo.useGeometry = NavMeshCollectGeometry.PhysicsColliders; surface = GameObject.Find("NavMesh").GetComponent <NavMeshSurface>(); surface.BuildNavMesh(); WaveAlgorithm waveAlgorithm = new WaveAlgorithm(sizeFieldX, sizeFieldZ, 30, 30, field); Debug.Log("GameField::Start(); -- End!"); }
void CheckGameOver() { bool isGameOver = true; for (int x = 0; x < bricksCount.x; x++) { for (int y = 0; y < bricksCount.y; y++) { Vector2Int coords = new Vector2Int(x, y); List <Vector2Int> area = WaveAlgorithm.GetArea( field, coords, GetAdjacentCoords, b => b != null && b.Number == field[coords.x, coords.y].Number ); if (area.Count > 1) { isGameOver = false; } } } if (!isGameOver) { return; } isAnimating = true; gameState.SetField(new int[0]); UserProgress.Current.SaveGameState(name); OnGameOver(); }
void OnEmptyBrickClick(Vector2Int coords) { if (isAnimating || currentBrick == null || field[coords.x, coords.y] != null) { return; } currentBrick.DoStopBlinking(); List <Vector2Int> path = WaveAlgorithm.GetPath(field, currentBrickCoords, coords, GetAdjacentCoords, brick => brick == null); if (path.Count < 2) { currentBrick = null; NoPath?.Invoke(); return; } field[currentBrickCoords.x, currentBrickCoords.y] = null; field[coords.x, coords.y] = currentBrick; int number = currentBrick.Number; List <Vector2> localPath = new List <Vector2>(path.Count); path.ForEach(c => localPath.Add(GetBrickPosition(c))); currentBrick.DoLocalPath( localPath, () => { List <Vector2Int> area = WaveAlgorithm.GetArea(field, coords, GetAdjacentCoords, b => b != null && b.Number == number); if (area.Count < bricksToMerge) { SpawnNewBricks( () => { SaveGame(); CheckGameOver(); } ); } else { Merge(coords, true, SaveGame); } } ); currentBrick = null; }
void Merge(List <Vector2Int> toMerge, Action onComplete) { isAnimating = true; List <Vector2Int> newCoords = new List <Vector2Int> (); int animationsLeft = 0; foreach (Vector2Int coords in toMerge) { if (field[coords.x, coords.y] == null) { continue; } NumberedBrick brick = field[coords.x, coords.y]; List <Vector2Int> area = WaveAlgorithm.GetArea( field, coords, GetAdjacentCoords, b => b != null && b.Number == brick.Number ); if (area.Count < 2) { continue; } newCoords.AddRange(area); List <BrickPath> paths = new List <BrickPath> (); foreach (Vector2Int toMove in area) { if (toMove == coords) { continue; } BrickPath brickPath = new BrickPath { brick = field[toMove.x, toMove.y], path = WaveAlgorithm.GetPath( field, toMove, coords, GetAdjacentCoords, b => b != null && b.Number == brick.Number ) }; brickPath.path.RemoveAt(0); paths.Add(brickPath); } foreach (Vector2Int toMove in area) { if (toMove != coords) { field[toMove.x, toMove.y] = null; } } animationsLeft++; int areaSize = area.Count; AnimateMerge( paths, () => { animationsLeft--; if (animationsLeft > 0) { return; } mergingSfx.Play(); brick.Number *= Mathf.ClosestPowerOfTwo(areaSize); brick.ColorIndex = GetColorIndex(brick.Number); brick.DoMergingAnimation( () => { if (Random.Range(0f, 1f) < coinProbability) { UserProgress.Current.Coins++; GameObject vfx = Resources.Load <GameObject> ("CoinVFX"); vfx = Instantiate(vfx, fieldTransform.parent); vfx.transform.position = brick.transform.position; Destroy(vfx, 1.5f); } if (newCoords.Count > 0) { Normalize( normalized => { newCoords.AddRange(normalized); Merge(newCoords, onComplete); } ); } } ); gameState.Score += brick.Number; speed = GetSpeed(); // Debug.Log(speed); } ); } if (newCoords.Count > 0) { return; } isAnimating = false; onComplete.Invoke(); }
void Merge(List <Vector2Int> toMerge, Action onComplete) { isAnimating = true; List <Vector2Int> newCoords = new List <Vector2Int>(); int animationsLeft = 0; foreach (Vector2Int coords in toMerge) { if (field[coords.x, coords.y] == null) { continue; } Brick brick = field[coords.x, coords.y]; List <Vector2Int> area = WaveAlgorithm.GetArea( field, coords, b => b != null && b.Number == brick.Number ); if (area.Count < 2) { continue; } newCoords.AddRange(area); List <BrickPath> paths = new List <BrickPath>(); foreach (Vector2Int toMove in area) { if (toMove == coords) { continue; } BrickPath brickPath = new BrickPath { brick = field[toMove.x, toMove.y], path = WaveAlgorithm.GetPath( field, toMove, coords, b => b != null && b.Number == brick.Number ) }; brickPath.path.RemoveAt(0); paths.Add(brickPath); } foreach (Vector2Int toMove in area) { if (toMove != coords) { field[toMove.x, toMove.y] = null; } } animationsLeft++; int areaSize = area.Count; AnimateMerge( paths, () => { animationsLeft--; if (animationsLeft > 0) { return; } mergingSfx.Play(); brick.Number *= Mathf.ClosestPowerOfTwo(areaSize); brick.DoMergingAnimation( () => { if (newCoords.Count > 0) { Normalize( normalized => { newCoords.AddRange(normalized); Merge(newCoords, onComplete); } ); } } ); UserProgress.Current.Score += brick.Number; } ); } if (newCoords.Count > 0) { return; } isAnimating = false; onComplete.Invoke(); }