private void Walk()
    {
        Vector3Int      startCoord   = new Vector3Int((int)transform.position.x, (int)transform.position.y, 0);
        Vector3Int      endCoord     = new Vector3Int((int)player.transform.position.x, (int)player.transform.position.y, 0);
        List <GridNode> pathToPlayer = GetShortestAvailablePath(startCoord, endCoord);

        pathToPlayer.Reverse();

        //Debug.Log(pathToPlayer.Count);

        // This means we didn't reach to attack.
        if (pathToPlayer.Count > skeletonUnit.currentCombatPoints)
        {
            List <GridNode> newPath = new List <GridNode>();
            for (int i = 0; i < skeletonUnit.currentCombatPoints; ++i)
            {
                newPath.Add(pathToPlayer[i]);
            }
            Vector3Int[] path = new Vector3Int[newPath.Count];
            for (int i = 0; i < newPath.Count; ++i)
            {
                path[i] = new Vector3Int((int)newPath[i].position.x, (int)newPath[i].position.y, 0);
            }

            skeletonUnitMovement.StartMoving(path, path.Length, false);
        }
        else
        {
            // This means we have reached to attack. We also need to check for combat points if we have to attack.
            Vector3Int[] path = new Vector3Int[pathToPlayer.Count];
            for (int i = 0; i < pathToPlayer.Count; ++i)
            {
                path[i] = new Vector3Int((int)pathToPlayer[i].position.x, (int)pathToPlayer[i].position.y, 0);
            }
            bool isAttacking = false;
            if (path.Length + 2 <= skeletonUnit.currentCombatPoints)
            {
                isAttacking = true;
                Vector2 overlapCirclePos = new Vector2(endCoord.x + 0.5f, endCoord.y + 0.5f);

                Collider2D[] cols = Physics2D.OverlapCircleAll(overlapCirclePos, 0.33f);
                foreach (Collider2D col in cols)
                {
                    if (col.gameObject.tag == "Player")
                    {
                        skeletonUnit.currentTarget = col.gameObject;
                        break;
                    }
                }
            }
            skeletonUnitMovement.StartMoving(path, path.Length, isAttacking);
        }
    }
Beispiel #2
0
    /* Method that drives the logic when click-to-move is registered. Most of the logic was done when hovering so
     * there aren't many things more that we need to do. */
    private void PlayerMove()
    {
        if (CursorManager.instance.inUse)
        {
            return;
        }

        // Check for the mouse click as well as the validity of the path.
        bool isAttacking = CursorManager.instance.CheckState("ATTACK");

        if (Input.GetKeyDown(KeyCode.Mouse0) && (isAttacking || pathfindingTiles.Count != 0))
        {
            bool exploringState = GameStateManager.instance.CheckState("EXPLORING");
            bool combatState    = GameStateManager.instance.CheckState("COMBAT");

            // Once again, this is only valid if we are exploring or its our turn in combat.
            if (exploringState || (combatState && CombatManager.instance.whoseTurn == "Player"))
            {
                // If exploring, change state to 'moving', else just clear the generated attack grid.
                if (!combatState)
                {
                    GameStateManager.instance.ChangeState("MOVING");
                }
                else
                {
                    CombatManager.instance.movementTilemap.ClearAllTiles();
                }

                // Copy the pathfinding tiles into an array because when using pathfindingTiles.Count, it leads to issues if clicking very fast.
                Vector3Int[] path = new Vector3Int[pathfindingTiles.Count];
                pathfindingTiles.CopyTo(path);
                ClearPathfindingTiles();

                // Clening up on the UI side a bit.
                CursorManager.instance.SetCursor("DEFAULT", string.Empty);
                UIManager.instance.skipButton.interactable = false;
                UIManager.instance.waitButton.interactable = false;

                /* Registering our target and starting to move. The target may something we hovered on in the past, but
                 * if we are hovering on an enemy, the variable is always up to date. */
                playerUnit.currentTarget = target;
                playerMovement.StartMoving(path, path.Length, isAttacking);
            }
        }
    }