//returns the number of tiles that were toggled. private bool ToggleAdjacent(GridTile clicked, Direction direction) { GridTile adjacent = clicked.GetAdjacentTile(direction); if (adjacent == null) { return(false); } if (adjacent.state == TileState.NULL) { return(false); } //depending on current direction, if tile doesn't contain opposite direction, then break //since it doesn't connect switch (direction) { case Direction.EAST: if (!adjacent.directions.Contains(Direction.WEST)) { return(false); } break; case Direction.NORTH: if (!adjacent.directions.Contains(Direction.SOUTH)) { return(false); } break; case Direction.WEST: if (!adjacent.directions.Contains(Direction.EAST)) { return(false); } break; case Direction.SOUTH: if (!adjacent.directions.Contains(Direction.NORTH)) { return(false); } break; } //when any tile turns off, we need to collapse ALL the connections between it and any other tile. adjacent.Toggle(); //fire bullet between clicked and adjacent. //state of clicked tile won't update until after loop finishes (since toggling state of clicked conditonal //on toggling state of at least one other grid tile) so we just preview the next state here. //(i.e., we know that clicked will toggle state, since if we're here then clicked toggled something) TileState clickedTileNextState = clicked.state == TileState.OFF ? TileState.ON : TileState.OFF; Bullet fired = null; if (adjacent.state == TileState.ON) { fired = Bullet.FireBulletFromTo(clicked.gameObject, adjacent.gameObject); } if (adjacent.state == TileState.OFF && clickedTileNextState == TileState.ON) { fired = Bullet.FireBulletFromTo(adjacent.gameObject, clicked.gameObject); } if (fired != null) { fired.transform.position += (fired.Trajectory * clicked.GetComponent <SpriteRenderer>().size.x / 2); } return(true); }