Ejemplo n.º 1
0
    Detection.DIRECTIONS GetAltDirection(Detection.DIRECTIONS movingDir, Detection.DIRECTIONS targetDir)
    {
        // Before checking for alternate path
        // check if it still needs to move
        // if there isn't then you don't have to find an alternate path
        if (mouseDetection.CheckForEnemies() == false && mouseDetection.playerInSight == false)
        {
            StopMovement();
            mouseDetection.StopMovement();
            UpdateAnimation(false);

            return(Detection.DIRECTIONS.NONE);
        }

        Detection.DIRECTIONS tempDirection  = Detection.DIRECTIONS.NONE;
        Vector2Int           currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position);

        // loop through all the 4 directions
        for (int index = 0; index < (int)Detection.DIRECTIONS.NONE; ++index)
        {
            // dont check the other 2 directions
            if (index == (int)movingDir)
            {
                continue;
            }
            else if (index == (int)targetDir)
            {
                continue;
            }

            Detection.DIRECTIONS currDirection = (Detection.DIRECTIONS)index;

            Vector2Int targetTilePos = Vector2Int.zero;
            if (mouseDetection.targetObject != null)
            {
                targetTilePos = MapManager.Instance.GetWorldToTilePos(mouseDetection.targetObject.transform.position);
            }
            else
            {
                return(Detection.DIRECTIONS.NONE);
            }

            // if its only chcking along the X Axis
            if (currDirection == Detection.DIRECTIONS.LEFT || currDirection == Detection.DIRECTIONS.RIGHT)
            {
                // if its more than 2 blocks to the left or more than 2 blocks to the right
                // then it can go left or right depending on which has more space
                //                if (Mathf.Abs(targetTilePos.x) < Mathf.Abs(currentTilePos.x - 1) || Mathf.Abs(targetTilePos.x) > Mathf.Abs(currentTilePos.x + 1))
                if (targetTilePos.x > (currentTilePos.x - 1) && targetTilePos.x < (currentTilePos.x + 1))
                {
                    // if the number of empty tiles on the left is less than the right
                    if (CheckDirectionSize(Detection.DIRECTIONS.LEFT) < CheckDirectionSize(Detection.DIRECTIONS.RIGHT))
                    {
                        // the moving direction should be right
//
                        tempDirection = Detection.DIRECTIONS.RIGHT;
                    }
                    else
                    {
                        // else the moving direction should be left
                        //Debug.Log("Moving Left");
                        tempDirection = Detection.DIRECTIONS.LEFT;
                    }
                }
                // else it will need to go in the direction that is further away from the target object
                else
                {
                    // if the target is on the left side
                    if (targetTilePos.x <= transform.position.x)
                    {
                        // Debug.Log("Moving Right 2");

                        tempDirection = Detection.DIRECTIONS.RIGHT;

                        if (CheckDirectionSize(Detection.DIRECTIONS.RIGHT) <= 1)
                        {
                            tempDirection = Detection.DIRECTIONS.LEFT;
                        }
                        //return Detection.DIRECTIONS.RIGHT;
                    }
                    else
                    // the target is on the right
                    {
                        // Debug.Log("Moving Left 2");

                        tempDirection = Detection.DIRECTIONS.LEFT;

                        if (CheckDirectionSize(Detection.DIRECTIONS.LEFT) <= 1)
                        {
                            tempDirection = Detection.DIRECTIONS.RIGHT;
                        }

                        //return Detection.DIRECTIONS.LEFT;
                    }
                }
            }
            // if its only checking along the Y Axis
            else if (currDirection == Detection.DIRECTIONS.UP || currDirection == Detection.DIRECTIONS.DOWN)
            {
                // if its more than 2 blocks to the left or more than 2 blocks to the right
                // then it can go left or right depending on which has more space
                // if (targetTilePos.y < (currentTilePos.y - 1) || targetTilePos.y > (currentTilePos.y + 1))
                if (targetTilePos.y > (currentTilePos.y - 1) && targetTilePos.y < (currentTilePos.y + 1))
                {
                    // if the number of empty tiles on above is less than below
                    if (CheckDirectionSize(Detection.DIRECTIONS.UP) < CheckDirectionSize(Detection.DIRECTIONS.DOWN))
                    {
                        // the moving direction should be right
                        tempDirection = Detection.DIRECTIONS.DOWN;
                    }
                    else
                    {
                        // else the moving direction should be left
                        tempDirection = Detection.DIRECTIONS.UP;
                    }
                }
                else
                {
                    // the target is below
                    //TODO::DUCTTAPE TO THE MAX, MAKE SURE THERES SPACE BEFORE RUNNING
                    if (targetTilePos.y <= transform.position.y)
                    {
                        tempDirection = Detection.DIRECTIONS.UP;

                        //if up no space
                        if (CheckDirectionSize(Detection.DIRECTIONS.UP) <= 1)
                        {
                            tempDirection = Detection.DIRECTIONS.DOWN;
                        }
                    }
                    // the target is ontop
                    else
                    {
                        tempDirection = Detection.DIRECTIONS.DOWN;

                        if (CheckDirectionSize(Detection.DIRECTIONS.DOWN) <= 1)
                        {
                            tempDirection = Detection.DIRECTIONS.UP;
                        }
                    }
                }
            }

            // might not need this
            // might need this
            // i dont know
            break;
        }

        switch (tempDirection)
        {
        case Detection.DIRECTIONS.UP:
            currentTilePos.y++;
            break;

        case Detection.DIRECTIONS.DOWN:
            currentTilePos.y--;
            break;

        case Detection.DIRECTIONS.LEFT:
            currentTilePos.x--;
            break;

        case Detection.DIRECTIONS.RIGHT:
            currentTilePos.x++;
            break;

        case Detection.DIRECTIONS.NONE:
            break;

        default:
            break;
        }

        if (MapManager.Instance.IsThereTileOnMap(currentTilePos) == false)
        {
            // Check if the kitten is along that direction
            if (mouseDetection.CheckForKitten(tempDirection) == false)
            {
                return(tempDirection);
            }
        }


        return(Detection.DIRECTIONS.NONE);
    }