//TEMP DEBUG void drawGrid(SqaureGrid grid, AStar astar, LinkedList <Location> route) { for (var y = 0; y <= width; y++) { for (var x = 0; x <= height; x++) { Location currentLocation = new Location(x, y); Location locationPtr = currentLocation; if (!astar.cameFrom.TryGetValue(currentLocation, out locationPtr)) { locationPtr = currentLocation; } //show path if (route.Contains(currentLocation)) { Transform wallPart = (Transform)Instantiate(pathNode, new Vector3(x, 0, y), Quaternion.identity); wallPart.parent = this.transform.parent; } else if (grid.walls.Contains(currentLocation)) { //show wall at current pos //Transform wallPart = (Transform)Instantiate(wallNode, new Vector3(x, 0, y), Quaternion.identity); //wallPart.parent = this.transform.parent; } } } }
public EnemyState(Enemy_Controller enemyController) { enemy = enemyController; grid = GameObject.FindGameObjectWithTag(Tags.World).GetComponent <World>().grid; player = GameObject.FindGameObjectWithTag(Tags.Player); route = new LinkedList <Location>(); }
public LinkedList <Location> optimiseRoute(SqaureGrid grid, AStar astar, LinkedList <Location> path) { LinkedList <Location> optimisedPath = new LinkedList <Location>(); LinkedListNode <Location> currentNode = path.First; bool isNodeRemoved = true; while (currentNode != path.Last) { LinkedListNode <Location> nextNode = currentNode.Next; if (nextNode != null) { if (!grid.forests.Contains(currentNode.Value) && isInLineOfSight(currentNode.Value, nextNode.Value)) { path.Remove(nextNode); Debug.Log("REMOVING NODE"); isNodeRemoved = true; } if (!isNodeRemoved) { currentNode = currentNode.Next; isNodeRemoved = false; } } else { break; } } return(optimisedPath); }
void Start() { drawBorder(); grid = new SqaureGrid(width, height); assembleForests(grid); assembleWalls(grid); assembleRoads(grid); }
void assembleRoads(SqaureGrid grid) { foreach (Vector2 road in roads) { Location currentLocation = new Location((int)road.x, (int)road.y); if (!grid.walls.Contains(currentLocation)) { grid.roads.Add(new Location((int)road.x, (int)road.y)); } } }
void assembleForests(SqaureGrid grid) { foreach (Vector2 forest in forests) { Location currentLocation = new Location((int)forest.x, (int)forest.y); if (!grid.walls.Contains(currentLocation)) { grid.forests.Add(new Location((int)forest.x, (int)forest.y)); } } }
// Use this for initialization void Start () { var grid = new SqaureGrid(width, height); assembleWalls(grid); assembleRoads(grid); assembleForests(grid); //randomise start and destination if (randomGen) { bool isGenerated = false; int randStartX = (int)startVec.x; int randStartY = (int)startVec.y; while (!isGenerated) { if (!isSeamless) { randStartX = Random.Range(0, width); randStartY = Random.Range(0, height); } int randDestX = Random.Range(1, width); int randDestY = Random.Range(1, height); if(!grid.walls.Contains(new Location(randStartX, randStartY)) && !grid.walls.Contains(new Location(randDestX, randDestY))) { startVec = new Vector2(randStartX, randStartY); destinationVec = new Vector2(randDestX, randDestY); isGenerated = true; } } } //set locations Location start = new Location((int)startVec.x, (int)startVec.y); Location destination = new Location((int)destinationVec.x, (int)destinationVec.y); transform.position = new Vector3(startVec.x, transform.position.y, startVec.y); var astar = new AStar(grid, start, destination); route = generatePath(grid, astar, destination, start); drawBorder(); drawGrid(grid, astar, route); routePos = route.First; distance = 1.2f; //set first person camera in active fpCamera = this.transform.FindChild("Camera").GetComponent<Camera>(); fpCamera.gameObject.SetActive(false); //set movement slider movementSlider = GameObject.FindGameObjectWithTag("movementSlider").GetComponent<Slider>(); isDone = false; }
public LinkedList <Location> createRoute(SqaureGrid grid, AStar astar, Location start, Location destination) { LinkedList <Location> path = new LinkedList <Location>(); Location current = destination; path.AddFirst(current); while (!current.Equals(start)) { current = astar.cameFrom[current]; path.AddFirst(current); } return(path); }
public AStar(SqaureGrid grid, Location startPos, Location destination) { //Debug.Log("START A STAR"); //setup variables PriorityQueue <int, Location> frontier = new PriorityQueue <int, Location>(); frontier.Enqueue(startPos, 0); cameFrom[startPos] = startPos; costSoFar[startPos] = 0; //main loop //while frontier is not empty //Debug.Log("START A STAR LOOP"); while (frontier.count > 0) { //get first Node in queue Location current = frontier.Dequeue(); //check if current Node is the Destination Node if (current.Equals(destination)) { Debug.Log("A*: DONE"); break;//Destination reached stop searching } foreach (var next in grid.Neighbours(current)) { // calculate cost to neighbour int newCost = costSoFar[current] + grid.cost(current, next); // if its cheaper to get here from this node than previous routes or // we haven't checked this neighbour if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next]) { //update costSoFar costSoFar[next] = newCost; //calculate priority int priority = distCalc(next, destination) + newCost; //add to frontier frontier.Enqueue(next, priority); //sent current node as the previous node //allows us to work our way back through cameFrom to find the route cameFrom[next] = current; } } } }
// Use this for initialization void Start() { status = 0; player = GameObject.FindGameObjectWithTag(Tags.Player); world = GameObject.FindGameObjectWithTag(Tags.World); grid = GameObject.FindGameObjectWithTag(Tags.World).GetComponent <World>().grid; width = world.GetComponent <World>().width; height = world.GetComponent <World>().height; isDone = false; weaponRange = 8; damage = 10; /* * if (task == 0) * { * patrol(patrolStart, patrolEnd); * }*/ //set states currentState = new EnemyState(this); chaseState = new ChaseState(this); attackState = new AttackState(this); fleeState = new FleeState(this); patrolState = new PatrolState(this); guardState = new GuardState(this); searchState = new SearchState(this); if (patrolPoints.Length == 0) { guardPosition = new Vector3(Mathf.FloorToInt(transform.position.x), 0, Mathf.FloorToInt(transform.position.z)); guardRotation = this.transform.rotation; currentState = guardState; } else { currentState = patrolState; } }
void assembleWalls(SqaureGrid grid) { foreach (Vector2 wall in walls) { grid.walls.Add(new Location((int)wall.x, (int)wall.y)); } GameObject[] worldWalls = GameObject.FindGameObjectsWithTag(Tags.Wall); foreach (GameObject wall in worldWalls) { int newX, newZ; newX = Mathf.FloorToInt(wall.transform.position.x); newZ = Mathf.FloorToInt(wall.transform.position.z); Location newWallPos = new Location(newX, newZ); grid.walls.Add(newWallPos); if (wall.name == "Cover") { Debug.Log("COVER POS: " + wall.transform.position); } } }
void drawGrid(SqaureGrid grid, AStar astar, LinkedList<Location> route) { string gridString = ""; for (var y = 0; y <= 10; y++) { for (var x = 0; x <= 10; x++) { Location currentLocation = new Location(x, y); Location locationPtr = currentLocation; if (!astar.cameFrom.TryGetValue(currentLocation, out locationPtr)) { locationPtr = currentLocation; } if (grid.forests.Contains(currentLocation)) { //show forest at current pos Transform wallPart = (Transform)Instantiate(forestNode, new Vector3(x, 0, y), Quaternion.identity); wallPart.parent = this.transform.parent; gridString += "FF"; } if (grid.roads.Contains(currentLocation)) { //show road at current pos Transform wallPart = (Transform)Instantiate(roadNode, new Vector3(x, 0, y), Quaternion.identity); wallPart.parent = this.transform.parent; gridString += "# "; } //show path if (route.Contains(currentLocation)) { Transform wallPart = (Transform)Instantiate(pathNode, new Vector3(x, 0, y), Quaternion.identity); wallPart.parent = this.transform.parent; gridString += "x "; } else if (grid.walls.Contains(currentLocation)) { //show wall at current pos Transform wallPart = (Transform) Instantiate(wallNode, new Vector3(x, 0, y), Quaternion.identity); wallPart.parent = this.transform.parent; gridString += "# "; } else if(astar.cameFrom.ContainsValue(currentLocation)) { Transform wallPart = (Transform)Instantiate(visitedNode, new Vector3(x, 0, y), Quaternion.identity); wallPart.parent = this.transform.parent; } else { gridString += "* "; } } gridString += "\n"; } Debug.Log(gridString); }
public HostageState(HostageController hostageController) { hostage = hostageController; grid = GameObject.FindGameObjectWithTag(Tags.World).GetComponent <World>().grid; player = GameObject.FindGameObjectWithTag(Tags.Player); }
void assembleWalls(SqaureGrid grid) { foreach(Vector2 wall in walls) { grid.walls.Add(new Location((int)wall.x, (int)wall.y)); } }
LinkedList<Location> generatePath(SqaureGrid grid, AStar astar, Location destination, Location start) { LinkedList<Location> newRoute = new LinkedList<Location>(); //Positions Location current = destination; newRoute.AddFirst(current); while(!current.Equals(start)) { current = astar.cameFrom[current]; newRoute.AddFirst(current); } //DEBUG //Print Route /* Debug.Log("ROUTE: COUNT: "+route.Count); LinkedListNode<Location> routeNode = route.First; for (int i = 0; i < route.Count; i++) { Debug.Log("X: " + routeNode.Value.x + " Y: " + routeNode.Value.y); routeNode = routeNode.Next; } */ return newRoute; }