// To search for free space around of ShipStatioN public void SearchEmptyNeighborsForSoldier() { ShipStationNeighbor.Clear(); int neighborCount = 2 * (width + height) + 4; Cordinat tempCenter = new Cordinat(centerCordinat.x, centerCordinat.y); for (int i = 0; i <= neighborCount; i++) { Cordinat temp = new Cordinat(tempCenter.x - 2, tempCenter.y - 2); if (temp.x >= 0 && temp.y >= 0) { if (GridController.m.GetGridIsEmpty(temp)) { ShipStationNeighbor.Add(temp); } } if (i < 4) { tempCenter.x++; } else if (i < 8) { tempCenter.y++; } else if (i < 12) { tempCenter.x--; } else { tempCenter.y--; } } }
// Coordinate calculation from position public Cordinat GetCordinatFromWorldPosition(Vector2 position) { Vector2 relativePosition = (newCenterPosition - position) * -1f; relativePosition = new Vector2(relativePosition.x, relativePosition.y * -1f); if (relativePosition.x < 0 || relativePosition.y < 0 || relativePosition.x > (columnSize * pixelSize) || relativePosition.y > (rowSize * pixelSize)) { return(null); } Vector2 snapPosition = new Vector2(relativePosition.x - (pixelSize / 2f), relativePosition.y - (pixelSize / 2f)); Cordinat cord = new Cordinat((int)(snapPosition.x / pixelSize), (int)(snapPosition.y / pixelSize)); if (snapPosition.x % pixelSize > (pixelSize / 2f)) { cord.x++; } if (snapPosition.y % pixelSize > (pixelSize / 2f)) { cord.y++; } return(cord); }
public void CheckArea(Cordinat pivotIndex, byte[,] areaMap, Vector2 position, float pixelSize) { if (centerCordinat.x <= pivotIndex.x || centerCordinat.y <= pivotIndex.y) { int minX, maxX; int minY, maxY; CalcMinMaxLimits(width, pivotIndex.x, out minX, out maxX); CalcMinMaxLimits(height, pivotIndex.y, out minY, out maxY); isForStay = true; List <Cordinat> buildArea = GetTargetAreaPoints(minX, maxX, minY, maxY); areaCordinats.ForEach(cord => areaMap[cord.x, cord.y] = 0); foreach (Cordinat cord in buildArea) { if (areaMap[cord.x, cord.y] == 1) { isForStay = false; break; } } if (isForStay) { areaCordinats = buildArea; spriteRenderer.color = Color.white; } else { spriteRenderer.color = Color.red; } } }
//Creation of the empty space of the castle public void SearchEmptyInCastle() { if (centerCordinat != null) { inCastle.Clear(); inCastle.Add(centerCordinat); GridController.m.gridArray[centerCordinat.x, centerCordinat.y] = 0; Cordinat tempCenter = new Cordinat(centerCordinat.x, centerCordinat.y); for (int i = 0; i < 9; i++) { Cordinat temp = new Cordinat(tempCenter.x - 1, tempCenter.y - 1); if (temp.x >= 0 && temp.y >= 0) { inCastle.Add(temp); GridController.m.gridArray[temp.x, temp.y] = 0; Debug.Log(temp.x + "x" + temp.y + "=" + GridController.m.gridArray[temp.x, temp.y]); } if (i < 2) { tempCenter.x++; if (i == 1) { GridController.m.gridArray[temp.x, temp.y - 1] = 0; } } else if (i < 4) { tempCenter.y++; if (i == 3) { GridController.m.gridArray[temp.x + 1, temp.y] = 0; } } else if (i < 6) { tempCenter.x--; if (i == 5) { GridController.m.gridArray[temp.x, temp.y + 1] = 0; } } else { tempCenter.y--; if (i == 7) { GridController.m.gridArray[temp.x - 1, temp.y] = 0; } } } } }
public Node(float needMoveValue, float heurosticValue, Vector2 position, Cordinat gridCordinat, Node parent) { this.needMoveValue = needMoveValue; this.heurosticValue = heurosticValue; this.totalValue = needMoveValue + heurosticValue; this.position = position; this.gridCordinat = gridCordinat; this.parent = parent; }
//Position calculation from Coordinate public Vector2 GetGridPositionFromCordinat(Cordinat cord) { Vector2 finalPosition = new Vector2((pixelSize * cord.x) + (pixelSize / 2f), (pixelSize * cord.y) + (pixelSize / 2f)); finalPosition = new Vector2(finalPosition.x, finalPosition.y * -1f); finalPosition *= -1f; finalPosition = newCenterPosition - finalPosition; return(finalPosition); }
public bool Isquals(Cordinat target) { if (target.x == x && target.y == y) { return(true); } else { return(false); } }
public void SnapPosition(Vector2 mousePosition) { mousePosition = Camera.main.ScreenToWorldPoint(mousePosition); centerCordinat = GridController.m.GetCordinatFromWorldPosition(mousePosition); if (centerCordinat != null) { Vector2 finalPosition = GridController.m.GetGridPositionFromCordinat(centerCordinat); CheckArea(centerCordinat, GridController.m.gridArray, finalPosition, GridController.m.pixelSize); SetPosition(finalPosition, GridController.m.pixelSize); } else { transform.position = mousePosition; isForStay = false; } }
public List <Cordinat> GetNeighbors() { List <Cordinat> neighbors = new List <Cordinat>(); Cordinat current = new Cordinat(x - 1, y - 1); for (int i = 0; i < 8; i++) { if (current.x - 1 >= 0 && current.y - 1 >= 0 && current.x < GridController.m.columnSize && current.y < GridController.m.rowSize) { if (GridController.m.GetGridIsEmpty(current)) { neighbors.Add(current); } } current = new Cordinat(current.x, current.y); if (i < 2) { current.x++; } else if (i < 4) { current.y++; } else if (i < 6) { current.x--; } else { current.y--; } } return(neighbors); }
public void SetCordinat(Cordinat cord) { soldierCord = cord; GridController.m.gridArray[soldierCord.x, soldierCord.y] = 1; }
public static Vector2[] SearchPath(Vector2 currentPosition, Vector2 targetPosition) { targetPosition = Camera.main.ScreenToWorldPoint(targetPosition); Cordinat targetCordinat = GridController.m.GetCordinatFromWorldPosition(targetPosition); if (!GridController.m.GetGridIsEmpty(targetCordinat)) { return(null); } targetPosition = GridController.m.GetGridPositionFromCordinat(targetCordinat); GridController.m.ShowTargetIcon(targetPosition); orderNodeList.Clear(); Cordinat cord = GridController.m.GetCordinatFromWorldPosition(currentPosition); Node currentNode = new Node(0, Vector2.Distance(currentPosition, targetPosition), currentPosition, cord, null); while (true) { if (currentNode.position == targetPosition) { break; } currentNode.isVisited = true; currentNode.gridCordinat.GetNeighbors().ForEach(c => { Vector2 neighborsNodePosition = GridController.m.GetGridPositionFromCordinat(c); Node neighborsNode = new Node(currentNode.needMoveValue + (Vector2.Distance(currentNode.position, neighborsNodePosition) / 2f) , Vector2.Distance(neighborsNodePosition, targetPosition), neighborsNodePosition, c, currentNode); AddAndOrder(neighborsNode); }); currentNode = null; for (int i = 0; i < orderNodeList.Count; i++) { if (!orderNodeList[i].isVisited) { currentNode = orderNodeList[i]; break; } } if (currentNode == null) { break; } } List <Node> reverseNodes = new List <Node>(); while (true) { reverseNodes.Add(currentNode); if (currentNode.parent == null) { break; } currentNode = currentNode.parent; } reverseNodes.Reverse(); Vector2[] finalPosition = new Vector2[reverseNodes.Count]; for (int i = 0; i < finalPosition.Length; i++) { finalPosition[i] = reverseNodes[i].position; } return(finalPosition); }
public bool GetGridIsEmpty(Cordinat c) { return(gridArray[c.x, c.y] == 0 ? true : false); }