public void AddBlockTo(Direction dir, PuzzleBlock newBlock) //add block to one direction { LevelGrid grid = GetGrid(dir); //Get grid LevelGrid coveringGrid = newBlock.GetGrid(GetOpposeDirection(dir)); //also get covering grid SetSprayable(false, dir); //This Grid is Not Sprayable Any More! newBlock.SetSprayable(false, GetOpposeDirection(dir)); //set covering grid to not sprayable grid.SetCoveringGrid(coveringGrid); coveringGrid.SetCoveringGrid(grid); for (int i = 1; i <= 4; i++) // Change All Nearby Grid And Status { //Get near Grid and new Grid LevelGrid nearGrid = grid.GetNearGrid((Direction)i); if (nearGrid == null)//this situation only happen when processing multiple covered grid { continue; } LevelGrid newGrid = newBlock.GetSelfRelativeDirectionGrid(dir, (Direction)i); //Get height status int height = grid.GetHeight((Direction)i); //Get Relative Direction Direction nearGridRelativeDirection = GetRelativeDirection(dir, (Direction)i, height); Direction newGridRelativeDirection; //Change nearby grids status if (height < 2) { int h = nearGrid.AddHeight(nearGridRelativeDirection, 1); // Change near grid height Status newGridRelativeDirection = GetRelativeDirection(nearGrid.direction, nearGridRelativeDirection, nearGrid.GetHeight(nearGridRelativeDirection)); newGrid.SetHeight(newGridRelativeDirection, h); //Also Change new grid height status = near grid height status nearGrid.SetNearGrid(newGrid, nearGridRelativeDirection); //Set new Nearby Grid newGrid.SetNearGrid(nearGrid, newGridRelativeDirection); // Set new Nearby Grid } else//if height = 2, it means this grid is covered too. { newGridRelativeDirection = GetRelativeDirection(nearGrid.direction, nearGridRelativeDirection, nearGrid.GetHeight(nearGridRelativeDirection)); nearGrid.block.SetSprayable(false, nearGrid.direction); // Change status to not sprayable nearGrid.SetCoveringGrid(newGrid); nearGrid.SetNearGrid(null, nearGridRelativeDirection); //set to null newGrid.SetCoveringGrid(nearGrid); newGrid.SetNearGrid(null, newGridRelativeDirection); } //clear grid and covering grid connection grid.SetConnection((Direction)i, false); grid.SetNearGrid(null, (Direction)i); coveringGrid.SetNearGrid(null, (Direction)i); //also delete the connection of near grid nearGrid.SetConnection(nearGridRelativeDirection, false); nearGrid.UpdateAllVisual(0); newGrid.UpdateAllVisual(0); } grid.UpdateAllVisual(0); coveringGrid.UpdateAllVisual(0); }
public void ClearConnection() { for (int i = 1; i <= 6; i++) { DisconnectAll((Direction)i); //Get selfCoveringGrid and grid LevelGrid grid = GetGrid((Direction)i); LevelGrid selfCoveringGrid = grid.coveringGrid; if (selfCoveringGrid != null) { for (int j = 1; j <= 4; j++) { LevelGrid selfNearGrid = GetSelfRelativeDirectionGrid((Direction)i, (Direction)j); LevelGrid coveringGrid = selfNearGrid.coveringGrid; //if there is only one covering grid, just pass if (coveringGrid == null) { continue; } //first delete covering selfCoveringGrid.SetCoveringGrid(null); coveringGrid.SetCoveringGrid(null); //Set Height and Near grid Direction CoveringGridRelativeDirection = GetRelativeDirection((Direction)i, GetOpposeDirection((Direction)j), 0); coveringGrid.SetNearGrid(selfCoveringGrid, CoveringGridRelativeDirection); coveringGrid.SetHeight(CoveringGridRelativeDirection, 2); Direction SelfCoveringGridRelativeDirection = GetRelativeDirection(coveringGrid.direction, CoveringGridRelativeDirection, 2); selfCoveringGrid.SetNearGrid(coveringGrid, SelfCoveringGridRelativeDirection); selfCoveringGrid.SetHeight(SelfCoveringGridRelativeDirection, 2); } } else { for (int j = 1; j <= 4; j++) { //Get height Status int height = grid.GetHeight((Direction)j); //if self connecting, continue if (height == 0) { continue; } //Get covering grid if there is one LevelGrid selfNearGrid = GetSelfRelativeDirectionGrid((Direction)i, (Direction)j); LevelGrid coveringGrid = selfNearGrid.coveringGrid; //Get Nearby Grid LevelGrid nearGrid = grid.GetNearGrid((Direction)j); Direction nearGridRelativeDirection = GetRelativeDirection((Direction)i, (Direction)j, height); if (coveringGrid != null) //if it is covering a grid, then this would be new nearby grid { coveringGrid.SetCoveringGrid(null); //first delete self nearGrid.AddHeight(nearGridRelativeDirection, -1); //lower the height nearGrid.SetNearGrid(coveringGrid, nearGridRelativeDirection); //Set new Near grid Direction newGridRelativeDirection = GetRelativeDirection(nearGrid.direction, nearGridRelativeDirection, nearGrid.GetHeight(nearGridRelativeDirection)); coveringGrid.SetNearGrid(nearGrid, newGridRelativeDirection); coveringGrid.SetHeight(newGridRelativeDirection, nearGrid.GetHeight(nearGridRelativeDirection)); Debug.Log($"Set CoveringGrid:{coveringGrid.direction} and NearbyGrid:{nearGrid.direction}, height:{nearGrid.GetHeight(nearGridRelativeDirection)}"); } else//if there is no such grid,then there must be one connect to self nearby grid { LevelGrid newGrid = selfNearGrid.GetNearGrid(GetRelativeDirection((Direction)i, (Direction)j, 0)); nearGrid.SetHeight(nearGridRelativeDirection, 0);//set to the lowest nearGrid.SetNearGrid(newGrid, nearGridRelativeDirection); Direction newGridRelativeDirection = GetRelativeDirection(nearGrid.direction, nearGridRelativeDirection, 0); newGrid.SetNearGrid(nearGrid, newGridRelativeDirection); newGrid.SetHeight(newGridRelativeDirection, 0); Debug.Log($"Set NewGrid:{newGrid.direction} and NearbyGrid:{nearGrid.direction}, height:{nearGrid.GetHeight(nearGridRelativeDirection)}"); } } } } for (int i = 1; i <= 6; i++) { LevelGrid grid = GetGrid((Direction)i); LevelGrid selfCoveringGrid = grid.coveringGrid; if (selfCoveringGrid != null) { selfCoveringGrid.UpdateAllVisual(1); } } }