public void CheckMovementPath() { if (movementPath.Count > 0 && walking == true) { float value = 0.4f; Gridtile gTile = movementPath[0]; Vector3 gTilePosition = gTile.transform.position; Vector3 targettedPosition = new Vector3(gTilePosition.x, gTilePosition.y + transform.GetComponent <EnemyController>().addedYPosition, gTilePosition.z); Vector3 currentPosition = new Vector3(Mathf.Clamp(transform.position.x, transform.position.x - value, transform.position.x + value), Mathf.Clamp(transform.position.y, transform.position.y - value, transform.position.y + value), Mathf.Clamp(transform.position.z, transform.position.z - value, transform.position.z + value)); transform.position = Vector3.MoveTowards(transform.position, new Vector3(targettedPosition.x, targettedPosition.y, targettedPosition.z), movementSpeed * Time.deltaTime); transform.LookAt(new Vector3(gTilePosition.x, transform.position.y, gTilePosition.z)); if (currentPosition == targettedPosition) { movementPath.RemoveAt(0); } if (movementPath.Count == 0) { GameObject.Find(Constants.NAME_FRIENDLY_BASE).GetComponent <Health>().TakeDamage(gameObject.GetComponent <EnemyController>().damage); gameObject.GetComponent <Health>().TakeDamage(1000); } } }
private static bool isDiagonal(Gridtile center, Gridtile neighbour) { if (center.transform.position.x != neighbour.transform.position.x && center.transform.position.z != center.transform.position.z) { return(true); } return(false); }
private static int sortOnF(Gridtile a, Gridtile b) { if (a.f > b.f || a.f == b.f && a.h > b.h) { return(1); } else { return(-1); } }
public static Gridtile getGridTile(Vector3 position) { Gridtile result = null; for (int tileCount = 0; tileCount < gridTiles.Count; tileCount++) { if (gridTiles[tileCount].transform.position == new Vector3(position.x, gridTiles[tileCount].transform.position.y, position.z)) { result = gridTiles[tileCount]; } } return(result); }
void spawnTile(Vector3 location, int type) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.parent = this.transform; cube.transform.name = Constants.NAME_TILE; Gridtile tileComp = cube.AddComponent <Gridtile>(); tileComp.type = (Gridtile.TileType)type; gridTiles.Add(tileComp); cube.GetComponent <Renderer>().material = tileMaterial; cube.transform.position = location; }
private static List <Gridtile> getPathToTile(Gridtile tile) { List <Gridtile> path = new List <Gridtile>(); while (tile.parent) { if (!path.Contains(tile)) { path.Add(tile); tile = tile.parent; } } path.Reverse(); return(path); }
public static List <Gridtile> Search(Gridtile start, Gridtile end) { if (start && end) { Gridsystem.ResetAllGridTiles(); List <Gridtile> openList = new List <Gridtile>(); List <Gridtile> closedList = new List <Gridtile>(); Gridtile currentTile; Gridtile neighbour; float gScore; bool gScoreIsBest; openList.Add(start); while (openList.Count > 0) { if (openList[0] != null) { openList.Sort(sortOnF); currentTile = openList[0]; if (currentTile == end) { return(getPathToTile(currentTile)); } openList.Remove(currentTile); closedList.Add(currentTile); currentTile.closed = true; currentTile.open = false; for (int tileCount = 0; tileCount < currentTile.neighbours.Count; tileCount++) { neighbour = currentTile.neighbours[tileCount]; if (neighbour.closed && neighbour.type == Gridtile.TileType.FRIENDLY || neighbour.closed && neighbour.type == Gridtile.TileType.ENEMY || neighbour.closed && neighbour.type == Gridtile.TileType.FORBIDDEN) { continue; } if (isDiagonal(currentTile, neighbour)) { gScore = currentTile.g + diagonalScore; } else { gScore = currentTile.g + horizontalScore; } gScoreIsBest = false; if (!neighbour.open && !neighbour.closed && neighbour.type == Gridtile.TileType.PATH || !neighbour.open && !neighbour.closed && neighbour.type == Gridtile.TileType.ENDPATH || !neighbour.open && !neighbour.closed && neighbour.type == Gridtile.TileType.STARTPATH) { gScoreIsBest = true; neighbour.h = heuristic(neighbour.transform.position, end.transform.position); openList.Add(neighbour); neighbour.open = true; } else if (gScore < neighbour.g) { gScoreIsBest = true; } if (gScoreIsBest) { neighbour.parent = currentTile; neighbour.g = gScore; neighbour.f = neighbour.g + neighbour.h; } } } else { openList.RemoveAt(0); } } } return(new List <Gridtile>()); }
public static List<Gridtile> Search(Gridsystem grid, Gridtile start, Gridtile end) { if (start && end && end.onPerspective) { Gridsystem.ResetAllGridTiles(); start.GetComponent<Renderer>().material.color = Color.yellow; end.GetComponent<Renderer>().material.color = Color.red; List<Gridtile> openList = new List<Gridtile>(); List<Gridtile> closedList = new List<Gridtile>(); Gridtile currentTile; Gridtile neighbour; float gScore; bool gScoreIsBest; openList.Add(start); while(openList.Count > 0) { if(openList[0] != null) { openList.Sort(sortOnF); currentTile = openList[0]; if(currentTile == end) { return getPathToTile(currentTile); } openList.Remove(currentTile); closedList.Add(currentTile); currentTile.closed = true; currentTile.open = false; for (int tileCount = 0; tileCount < currentTile.neighbours.Count; tileCount++) { neighbour = currentTile.neighbours[tileCount]; if (neighbour.closed && !neighbour.onPerspective) { continue; } if (isDiagonal(currentTile, neighbour)) { gScore = currentTile.g + diagonalScore; } else { gScore = currentTile.g + horizontalScore; } gScoreIsBest = false; if(!neighbour.open && !neighbour.closed) { gScoreIsBest = true; neighbour.h = heuristic(neighbour.transform.position, end.transform.position); openList.Add(neighbour); neighbour.open = true; if (neighbour != end) neighbour.GetComponent<Renderer>().material.color = Color.green; } else if (gScore < neighbour.g) { gScoreIsBest = true; } if(gScoreIsBest) { neighbour.parent = currentTile; neighbour.g = gScore; neighbour.f = neighbour.g + neighbour.h; } } } else { openList.RemoveAt(0); } } } return new List<Gridtile>(); }
private static int sortOnF(Gridtile a, Gridtile b) { if (a.f > b.f || a.f == b.f && a.h > b.h) { return 1; } else { return -1; } }
private static bool isDiagonal(Gridtile center, Gridtile neighbour) { if (center.transform.position.x != neighbour.transform.position.x && center.transform.position.z != center.transform.position.z) { return true; } return false; }
private static List<Gridtile> getPathToTile(Gridtile tile) { List<Gridtile> path = new List<Gridtile>(); while (tile.parent) { if (!path.Contains(tile)) { path.Add(tile); tile = tile.parent; } } path.Reverse(); return path; }
public static List<Gridtile> Search(Gridtile start, Gridtile end) { if (start && end) { Gridsystem.ResetAllGridTiles(); List<Gridtile> openList = new List<Gridtile>(); List<Gridtile> closedList = new List<Gridtile>(); Gridtile currentTile; Gridtile neighbour; float gScore; bool gScoreIsBest; openList.Add(start); while (openList.Count > 0) { if (openList[0] != null) { openList.Sort(sortOnF); currentTile = openList[0]; if (currentTile == end) { return getPathToTile(currentTile); } openList.Remove(currentTile); closedList.Add(currentTile); currentTile.closed = true; currentTile.open = false; for (int tileCount = 0; tileCount < currentTile.neighbours.Count; tileCount++) { neighbour = currentTile.neighbours[tileCount]; if (neighbour.closed && neighbour.type == Gridtile.TileType.FRIENDLY || neighbour.closed && neighbour.type == Gridtile.TileType.ENEMY || neighbour.closed && neighbour.type == Gridtile.TileType.FORBIDDEN) { continue; } if (isDiagonal(currentTile, neighbour)) { gScore = currentTile.g + diagonalScore; } else { gScore = currentTile.g + horizontalScore; } gScoreIsBest = false; if (!neighbour.open && !neighbour.closed && neighbour.type == Gridtile.TileType.PATH || !neighbour.open && !neighbour.closed && neighbour.type == Gridtile.TileType.ENDPATH || !neighbour.open && !neighbour.closed && neighbour.type == Gridtile.TileType.STARTPATH) { gScoreIsBest = true; neighbour.h = heuristic(neighbour.transform.position, end.transform.position); openList.Add(neighbour); neighbour.open = true; } else if (gScore < neighbour.g) { gScoreIsBest = true; } if (gScoreIsBest) { neighbour.parent = currentTile; neighbour.g = gScore; neighbour.f = neighbour.g + neighbour.h; } } } else { openList.RemoveAt(0); } } } return new List<Gridtile>(); }