List <Vector2> AStarPathFinding(Vector2Int start, Vector2Int goal, Vector2 realGoal) { List <Vector2Int> closedSet = new List <Vector2Int>(); //Pisteet jotka on jo tutkittu? List <Vector2Int> openSet = new List <Vector2Int>(); //Pisteet mitkä pitää tutkia? openSet.Add(start); IDictionary <Vector2Int, Vector2Int> cameFrom = new Dictionary <Vector2Int, Vector2Int>(); //Tyhjä kartta aluksi. Lopuksi jollakin avaimella saadaan piste mistä kannattee mennä siihen pisteeseen. cameFrom[jokinPiste] = lyhinPiste tästä tuohon johonkin pisteeseen(?) EHKÄ? IDictionary <Vector2Int, float> gScore = new Dictionary <Vector2Int, float>(); //Hinta alkupisteetä tähän. gScore[start] = 0; IDictionary <Vector2Int, float> fScore = new Dictionary <Vector2Int, float>(); //Koko matkan hinta tänne? fScore[start] = HeuresticEstimate(start, goal); while (openSet.Count != 0) { Vector2Int current = SmallestFScoreFromOpenSet(openSet, fScore); if (current == goal) { return(ReconstructPath(cameFrom, current, realGoal)); } else if ((current - goal).magnitude <= 2 && !UF.CheckForMapCollider(UF.FromCoordinatesToWorld(current), realGoal)) { return(ReconstructPath(cameFrom, current, realGoal)); } openSet.Remove(current); closedSet.Add(current); List <Vector2Int> neighbors = FindNeighbors(current); foreach (Vector2Int neighbor in neighbors) { if (closedSet.Contains(neighbor)) { continue; } float tentativeGScore = gScore[current] + (current - neighbor).magnitude; if (!openSet.Contains(neighbor)) { openSet.Add(neighbor); } else if (tentativeGScore >= gScore[neighbor]) { continue; } cameFrom[neighbor] = current; gScore[neighbor] = tentativeGScore; fScore[neighbor] = tentativeGScore + HeuresticEstimate(neighbor, goal); if (neighbor == goal) { break; } } } return(new List <Vector2>()); //Jos ei löydy polkua niin tulee tyhjä lista... }
Vector2 CorrectStart(Vector2 start) { Vector2[] pointsToCheck = new Vector2[4] { new Vector2(GameManager.coordinateSize.x, 0), new Vector2(-GameManager.coordinateSize.x, 0), new Vector2(0, GameManager.coordinateSize.y), new Vector2(0, -GameManager.coordinateSize.y) }; foreach (Vector2 pointToCheck in pointsToCheck) { Vector2 point = UF.FromCoordinatesToWorld(UF.CoordinatePosition(start)) + pointToCheck; if (IsInsideMap(point) && !UF.CheckForMapCollider(start, point)) { return(point); } } return(start); }
private void Update() { if (shouldBeMoving) { while (ipath < path.Count - 1 && !UF.CheckForMapCollider(transform.position, path[ipath + 1])) { ipath++; } Vector2 nextPoint = path[ipath]; Vector2 movementDirection = (nextPoint - (Vector2)transform.position).normalized; rb.velocity = movementDirection * speed; if (Mathf.Abs(movementDirection.x) > Mathf.Abs(movementDirection.y)) { if (movementDirection.x > 0) { animator.SetInteger("Direction", 2); } else { animator.SetInteger("Direction", 0); } } else { if (movementDirection.y > 0) { animator.SetInteger("Direction", 1); } else { animator.SetInteger("Direction", 3); } } if (UF.DistanceBetween2Units(transform.position, goal) < 0.05f) { StopMoving(); if (currentTarget != null) { currentTarget.SendMessage("Activate"); currentTarget = null; } } } }
List <Vector2Int> FindNeighbors(Vector2Int point) { List <Vector2Int> neighbors = new List <Vector2Int>(); for (int iy = -1; iy <= 1; iy++) { Vector2Int pointBeingChecked = new Vector2Int(point.x, point.y + iy); if (!UF.CheckForMapCollider(UF.FromCoordinatesToWorld(point), UF.FromCoordinatesToWorld(pointBeingChecked))) { neighbors.Add(pointBeingChecked); } } for (int ix = -1; ix <= 1; ix++) { Vector2Int pointBeingChecked = new Vector2Int(point.x + ix, point.y); if (!UF.CheckForMapCollider(UF.FromCoordinatesToWorld(point), UF.FromCoordinatesToWorld(pointBeingChecked))) { neighbors.Add(pointBeingChecked); } } return(neighbors); }