public void BuildTowerOn(TileCheck tile) { if (PlayerStats.Money < towerToBuild.cost) { Debug.Log("Need more gold!"); return; } // Builds the tower on the given tile(s) to where it "instantiates" the tower alongside deducting from the player's money PlayerStats.Money -= towerToBuild.cost; GameObject tower = (GameObject)Instantiate(towerToBuild.prefab, tile.GetBuildPosition(), Quaternion.identity); tile.tower = tower; }
public static void FillArray(int arrayWidth, int arrayHeight, TileCheck checkOp, TileOperation fillOp, Loc2D loc) { Stack <Tuple <int, int, int, Direction4, bool, bool> > stack = new Stack <Tuple <int, int, int, Direction4, bool, bool> >(); stack.Push(new Tuple <int, int, int, Direction4, bool, bool>(loc.X, loc.X, loc.Y, Direction4.None, true, true)); fillOp(loc.X, loc.Y); while (stack.Count > 0) { Tuple <int, int, int, Direction4, bool, bool> this_should_really_be_a_class = stack.Pop(); Direction4 dir = this_should_really_be_a_class.Item4; int minX = this_should_really_be_a_class.Item1; int maxX = this_should_really_be_a_class.Item2; int y = this_should_really_be_a_class.Item3; bool goLeft = this_should_really_be_a_class.Item5; bool goRight = this_should_really_be_a_class.Item6; int newMinX = minX; if (goLeft) { while (newMinX - 1 >= 0 && checkOp(newMinX - 1, y)) { newMinX--; fillOp(newMinX, y); } } int newMaxX = maxX; if (goRight) { while (newMaxX + 1 < arrayWidth && checkOp(newMaxX + 1, y)) { newMaxX++; fillOp(newMaxX, y); } } minX--; maxX++; if (y < arrayHeight - 1) { AddNextScanLine(checkOp, fillOp, minX, maxX, newMinX, newMaxX, y + 1, dir != Direction4.Up, Direction4.Down, stack); } if (y > 0) { AddNextScanLine(checkOp, fillOp, minX, maxX, newMinX, newMaxX, y - 1, dir != Direction4.Down, Direction4.Up, stack); } } }
public static void FillArray(int arrayWidth, int arrayHeight, TileCheck checkOp, TileOperation fillOp, Loc2D loc) { var stack = new Stack <StackItem>(); stack.Push(new StackItem(loc.X, loc.X, loc.Y, Direction4.None, true, true)); fillOp(loc.X, loc.Y); while (stack.Count > 0) { var item = stack.Pop(); int minX = item.MinX; int maxX = item.MaxX; int y = item.Y; Direction4 dir = item.Direction; bool goLeft = item.GoLeft; bool goRight = item.GoRight; int newMinX = minX; if (goLeft) { while (newMinX - 1 >= 0 && checkOp(newMinX - 1, y)) { newMinX--; fillOp(newMinX, y); } } int newMaxX = maxX; if (goRight) { while (newMaxX + 1 < arrayWidth && checkOp(newMaxX + 1, y)) { newMaxX++; fillOp(newMaxX, y); } } minX--; maxX++; if (y < arrayHeight - 1) { AddNextScanLine(checkOp, fillOp, minX, maxX, newMinX, newMaxX, y + 1, dir != Direction4.Up, Direction4.Down, stack); } if (y > 0) { AddNextScanLine(checkOp, fillOp, minX, maxX, newMinX, newMaxX, y - 1, dir != Direction4.Down, Direction4.Up, stack); } } }
static void AddNextScanLine(TileCheck checkOp, TileOperation fillOp, int min, int max, int range_min, int range_max, int y, bool isNext, Direction4 dir, Stack <Tuple <int, int, int, Direction4, bool, bool> > stack) { int rMinX = range_min; bool inRange = false; int x = range_min; for (; x <= range_max; x++) { //// skip testing, if testing previous line within previous range bool empty = (isNext || (x <min || x> max)) && checkOp(x, y); if (!inRange && empty) { rMinX = x; inRange = true; } else if (inRange && !empty) { stack.Push(new Tuple <int, int, int, Direction4, bool, bool>(rMinX, x - 1, y, dir, rMinX == range_min, false)); inRange = false; } if (inRange) { fillOp(x, y); } if (!isNext && x == min) { break; } } if (inRange) { stack.Push(new Tuple <int, int, int, Direction4, bool, bool>(rMinX, x - 1, y, dir, rMinX == range_min, true)); } }
void Awake() { grid = GetComponent <Grid>(); tilecheck = GetComponent <TileCheck>(); }
void Start() { grid = GetComponent <Grid>(); tileColor = GetComponent <TileColor>(); check = GetComponent <TileCheck>(); }