コード例 #1
0
ファイル: ChildAI.cs プロジェクト: AlexTagun/DTF_jam_2020
    //Vector3 theLastMovingFormingStartingPlaceOnFloor_ = Vector3.zero;
    //Vector3 theOriginalCurrentTargetPoint_ = Vector3.zero;
    //Vector3 theCurrentTargetPoint_ = Vector3.zero;

    private List <MovingScheduleElement> buildMovingScheduleToPoint(Vector2 inPoint)
    {
        List <MovingScheduleElement> theResult = new List <MovingScheduleElement>();

        //Debug.Log("~~~~~~~~~~~{{{");
        Vector2 theCurrentPointOnFloor = getPointOnFloor(transform.position);
        //Debug.Log("~~~~~~~~~~~}}}");

        Vector2 theTargetPointOnFloor = getPointOnFloor(inPoint);

        //theLastMovingFormingStartingPlaceOnFloor_ = theCurrentPointOnFloor;
        //theOriginalCurrentTargetPoint_ = inPoint;
        //theCurrentTargetPoint_ = theTargetPointOnFloor;

        int theIterationsGuard = 10;

        while (true)
        {
            FloorDirection theDirection = getFloorDirectionToAchievePosition(theCurrentPointOnFloor.y, theTargetPointOnFloor.y);
            if (FloorDirection.None == theDirection)
            {
                break;
            }

            Stairs theRightNearestStairs = getNearestStairs(theCurrentPointOnFloor, true, theDirection);
            Stairs theLeftNearestStairs  = getNearestStairs(theCurrentPointOnFloor, false, theDirection);

            if (!theRightNearestStairs && !theLeftNearestStairs)
            {
                throw new System.Exception("Cannot find any stairs");
            }

            MovingScheduleElement theStairsScheduleElement = new MovingScheduleElement();
            theStairsScheduleElement.movingDirection = theRightNearestStairs ? MovingDirection.Right : MovingDirection.Left;
            theStairsScheduleElement.movingTarget    = MovingTarget.MoveUntilStairs;
            theStairsScheduleElement.targetStairs    = theRightNearestStairs ? theRightNearestStairs : theLeftNearestStairs;
            theResult.Add(theStairsScheduleElement);

            theCurrentPointOnFloor = getPointOnFloor(theStairsScheduleElement.targetStairs.endPointPosition);

            if (--theIterationsGuard < 0)
            {
                throw new System.Exception("Too many pathfind iterationis");
            }
        }

        MovingScheduleElement theScheduleElement = new MovingScheduleElement();

        theScheduleElement.movingDirection = (theTargetPointOnFloor.x > theCurrentPointOnFloor.x) ? MovingDirection.Right : MovingDirection.Left;
        theScheduleElement.movingTarget    = MovingTarget.MoveUntilPoint;
        theScheduleElement.targetPoint     = theTargetPointOnFloor;
        theResult.Add(theScheduleElement);

        //Debug.Log("PATH REQUEST [" + gameObject.name + "] {{{");
        //foreach (MovingScheduleElement theElement in theResult)
        //    Debug.Log("[" + theElement.movingDirection + "|" + theElement.movingTarget + "|" + theElement.targetPoint + "|" + theElement.targetStairs + "]");
        //Debug.Log("}}}");

        return(theResult);
    }
コード例 #2
0
ファイル: ChildAI.cs プロジェクト: AlexTagun/DTF_jam_2020
    private IEnumerator executeMovingScheduleCoroutine(List <MovingScheduleElement> inSchedule, System.Action inOnEndCallback)
    {
        float theMinimumDistance = 2f;

        List <MovingScheduleElement> theSchedule = new List <MovingScheduleElement>(inSchedule);

        Boxed <MovingDirection> theStairsMovingDirection = null;

        System.Action theGetNextScheduleElement = () => {
            theStairsMovingDirection = null;
            theSchedule.RemoveAt(0);
        };

        while (theSchedule.Count > 0)
        {
            while (pauseAI)
            {
                yield return(null);
            }

            MovingScheduleElement theCurrentScheduleElement = theSchedule[0];

            MovingDirection theActualMovingDirection = theCurrentScheduleElement.movingDirection;

            switch (theCurrentScheduleElement.movingTarget)
            {
            case MovingTarget.MoveUntilStairs:
                if (null == theStairsMovingDirection)
                {
                    if (movementComponent.stairsThatMayBeUsed == theCurrentScheduleElement.targetStairs)
                    {
                        theStairsMovingDirection = new Boxed <MovingDirection>(
                            movementComponent.stairsThatMayBeUsed.isRightOriented ? MovingDirection.Right : MovingDirection.Left);
                        movementComponent.useStairs();

                        theActualMovingDirection = theStairsMovingDirection.value;
                    }
                }
                else
                {
                    theActualMovingDirection = theStairsMovingDirection.value;
                    if (!movementComponent.isUsingStairs)
                    {
                        theGetNextScheduleElement();
                    }
                }
                break;

            case MovingTarget.MoveUntilPoint:
                float theDistance = (new Vector2(transform.position.x, transform.position.y) -
                                     theCurrentScheduleElement.targetPoint).magnitude;
                if (theDistance < theMinimumDistance)
                {
                    theGetNextScheduleElement();
                }
                break;
            }

            switch (theActualMovingDirection)
            {
            case MovingDirection.Left:    movementComponent.moveLeft();   break;

            case MovingDirection.Right:   movementComponent.moveRight();  break;
            }

            if (updateShouldDropMovingScheduleForStackingWorkaround())
            {
                theSchedule.Clear();
                break;
            }

            yield return(null);
        }

        if (null != inOnEndCallback)
        {
            inOnEndCallback();
        }
    }