public void moveCurrentPlayer(Location location) { if (isLocationOccupied(location)) { Debug.Log("Can't move to occupied locaiton"); return; } //Get the player that wants to move Player mover = players[currentPlayerIndex]; //Get the current location Location current = mover.getLocation(); //Remove the highlighted tiles removeTileHighlights(); //Get the movement List <Tile> tiles = TilePathFinder.FindPath(map[current.X][current.Y], map[location.X][location.Y]); foreach (Tile t in tiles) { mover.positionQueue.Add(map[(int)t.gridPosition.x][(int)t.gridPosition.y].transform.position + 1.5f * Vector3.up); } //Let the player know he has moved mover.move(location); //Update the grid grid[current.X, current.Y] = null; grid[location.X, location.Y] = mover; }
private void mapHeightsInitialize() { mapHeights1D = new float[maxX * maxY]; mapHeights = new float[maxX, maxY]; for (int i = 0; i < maxX; i++) { for (int k = 0; k < maxY; k++) { mapHeights[i, k] = UnityEngine.Random.Range(0, 5); if (mapHeights[i, k] < 1f) { mapHeights[i, k] = 0f; } if (mapHeights[i, k] > 0) { GameObject.Instantiate(originalTile, new Vector3((float)i, 0, (float)k), new Quaternion()); } mapHeights1D[maxX * i + k] = mapHeights[i, k]; } } tiles = GameObject.FindGameObjectsWithTag("Tile"); tileInitializer = new TileInitializer[tiles.Length]; tilePathFinder = new TilePathFinder[tiles.Length]; mapHeights1DCleaned = mapHeights1D.Where(val => val != 0).ToArray(); }
private Tile retiradaDaBatalhaDaAI(Tile tileDeOrigem) { if (tileDeOrigem.vizinhos.Count != 4) { return(tileDeOrigem); } List <TilePath> pathsParaRetirada = new List <TilePath>(); List <Tile> movementTiles = TileHighlight.FindHighlight(tileDeOrigem, unidadeAssociada.status[3]); foreach (Tile tilePossivelDeRetirar in BatalhaController.instancia.locaisDeRetiradaInimiga.Where(tile => movementTiles.Contains(tile))) { pathsParaRetirada.Add(TilePathFinder.FindPathTilePathReturn(tileDeOrigem, tilePossivelDeRetirar)); } List <Tile> caminhoEscolhido = pathsParaRetirada.First(tilePath => tilePath.custoDoCaminho == pathsParaRetirada.Min(custos => custos.custoDoCaminho)).listaDeTiles; for (int i = caminhoEscolhido.Count - 1; i >= 0; i--) { if (movementTiles.Contains(caminhoEscolhido[i])) { return(caminhoEscolhido[i]); } } return(null); }
public void moveCurrentPlayer(Tile destTile) { if (destTile.visual.transform.GetComponent <Renderer>().materials[0].color != Color.white && !destTile.impassible && players[currentPlayerIndex].positionQueue.Count == 0) { removeTileHighlights(); players[currentPlayerIndex].moving = false; foreach (Tile t in TilePathFinder.FindPath(map[(int)players[currentPlayerIndex].gridPosition.x][(int)players[currentPlayerIndex].gridPosition.y], destTile, players.Where(x => x.gridPosition != destTile.gridPosition && x.gridPosition != players[currentPlayerIndex].gridPosition).Select(x => x.gridPosition).ToArray())) { players[currentPlayerIndex].positionQueue.Add(new Vector3( t.transform.position.x, players[currentPlayerIndex].transform.position.y, t.transform.position.z )); Debug.Log("map Position: x = " + map[(int)t.gridPosition.x][(int)t.gridPosition.y].transform.position.x + "; y = " + map[(int)t.gridPosition.x][(int)t.gridPosition.y].transform.position.y + "; z = " + map[(int)t.gridPosition.x][(int)t.gridPosition.y].transform.position.z); Debug.Log("tile Position: x = " + t.gameObject.transform.position.x + "; y = " + t.gameObject.transform.position.y + "; z = " + t.gameObject.transform.position.z); Debug.Log("player Position: x = " + players[currentPlayerIndex].gameObject.transform.position.x + "; y = " + players[currentPlayerIndex].gameObject.transform.position.y + "; z = " + players[currentPlayerIndex].gameObject.transform.position.z); //t.visual.transform.GetComponent<Renderer>().sharedMaterial = materials[2]; //Debug.Log("(" + players[currentPlayerIndex].positionQueue[players[currentPlayerIndex].positionQueue.Count - 1].x + "," + players[currentPlayerIndex].positionQueue[players[currentPlayerIndex].positionQueue.Count - 1].y + ")"); } players[currentPlayerIndex].gridPosition = destTile.gridPosition; } else { Debug.Log("destination invalid"); //destTile.visual.transform.GetComponent<Renderer>().materials[0].color = Color.cyan; } }
public void moveCurrentPlayer(GameTile destTile) { if (destTile.transform.GetComponent <Renderer>().material.color == instance.blueColor) { bool canMove = true; foreach (Player enemy in players) { if (enemy.gridPosition == destTile.gridPosition) { canMove = false; Debug.Log("Two players cannot occupy the same space"); } } if (canMove) { players[currentPlayerIndex].moveDestination = destTile.transform.position + new Vector3(0, 0, -1.5f); players[currentPlayerIndex].moving = false; foreach (GameTile tile in TilePathFinder.FindPath(map[(int)players[currentPlayerIndex].gridPosition.x][(int)players[currentPlayerIndex].gridPosition.y], destTile).tiles) { players[currentPlayerIndex].positionQueue.Add(map[(int)tile.gridPosition.x][(int)tile.gridPosition.y].transform.position); } players[currentPlayerIndex].gridPosition = destTile.gridPosition; foreach (Vector3 tile in players[currentPlayerIndex].positionQueue) { players[currentPlayerIndex].moveDestination = tile + new Vector3(0, 0, -2.5f); } } } else { Debug.Log("You cannot move that far."); } }
// Use this for initialization void Start() { getDataFromBase(); tilePathFinder = GetComponent <TilePathFinder>(); pathFinder = GameObject.Find("GameManager").GetComponent <PathFinder>(); inputManager = GameObject.Find("GameManager").GetComponent <InputManager>(); height = tileTransform.localScale.y; }
public void setAdjacentTiles(ref GameObject tile0) { adjacentTiles = new GameObject[1]; adjacentTiles[0] = tile0; adjacentTilePathFinder = new TilePathFinder[1]; adjacentTilePathFinder[0] = adjacentTiles[0].GetComponent <TilePathFinder>(); setDataInBase(); }
public void FindFirst_5x5EmptyMap_FindsPath() { TilePathFinder pathfinder = new TilePathFinder((TileWorld)this.World); var start = new TilePosition(new Point(0, 0)); var goal = new TilePosition(new Point(0, -2)); Path <TileWorld, TilePosition> foundPath; var hasPath = pathfinder.FindFirst(start, goal, out foundPath); Assert.IsTrue(hasPath); }
public void FindFirst_5x5EmptyMap_FindsPath() { TilePathFinder pathfinder = new TilePathFinder((TileWorld)this.World); var start = new TilePosition(new Point(0, 0)); var goal = new TilePosition(new Point(0, -2)); Path<TileWorld, TilePosition> foundPath; var hasPath = pathfinder.FindFirst(start, goal, out foundPath); Assert.IsTrue(hasPath); }
bool CanFinishPathInMoves(int moves, List <List <Tile> > paths) { int range = moves * getMove().GetMoveRange(); foreach (List <Tile> path in paths) { if (TilePathFinder.GetPathLengthForUnit(m_unit, path) <= range) { return(true); } } return(false); }
// Start is called before the first frame update void Start() { if (rb == null) { rb = gameObject.GetComponent <Rigidbody2D>(); } if (pathFinder == null) { pathFinder = GameObject.Find("Grid").GetComponent <TilePathFinder>(); } target = GameObject.Find("Player"); RecalculatePath(); }
public void setAdjacentTiles(ref GameObject tile0, ref GameObject tile1, ref GameObject tile2) { adjacentTiles = new GameObject[3]; adjacentTiles[0] = tile0; adjacentTiles[1] = tile1; adjacentTiles[2] = tile2; adjacentTilePathFinder = new TilePathFinder[3]; adjacentTilePathFinder[0] = adjacentTiles[0].GetComponent <TilePathFinder>(); adjacentTilePathFinder[1] = adjacentTiles[1].GetComponent <TilePathFinder>(); adjacentTilePathFinder[2] = adjacentTiles[2].GetComponent <TilePathFinder>(); setDataInBase(); }
public void moveCurrentPlayer(Tile destinationTile) { if (destTile.Rend.Color != Color.white && !destTile.imPassable) { RemoveTileHightlights(); foreach (Tile t in TilePathFinder.FindPath(map[(int)players[PlayerIndex].GridPosition.x][(int)players[PlayerIndex].GridPosition.y]), destTile, players.Where(x => x.GridPosition != players[PlayerIndex].GridPosition).Select(x => x.GridPosition).toArray()) { players[PlayerIndex].positionQueue.Add(map[(int)t.GridPosition.x][(int)t.GridPosition.y].transform.position + 1.5f + Vector3.up); } players[PlayerIndex].GridPosition = destTile.GridPosition; players[PlayerIndex].positionQueue.Reverse(); } else { Debug.Log("Destination invalid"); } }
private void moveCurrentPlayer() { if (destTile.visual.transform.GetComponent <Renderer>().materials[0].color != Color.white && !destTile.impassible && players[currentPlayerIndex].positionQueue.Count == 0) { removeTileHighlights(); foreach (Tile t in TilePathFinder.FindPath(map[(int)players[currentPlayerIndex].gridPosition.x][(int)players[currentPlayerIndex].gridPosition.y], destTile, players.Where(x => x.gridPosition != destTile.gridPosition && x.gridPosition != players[currentPlayerIndex].gridPosition).Select(x => x.gridPosition).ToArray())) { players[currentPlayerIndex].positionQueue.Add(map[(int)t.gridPosition.x][(int)t.gridPosition.y].transform.position + 1.5f * Vector3.up); Debug.Log("(" + players[currentPlayerIndex].positionQueue[players[currentPlayerIndex].positionQueue.Count - 1].x + "," + players[currentPlayerIndex].positionQueue[players[currentPlayerIndex].positionQueue.Count - 1].y + ")"); } players[currentPlayerIndex].gridPosition = destTile.gridPosition; } else { destTile.visual.transform.GetComponent <Renderer>().materials[0].color = Color.cyan; } }
void CreatePassage(Room roomA, Room roomB, Tile tileA, Tile tileB) { Room.ConnectRooms(roomA, roomB); // create a path between the regions List <Tile> pathToRegions = new List <Tile> (); pathToRegions = (List <Tile>)TilePathFinder.FindPathBetweenRooms(tileA, tileB, map); if (pathToRegions != null) { foreach (Tile t in pathToRegions) { // break the walls between the regions and change the color t.impassible = false; t.GetComponent <Renderer>().material.color = Color.white; } } }
public Tile GetFurthestMovibleTileOnPath(List <Tile> path) { float longest = 0; Tile t = null; for (int i = 0; i < path.Count; i++) { List <Tile> sub_path = path.GetRange(0, i + 1); float length = TilePathFinder.GetPathLengthForUnit(Owner, sub_path); if (PathWalkable(sub_path) && length > longest) { t = path[i]; longest = length; } } return(t); }
public bool moveCurrentPlayer(Tile destTile) { if (destTile.transform.GetComponent <Renderer> ().material.color != Color.white && !destTile.impassible && players[currentPlayerIndex].positionQueue.Count == 0) { map[(int)players[currentPlayerIndex].gridPosition.x][(int)players[currentPlayerIndex].gridPosition.y].occupied = false; removeTileHighlights(); players[currentPlayerIndex].moving = false; foreach (Tile t in TilePathFinder.FindPath(map[(int)players[currentPlayerIndex].gridPosition.x][(int)players[currentPlayerIndex].gridPosition.y], destTile, players.Where(x => x.gridPosition != players[currentPlayerIndex].gridPosition && x.gridPosition != players[currentPlayerIndex].gridPosition).Select(x => x.gridPosition).ToArray())) { players[currentPlayerIndex].positionQueue.Add(map[(int)t.gridPosition.x][(int)t.gridPosition.y].transform.position + 1.5f * Vector3.up); } players [currentPlayerIndex].gridPosition = destTile.gridPosition; destTile.occupied = true; return(true); } else { Debug.Log("Destination invalid" + destTile.gridPosition); return(false); } }
public override void AImove() { // Search thru list of players and choose closest one closestPlayerPosition = new Vector2(100, 100); foreach (PlayerController prey in BoardManager.Boardo.players.Where(y => y.GetType() != typeof(AIPlayer))) { Debug.Log(prey.GetType()); if (prey.gridPosition.sqrMagnitude < closestPlayerPosition.sqrMagnitude) { this.closestPlayer = prey; //Debug.Log(prey); this.closestPlayerPosition = prey.gridPosition; } } moving = true; attacking = false; //Debug.Log(closestPlayer); // Calculate the path to the player path = TilePathFinder.FindPath(BoardManager.Boardo.map[(int)gridPosition.x][(int)gridPosition.y], BoardManager.Boardo.map[(int)closestPlayer.gridPosition.x][(int)closestPlayer.gridPosition.y]); Debug.Log(path); // Why is it returning null? //path.Reverse(); path.ToArray(); if (path[0] != null && path.Count > 1) // If null, is next to player { Debug.Log("AI moving to" + path[0].gridPosition); BoardManager.Boardo.moveCurrentPlayer(path[0]); this.gridPosition = path[0].gridPosition; } else { // if next to player turn cannons towards player AIturn(); } }
public void TrackDownEnemies(UnidadeNoTabuleiro target, Tile tileDeOrigem) { Tile tileDoTarget = BatalhaController.instancia.tabuleiro[(int)target.gridPosition.x][(int)target.gridPosition.y]; //Primeiro Caso, Alvo em Range e não precisa se mover a ele, portanto double attack gridDeAtaque = true; gridDeMovimento = false; if (TileHighlight.FindHighlight(tileDeOrigem, unidadeAssociada.status[4]).Contains(tileDoTarget)) { BatalhaController.instancia.highlightTilesAt(tileDeOrigem.posicaoNoTabuleiro, BatalhaController.instancia.TileGridAtaque, unidadeAssociada.status[4]); BatalhaController.instancia.atacarComPlayerAtual(tileDoTarget); } //Segundo caso, Alvo não está em range, mas pode ser alcançado com um movimento else { gridDeAtaque = false; gridDeMovimento = true; BatalhaController.instancia.highlightTilesAt(tileDeOrigem.posicaoNoTabuleiro, BatalhaController.instancia.TileGridMovimento, unidadeAssociada.status[3]); //Pegar todos os tiles de movimento possivel List <Tile> movementTiles = TileHighlight.FindHighlight(tileDeOrigem, unidadeAssociada.status[3]); //Verificar se algums dos tiles está a uma distancia que a unidade pode atacar após mover if (movementTiles.Any(tile => Vector2.Distance(tile.posicaoNoTabuleiro, tileDoTarget.posicaoNoTabuleiro) == unidadeAssociada.status[4])) { //Pegar qualquer tile cuja distancia para o alvo seja igual ao range maximo do seu ataque //List<Tile> possiveisParaMovimento = movementTiles.Where(tile => Vector2.Distance(tile.posicaoNoTabuleiro, tileDoTarget.posicaoNoTabuleiro) == unidadeAssociada.status[4]).ToList(); //float maiorDistancia = movementTiles.Max(tile => Vector2.Distance(tileDoTarget.posicaoNoTabuleiro, tile.posicaoNoTabuleiro)); Tile tileParaMoverMaisProximo = movementTiles.First(tile => Vector2.Distance(tile.posicaoNoTabuleiro, tileDoTarget.posicaoNoTabuleiro) == unidadeAssociada.status[4]); BatalhaController.instancia.verificarTentativaDeMovimento(tileParaMoverMaisProximo); alvoDeAtaque = target; } //Terceiro caso, Muito longe para atacar o inimigo depois do movimento, porntanto so se importar com chegar o mais proximo possivel else { List <Tile> CaminhoMaisCurtoParaOAlvo = null; foreach (Tile tile in tileDoTarget.vizinhos) { if (!tile.ocupado) { CaminhoMaisCurtoParaOAlvo = TilePathFinder.FindPathTilesReturn(tileDeOrigem, tile); break; } } if (CaminhoMaisCurtoParaOAlvo == null) { BatalhaController.instancia.proximaVez(); return; } for (int i = CaminhoMaisCurtoParaOAlvo.Count - 1; i >= 0; i--) { if (movementTiles.Contains(CaminhoMaisCurtoParaOAlvo[i])) { BatalhaController.instancia.verificarTentativaDeMovimento(CaminhoMaisCurtoParaOAlvo[i]); break; } } //movementTiles.Any(tile => CaminhoMaisCurtoParaOAlvo.Contains(tile)); } gridDeMovimento = false; } }
public override void Update() { GameObject[] gobjs = this.GuiController.GetGameObjectsOnMouse(); GameObject firstter = gobjs.FirstOrDefault(go => go.gameObject.GetComponent <TerrainInformation>() != null); UnitEntity attackUnit = null; if (firstter != null) { TerrainInformation terinfo = firstter.GetComponent <TerrainInformation>(); TilePosition tilepos = (TilePosition)terinfo.Terrain.Position; if (tilepos != null && (mousePoint != tilepos.Point && lastpos.Point != tilepos.Point) || refindMouse) { refindMouse = false; var oldMousePath = mousePath; TilePathFinder path = new TilePathFinder((TileWorld)this.World); mousePoint = tilepos.Point; GuiViewHandler view = this.GuiController.GuiView; Path <TileWorld, TilePosition> foundPath; bool hasPath; var entities = this.World.GetEntities(tilepos).OfType <UnitEntity>(); attackUnit = entities.FirstOrDefault(ent => ent.Module <UnitInfoModule>().Controller != this.GuiController.GuiInfo.Player); RemoveColorOfLastFound(); if (attackUnit == null) { hasPath = path.FindFirst(lastpos, tilepos, out foundPath); this.lastFoundEnemy = null; } else { Predicate <TilePosition> goalcond = pos => { //var pointP = pos.Point; //int difx = Math.Abs(pointP.X - mousePoint.X); //int dify = Math.Abs(pointP.Y - mousePoint.Y); //int attackrange = unitEntity.Module<AttackModule>().AttackRange; //return attackrange >= difx && attackrange >= dify; return(unitEntity.Module <AttackModule>().CanReachPoint(pos.Point, mousePoint)); }; hasPath = path.FindFirst(lastpos, goalcond, pos => new Vector(pos.Point, mousePoint).Distance, out foundPath); this.lastFoundEnemy = attackUnit; var gobj = this.GuiController.Factory.GameObjectFromModel(attackUnit); gobj.renderer.material.color = Color.red; } if (hasPath) { int movelength = unitEntity.Module <MoveModule>().MoveLength - this.pathLength(); mousePath = new Path <TileWorld, TilePosition>(foundPath.Map, foundPath.Road.Skip(1).Take(movelength)); } view.drawRoute(mousePath, this.GuiController.GuiInfo.FocusColor); if (oldMousePath.Map != null) { view.unDrawRoute(oldMousePath); } } } if (Input.GetButtonDown("select_object") && !enemySelected) { if (mousePath.Map != null) { fullroute.AddLast(mousePath); if (mousePath.Road.Last != null) { this.lastpos = mousePath.Road.Last.Value; } this.GuiController.GuiView.unDrawRoute(mousePath); mousePath = default(Path <TileWorld, TilePosition>); QueueDeclareAction(this.lastFoundEnemy); this.enemySelected = this.lastFoundEnemy != null; //Finished = true; } } else if (Input.GetButtonDown("deselect_object")) { enemySelected = false; fullroute.RemoveLast(); if (fullroute.Count == 0) { lastpos = this.unitEntity.PositionAs <TilePosition>(); } else { lastpos = this.fullroute.Last.Value.Road.Last.Value; } QueueDeclareAction(); this.refindMouse = true; } else if (Input.GetButtonDown("accept") || enemySelected || this.unitEntity.Module <MoveModule>().MoveLength == pathLength()) { this.unit.renderer.material.color = originalColor; if (mousePath.Map != null) { this.GuiController.GuiView.unDrawRoute(this.mousePath); } RemoveColorOfLastFound(); Finished = true; } }
public void SetMovementOrders() { List <Tile> movementTilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint + 100000); //heal first i think? if (this.isHealer) { if (!moving && movementTilesInRange.Where(x => GameManager.instance.players.Where(y => y.isOwnedByAI == true && y.HP > 0 && y.HP < 9 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count() > 0) { var opponentsInRange = movementTilesInRange.Select(x => GameManager.instance.players.Where(y => y.isOwnedByAI == true && y.HP > 0 && y.HP < 9 && y != this && y.gridPosition == x.gridPosition).Count() > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition && y.HP > 0).First() : null).ToList(); Player opponent = opponentsInRange.OrderBy(x => x != null ? -x.HP : 1000).OrderBy(x => x != null ? TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)x.gridPosition.x][(int)x.gridPosition.y]).Count() : 1000).First(); GameManager.instance.aiPlayers[GameManager.instance.currentAIUnitIndex].moving = true; GameManager.instance.aiPlayers[GameManager.instance.currentAIUnitIndex].attacking = false; GameManager.instance.highlightTilesAt(gridPosition, Color.blue, movementPerActionPoint, false, true); List <Tile> path = TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y], GameManager.instance.players.Where(x => x.gridPosition != gridPosition && x.gridPosition != opponent.gridPosition).Select(x => x.gridPosition).ToArray()); if (path == null) { GameManager.instance.removeTileHighlights(); GameManager.instance.aiPlayers[GameManager.instance.currentAIUnitIndex].moving = false; } else if (path.Count() > 1) { List <Tile> actualMovement = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint, GameManager.instance.players.Where(x => x.gridPosition != gridPosition).Select(x => x.gridPosition).ToArray()); path.Reverse(); if (path.Where(x => actualMovement.Contains(x)).Count() > 0) { GameManager.instance.moveCurrentPlayer(path.Where(x => actualMovement.Contains(x)).First()); } } } } // after healing (or if no healing) else if (!moving && movementTilesInRange.Where(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayerFix) && y.isOwnedByAI != true && y.inStorage != true && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count() > 0) { var opponentsInRange = movementTilesInRange.Select(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayerFix) && y.isOwnedByAI != true && y.inStorage != true && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition && y.HP > 0).First() : null).ToList(); Player opponent = opponentsInRange.OrderBy(x => x != null ? -x.HP : 1000).OrderBy(x => x != null ? TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)x.gridPosition.x][(int)x.gridPosition.y]).Count() : 1000).First(); GameManager.instance.aiPlayers[GameManager.instance.currentAIUnitIndex].moving = true; GameManager.instance.aiPlayers[GameManager.instance.currentAIUnitIndex].attacking = false; GameManager.instance.highlightTilesAt(gridPosition, Color.blue, movementPerActionPoint, false, true); List <Tile> path = TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y], GameManager.instance.players.Where(x => x.gridPosition != gridPosition && x.gridPosition != opponent.gridPosition).Select(x => x.gridPosition).ToArray()); if (path == null) { GameManager.instance.removeTileHighlights(); GameManager.instance.aiPlayers[GameManager.instance.currentAIUnitIndex].moving = false; } else if (path.Count() > 1) { List <Tile> actualMovement = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint, GameManager.instance.players.Where(x => x.gridPosition != gridPosition).Select(x => x.gridPosition).ToArray()); path.Reverse(); if (path.Where(x => actualMovement.Contains(x)).Count() > 0) { GameManager.instance.moveCurrentPlayer(path.Where(x => actualMovement.Contains(x)).First()); } } MovementHasBeenSet = true; // i think we want this in updateunitposition GameManager.instance.aiPlayers[GameManager.instance.currentAIUnitIndex].waiting = true; } }
public override void TurnUpdate() { if (positionQueue.Count > 0) { transform.position += (positionQueue[0] - transform.position).normalized * moveSpeed * Time.deltaTime; if (Vector3.Distance(positionQueue[0], transform.position) <= 0.1f) { transform.rotation = Quaternion.LookRotation((positionQueue [0] - transform.position).normalized, Vector3.up); Debug.Log("forwardVector" + Quaternion.LookRotation((positionQueue[0] - transform.position).normalized, Vector3.up)); transform.position = positionQueue[0]; positionQueue.RemoveAt(0); if (positionQueue.Count == 0) { moving = true; } } } else { //priority queue List <Tile> attacktilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], attackRange, true); //List<Tile> movementToAttackTilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint + attackRange); List <Tile> movementTilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint + 1000); //attack if in range and with lowest HP if (!attacking && attacktilesInRange.Where(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count() > 0) { var opponentsInRange = attacktilesInRange.Select(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition).First() : null).ToList(); Player opponent = opponentsInRange.OrderBy(x => x != null ? -x.HP : 1000).First(); GameManager.instance.removeTileHighlights(); GameManager.instance.highlightTilesAt(gridPosition, Color.red, attackRange); GameManager.instance.attackWithCurrentPlayer(GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y]); } else if (!moving && movementTilesInRange.Where(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count() > 0) { var opponentsInRange = movementTilesInRange.Select(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition).First() : null).ToList(); Player opponent = opponentsInRange.OrderBy(x => x != null ? -x.HP : 1000).ThenBy(x => x != null ? TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)x.gridPosition.x][(int)x.gridPosition.y]).Count() : 1000).First(); GameManager.instance.removeTileHighlights(); GameManager.instance.highlightTilesAt(gridPosition, Color.blue, movementPerActionPoint, false); List <Tile> path = TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y], GameManager.instance.players.Where(x => x.gridPosition != gridPosition && x.gridPosition != opponent.gridPosition).Select(x => x.gridPosition).ToArray()); if (path == null) { GameManager.instance.nextTurn(); } if (path.Count() > 1) { List <Tile> actualMovement = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint, GameManager.instance.players.Where(x => x.gridPosition != gridPosition).Select(x => x.gridPosition).ToArray()); path.Reverse(); if (path.Where(x => actualMovement.Contains(x)).Count() > 0) { GameManager.instance.moveCurrentPlayer(path.Where(x => actualMovement.Contains(x)).First()); } } } else if (moving && attacktilesInRange.Where(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count() <= 0) { attacking = true; } } base.TurnUpdate(); }
public override void TurnUpdate() { if (positionQueue.Count > 0) { transform.position += (positionQueue [0] - transform.position).normalized * moveSpeed * Time.deltaTime; if (Vector3.Distance(positionQueue [0], transform.position) <= 0.1f) { transform.position = positionQueue [0]; positionQueue.RemoveAt(0); if (positionQueue.Count == 0) { actionPoints--; } } } else { // queue // attack if in range and with lowest hp List <Tile> attacktilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], attackRange); List <Tile> movementTilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint + 1000); //attack if in range and with lowest HP Debug.Log("attackTiles in range are " + attackRange + " and the return is " + attacktilesInRange.Count); if (attacktilesInRange.Where(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count() > 0) { var opponentsInRange = attacktilesInRange.Select(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition).First() : null).ToList(); Player opponent = opponentsInRange.OrderBy(x => x != null ? -x.HP : 1000).First(); GameManager.instance.removeTileHighlights(); moving = false; attacking = true; GameManager.instance.highlightTilesAt(gridPosition, Color.red, attackRange); GameManager.instance.attackWithCurrentPlayer(GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y]); } // if not, move towards nearest opponent else if (!moving && movementTilesInRange.Where(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count() > 0) { var opponentsInRange = movementTilesInRange.Select(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition).First() : null).ToList(); Player opponent = opponentsInRange.OrderBy(x => x != null ? -x.HP : 1000).ThenBy(x => x != null ? (TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)x.gridPosition.x][(int)x.gridPosition.y])) != null ? TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)x.gridPosition.x][(int)x.gridPosition.y]).Count() : 1000 : 1000).First(); GameManager.instance.removeTileHighlights(); moving = true; attacking = false; GameManager.instance.highlightTilesAt(gridPosition, Color.blue, movementPerActionPoint, false); List <Tile> path = TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y], GameManager.instance.players.Where(x => x.gridPosition != gridPosition && x.gridPosition != opponent.gridPosition).Select(x => x.gridPosition).ToArray()); if (path != null && path.Count() > 1) { List <Tile> actualMovement = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint, GameManager.instance.players.Where(x => x.gridPosition != gridPosition).Select(x => x.gridPosition).ToArray()); path.Reverse(); if (path.Where(x => actualMovement.Contains(x)).Count() > 0) { GameManager.instance.moveCurrentPlayer(path.Where(x => actualMovement.Contains(x)).First()); } } else { SkipTurn(); } } } base.TurnUpdate(); }
public override void Update() { GameObject[] gobjs = this.GuiController.GetGameObjectsOnMouse(); GameObject firstter = gobjs.FirstOrDefault(go => go.gameObject.GetComponent<TerrainInformation>() != null); UnitEntity attackUnit = null; if (firstter != null) { TerrainInformation terinfo = firstter.GetComponent<TerrainInformation>(); TilePosition tilepos = (TilePosition)terinfo.Terrain.Position; if (tilepos != null && (mousePoint != tilepos.Point && lastpos.Point != tilepos.Point) || refindMouse) { refindMouse = false; var oldMousePath = mousePath; TilePathFinder path = new TilePathFinder((TileWorld)this.World); mousePoint = tilepos.Point; GuiViewHandler view = this.GuiController.GuiView; Path<TileWorld, TilePosition> foundPath; bool hasPath; var entities = this.World.GetEntities(tilepos).OfType<UnitEntity>(); attackUnit = entities.FirstOrDefault(ent => ent.Module<UnitInfoModule>().Controller != this.GuiController.GuiInfo.Player); RemoveColorOfLastFound(); if (attackUnit == null) { hasPath = path.FindFirst(lastpos, tilepos, out foundPath); this.lastFoundEnemy = null; } else { Predicate<TilePosition> goalcond = pos => { //var pointP = pos.Point; //int difx = Math.Abs(pointP.X - mousePoint.X); //int dify = Math.Abs(pointP.Y - mousePoint.Y); //int attackrange = unitEntity.Module<AttackModule>().AttackRange; //return attackrange >= difx && attackrange >= dify; return unitEntity.Module<AttackModule>().CanReachPoint(pos.Point, mousePoint); }; hasPath = path.FindFirst(lastpos, goalcond, pos => new Vector(pos.Point, mousePoint).Distance, out foundPath); this.lastFoundEnemy = attackUnit; var gobj= this.GuiController.Factory.GameObjectFromModel(attackUnit); gobj.renderer.material.color = Color.red; } if (hasPath) { int movelength = unitEntity.Module<MoveModule>().MoveLength-this.pathLength(); mousePath = new Path<TileWorld,TilePosition>(foundPath.Map,foundPath.Road.Skip(1).Take(movelength)); } view.drawRoute(mousePath,this.GuiController.GuiInfo.FocusColor); if (oldMousePath.Map != null) view.unDrawRoute(oldMousePath); } } if (Input.GetButtonDown("select_object") && !enemySelected) { if(mousePath.Map != null) { fullroute.AddLast(mousePath); if(mousePath.Road.Last != null) this.lastpos = mousePath.Road.Last.Value; this.GuiController.GuiView.unDrawRoute(mousePath); mousePath = default(Path<TileWorld, TilePosition>); QueueDeclareAction(this.lastFoundEnemy); this.enemySelected = this.lastFoundEnemy != null; //Finished = true; } } else if (Input.GetButtonDown("deselect_object")) { enemySelected = false; fullroute.RemoveLast(); if (fullroute.Count == 0) lastpos = this.unitEntity.PositionAs<TilePosition>(); else lastpos = this.fullroute.Last.Value.Road.Last.Value; QueueDeclareAction(); this.refindMouse = true; } else if (Input.GetButtonDown("accept") || enemySelected || this.unitEntity.Module<MoveModule>().MoveLength == pathLength()) { this.unit.renderer.material.color = originalColor; if(mousePath.Map !=null) this.GuiController.GuiView.unDrawRoute(this.mousePath); RemoveColorOfLastFound(); Finished = true; } }
public override void TurnUpdate() { if (positionQueue.Count > 0) { transform.position += (positionQueue[0] - transform.position).normalized * moveSpeed * Time.deltaTime; if (Vector3.Distance(positionQueue[0], transform.position) <= 0.1f) { transform.position = positionQueue[0]; positionQueue.RemoveAt(0); if (positionQueue.Count == 0) { actionPoints--; } } } else { //priority queue List <Tile> attacktilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], attackRange, true); //List<Tile> movementToAttackTilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint + attackRange); List <Tile> movementTilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint + 1000); //attack if in range and with lowest HP if (attacktilesInRange.Where(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count() > 0) { var opponentsInRange = attacktilesInRange.Select(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition).First() : null).ToList(); Player opponent = opponentsInRange.OrderBy(x => x != null ? -x.HP : 1000).First(); GameManager.instance.removeTileHighlights(); moving = false; attacking = true; GameManager.instance.highlightTilesAt(gridPosition, Color.red, attackRange); GameManager.instance.attackWithCurrentPlayer(GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y]); } //move toward nearest attack range of opponent // else if (!moving && movementToAttackTilesInRange.Where(x => GameManager.instance.players.Where (y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count () > 0) { // var opponentsInRange = movementToAttackTilesInRange.Select(x => GameManager.instance.players.Where (y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count () > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition && y.HP > 0).First() : null).ToList(); // Player opponent = opponentsInRange.OrderBy (x => x != null ? -x.HP : 1000).ThenBy (x => x != null ? TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y],GameManager.instance.map[(int)x.gridPosition.x][(int)x.gridPosition.y]).Count() : 1000).First (); // // GameManager.instance.removeTileHighlights(); // moving = true; // attacking = false; // GameManager.instance.highlightTilesAt(gridPosition, Color.blue, movementPerActionPoint, false); // // List<Tile> path = TilePathFinder.FindPath (GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y],GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y], GameManager.instance.players.Where(x => x.gridPosition != gridPosition && x.gridPosition != opponent.gridPosition).Select(x => x.gridPosition).ToArray()); // if (path.Count() > 1) { // GameManager.instance.moveCurrentPlayer(path[(int)Mathf.Max(0, path.Count - 1 - attackRange)]); // } // } //move toward nearest opponent else if (!moving && movementTilesInRange.Where(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count() > 0) { var opponentsInRange = movementTilesInRange.Select(x => GameManager.instance.players.Where(y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition).First() : null).ToList(); Player opponent = opponentsInRange.OrderBy(x => x != null ? -x.HP : 1000).ThenBy(x => x != null ? TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)x.gridPosition.x][(int)x.gridPosition.y]).Count() : 1000).First(); GameManager.instance.removeTileHighlights(); moving = true; attacking = false; GameManager.instance.highlightTilesAt(gridPosition, Color.blue, movementPerActionPoint, false); List <Tile> path = TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y], GameManager.instance.players.Where(x => x.gridPosition != gridPosition && x.gridPosition != opponent.gridPosition).Select(x => x.gridPosition).ToArray()); if (path.Count() > 1) { List <Tile> actualMovement = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint, GameManager.instance.players.Where(x => x.gridPosition != gridPosition).Select(x => x.gridPosition).ToArray()); path.Reverse(); if (path.Where(x => actualMovement.Contains(x)).Count() > 0) { GameManager.instance.moveCurrentPlayer(path.Where(x => actualMovement.Contains(x)).First()); } } } } // if (!attacking && !moving) { // actionPoints = 2; // GameManager.instance.nextTurn(); // return; // } if (actionPoints <= 1 && (attacking || moving)) { moving = false; attacking = false; } base.TurnUpdate(); }
public bool PathWalkable(List <Tile> p) { float MoveRange = GetMoveRange(); return(p != null && p.Count > 1 && TilePathFinder.GetPathLengthForUnit(Owner, p) <= MoveRange); }