示例#1
0
    private void AlignToGrid()
    {
        TilemapNavigator navigator       = TilemapNavigator.Instance;
        Vector3          alignedPosition = navigator.CellToWorldPos(navigator.WorldToCellPos(transform.position));

        transform.position = new Vector3(alignedPosition.x, alignedPosition.y, UNIT_Z_POSITION);
    }
示例#2
0
    public IEnumerator Push(WorldUtils.Direction direction, int distance)
    {
        TilemapNavigator navigator = TilemapNavigator.Instance;
        Vector3Int       step      = new Dictionary <WorldUtils.Direction, Vector3Int> {
            { WorldUtils.Direction.Up, Vector3Int.up },
            { WorldUtils.Direction.Down, Vector3Int.down },
            { WorldUtils.Direction.Left, Vector3Int.left },
            { WorldUtils.Direction.Right, Vector3Int.right },
        }[direction];

        Vector3Int?positionAfterPush = null;

        for (int i = 1; i <= distance; i++)
        {
            Vector3Int testedSpot = CellPosition + step * i;
            if (navigator.IsTileWalkable(testedSpot) && !navigator.IsTileTaken(testedSpot))
            {
                positionAfterPush = testedSpot;
            }
            else
            {
                break;
            }
        }

        if (positionAfterPush.HasValue)
        {
            yield return(StartCoroutine(Move(positionAfterPush.Value, 15, true)));
        }
    }
示例#3
0
    public IEnumerator Play(Vector3Int cellStart, Vector3Int cellEnd, DamageTrajectory trajectory)
    {
        TilemapNavigator navigator  = TilemapNavigator.Instance;
        Vector3          startWorld = navigator.CellToWorldPos(cellStart);
        Vector3          endWorld   = navigator.CellToWorldPos(cellEnd);
        Vector3          midPoint   = trajectory == DamageTrajectory.Curve ?
                                      GetArcMiddlePoint(startWorld, endWorld) : GetMiddlePoint(startWorld, endWorld);

        float movementSpeed = trajectory == DamageTrajectory.Curve ? 6f : 20f;
        float distance      = Vector3.Distance(startWorld, endWorld);

        float duration    = distance * 1 / movementSpeed;
        float elapsedTime = 0f;

        projectile.gameObject.SetActive(true);
        while (elapsedTime < duration)
        {
            float   currentState = elapsedTime / duration;
            Vector3 m1           = Vector3.Lerp(startWorld, midPoint, currentState);
            Vector3 m2           = Vector3.Lerp(midPoint, endWorld, currentState);

            projectile.position = Vector3.Lerp(m1, m2, currentState);
            elapsedTime        += Time.deltaTime;
            yield return(null);
        }
        projectile.gameObject.SetActive(false);
    }
示例#4
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);
    }
示例#5
0
    private void Awake()
    {
        mainCamera  = Camera.main;
        canvasRect  = GetComponent <RectTransform>();
        canvasGroup = GetComponent <CanvasGroup>();
        navigator   = TilemapNavigator.Instance;
        ResetTint(movementAreaTilemap);
        ResetTint(attackAreaTilemap);

        musicPlayer.volume = 0;
        overlay.SetFloat("_Alpha", 1);

        InvokeRepeating("HandleMouseHover", 0f, .1f);
    }
示例#6
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));
        }
    }
示例#7
0
    private SerializableDictionary <Vector3Int, AttackPatternField> CalculateStraightRange(SkillConfig config)
    {
        TilemapNavigator navigator = TilemapNavigator.Instance;

        List <Vector3Int> directions = new List <Vector3Int>()
        {
            Vector3Int.up,
            Vector3Int.down,
            Vector3Int.left,
            Vector3Int.right,
        };

        SerializableDictionary <Vector3Int, AttackPatternField> resultPattern =
            new SerializableDictionary <Vector3Int, AttackPatternField>()
        {
            { attackerUnit.CellPosition, AttackPatternField.Player }
        };

        foreach (Vector3Int direction in directions)
        {
            for (int step = 1; step <= config.straightRange; step++)
            {
                Vector3Int cellPos = attackerUnit.CellPosition + direction * step;

                if (!navigator.IsTileWalkable(cellPos))
                {
                    break;
                }
                if (navigator.IsTileTaken(cellPos))
                {
                    resultPattern.Add(cellPos, AttackPatternField.On);
                    break;
                }
                resultPattern.Add(cellPos, AttackPatternField.On);
            }
        }

        return(resultPattern);
    }
示例#8
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);
                }
            }
        }
    }
示例#9
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;
    }