public void ChangeTile(HaxNode hax) { if (m_Node != null) { m_Node.SetUnit(null); } m_Node = hax; m_Node.SetUnit(this); SetPosition(); }
public HaxNode[] ShowAttackable(HaxNode hax, int movePower) { ResetSelect(); HaxNode[] reachable = BreadthFirstAlgorithms.FindReachable(hax, movePower); for (int i = 0; i < reachable.Length; i++) { reachable[i].m_SpriteRenderer.color = Color.red; } return(reachable); }
public HaxNode[] ShowPath(HaxNode Start, HaxNode Dest) { ResetSelect(); HaxNode[] path = AStarAlgorithms.GetPath(Start, Dest); for (int i = 0; i < path.Length; i++) { path[i].m_SpriteRenderer.color = new Color(path[i].m_SpriteRenderer.color.r * m_SelectFactor, path[i].m_SpriteRenderer.color.g * m_SelectFactor, path[i].m_SpriteRenderer.color.b * m_SelectFactor); } return(path); }
public void Update() { if (Input.GetMouseButtonDown(0)) { GameObject hax = Grid.instance.GetHaxClicked(); if (hax != null) { HaxNode haxNode = hax.GetComponent <HaxNode>(); m_RefObject.SwitchState(new ShowReachAble(m_RefObject, haxNode)); } } }
static private void CreatPath(List <HaxNode> path, HaxNode nodetogetparant) { if (nodetogetparant.AstarProperties.Parent == null) { return; } else { path.Add(nodetogetparant.AstarProperties.Parent); } CreatPath(path, nodetogetparant.AstarProperties.Parent); }
static public HaxNode[] FindReachable(HaxNode start, int maxMovement) { List <HaxNode> open = new List <HaxNode>(); List <HaxNode> visited = new List <HaxNode>(); open.Add(start); visited.Add(start); start.BreadthFirstProperties.DistanceMoved = 0; int SavetyLoopCount = 0; while (open.Count > 0) { HaxNode[] neighbors = Grid.instance.GetNeighborsHax(open[0]).ToArray(); for (int i = 0; i < neighbors.Length; i++) { if (open[0].BreadthFirstProperties.DistanceMoved + neighbors[i].MoveCost >= neighbors[i].BreadthFirstProperties.DistanceMoved || open[0].BreadthFirstProperties.DistanceMoved + neighbors[i].MoveCost > maxMovement) { continue; } neighbors[i].BreadthFirstProperties.DistanceMoved = open[0].BreadthFirstProperties.DistanceMoved + neighbors[i].MoveCost; neighbors[i].BreadthFirstProperties.Parant = open[0]; if (neighbors[i].IsWalkable) { if (!visited.Contains(neighbors[i])) { visited.Add(neighbors[i]); } if (!open.Contains(neighbors[i])) { open.Add(neighbors[i]); } } } open.RemoveAt(0); SavetyLoopCount++; if (SavetyLoopCount > 1000) { break; } } HexNodeCleanUp(visited); return(visited.ToArray()); }
public void Update() { if (Input.GetMouseButtonDown(0)) { GameObject hax = Grid.instance.GetHaxClicked(); if (hax != null) { HaxNode haxNode = hax.GetComponent <HaxNode>(); if (ArrayUtility.Contains(m_Reachable, haxNode)) { m_RefObject.SwitchState(new ShowPath(m_RefObject, m_SelectedNode, haxNode)); } } } }
// Update is called once per frame void Update() { //if (Input.GetKeyDown(KeyCode.Alpha1)) // if (Grid.instance.GetHaxClicked() != null) // A = Grid.instance.GetHaxClicked().GetComponent<HaxNode>(); //if (Input.GetKeyDown(KeyCode.Alpha2)) // if(Grid.instance.GetHaxClicked()!= null) // B = Grid.instance.GetHaxClicked().GetComponent<HaxNode>(); //if (Input.GetKeyDown(KeyCode.K)) //{ // if (A != null && B != null) // { // HaxNode[] path = AStarAlgorithms.GetPath(A, B); // Grid.instance.ShowPath(A,B); // } //} if (Input.GetMouseButton(0)) { GameObject hax = Grid.instance.GetHaxClicked(); if (hax != null) { HaxNode haxNode = hax.GetComponent <HaxNode>(); haxNode.ChangeWalkableState(m_HaxType); } } //if (Input.GetMouseButtonDown(1)) //{ // GameObject hax = Grid.instance.GetHaxClicked(); // if (hax != null) // { // HaxNode haxNode = hax.GetComponent<HaxNode>(); // Grid.instance.ShowPath(Grid.instance.m_Nodes[0, 0], haxNode); // } //} //if (Input.GetMouseButtonDown(2)) //{ // GameObject hax = Grid.instance.GetHaxClicked(); // if (hax != null) // { // HaxNode haxNode = hax.GetComponent<HaxNode>(); // Grid.instance.ShowReachable(haxNode, ReachableAmount); // } //} }
static private HaxNode[] RetracePath(HaxNode start, HaxNode destination) { List <HaxNode> path = new List <HaxNode>(); HaxNode currentNode = destination; while (currentNode != start) { path.Add(currentNode); currentNode = currentNode.AstarProperties.Parent; } path.Reverse(); return(path.ToArray()); }
public List <HaxNode> GetNeighborsHax(HaxNode haxnode) { Vector2Int[] neighborsPositions = { HaxCoordAlgorithms.CubeToOddr(new Vector3Int(haxnode.HaxCoordinate.CubeCoordinate.x + 1, haxnode.HaxCoordinate.CubeCoordinate.y - 1, haxnode.HaxCoordinate.CubeCoordinate.z)), HaxCoordAlgorithms.CubeToOddr(new Vector3Int(haxnode.HaxCoordinate.CubeCoordinate.x, haxnode.HaxCoordinate.CubeCoordinate.y - 1, haxnode.HaxCoordinate.CubeCoordinate.z + 1)), HaxCoordAlgorithms.CubeToOddr(new Vector3Int(haxnode.HaxCoordinate.CubeCoordinate.x - 1, haxnode.HaxCoordinate.CubeCoordinate.y, haxnode.HaxCoordinate.CubeCoordinate.z + 1)), HaxCoordAlgorithms.CubeToOddr(new Vector3Int(haxnode.HaxCoordinate.CubeCoordinate.x - 1, haxnode.HaxCoordinate.CubeCoordinate.y + 1, haxnode.HaxCoordinate.CubeCoordinate.z)), HaxCoordAlgorithms.CubeToOddr(new Vector3Int(haxnode.HaxCoordinate.CubeCoordinate.x, haxnode.HaxCoordinate.CubeCoordinate.y + 1, haxnode.HaxCoordinate.CubeCoordinate.z - 1)), HaxCoordAlgorithms.CubeToOddr(new Vector3Int(haxnode.HaxCoordinate.CubeCoordinate.x + 1, haxnode.HaxCoordinate.CubeCoordinate.y, haxnode.HaxCoordinate.CubeCoordinate.z - 1)) }; List <HaxNode> neighbors = new List <HaxNode>(); for (int i = 0; i < neighborsPositions.Length; i++) { if (IsIndexValid(m_Nodes, neighborsPositions[i].x, neighborsPositions[i].y)) { neighbors.Add(m_Nodes[neighborsPositions[i].x, neighborsPositions[i].y]); } } return(neighbors); }
public int GetDistanceHax(HaxNode a, HaxNode b) { return(Mathf.RoundToInt((Mathf.Abs(a.HaxCoordinate.CubeCoordinate.x - b.HaxCoordinate.CubeCoordinate.x) + Mathf.Abs(a.HaxCoordinate.CubeCoordinate.y - b.HaxCoordinate.CubeCoordinate.y) + Mathf.Abs(a.HaxCoordinate.CubeCoordinate.z - b.HaxCoordinate.CubeCoordinate.z)) * .5f)); }
public ShowPath(GameManager refObject, HaxNode prePos, HaxNode moveToHax) { m_RefObject = refObject; m_MoveToHax = moveToHax; PrePos = prePos; }
public ShowReachAble(GameManager refObject, HaxNode node) { m_RefObject = refObject; m_SelectedNode = node; }
static public HaxNode[] GetPatsh(HaxNode start, HaxNode destination) { //G = is the movement cost from the start point A to the current square. //H = is the estimated movement cost from the current square to the destination point (heuristic) //F = G+H List <HaxNode> open = new List <HaxNode>(); List <HaxNode> closed = new List <HaxNode>(); open.Add(start); while (open.Count > 0) { HaxNode currentNode = open[0]; for (int i = 1; i < open.Count; i++) { if (open[i].AstarProperties.FCost < currentNode.AstarProperties.FCost || open[i].AstarProperties.FCost == currentNode.AstarProperties.FCost) { if (open[i].AstarProperties.HCost < currentNode.AstarProperties.HCost) { currentNode = open[i]; } } } open.Remove(currentNode); closed.Add(currentNode); if (currentNode == destination) { return(RetracePath(start, destination)); } foreach (HaxNode neighbour in Grid.instance.GetNeighborsHax(currentNode)) { if (closed.Contains(neighbour) || !neighbour.IsWalkable) { continue; } int newCostToNeighbour = currentNode.AstarProperties.GCost + Grid.instance.GetDistanceHax(currentNode, neighbour); if (newCostToNeighbour >= neighbour.AstarProperties.GCost && open.Contains(neighbour)) { continue; } neighbour.AstarProperties.GCost = newCostToNeighbour; neighbour.AstarProperties.HCost = Grid.instance.GetDistanceHax(neighbour, destination); neighbour.AstarProperties.Parent = currentNode; if (!open.Contains(neighbour)) { open.Add(neighbour); } } } return(null); }
static public HaxNode[] GetPath(HaxNode start, HaxNode destination) { List <HaxNode> open = new List <HaxNode>(); List <HaxNode> closed = new List <HaxNode>(); start.AstarProperties.GCost = 0; start.AstarProperties.GCost = Grid.instance.GetDistanceHax(start, destination); open.Add(start); do { int currentNodeIndex = 0; int lowestFCost = int.MaxValue; HaxNode currentNode = null; for (int i = 0; i < open.Count; i++) { if (open[i].AstarProperties.FCost < lowestFCost) { lowestFCost = open[i].AstarProperties.FCost; currentNodeIndex = i; currentNode = open[i]; } } open.RemoveAt(currentNodeIndex); closed.Add(currentNode); foreach (HaxNode neighbor in Grid.instance.GetNeighborsHax(currentNode).ToArray()) { if (!neighbor.IsWalkable || closed.Contains(neighbor)) { continue; } neighbor.AstarProperties.HCost = Grid.instance.GetDistanceHax(neighbor, destination); int newCostToNeighbour = currentNode.AstarProperties.GCost + neighbor.MoveCost; if (newCostToNeighbour < neighbor.AstarProperties.GCost || !open.Contains(neighbor) /*|| currentNode.AstarProperties.GCost + neighbor.MoveCost < neighbor.AstarProperties.GCost*/) { neighbor.AstarProperties.Parent = currentNode; neighbor.AstarProperties.GCost = currentNode.AstarProperties.GCost + neighbor.MoveCost; if (!open.Contains(neighbor)) { open.Add(neighbor); } } } if (currentNode == destination) { List <HaxNode> path = new List <HaxNode>(); path.Add(currentNode); CreatPath(path, currentNode); ClearNodes(); return(path.ToArray()); } }while (open.Count > 0); ClearNodes(); return(new HaxNode[0]); }
public void Clear() { GCost = int.MaxValue; HCost = 0; Parent = null; }