public bool IsConnectedWithout(DVector position) { if (!states.ContainsKey(position)) { return(true); } HashSet <DVector> visitedCells = new HashSet <DVector>() { position }; Queue <DVector> toVisit = new Queue <DVector>(); toVisit.Enqueue(new DVector(0, 0)); while (toVisit.Count > 0) { var next = toVisit.Dequeue(); visitedCells.Add(next); foreach (var neighbor in next.Get4Neighbours()) { if (!states.ContainsKey(neighbor) || visitedCells.Contains(neighbor) || !TileTypesHelper.IsFilled((TileType)states[neighbor])) { continue; } toVisit.Enqueue(neighbor); } } return(visitedCells.SetEquals(GetFilledCells())); }
private static void TurnRocksActive() { foreach (var rockCell in GameState.Instance.ActualGrid.GetCellsWithValue((int)TileType.InactiveRockNode)) { bool anyNeighborIsFilled = rockCell.Get4Neighbours() .Any(n => TileTypesHelper.IsFilled((TileType)GameState.Instance.ActualGrid.GetCell(n))); if (anyNeighborIsFilled) { GameState.Instance.ActualGrid.SetCell(rockCell, (int)TileType.ActiveRockNode); } } }
public static void ExpandAreaAround(DVector pos) { foreach (var neighbor in pos.Get4Neighbours()) { if (GameState.Instance.InitialGrid.IsEmpty(neighbor) || TileTypesHelper.IsFilled((TileType)GameState.Instance.ActualGrid.GetCell(neighbor))) { continue; } var targetCellType = GameState.Instance.InitialGrid.GetCell(neighbor); if (targetCellType == (int)TileType.ActiveRockNode && !GameState.Instance.CanGrowRocks) { targetCellType = (int)TileTypesHelper.ToInactive((TileType)targetCellType); } GameState.Instance.ActualGrid.SetCell(neighbor, targetCellType); } }
public static bool ProcessCellClick(DVector position) { if (GameState.Instance.RevealInAction) { if (GameState.Instance.Energy >= GameState.Instance.CurrentRevealCost) { RevealCellsAround(position, GameConstants.RevealRadius); GameState.Instance.AddToEnergy(-GameState.Instance.CurrentRevealCost); GameState.Instance.CurrentRevealCost = IncrementCost(GameState.Instance.CurrentRevealCost); Sounds.Instance.PlaySound(Sounds.RevealSound); } else { // should not happen now // play some sound or show message } GameState.Instance.RevealInAction = false; return(true); } if (GameState.Instance.PruneEdgesInAction) { if (!TileTypesHelper.IsFilled((TileType)GameState.Instance.ActualGrid.GetCell(position))) { return(false); } if (GameState.Instance.Energy >= GameState.Instance.CurrentPruneCost) { if (!position.Equals(new DVector(0, 0))) { if (GameState.Instance.ActualGrid.IsConnectedWithout(position)) { PruneCell(position); GameState.Instance.AddToEnergy(-GameState.Instance.CurrentPruneCost); GameState.Instance.CurrentPruneCost = IncrementCost(GameState.Instance.CurrentPruneCost); Sounds.Instance.PlaySound(Sounds.PruneSound); } else { GameState.Instance.CurentInstructions = GameConstants.PruneNotConnectedMessage; Sounds.Instance.PlaySound(Sounds.NopeSound); } } else { GameState.Instance.CurentInstructions = GameConstants.PruneRootMessage; Sounds.Instance.PlaySound(Sounds.NopeSound); } } else { // should not happen now // play some sound or show message } GameState.Instance.PruneEdgesInAction = false; return(true); } TileType posType = (TileType)GameState.Instance.ActualGrid.GetCell(position); if (!TileTypesHelper.IsActive(posType)) { return(false); } switch (posType) { case TileType.ActiveBaseNode: GameState.Instance.ActualGrid.SetCell(position, (int)TileType.FilledBaseNode); GameState.Instance.FoodSpenders.Add(position); RevealCellsAround(position, GameState.Instance.CurrentStepVisionRadius); ExpandAreaAround(position); Sounds.Instance.PlaySound(Sounds.GrowSound); ProcessTurn(); return(true); case TileType.ActiveFoodNode: GameState.Instance.ActualGrid.SetCell(position, (int)TileType.FilledFoodNode); GameState.Instance.FoodSpenders.Add(position); GameState.Instance.OneTimeSources.Enqueue(position); GameState.Instance.InitialGrid.SetCell(position, (int)TileType.ActiveBaseNode); GameState.Instance.AddToEnergy(GameConstants.FoodNodeIncome); RevealCellsAround(position, GameState.Instance.CurrentStepVisionRadius); ExpandAreaAround(position); Sounds.Instance.PlaySound(Sounds.FoodSound); ProcessTurn(); return(true); case TileType.ActiveWaterNode: GameState.Instance.ActualGrid.SetCell(position, (int)TileType.FilledWaterNode); GameState.Instance.FoodSources.Add(position); RevealCellsAround(position, GameState.Instance.CurrentStepVisionRadius); ExpandAreaAround(position); Sounds.Instance.PlaySound(Sounds.WaterSound); ProcessTurn(); return(true); case TileType.ActiveRockNode: if (!GameState.Instance.CanGrowRocks) { return(false); } GameState.Instance.ActualGrid.SetCell(position, (int)TileType.FilledRockNode); GameState.Instance.FoodSpenders.Add(position); RevealCellsAround(position, GameState.Instance.CurrentStepVisionRadius); ExpandAreaAround(position); Sounds.Instance.PlaySound(Sounds.RockSound); ProcessTurn(); return(true); } return(false); }