// specify whether group units must be of same player with distinguishPlayers public List <ResUnit> GridSearchGroup(VectorHex posStart, bool distinguishPlayers = false) { List <VectorHex> visited = new List <VectorHex>(); List <VectorHex> frontier = new List <VectorHex>(); if (GetAt(posStart) == null) { return(new List <ResUnit>()); } frontier.Add(posStart); int playerAtPos = GetAt(posStart).owner.playerID; while (frontier.Count > 0) { VectorHex posCurrent = frontier[0]; frontier.RemoveAt(0); List <VectorHex> posNeighbors = GetPosNeighbors(posCurrent); foreach (VectorHex posNeighbor in posNeighbors) { if (!visited.Contains(posNeighbor) && !frontier.Contains(posNeighbor) && GetAt(posNeighbor).owner.playerID == playerAtPos) { frontier.Add(posNeighbor); } } visited.Add(posCurrent); } return(GetUnitsAt(visited)); }
// posHex - the queried position // distinguishPlayers - retreive groups of the specified player // playerQueried - the id of the specified player // ignoreOwnGroup - ignore the group the position's unit is in // invertPlayerSelection - retreive groups not of the specified player public List <UnitGroup> GetPosNeighborGroups(VectorHex posHex, bool distinguishPlayers = false, int playerQueried = 0, bool ignoreOwnGroup = true, bool invertPlayerSelection = false) { List <ResUnit> neighbors = GetNeighbors(posHex); List <UnitGroup> neighborGroups = new List <UnitGroup>(); ResUnit unitAtPos = GetAt(posHex); foreach (ResUnit neighbor in neighbors) { // reference comparison if (!neighborGroups.Contains(neighbor.group)) { if (ignoreOwnGroup && unitAtPos != null && neighbor.group == unitAtPos.group) { continue; } if (distinguishPlayers) { bool unitIsPlayers = invertPlayerSelection ^ (neighbor.owner.playerID == playerQueried); if (!unitIsPlayers) { continue; } } neighborGroups.Add(neighbor.group); } } return(neighborGroups); }
public void Initialize(Vector3 attributes, TileBehavior tile, PlayerController owner) { rend = GetComponentInChildren <Renderer>(); SetAttributes(attributes); this.tile = tile; this.posHex = tile.posHex; this.owner = owner; }
public ResUnit RemoveAt(VectorHex posHex) { VectorHex offsetPos = OffsetForGrid(posHex); ResUnit removed = grid[offsetPos.q, offsetPos.r]; grid[offsetPos.q, offsetPos.r] = null; return(removed); }
public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return(false); } VectorHex other = (VectorHex)obj; return((q == other.q) && (r == other.r)); }
public List <VectorHex> GetPosNeighbors(VectorHex posHex) { List <VectorHex> neighbors = new List <VectorHex>(); foreach (VectorHex direction in VectorHex.AXIAL_DIRECTIONS) { VectorHex neighborPos = new VectorHex(posHex.q + direction.q, posHex.r + direction.r); if (InGrid(neighborPos) && (GetAt(neighborPos) != null)) { neighbors.Add(neighborPos); } } return(neighbors); }
public void UpdateMarkers(List <VectorHex> posHighlighted) { foreach (GameObject tile in BoardManager.Instance.tiles) { TileBehavior tb = tile.GetComponentInChildren <TileBehavior>(); VectorHex offsetPos = UnitGrid.OffsetForGrid(tb.posHex); GameObject marker = markerPool[offsetPos.q, offsetPos.r]; if (posHighlighted.Contains(tb.posHex)) { marker.GetComponentInChildren <Image>().color = Color.cyan; } else { marker.GetComponentInChildren <Image>().color = Color.grey; } } }
// Remove these initialization functions later. Reduce the coupling in these start function. Possibly handle them in a dedicated initialization script? public void Initialize() { markerPool = new GameObject[UnitGrid.GRID_DIM * 2 - 1, UnitGrid.GRID_DIM * 2 - 1]; for (int i = 0; i < BoardManager.Instance.tiles.Count; i++) { GameObject tile = BoardManager.Instance.tiles[i]; Debug.Log(tile); VectorHex tilePos = tile.GetComponentInChildren <TileBehavior>().posHex; VectorHex offsetPos = UnitGrid.OffsetForGrid(tilePos); GameObject marker = Instantiate(pf_Marker, transform); markerPool[offsetPos.q, offsetPos.r] = marker; marker.SetActive(true); marker.transform.position = new Vector3(tile.transform.position.x, 1.5f, tile.transform.position.z); marker.GetComponentInChildren <Image>().color = Color.grey; marker.GetComponentInChildren <Text>().text = "" + tilePos.q + " " + tilePos.r; } }
// posStart must contain a unit public List <UnitGroup> GridSearchGroupNeighborGroups(VectorHex posStart) { List <VectorHex> visited = new List <VectorHex>(); List <VectorHex> frontier = new List <VectorHex>(); List <UnitGroup> neighbors = new List <UnitGroup>(); if (GetAt(posStart) == null) { return(neighbors); } frontier.Add(posStart); UnitGroup groupStart = GetAt(posStart).group; int playerAtPos = GetAt(posStart).owner.playerID; while (frontier.Count > 0) { VectorHex posCurrent = frontier[0]; frontier.RemoveAt(0); List <VectorHex> posNeighbors = GetPosNeighbors(posCurrent); foreach (VectorHex posNeighbor in posNeighbors) { if (!visited.Contains(posNeighbor) && !frontier.Contains(posNeighbor)) { UnitGroup groupNeighbor = GetAt(posNeighbor).group; // if the id is different, then the unit must be part of another enemy group if (GetAt(posNeighbor).owner.playerID == playerAtPos) { frontier.Add(posNeighbor); } else if (!neighbors.Contains(GetAt(posNeighbor).group)) { neighbors.Add(groupNeighbor); } } } visited.Add(posCurrent); } return(neighbors); }
// Use this for initialization void Start() { tiles = new List <GameObject>(); int boardWidth = boardSize - 1; for (int q = -boardWidth; q < boardSize; q++) { int rLowBound = -boardWidth - q; int rHighBound = boardWidth; if (q > 0) { rLowBound = -boardWidth; rHighBound = boardWidth - q; } for (int r = rLowBound; r < rHighBound + 1; r++) { VectorHex posHex = new VectorHex(q, r); Vector2 cartCoord = HexToCart(posHex); GameObject newTile = Instantiate(boardTile, board.transform); newTile.transform.position = new Vector3(cartCoord.x, 0f, cartCoord.y); TileBehavior tb = newTile.GetComponentInChildren <TileBehavior>(); tb.Initialize(posHex); tiles.Add(newTile); } } /* * for (int i = 0; i < boardSize; i++) * { * * * * } */ UIManager.Instance.Initialize(); }
// places new unit within nearby group if one exists, aggregating multiple should it bridge different groups. private void IntegrateUnit(ResUnit unit) { Debug.Log("INTEGRATING"); VectorHex posHex = unit.posHex; Debug.Log("ID: " + unit.owner.playerID); List <UnitGroup> neighborGroups = grid.GetPosNeighborGroups(posHex, distinguishPlayers: true, playerQueried: unit.owner.playerID, ignoreOwnGroup: true, invertPlayerSelection: false); // if there are no neighbors if (neighborGroups.Count < 1) { UnitGroup newGroup = new UnitGroup(unit.owner); newGroup.AddUnit(unit); allGroups.Add(newGroup); } else if (neighborGroups.Count > 1) { UnitGroup superGroup = neighborGroups[0]; for (int i = 1; i < neighborGroups.Count; i++) { superGroup.AddUnits(neighborGroups[i].units); if (allGroups.Contains(neighborGroups[i])) { allGroups.Remove(neighborGroups[i]); } } superGroup.AddUnit(unit); } else { neighborGroups[0].AddUnit(unit); } grid.PlaceUnit(unit); allUnits.Add(unit); }
public VectorHex(VectorCube vc) { VectorHex vx = vc.ToAxial(); Set(vx.q, vx.r); }
public void Initialize(VectorHex posHex) { this.posHex = posHex; }
private Vector2 HexToCart(VectorHex vx) { return(CubeToCart(new VectorCube(vx))); }
public List <ResUnit> GetNeighbors(VectorHex posHex) { return(GetUnitsAt(GetPosNeighbors(posHex))); }
public bool InGrid(VectorHex posHex) { return(Mathf.Abs(posHex.q) < GRID_DIM && Mathf.Abs(posHex.r) < GRID_DIM); }
public VectorCube(VectorHex vx) { VectorCube vc = vx.ToCube(); Set(vc.x, vc.y, vc.z); }
public ResUnit GetAt(VectorHex posHex) { VectorHex offsetPos = OffsetForGrid(posHex); return(grid[offsetPos.q, offsetPos.r]); }
public void PlaceUnit(ResUnit unit) { VectorHex offsetPos = OffsetForGrid(unit.posHex); grid[offsetPos.q, offsetPos.r] = unit; }
public static VectorHex OffsetForGrid(VectorHex posHex) { return(new VectorHex(posHex.q + GRID_DIM - 1, posHex.r + GRID_DIM - 1)); }