void CheckCellNeighbours(int x, int y, Cell.RootDirection previousPosition) { if (x < 0 || x >= cells.GetLength(0) || y < 0 || y >= cells.GetLength(1)) { return; } var cell = cells[x, y]; if (cell.InitDone) { return; } cell.InitDone = true; cell.PreviousPosition = previousPosition; List <Cell.RootDirection> directions = new List <Cell.RootDirection>(); if (x > 0 && cells[x - 1, y].ContainsRoot) { if (previousPosition != Cell.RootDirection.Left) { directions.Add(Cell.RootDirection.Left); } CheckCellNeighbours(x - 1, y, Cell.RootDirection.Right); } if (x < cells.GetLength(0) - 1 && cells[x + 1, y].ContainsRoot) { if (previousPosition != Cell.RootDirection.Right) { directions.Add(Cell.RootDirection.Right); } CheckCellNeighbours(x + 1, y, Cell.RootDirection.Left); } if (y > 0 && cells[x, y - 1].ContainsRoot) { if (previousPosition != Cell.RootDirection.Down) { directions.Add(Cell.RootDirection.Down); } CheckCellNeighbours(x, y - 1, Cell.RootDirection.Up); } if (y < cells.GetLength(1) - 1 && cells[x, y + 1].ContainsRoot) { if (previousPosition != Cell.RootDirection.Up) { directions.Add(Cell.RootDirection.Up); } CheckCellNeighbours(x, y + 1, Cell.RootDirection.Down); } cell.Direction = directions.ToArray(); cell.CheckEdge(); // Debug.Log($"directions: {directions.Count}"); }
public MoveOutcome ExecuteMove(Move move) { MoveOutcome outcome = MoveOutcome.OK; List <Cell> newCells = new List <Cell>(); List <Cell> oldCells = new List <Cell>(); // Update history with activeCells List <Cell> historyStep = new List <Cell>(activeCells); history.Add(historyStep); // playerCameraControl = false; Cell.RootDirection direction = Cell.RootDirection.Up; switch (move) { case Move.UP: direction = Cell.RootDirection.Up; break; case Move.DOWN: direction = Cell.RootDirection.Down; break; case Move.RIGHT: direction = Cell.RootDirection.Right; break; case Move.LEFT: direction = Cell.RootDirection.Left; break; } activeCells.ForEach(cell => { int newX = Mathf.RoundToInt(cell.transform.position.x); // TODO store X/Y in cell int newY = Mathf.RoundToInt(cell.transform.position.y); // TODO store X/Y in cell Cell.RootDirection previousPosition = Cell.RootDirection.Up; var cellDirection = direction; var cellMove = move; if (cell.IsInverted) { switch (direction) { case Cell.RootDirection.Left: cellDirection = Cell.RootDirection.Right; break; case Cell.RootDirection.Right: cellDirection = Cell.RootDirection.Left; break; } switch (move) { case Move.LEFT: cellMove = Move.RIGHT; break; case Move.RIGHT: cellMove = Move.LEFT; break; } } if (cell.Status != Cell.RootStatus.WATER && cell.Status != Cell.RootStatus.POISON) { switch (cellMove) { case Move.UP: newY++; break; case Move.DOWN: newY--; break; case Move.RIGHT: newX++; break; case Move.LEFT: newX--; break; } } if (newY < cell.transform.position.y) { previousPosition = Cell.RootDirection.Up; } if (newY > cell.transform.position.y) { previousPosition = Cell.RootDirection.Down; } if (newX < cell.transform.position.x) { previousPosition = Cell.RootDirection.Right; } if (newX > cell.transform.position.x) { previousPosition = Cell.RootDirection.Left; } var cellOutcome = CheckCell(newX, newY); switch (cellOutcome) { case MoveOutcome.NONE: oldCells.Add(cell); AddFx(fxRock, new Vector3(cell.transform.position.x, cell.transform.position.y, -2)); break; case MoveOutcome.DEATH: ProcessRoot(cells[newX, newY], cell, previousPosition, cellDirection); outcome = MoveOutcome.DEATH; soundLose.Play(); cells[newX, newY].Fx = AddFx(fxDeath, new Vector3(newX, newY, -1), true); newCells.Add(cells[newX, newY]); cells[newX, newY].Status = Cell.RootStatus.POISON; break; case MoveOutcome.OK: case MoveOutcome.OK_INVERT: AddFx(fxEarth, new Vector3(newX, newY, -2)); soundDig.Play(); if (cellOutcome == MoveOutcome.OK_INVERT) { soundInvert.Play(); } if (cellOutcome == MoveOutcome.OK_INVERT) { cells[newX, newY].IsInverted = !cell.IsInverted; } else { cells[newX, newY].IsInverted = cell.IsInverted; } newCells.Add(cells[newX, newY]); ProcessRoot(cells[newX, newY], cell, previousPosition, cellDirection); break; case MoveOutcome.WATER: ProcessRoot(cells[newX, newY], cell, previousPosition, cellDirection); cells[newX, newY].Fx = AddFx(fxWater, new Vector3(newX, newY + 0.7f, -2), true); soundWater.Play(); newCells.Add(cells[newX, newY]); cells[newX, newY].Status = Cell.RootStatus.WATER; waterCount--; break; } }); activeCells = oldCells; newCells.ForEach(cell => activeCells.Add(cell)); if (outcome == MoveOutcome.DEATH) { // StartCoroutine(LoseSequence()); isDead = true; return(MoveOutcome.DEATH); } // ProcessRoot(newCells, direction); if (waterCount <= 0) { StartCoroutine(WinSequence()); gameOver = true; return(MoveOutcome.WIN); } return(outcome); }
void ProcessRoot(Cell newCell, Cell oldCell, Cell.RootDirection previousPosition, Cell.RootDirection direction) { newCell.PreviousPosition = previousPosition; oldCell.Direction = new Cell.RootDirection[] { direction }; newCell.ContainsRoot = true; }