Ejemplo n.º 1
0
    public IEnumerator Move(Vector3Int cellTargetPos, float movementSpeed = 3, bool skipTurning = false)
    {
        TilemapNavigator navigator = TilemapNavigator.Instance;

        Vector3 startingPos    = transform.position;
        Vector3 targetWorldPos = navigator.CellToWorldPos(cellTargetPos);
        Vector3 targetPos      = new Vector3(targetWorldPos.x, targetWorldPos.y, UNIT_Z_POSITION);

        float movementDuration = 1 / movementSpeed;

        UnitBlockManager.Instance.traversalProvider.selectedUnitCell = CellPosition;
        Path path = ABPath.Construct(CellPosition, cellTargetPos, null);

        path.traversalProvider = tilesetTraversalProvider;
        AstarPath.StartPath(path);

        yield return(StartCoroutine(path.WaitForPath()));

        if (!path.error)
        {
            tilesetTraversalProvider.ReleaseNode(CellPosition);
            List <Vector3> waypoints = path.vectorPath.ConvertAll(node => navigator.CellToWorldPos(navigator.WorldToCellPos(node)));
            navigator.GetTile(CellPosition).OnUnitLeave(this);

            for (int currentTarget = 1; currentTarget < waypoints.Count; currentTarget++)
            {
                Vector3 currentStart = currentTarget == 1 ? startingPos : waypoints[currentTarget - 1];
                if (!skipTurning)
                {
                    yield return(StartCoroutine(CheckTurningAnimation(currentStart, waypoints[currentTarget])));
                }

                float elapsedTime = 0f;
                while (elapsedTime < movementDuration)
                {
                    transform.position = Vector3.Lerp(currentStart, waypoints[currentTarget], (elapsedTime / movementDuration));
                    elapsedTime       += Time.deltaTime;
                    yield return(null);
                }
            }

            navigator.GetTile(CellPosition).OnUnitEnter(this);
            tilesetTraversalProvider.ReserveNode(cellTargetPos);
            transform.position = targetPos;
            yield return(AnimateFlip(Player.FacingLeft));
        }
    }
Ejemplo n.º 2
0
    private PointNode CreateNode(Vector3Int cellPos)
    {
        TilemapNavigator navigator    = TilemapNavigator.Instance;
        LevelTile        tile         = navigator.GetTile(cellPos);
        PointNode        node         = new PointNode(active);
        Vector3          floatCellPos = cellPos;

        node.position = (Int3)floatCellPos;
        node.Walkable = tile.Type == TileType.Walkable;
        return(node);
    }
Ejemplo n.º 3
0
    void RenderAvailableAttacks(Unit unit)
    {
        if (unit == null)
        {
            return;
        }

        SkillConfig skillConfig = unit.GetSkillConfig(unit == ActivePlayer.CurrentUnit
            ? ActivePlayer.AttackMode
            : AttackModes.Attack
                                                      );

        if (skillConfig == null)
        {
            return;
        }

        TilemapNavigator navigator = TilemapNavigator.Instance;
        SerializableDictionary <Vector3Int, AttackPatternField> pattern = unit.skillHandler.AttackArea(skillConfig);

        if (pattern != null)
        {
            foreach (KeyValuePair <Vector3Int, AttackPatternField> field in pattern)
            {
                LevelTile targetTile = navigator.GetTile(field.Key);
                bool      hasTile    = targetTile != null;
                Unit      targetUnit = navigator.GetUnit(field.Key);

                if (field.Value == AttackPatternField.On && hasTile)
                {
                    bool canAttack = skillConfig.CanPerformAttack(unit, targetUnit, targetTile);
                    TintMarker(attackAreaTilemap, field.Key, canAttack && unit != HoveredUnit ? MarkerTypes.Attack : MarkerTypes.AttackPreview);
                    availableAttacks.Add(field.Key);
                }
            }
        }
    }
Ejemplo n.º 4
0
    protected override IEnumerable <Progress> ScanInternal()
    {
        TilemapNavigator navigator = TilemapNavigator.Instance;
        BoundsInt        mapBounds = navigator.GetTilemapBounds();
        Dictionary <int, Dictionary <int, PointNode> > gridNodes = new Dictionary <int, Dictionary <int, PointNode> >();

        // Find all tiles
        for (int xPos = mapBounds.xMin; xPos <= mapBounds.xMax; xPos++)
        {
            for (int yPos = mapBounds.yMin; yPos <= mapBounds.yMax; yPos++)
            {
                Vector3Int cellPos = GetCellPos(xPos, yPos);
                if (navigator.HasTile(cellPos))
                {
                    if (!gridNodes.ContainsKey(xPos))
                    {
                        gridNodes.Add(xPos, new Dictionary <int, PointNode>());
                    }

                    gridNodes[xPos].Add(yPos, CreateNode(cellPos));
                }
            }
        }

        // Connect adjacent tiles
        for (int xPos = mapBounds.xMin; xPos <= mapBounds.xMax; xPos++)
        {
            for (int yPos = mapBounds.yMin; yPos <= mapBounds.yMax; yPos++)
            {
                PointNode node = GetNodeInDict(gridNodes, xPos, yPos);
                if (node != null && node.Walkable)
                {
                    Vector3Int        cellPos     = GetCellPos(xPos, yPos);
                    LevelTile         tile        = navigator.GetTile(cellPos);
                    List <Connection> connections = new List <Connection>();
                    Vector2Int[]      neighbors   =
                    {
                        Vector2Int.up,
                        Vector2Int.down,
                        Vector2Int.left,
                        Vector2Int.right,
                    };

                    node.Penalty = tile.Cost;

                    foreach (Vector2Int offset in neighbors)
                    {
                        PointNode neighborNode = GetNodeInDict(gridNodes, xPos + offset.x, yPos + offset.y);
                        if (neighborNode != null && neighborNode.Walkable)
                        {
                            connections.Add(new Connection(neighborNode, 0));
                        }
                    }

                    node.connections = connections.ToArray();
                }
            }
        }


        List <PointNode> allNodes = new List <PointNode>();

        foreach (KeyValuePair <int, Dictionary <int, PointNode> > column in gridNodes)
        {
            foreach (KeyValuePair <int, PointNode> cell in column.Value)
            {
                allNodes.Add(cell.Value);
            }
        }
        nodes = allNodes.ToArray();
        yield break;
    }