/// <summary> /// Could make this target the unit position instead /// </summary> /// <param name="unit"></param> /// <returns></returns> Unit FindTarget(Unit unit) { Unit result = null; int closestDistance = 0; int thisDistance; foreach (KeyValuePair <Vector3Int, Unit> playerUnit in unitsManager.GetAllPlayerUnits()) { Stack <Vector3Int> path = astar.GetPath(unit.BoardPos, playerUnit.Key); if (closestDistance == 0) { closestDistance = path.Count; result = playerUnit.Value; } else { thisDistance = path.Count; if (thisDistance < closestDistance) { result = playerUnit.Value; closestDistance = thisDistance; } } } return(result); }
void SetPlayerParams(float x, float y, MovePlayer playerMove, Vector3 movePoint) { path = new Astar(new Vector3(x, y), movePoint); player.GetComponent <Path>().path = path.GetPath(); playerMove.path = path.GetPath(); marker.transform.position = movePoint; playerMove.index = 1; }
private void PlaceTower() { WalkAble = false; if (Astar.GetPath(LevelManager.Instance.BlueSpawn, LevelManager.Instance.RedSpawn) == null) { WalkAble = true; return; } if (GridPosition == LevelManager.Instance.BlueSpawn || GridPosition == LevelManager.Instance.RedSpawn) { WalkAble = true; return; } GameObject tower = (GameObject)Instantiate(GameManager.Instance.ClickedBtn.TowerPerfab, WorldPositionTower, Quaternion.identity); tower.GetComponent <SpriteRenderer>().sortingOrder = GridPosition.Y; tower.transform.SetParent(transform); this.myTower = tower.transform.GetChild(0).GetComponent <Tower>(); IsEmpty = false; ColorTile(Color.white); myTower.Price = GameManager.Instance.ClickedBtn.Price; GameManager.Instance.BuyTower(); WalkAble = false; }
// Use this for initialization void Start() { step = stepObj.GetComponent <Step>(); point = new Vector3(7, 2); ph = new PathController(gameObject); gameObject.transform.GetComponentInParent <CheckPath>().OnTriggerObjects += delegate() { Vector2 pos; IEnumerable <GameObject> boots; ph.GetOverlap(out pos, out boots); ph.GetValue(boots, pos); }; //При поступлении шага step.StepDone += delegate() { CheckPath.stop = false; Astar star = new Astar(transform.position, point); path = star.GetPath(); gameObject.GetComponent <Path>().path = path; }; }
Queue <Vector3Int> GetEnemyPath(Transform unit) { Stack <Vector3Int> tilemapPath = pathfinder.GetPath(unitPos, targetPos); Stack <Vector3Int> trimmedTilemapPath = TrimRange(tilemapPath, unit.GetComponent <Unit>().AttackRange); Stack <Vector3Int> maxPath = new Stack <Vector3Int>(); Stack <Vector3Int> tempMaxPath = new Stack <Vector3Int>(); Queue <Vector3Int> moveablePath = new Queue <Vector3Int>(); for (int i = 0; i < unit.GetComponent <Unit>().MovementSpeed&& trimmedTilemapPath.Count > 0; i++) { Vector3Int worldPos = Vector3Int.RoundToInt(movementBoard.CellToWorld(trimmedTilemapPath.Pop())); maxPath.Push(worldPos); } Debug.Log("Max path length = " + maxPath.Count); bool furthestPointFound = false; while (maxPath.Count != 0) { if (furthestPointFound == false) { if (CheckIfOccupied(movementBoard.WorldToCell(maxPath.Peek())) == false) { furthestPointFound = true; tempMaxPath.Push(maxPath.Pop()); } else { maxPath.Pop(); } } else { tempMaxPath.Push(maxPath.Pop()); } } while (tempMaxPath.Count > 0) { moveablePath.Enqueue(tempMaxPath.Pop()); } return(moveablePath); }
private void MakeOneStepTowardsTheTarget() { Vector3Int startNode = tilemap.WorldToCell(transform.position); Vector3Int endNode = targetInGrid; List <Vector3Int> shortestPath = Astar.GetPath(tilemapGraph, startNode, endNode, maxIterations); Debug.Log("shortestPath = " + string.Join(" , ", shortestPath)); if (shortestPath.Count >= 2) { Vector3Int nextNode = shortestPath[1]; transform.position = tilemap.GetCellCenterWorld(nextNode); } else { atTarget = true; } }
private void OnButtonStartSearchClick(object sender, EventArgs e) { try { var graph = gridMaze.GetWeightedGraph(); var star = new Astar <Vertex2D>(graph, ManhattanDistanceHeuristic); var path = star.GetPath(new Node <Vertex2D>(startCell, 0), targetCell); foundPath = RemoveStartAndGoalFromPath(path); RenderGeneratedPath(); } catch (Exception ex) { MessageBox.Show(ex.Message); } btn_startSearch.Enabled = false; btn_deletePath.Enabled = true; }
private IEnumerator createPath(Astar astar) { // una vez tenemos el camino, lo recorremos posicion a posicion hasta llegar al destino // moviendo al tanque por cada una de las casillas intermedias List <int> path = astar.GetPath(); List <GameObject> markers = new List <GameObject>(); setPathMarkers(path, ref markers); int r = 0, c = 0; int i = 1; tankMoving = true; if (path != null) { while (i < path.Count) { r = (int)(path[i] / columns); c = (int)(path[i] - r * columns); setTankPosition(tablero.getCasPos(r, c)); // posicion fisica if (i != path.Count - 1) { setTankRotation(markers[i - 1].transform.rotation.eulerAngles); Destroy(markers[i - 1].gameObject); } i++; steps++; cost += values[puzzle.GetType(r, c)]; UpdateInfo(); yield return(new WaitForSecondsRealtime(0.2f)); // delay para verlo mejor } markers.Clear(); puzzle.TankPosition = new Position((uint)r, (uint)c); // posicion logica } else { StartCoroutine(changeCanMove(2.0f)); // mostrara por pantalla que no podemos movernos a esa casilla } tankMoving = false; changeTankSelected(); }
private void PlaceTower() { WalkAble = false; if (Astar.GetPath(LevelManager.Instance.PortalStart1, LevelManager.Instance.PortalFinish1) == null) { //we dont have path WalkAble = true; return; } GameObject tower = (GameObject)Instantiate(GameManagerScript.Instance.ClickedBtn.TowerPrefab, transform.position, Quaternion.identity); tower.GetComponent <SpriteRenderer>().sortingOrder = GridPosition.Y; tower.transform.SetParent(transform); this.myTower = tower.transform.GetChild(0).GetComponent <Tower>(); IsEmpty = false; ColorTile(Color.white); myTower.Price = GameManagerScript.Instance.ClickedBtn.Price; GameManagerScript.Instance.BuyTower(); WalkAble = false; }
public void GeneratePath() { //Generates a path from start to finish and save the path fullPath = Astar.GetPath(soldierSpawn, soldierGoal); }
public void GeneratePath() { path = Astar.GetPath(PortalStart1, PortalFinish1); }
public void GeneratePath() { path = Astar.GetPath(BlueSpawn, redSpawn); }
protected override void StateUpdate() { if (BehaviorState.Bike_riding == state) { if (PlayerTracking()) { if (Vector2.Distance(Vector2ex.By3(GameObject.FindWithTag("Player").transform.position), Vector2ex.By3(transform.position)) < 100) { SetState_BikeOut(); animator.SetTrigger("isBikeFinish"); } } //으아 길찾기 if (TimeEx.Cooldown(ref astar_cooldown) == 0) { astar_cooldown = 0.5f; if ((astar_lastSucceeded = astarPathFinder.경로검색(GameObject.FindWithTag("Player").transform.position)) == true) { List <Astar.최단경로노드> path = astarPathFinder.GetPath(); astar_index = 0; astar_dir = Vector2ex.By3(path[0].월드위치) - Vector2ex.By3(transform.position); astar_remainDist = astar_dir.magnitude; astar_dir /= astar_remainDist; } } if (astar_lastSucceeded) { float movement = bikeSpeed * Time.deltaTime; while (true) { if (astar_remainDist < movement) { transform.position += Vector2ex.To3(astar_dir) * astar_remainDist; movement -= astar_remainDist; List <Astar.최단경로노드> path = astarPathFinder.GetPath(); astar_index++; if (astar_index >= path.Count) { astar_cooldown = 0; break; } astar_dir = Vector2ex.By3(path[astar_index].월드위치) - Vector2ex.By3(transform.position); astar_remainDist = astar_dir.magnitude; astar_dir /= astar_remainDist; } else { transform.position += Vector2ex.To3(astar_dir) * movement; astar_remainDist -= movement; break; } } } lrFliper.In(astar_dir); } else if (BehaviorState.Bike_out == state) { float movement = bikeSpeed * Time.deltaTime; transform.position += Vector2ex.To3(astar_dir) * movement; } else if (BehaviorState.Attack_machinegun == state || BehaviorState.Attack_machinegun_prefire == state || BehaviorState.Attack_grenadelauncher == state) { if (PlayerTracking()) { GameObject player = GameObject.FindWithTag("Player"); Vector2 destDir = (Vector2ex.By3(player.transform.position) - Vector2ex.By3(transform.position)).normalized; float angle = Vector2.Angle(destDir, direction); if (angle <= weaponRotateSpeed * Time.deltaTime) { if (state == BehaviorState.Attack_grenadelauncher) { weapon_GrenadeLauncher.WeaponFire(); } else { weapon_MG.WeaponFire(); } direction = destDir; } else { bool isRevClockwise = direction.x * destDir.y - direction.y * destDir.x >= 0; direction = Vector2ex.Rotate(direction, weaponRotateSpeed * Mathf.Deg2Rad * Time.deltaTime * (isRevClockwise ? 1 : -1)); } } else { if (BehaviorState.Attack_machinegun == state) { stateTime -= 1; if (stateTime <= 0) { SetState_Idle(); } else { SetState_AttackMachinegun_Prefire(); } } } } else if (BehaviorState.Moving == state) { transform.position += Vector2ex.To3(direction); } }
public void GeneratePath() { path = Astar.GetPath(spawnPoint, finishPoint); }
public void GeneratePath() { finalPath = Astar.GetPath(spawnBegin, endSpawn); }