Ejemplo n.º 1
0
    public List <Vector2i> DecideNextMove(int targetX, int targetY, bool staticOnly)
    {
        // if targetX,targetY is blocked, refuse to pathfind.
        if (!Interaction.CheckWalkableForUnit(targetX, targetY, staticOnly))
        {
            return(null);
        }

        // init astar searcher
        if (AstarSearcherH == null)
        {
            AstarSearcherH = new UnitAstarHelper(this);
        }
        if (AstarSearcher == null)
        {
            AstarSearcher = new ShortestPathGraphSearch <Vector2i, Vector2i>(AstarSearcherH);
        }
        AstarSearcherH.StaticLookup = staticOnly;

        try
        {
            List <Vector2i> nodes = AstarSearcher.GetShortestPath(new Vector2i(X, Y), new Vector2i(targetX, targetY));
            if (nodes == null)
            {
                return(null);
            }
            nodes.Add(new Vector2i(targetX, targetY));
            return(nodes);
        }
        catch (Exception)
        {
            return(null);
        }
    }
Ejemplo n.º 2
0
    public void TestNoObstacles()
    {
        ShortestPathGraphSearch <Vector2i, Vector2i> search = new ShortestPathGraphSearch <Vector2i, Vector2i>(this);
        List <Vector2i> list = search.GetShortestPath(new Vector2i(1, 0), new Vector2i(3, 0));

        Assert.Equals(2, list.Count);
        Debug.Log("Test No Obstacles");
        foreach (Vector2i pos in list)
        {
            Debug.Log("Position " + pos);
        }
    }
Ejemplo n.º 3
0
    public void TestObstaclesNoDiagonal()
    {
        moveDiagonal = false;
        ShortestPathGraphSearch <Vector2i, Vector2i> search = new ShortestPathGraphSearch <Vector2i, Vector2i>(this);
        List <Vector2i> list = search.GetShortestPath(new Vector2i(-1, 0), new Vector2i(1, 0));

        Assert.Equals(4, list.Count);
        Debug.Log("Test Obstacles");
        foreach (Vector2i pos in list)
        {
            Debug.Log("Position " + pos);
        }
    }
Ejemplo n.º 4
0
    /// <summary>
    /// To be called, when a Hex is clicked while the game is in Battle state.
    /// Processes and decides how the input should be handled.
    /// </summary>
    /// <param name="targetHex"></param>
    private void OnHexClickedBattle(HexTile targetHex)
    {
        if (!_selectedUnit)
        {
            SelectHex(targetHex);
            return;
        }

        //Still animating.
        if (_activeUnit.IsBusy || _activeUnit.IsAnySkillBusy)
        {
            return;
        }

        if (targetHex.IsOccupied)
        {
            HexUnit occupyingUnit = targetHex.OccupyingObject.GetComponent <HexUnit>();

            if (occupyingUnit)
            {
                //Ability will handle it.
                if (_hexTileCache.Contains(CACHE_DIRECT_ACTION, targetHex))
                {
                    //Deactivate normal attacks to prevent double cast.
                    if (_activeUnit.MeleeAttack)
                    {
                        _activeUnit.MeleeAttack.Deactivate();
                    }
                    if (_activeUnit.RangedAttack)
                    {
                        _activeUnit.RangedAttack.Deactivate();
                    }
                    return;
                }

                if (_hexTileCache.Contains(CACHE_DIRECT_ATTACK, targetHex))
                {
                    //Deactivate active ability to prevent double cast.
                    Ability activeSkill = _activeUnit.ActiveSkill();
                    if (activeSkill)
                    {
                        activeSkill.Deactivate();
                    }

                    if (_activeUnit.RangedAttack)
                    {
                        if (HexCoord.Distance(targetHex.Coord, _selectedHex.Coord) > 1)
                        {
                            _activeUnit.MeleeAttack.Deactivate();
                            _activeUnit.RangedAttack.ActivateAutoCast(occupyingUnit.gameObject);
                            return;
                        }
                        _activeUnit.RangedAttack.Deactivate();
                    }
                    _activeUnit.MeleeAttack.ActivateAutoCast(occupyingUnit.gameObject);
                    return;
                }

                //Look if we can melee attack with movement.
                //Only check cached attack tuples found with Breadth First Search.
                List <HexCoord> shortestPath = null;
                foreach (Tuple <HexTile, HexTile> tuple in _cachedMoveActions.Where(c => c.second == targetHex))
                {
                    List <HexCoord> path = _pathGraphSearch.GetShortestPath(_selectedHex.Coord, tuple.first.Coord);
                    if (shortestPath == null)
                    {
                        shortestPath = path;
                    }
                    else if (shortestPath.Count > path.Count)
                    {
                        shortestPath = path;
                    }

                    //Can't find a shorter path, early exit.
                    if (shortestPath.Count == 1)
                    {
                        break;
                    }
                }
                if (shortestPath != null && shortestPath.Count > 0)
                {
                    _queuedActions.Enqueue(() => _activeUnit.MeleeAttack.ActivateAutoCast(occupyingUnit.gameObject));
                    _activeUnit.Move(_selectedHex, shortestPath, _hexGrid);
                    return;
                }
            }
            SelectHex(targetHex);
        }
        else
        {
            //If we can move there, move.
            if (_hexTileCache.Contains(CACHE_MOVES, targetHex))
            {
                //Deactivate active ability to prevent double cast.
                Ability activeSkill = _activeUnit.ActiveSkill();
                if (activeSkill)
                {
                    activeSkill.Deactivate();
                }
                //Deactivate normal attacks to prevent double cast.
                if (_activeUnit.MeleeAttack)
                {
                    _activeUnit.MeleeAttack.Deactivate();
                }
                if (_activeUnit.RangedAttack)
                {
                    _activeUnit.RangedAttack.Deactivate();
                }

                List <HexCoord> path = _pathGraphSearch.GetShortestPath(_selectedHex.Coord, targetHex.Coord);
                if (path != null && path.Count > 0)
                {
                    _activeUnit.Move(_selectedHex, path, _hexGrid);
                }
            }
            else
            {
                SelectHex(targetHex);
            }
        }
    }