/// <summary> /// Places the tile at the input position. /// </summary> /// <param name="inputPos">The position of the input(touch/mouse).</param> private void PlaceTile(Vector2 inputPos) { _currentNode = _grid.GetNodeFromWorldPos(_grid.FindNearestPosition(Camera.main.ScreenToWorldPoint(inputPos))); if (_currentNode.GetTileType() == TileType.None) { if (_numberOfTilesPlaced == 0 && (_currentNode.GetGridPos().y > 0 || _currentNode.GetGridPos().x != 1)) { return; } _currentNode.SetTileType(_currentTile.GetComponent <Tile>().GetTileType()); _finding.CalculatePath(_currentNode, PlaceTileCallback); _numberOfTilesPlaced++; if (_numberOfTilesPlaced == 2) { if (OnLavaStart != null) { OnLavaStart(); } } } }
void Update() { stopwatch.Start(); path = Pathfinding.CalculatePath(obstacleMap, start.position, goal.position); stopwatch.Stop(); Debug.Log(stopwatch.Elapsed.TotalMilliseconds); stopwatch.Reset(); }
void initPathfinding() { targetCell = levelBuilder.FreeCells[Random.Range(0, levelBuilder.FreeCells.Count - 1)]; pathfinding = new Pathfinding(maze); var startPos = new Vector2Int((int)transform.position.x, -(int)transform.position.z); path = pathfinding.CalculatePath(startPos, targetCell); }
public void CalculatePathTest() { #region INITIAL SETUP List <Tile> fireList = new List <Tile>(); List <Tile> noFireList = new List <Tile>(); Tile huan = new Tile(null, 0, null, null, null, null, Color.Pink, 0, 1); Tile jose = new Tile(null, 0, huan, null, null, null, Color.Red, 1, 1); Tile antoan = new Tile(null, 0, jose, null, null, null, Color.Blue, 2, 1); Tile esmeraldo = new Tile(null, 0, null, null, null, huan, Color.Pink, 0, 0); Tile javier = new Tile(null, 0, esmeraldo, null, null, jose, Color.Red, 1, 0); Tile julio = new Tile(null, 0, javier, null, null, antoan, Color.Blue, 2, 0); huan.down = jose; jose.down = antoan; esmeraldo.down = javier; javier.down = julio; huan.left = esmeraldo; jose.left = javier; antoan.left = julio; Tile[,] grid = new Tile[3, 2]; //Pattern is like grid[0, 1] = huan; // X X - start grid[1, 1] = jose; // X X - on fire (Whoosh checks through the fire) grid[2, 1] = antoan; // X X - goal grid[0, 0] = esmeraldo; grid[1, 0] = javier; grid[2, 0] = julio; foreach (Tile t in grid) { t.walkable = true; t.onFire = false; t.AddNeighbours(); } jose.onFire = true; #endregion Pathfinding p = new Pathfinding(huan, antoan); fireList = p.CalculatePath(false); //calculate without checking if it is on fire, list is reverse (END - START) Assert.AreEqual(3, fireList.Count); Assert.AreEqual(fireList[0], antoan); Assert.AreEqual(fireList[1], jose); Assert.AreEqual(fireList[2], huan); p = new Pathfinding(huan, antoan); noFireList = p.CalculatePath(true); //calculate with check if it is on fire Assert.AreEqual(5, noFireList.Count); Assert.AreEqual(noFireList[0], antoan); Assert.AreEqual(noFireList[1], julio); Assert.AreEqual(noFireList[2], javier); Assert.AreEqual(noFireList[3], esmeraldo); Assert.AreEqual(noFireList[4], huan); }
public void DoNextAction() { if (isFrozen) { return; } if (enemyFOV.isPlayerInSight) { pathfindingDirections = pf.CalculatePath(enemyFOV.playerTransform); pathfindingIndex = 0; isInPursuit = true; } if (isInPursuit) { if (pathfindingIndex < pathfindingDirections.Length - 1) { Move(pathfindingDirections, pathfindingIndex); pathfindingIndex++; } else { isInPursuit = false; } } else { if (pathIndex < patrolPathWorld.Length) { if (!isReversed) { MoveFrontBack(patrolPathWorld, pathIndex); } else { MoveFrontBack(patrolPathWorldReverse, pathIndex); } pathIndex++; } if (pathIndex == patrolPathWorld.Length) { Directions _dir = !isReversed ? patrolPathWorld[patrolPathWorld.Length - 1] : patrolPathWorldReverse[patrolPathWorld.Length - 1]; //retrieve last element of the current directions array Turn180(_dir); pathIndex = 0; isReversed = !isReversed; } } }
// Test the connection of all BaseTiles public void CheckBaseTilesConnection(List <HexTile> baseTiles) { // For now only 2 possible Teams/BaseTiles HexTile baseOne = baseTiles[0]; HexTile baseTwo = baseTiles[1]; path = Pathfinding.CalculatePath(baseOne, baseTwo); if (path.Count - 2 < gridSize / 2) { StopAllCoroutines(); GameManager.current.InitializeGame(); Start(); } else { GameManager.current.activeTeam = GameManager.Teams.Blue; GameManager.current.NextTeam(); StartCoroutine(Fades.current.FadeIn()); } }
public void FindPath() { Path.Clear(); List <Point> container = Pathfinder.CalculatePath(Helper.GetBottomLeftPoint(), goal.ToPoint(), Helper.GetPlayerWidth(), Helper.GetPlayerHeight(), 6); if (container != null && container.Count > 1) { Main.NewText("Successfully found a path"); for (var i = container.Count - 1; i >= 0; --i) { Path.Add(container[i]); } Main.NewText("Beginning movement..."); MovementBot.TappedOnTile(Path, goal.ToPoint()); } else { Main.NewText("Couldn't find a path!"); } }
// Tap was on another HexTile while in attack mode void TapOnOtherAttack(HexTile tappedHexTile) { // Selected HexTile should stay selected selectedHexTile.SetSelected(true); // Does the selected tile even have enough units to keep moving || && selectedHexTile.IsNeighbourTo(tappedHexTile) if (selectedHexTile.unitsAfterMovement > 0) { List <HexTile> path = Pathfinding.CalculatePath(selectedHexTile, tappedHexTile); if (path.Count > 0) { // Selected looses one Unit selectedHexTile.unitsAfterMovement--; movements.Add(new Movement(1, path, GameManager.current.activeTeam)); } else { Debug.Log("Path Not Found"); } } }
private void PathTest() { //generate a test map var map = new int[100, 100]; for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { map[i, j] = i + j + 1; } } //initialise pathfinder Pathfinder = new Pathfinding(new Vector2(0, 0), new Vector2(2, 10), map, 100, 100); if (Pathfinder.isReachable()) { //calculate paths and get lists Pathfinder.CalculatePath(); var coords = Pathfinder.GetPathCoords(); var directions = Pathfinder.GetPathDirections(); var vectors = Pathfinder.GetPathVector(); } }
private bool QueueActions(Vector3 position) { Grid.Grid.Node clickedNode = new Grid.Grid.Node(); var isClickedNodeValid = _unit.Occupant.Grid.TryGetNodeAtWorldPosition(position, ref clickedNode); Grid.Grid.Node myNode = new Grid.Grid.Node(); var isMyNodeValid = _unit.Occupant.Grid.TryGetNodeAtWorldPosition(transform.position, ref myNode); if (isMyNodeValid && isClickedNodeValid) { if (_isUsingBomb) { if (_inventory == null) { return(false); } var diff = clickedNode.Position - myNode.Position; if (!diff.IsCardinal()) { return(false); } AggregateSlot slot; if (!_inventory.RetrieveSlot(bombKey, out slot)) { return(false); } slot.Total -= 1; _inventory.UpdateSlot(bombKey, slot); _unit.QueueRange(bombChucker.Use(_unit.Occupant.Occupant, diff, _unit.Occupant.Grid)); return(true); } else if (_isUsingItem) { if (_item != null) { var direction = clickedNode.Position - _unit.Occupant.Occupant.Position; var actions = _item.Use(_unit.Occupant.Occupant, direction, _unit.Occupant.Grid); if (actions != null && actions.Count > 0) { _unit.QueueRange(actions); return(true); } } else { return(false); } } else { // attack var direction = clickedNode.Position - _unit.Occupant.Occupant.Position; var targets = mainWeapon.FindTargets(_unit.Occupant.Occupant, direction, _unit.Occupant.Grid); if (Array.Exists(targets, it => it.Position == clickedNode.Position)) { var actions = mainWeapon.Use(_unit.Occupant.Occupant, direction, _unit.Occupant.Grid); if (actions != null && actions.Count > 0) { _unit.QueueRange(actions); return(true); } } // move var path = _pathfinding.CalculatePath(transform.position, position); if (path != null && path.Count > 0) { var first = path[0]; _unit.QueueAction(new MoveToPointAction(_unit, first)); return(true); } } } return(false); }
void calculateNewPath(Vector2Int target) { path = pathfinding.CalculatePath(MazeGenerator.ConvertToMazeCoord(transform.position), target); pathIndex = 0; }
// Tries to send as many possible units to all hostile Tiles while still considering Conquering private void Defend() { HexTile baseTile = playerComponent.tiles[0]; int difficultyInv = 100 - difficulty; float totalBorderCount = hostileBorderTiles.Count + borderTiles.Count; // The Ideal Amount to spend Units int unitsToSpare = (baseTile.units - playerComponent.tiles.Count); unitsToSpare = (int)(unitsToSpare * Mathf.Min(1, (difficulty + 10) / 100f)); int unitsToHostile = (int)(unitsToSpare * (hostileBorderTiles.Count / totalBorderCount)); if (borderTiles.Count != 0) { unitsToHostile = (int)(unitsToHostile * (Mathf.Min(1, difficultyInv + 10 / 100f))); } int unitsToBorder = unitsToSpare - unitsToHostile; while (unitsToBorder > 0 && borderTiles.Count > 0) { int random = Random.Range(0, borderTiles.Count); HexTile target = borderTiles[random]; List <HexTile> path = Pathfinding.CalculatePath(baseTile, target); if (path.Count == 0) { continue; } Movement mov = new Movement(1, path, playerComponent.team); playerComponent.movements.Add(mov); mov.Move(true); if (mov.path.Count <= 1 || mov.path[0].team != playerComponent.team || mov.path[0].units == 0) { mov.path[0].movementsFromTile.Remove(mov); playerComponent.movements.Remove(mov); } borderTiles.RemoveAt(random); unitsToBorder--; } while (unitsToHostile > 0) { foreach (HexTile target in hostileBorderTiles) { if (Random.Range(0, 100) <= difficulty) { List <HexTile> path = Pathfinding.CalculatePath(baseTile, target); if (path.Count == 0) { continue; } Movement mov = new Movement(1, path, playerComponent.team); playerComponent.movements.Add(mov); mov.Move(true); if (mov.path.Count <= 1 || mov.path[0].team != playerComponent.team || mov.path[0].units == 0) { mov.path[0].movementsFromTile.Remove(mov); playerComponent.movements.Remove(mov); } } } unitsToHostile--; } }
// Tries to Conquer as many Tiles as possible private void Conquer() { HexTile baseTile = playerComponent.tiles[0]; int difficultyAdj = 100 - difficulty; // Remove all already Targeted Tiles foreach (Movement mov in playerComponent.movements) { borderTiles.Remove(mov.path[mov.path.Count - 1]); } // Diffulty Adjustments int removeTileCount = (int)((borderTiles.Count / 100f) * difficultyAdj); for (int i = 0; i < removeTileCount; i++) { borderTiles.RemoveAt(Random.Range(0, borderTiles.Count)); } if (borderTiles.Count > 0) { int factor = (int)((baseTile.units * difficultyAdj / 100f - playerComponent.tiles.Count) / borderTiles.Count); while (factor > 0) { foreach (HexTile target in borderTiles) { List <HexTile> path = Pathfinding.CalculatePath(baseTile, target); if (path.Count == 0) { continue; } Movement mov = new Movement(1, path, playerComponent.team); playerComponent.movements.Add(mov); mov.Move(true); if (mov.path.Count <= 1 || mov.path[0].team != playerComponent.team || mov.path[0].units == 0) { mov.path[0].movementsFromTile.Remove(mov); playerComponent.movements.Remove(mov); } } factor--; } while (baseTile.units > playerComponent.tiles.Count) { int randomID = Random.Range(0, borderTiles.Count); HexTile target = borderTiles[randomID]; List <HexTile> path = Pathfinding.CalculatePath(baseTile, target); if (path.Count == 0) { continue; } Movement mov = new Movement(1, path, playerComponent.team); playerComponent.movements.Add(mov); mov.Move(true); if (mov.path.Count <= 1 || mov.path[0].team != playerComponent.team || mov.path[0].units == 0) { mov.path[0].movementsFromTile.Remove(mov); playerComponent.movements.Remove(mov); } } } }