Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Node
- {
- public int gridX;
- public int gridY;
- public Vector3Int location;
- public List<NodeLink> links;
- public class NodeLink
- {
- public int weight;
- public Node parent;
- public Node child;
- }
- #region Link Functions
- // Create a new link, connecting this Node to the Node passed in the parameter
- public void AddLink(Node _child)
- {
- if(_child != null && _child != this)
- {
- links.Add(new NodeLink
- {
- parent = this,
- child = _child,
- weight = _child.MoveCost()
- });
- if (!_child.links.Exists(a => a.parent == _child && a.child == this))
- {
- _child.AddLink(this);
- }
- }
- }
- public void SetLinks()
- {
- foreach(Node neighbor in GetNeighbors(NeighborCheckMethod.Standard))
- {
- AddLink(neighbor);
- }
- }
- public void WipeLinks()
- {
- links = new List<NodeLink>();
- }
- public void RefreshLinks()
- {
- WipeLinks();
- SetLinks();
- }
- #endregion
- #region Node Functions
- /*--Can be gone through but cannot be stopped on--*/
- public bool Walkable()
- {
- if (battleGrid.NodeGetUnitTile(location) != null)
- {
- /*--if blocker is true, walkable must be false and vice versa--*/
- return !battleGrid.NodeGetUnitTile(location).blocker;
- }
- else
- return true;
- }
- /*--Can be stopped on--*/
- public bool Travelable() {
- /*--Checks if units are in the same space--*/
- if (battleGrid.NodeGetUnitTile(location) != null)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public int MoveCost()
- {
- int baseMC = battleGrid.NodeGetBaseTile(location).baseMoveCost;
- int mod = 0;
- /*--needed for ever--*/
- if (battleGrid.NodeGetModTile(location) != null)
- {
- switch (battleGrid.NodeGetModTile(location).modType)
- {
- case Constants.TileModType.None:
- break;
- case Constants.TileModType.Blood:
- Debug.Log("Got a blood tile and you need to set a move modifier for it");
- break;
- }
- return baseMC + mod;
- }
- else
- return baseMC;
- }
- public Unit OccupyingUnit()
- {
- //Debug.Log("OccupyingUnit being called from " + gridX + "" + gridY);
- if (!Travelable())
- {
- //Debug.Log("Got this far");
- foreach (GameObject entity in battleGrid.unitCollector.allUnits)
- {
- if (entity.GetComponent<Unit>().currTile.node == this)
- {
- //Debug.Log(entity.gameObject.name);
- return entity.GetComponent<Unit>();
- }
- }
- return null;
- }
- else
- return null;
- }
- public List<Node> GetNeighbors(NeighborCheckMethod checkMethod)
- {
- List<Node> neighbors = new List<Node>();
- switch (checkMethod)
- {
- case NeighborCheckMethod.CardinalsFirst:
- for (int y = gridY - 1; y <= gridY + 1; y++)
- {
- if (y <= gridY && y >= gridY)
- {
- continue;
- }
- if (y < 0 || y > battleGrid.overallMapBounds.y - 1)
- {
- //Debug.Log("Neighbor outside map bounds through X value");
- }
- else
- {
- neighbors.Add(nodeGrid[gridX, y]);
- }
- }
- for (int x = gridX - 1; x <= gridX + 1; x++)
- {
- if (x <= gridX && x >= gridX)
- {
- continue;
- }
- if (x < 0 || x > battleGrid.overallMapBounds.x - 1)
- {
- //Debug.Log("Neighbor outside map bounds through X value");
- }
- else
- {
- neighbors.Add(nodeGrid[x, gridY]);
- }
- }
- for (int x = gridX - 1; x <= gridX + 1; x++)
- {
- for (int y = gridY - 1; y <= gridY + 1; y++)
- {
- if (x <= gridX && x >= gridX)
- {
- if (y <= gridY && y >= gridY)
- {
- continue;
- }
- }
- if (x < 0 || x > battleGrid.overallMapBounds.x - 1)
- {
- //Debug.Log("Neighbor outside map bounds through X value");
- }
- else if (y < 0 || y > battleGrid.overallMapBounds.y - 1)
- {
- //Debug.Log("Neighbor outside map bounds through Y value");
- }
- else if (!neighbors.Contains(nodeGrid[x, y]))
- {
- neighbors.Add(nodeGrid[x, y]);
- }
- }
- }
- break;
- case NeighborCheckMethod.Standard:
- /*--Checks all adjacent tiles by going through all of the nodes within 1 tile in the X and Y dimensions--*/
- for (int x = gridX - 1; x <= gridX + 1; x++)
- {
- for (int y = gridY - 1; y <= gridY + 1; y++)
- {
- if (x <= gridX && x >= gridX)
- {
- if (y <= gridY && y >= gridY)
- {
- continue;
- }
- }
- if (x < 0 || x > battleGrid.overallMapBounds.x - 1)
- {
- //Debug.Log("Neighbor outside map bounds through X value");
- }
- else if (y < 0 || y > battleGrid.overallMapBounds.y - 1)
- {
- //Debug.Log("Neighbor outside map bounds through Y value");
- }
- else
- {
- neighbors.Add(nodeGrid[x, y]);
- }
- }
- }
- break;
- }
- //foreach(Node node in neighbors)
- //{
- // Debug.Log(location + " " + node.location);
- //}
- return neighbors;
- }
- #endregion
- ///*--Used Solely in DStar--*/
- //public int pathCost;
- //public Node pathParent;
- /*--Constructor--*/
- public Node(int _gridX, int _gridY)
- {
- gridX = _gridX;
- gridY = _gridY;
- location = new Vector3Int(gridX, gridY, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement