Esempio n. 1
0
        /// <summary>
        /// Uses an AsyncListOperation to pathfind to all exits of the _roomOfInterest
        /// This process is asynchronous and it sets _roomExitFindingComplete to true when done
        /// </summary>
        protected void PerformRoomExitPathFinding()
        {
            _pathObjectPairsForRoomRoomExitFinding = new List <PathObjectPair> ();
            _roomExitFindingComplete = false;

            AsyncListOperation <RoomConnection> listOperation =
                new AsyncListOperation <RoomConnection> (
                    _roomOfInterest.allConnections,
                    (
                        RoomConnection roomConnection,
                        System.Action callbackOperation
                    ) =>
            {
                _pathManager.RequestTask(
                    this,
                    roomConnection.graphicalRepresentation,
                    (PathObjectPair receivedPath) =>
                {
                    _pathObjectPairsForRoomRoomExitFinding.Add(receivedPath);
                    callbackOperation();
                }


                    );
            },
                    () =>
            {
                Debug.Log(_pathObjectPairsForRoomRoomExitFinding.Count);
                _movementGoal = _pathObjectPairsForRoomRoomExitFinding[0].GetObject2();

                _pathObjectPairsForRoomRoomExitFinding.Sort((PathObjectPair a, PathObjectPair b) =>
                {
                    return(a.GetPathLength() - b.GetPathLength());
                });
                _roomExitFindingComplete = true;
            }

                    );


            listOperation.RunParallel();
        }
Esempio n. 2
0
        public void SetMovementGoal(MovementGoalType movementGoalType)
        {
            _movementGoalType = movementGoalType;
            if (movementGoalType == MovementGoalType.PLAYER_ROOM)
            {
                _roomOfInterest = _levelLoadingManager
                                  .RoomGraphManager
                                  .GetRoomContainingObject(_player);
                movementGoalType = MovementGoalType.ROOM;
            }


            switch (movementGoalType)
            {
            case MovementGoalType.FOOTSTEP:
                SetMovementGoal(_footStepFactory.FirstFootStep, null);
                Task.current.Complete(true);
                break;

            case MovementGoalType.FOOTSTEP_PATH:
                SetMovementGoal(_footStepFactory.LatestFootStep, _footStepFactory.Path);
                Task.current.Complete(true);
                break;

            case MovementGoalType.HEARD_NOISE:  SetMovementGoal(_heardNoise); Task.current.Complete(true); break;

            case MovementGoalType.LOCKER:
            {
                if (_lockerManager.CurrentLocker != null)
                {
                    SetMovementGoal(_lockerManager.CurrentLocker.gameObject.transform.parent.transform.GetChild(1).transform.gameObject.GetComponent <DestinationSearch>());
                }
                Task.current.Complete(true); break;
            }

            case MovementGoalType.TARGET:       SetMovementGoal(_target); Task.current.Complete(true); break;

            case MovementGoalType.PLAYER:       SetMovementGoal(_player); Task.current.Complete(true); break;

            case MovementGoalType.DEAD_GUARD:   SetMovementGoal(_deadGuardController); Task.current.Complete(true); break;

            case MovementGoalType.COVER_TARGET:
            case MovementGoalType.COVER_SELF:
            {
                if (Task.current.isStarting)
                {
                    _coverPointFound = false;
                    _coverPoint      = null;
                    List <PathObjectPair> pops = new List <PathObjectPair> ();
                    List <Vector2>        blockedCoordinates = GetBlockedCoordinates();

                    AsyncListOperation <CoverPoint> listOperation =
                        new AsyncListOperation <CoverPoint>(
                            _coverPoints,
                            (
                                CoverPoint coverPoint,
                                System.Action callbackOperation
                            ) =>
                        {
                            _pathManager.RequestTask(
                                this,
                                coverPoint,
                                ( PathObjectPair pop ) =>
                            {
                                pops.Add(pop);
                                callbackOperation();
                            },
                                blockedCoordinates
                                );
                        },
                            () =>
                        {
                            pops.Sort((PathObjectPair a, PathObjectPair b) =>
                            {
                                float distanceA = a.GetPathLength();
                                float distanceB = b.GetPathLength();

                                if (distanceA == 0)
                                {
                                    distanceA = 999999;
                                }

                                if (distanceB == 0)
                                {
                                    distanceB = 999999;
                                }

                                return((int)distanceA - (int)distanceB);
                            });

                            GameObject coverFrom = gameObject;

                            if (movementGoalType == MovementGoalType.COVER_TARGET)
                            {
                                coverFrom = _target.gameObject;
                            }

                            foreach (var pop in pops)
                            {
                                RaycastHit2D hit = _mapHelper.Probe(
                                    coverFrom,
                                    ((MonoBehaviour)pop.GetObject2()).gameObject,
                                    new string[] { "CeilingLayer", "WallLayer" }
                                    );


                                if (hit.collider != null)
                                {
                                    _coverPoint = (CoverPoint)pop.GetObject2();
                                    SetMovementGoal(_coverPoint);
                                    _preComputedPathToMovementGoal = pop.GetPath();
                                    _coverPointFound = true;
                                    break;
                                }
                            }
                        }
                            );
                    listOperation.RunParallel();
                }
                else if (_coverPointFound)
                {
                    Task.current.Complete(true);
                }



                // _coverPoints.Sort( (CoverPoint a, CoverPoint b) =>
                // {

                //     float distanceA = (a.transform.position - transform.position).magnitude;
                //     float distanceB = (b.transform.position - transform.position).magnitude;
                //     return (int) distanceA - (int) distanceB;

                // });
                // GameObject coverFrom = gameObject;
                // if( movementGoalType == MovementGoalType.COVER_TARGET )
                // {
                //     coverFrom = _target.gameObject;
                // }
                // foreach( CoverPoint coverPoint in _coverPoints )
                // {
                //     RaycastHit2D hit = _mapHelper.Probe(
                //         coverFrom,
                //         coverPoint.gameObject,
                //         new string[] {"CeilingLayer", "WallLayer"}
                //     );


                //     if( hit.collider != null )
                //     {
                //         _coverPoint = coverPoint;
                //         break;
                //     }
                // }

                // // if no cover was found we should probably be more aggressive.
                // SetMovementGoal(_coverPoint);
                // Task.current.Complete(true);
            }; break;

            case MovementGoalType.ROOM:
            {
                if (Task.current.isStarting)
                {
                    PerformRoomExitPathFinding();
                }
                else if (_roomExitFindingComplete)
                {
                    SetMovementGoal(
                        _pathObjectPairsForRoomRoomExitFinding[0].GetObject2(),
                        _pathObjectPairsForRoomRoomExitFinding[0].GetPath()
                        );
                    Task.current.Complete(true);
                }
            }; break;
            }
        }