Пример #1
0
 public NavigationRequest(NavEntity e, Vector3 dest)
 {
     entity      = e;
     destination = dest;
     state       = ENavReqState.WaitForProcess;
     reqID       = entity.entityID;
 }
Пример #2
0
 public NavHandleData(NavigationRequest req, NavEntity targetEntity)
 {
     entity           = targetEntity;
     sourceRequest    = req;
     entityID         = targetEntity.entityID;
     _movementRequest = new MovementRequest
     {
         entityID = entityID
     };
     isGroup = targetEntity.navEntityType == ENavEntityType.Group;
     if (isGroup)
     {
         _childEntityDataList = new List <NavHandleData>();
         NavGroup group = (NavGroup)targetEntity;
         for (int i = 0; i < group.individualList.Count; i++)
         {
             _childEntityDataList.Add(new NavHandleData(req, group.individualList[i]));
             // 将成员添加进RVO的模拟列表中
             Simulator.Instance.addAgent(
                 group.individualList[i].controlledAgent.GetCurrentPosition().ToRVOVec2()
                 , ENTITY_COLLIDER_RADIUS, 8, 1f, 2f, 1f, NavEntity.GetMaxSpeed(group.individualList[i].entityID), new RVO.Vector2(0, 0));
         }
     }
     destination   = req.destination;
     startPosition = targetEntity.controlledAgent.GetCurrentPosition();
 }
    public override void ProcessStep()
    {
        _childData = navHandleData.GetChildNavData();
        if (_childData != null)
        {
            NavGroup group = (NavGroup)navHandleData.entity;
            for (int i = 0; i < _childData.Count; i++)
            {
                _slotInfo = group.GetSlotInfoByEntityID(_childData[i].entityID);
                if (_slotInfo == null)
                {
                    continue;
                }

                float speed    = _childData[i].entity.maxSpeed;
                float distance = Vector3.Distance(NavEntity.GetCurrentPosition(_childData[i].entityID), _slotInfo.slotWorldPosition) *
                                 (1f / NavHandleData.NAV_TICK_TIME);
                // 例如 一秒内距离小于3 速度为3 则速度为初始最大不变
                // 若距离大于3 例如6 则为两倍速
                if (distance > speed)
                {
                    speed = (distance / speed) * speed;
                }

                _childData[i].realVelocity = (_slotInfo.slotWorldPosition - NavEntity.GetCurrentPosition(_childData[i].entityID)).normalized * speed;
                Simulator.Instance.setAgentPrefVelocity(i, _childData[i].realVelocity.ToRVOVec2());
                //var dir = navHandleData.destination.XZ() - NavEntity.GetCurrentPosition(_childData[i].entityID).XZ();
                //Simulator.Instance.setAgentPrefVelocity(i, RVOMath.normalize(dir.ToRVOVec2()));
            }
        }
    }
Пример #4
0
 public void AssignFormation(FormationPattern formationPattern)
 {
     _formationPattern = formationPattern;
     AssignSlots(this.controlledAgent.GetCurrentPosition(), this.controlledAgent.GetForward());
     for (int i = 0; i < _formationPattern.slotInfoList.Count; i++)
     {
         NavEntity.InitAgent(_formationPattern.slotInfoList[i].entityID, _formationPattern.slotInfoList[i].slotWorldPosition);
     }
 }
Пример #5
0
 public override void EnablePowerUp()
 {
     base.EnablePowerUp();
     CancelInvoke();
     _animator.SetInteger("state", (int)AnimationStates.Action2);
     NavEntity.SetSpeed(
         GameController.Instance.PLAYER_DEFAULT_SPEED *
         GameController.Instance.KNIGHT_POWERUP_SPEED_MUL);
 }
Пример #6
0
 public override void DisablePowerUp()
 {
     base.DisablePowerUp();
     _animator.SetInteger("state", (int)AnimationStates.Idle);
     NavEntity.SetSpeed(GameController.Instance.PLAYER_DEFAULT_SPEED);
     if (_fireKnivesCoroutine != null)
     {
         StopCoroutine(_fireKnivesCoroutine);
     }
 }
Пример #7
0
 public Vector3 GetPathfindingVelocity()
 {
     if (wayPointList != null && nextWaypointIndex < wayPointList.Count)
     {
         return((wayPointList[nextWaypointIndex].XZ() - NavEntity.GetCurrentPosition(entityID).XZ()).normalized
                //* NavEntity.GetMaxSpeed(entityID);
                * GetChildMinSpeed());
     }
     return(Vector3.zero);
 }
Пример #8
0
 public virtual void Die()
 {
     CanBeDamaged = false;
     IsDead       = true;
     CancelInvoke();
     NavEntity.DisableMoving();
     DisableSkill();
     DisablePowerUp();
     _animator.SetInteger("state", (int)AnimationStates.Dead);
 }
Пример #9
0
 public override void EnablePowerUp()
 {
     base.EnablePowerUp();
     _animator.SetInteger("state", (int)AnimationStates.Action2);
     NavEntity.SetSpeed(
         GameController.Instance.PLAYER_DEFAULT_SPEED *
         GameController.Instance.MAGE_POWERUP_SPEED_MUL);
     _lastNodeIndexes = new Vector2(
         NavEntity.LastIndexes.Item1,
         NavEntity.LastIndexes.Item2);
 }
Пример #10
0
 private void HandleMovement()
 {
     foreach (KeyValuePair <Guid, MovementRequest> tPair in _movementReqDic)
     {
         if (tPair.Value.velocity == Vector3.zero)
         {
             continue;
         }
         NavEntity.DoMovement(tPair.Key, tPair.Value);
     }
 }
 public void SetDestination(NavEntity navEntity)
 {
     if (navEntity == null)
     {
         GameManager.Instance.DealDamage(damage);
         ReturnToPool();
         return;
     }
     currentNavEntity = navEntity;
     destination      = (Vector2)navEntity.transform.position + moveOffset * navEntity.GetRadius();
 }
Пример #12
0
 public override void EnablePowerUp()
 {
     base.EnablePowerUp();
     NavEntity.SetSpeed(GameController.Instance.PLAYER_DEFAULT_SPEED *
                        GameController.Instance.ELF_POWERUP_SPEED_MUL);
     _animator.SetInteger("state", (int)AnimationStates.Action2);
     if (_fireKnivesCoroutine != null)
     {
         StopCoroutine(_fireKnivesCoroutine);
     }
     _fireKnivesCoroutine = StartCoroutine(FireKnives());
 }
Пример #13
0
 protected virtual void Die()
 {
     NavEntity.DisableMoving();
     ActionState  = EnemyActionState.Dead;
     CanBeDamaged = false;
     CanDamage    = false;
     IsDead       = true;
     _animator.SetInteger("state", (int)AnimationStates.Dead);
     Invoke("RecoverFromDeathAnimation", GameController.Instance.DEATH_ANIMATION_INTERVAL);
     GameEvents.Instance.OnEnemyDead(new GameEvents.OnEnemyDeadEventArgs {
         EnemyType = EnemyType
     });
 }
Пример #14
0
        public override void Initialize(int x, int y)
        {
            base.Initialize(x, y);

            CanBeDamaged = false;
            IsDead       = false;
            CancelInvoke();
            NavEntity.DisableMoving();
            DisablePowerUp();
            _animator.SetInteger("state", (int)AnimationStates.Idle);
            MovementDirection = MovementDirections.Left;
            NavEntity.SetSpeed(GameController.Instance.PLAYER_DEFAULT_SPEED);
        }
Пример #15
0
 protected virtual void RecoverFromDamage()
 {
     if (Life == 0)
     {
         Die();
     }
     else
     {
         _animator.SetInteger("state", (int)AnimationStates.Idle);
         NavEntity.EnableMoving();
         CanBeDamaged = true;
         CanDamage    = true;
     }
 }
Пример #16
0
        public override void Initialize(int x, int y)
        {
            base.Initialize(x, y);

            ActionState  = EnemyActionState.Waiting;
            Life         = GameController.Instance.MEDIUM_LIFE;
            IsDead       = false;
            CanBeDamaged = false;
            CanDamage    = false;
            _animator.SetInteger("state", (int)AnimationStates.Idle);
            NavEntity.SetSpeed(GameController.Instance.ENEMY_MEDIUM_SPEED);
            NavEntity.DisableMoving();
            CancelInvoke();
        }
Пример #17
0
        protected override void Update()
        {
            base.Update();
            if (Spawned)
            {
                switch (ActionState)
                {
                case EnemyActionState.Following:
                    if (NavEntity.CanMove && NavEntity.ReachedDestination && !IsDead)
                    {
                        //follow player
                        var currentNode =
                            GameController.Instance.LevelManager.NavGraph.GetNode(
                                NavEntity.LastIndexes.Item1, NavEntity.LastIndexes.Item2);

                        var path =
                            GameController.Instance.LevelManager.NavGraph.ShortestPath(
                                currentNode, GameController.Instance.GetPlayerCurrentNode(),
                                new System.Collections.Generic.List <Pacman.NodeType>()
                        {
                            Pacman.NodeType.NonWalkable
                        });

                        //moves a max of 3 nodes per time
                        NavEntity.SetPath(path.GetRange(0, Mathf.Min(5, path.Count)));
                    }
                    break;

                case EnemyActionState.Fleeing:
                    if (NavEntity.CanMove && NavEntity.ReachedDestination && !IsDead)
                    {
                        //avoid player
                        var currentNode =
                            GameController.Instance.LevelManager.NavGraph.GetNode(
                                NavEntity.LastIndexes.Item1, NavEntity.LastIndexes.Item2);

                        var path =
                            GameController.Instance.LevelManager.NavGraph.FarthestPath(
                                currentNode, GameController.Instance.GetPlayerCurrentNode(),
                                new System.Collections.Generic.List <Pacman.NodeType>()
                        {
                            Pacman.NodeType.NonWalkable
                        });

                        NavEntity.SetPath(path);
                    }
                    break;
                }
            }
        }
Пример #18
0
    public void RequestNavigation(NavEntity entity, Vector3 targetPos)
    {
        NavigationRequest req = new NavigationRequest(entity, targetPos);

        switch (req.entity.navEntityType)
        {
        case ENavEntityType.Group:
            _groupPipeline.AddRequest(req);
            break;

        case ENavEntityType.Individual:
            _indiviualPipeline.AddRequest(req);
            break;
        }
    }
Пример #19
0
 private void CompleteDodge()
 {
     if (PowerUpIsActive)
     {
         NavEntity.SetSpeed(
             GameController.Instance.PLAYER_DEFAULT_SPEED *
             GameController.Instance.ELF_POWERUP_SPEED_MUL);
     }
     else
     {
         NavEntity.SetSpeed(GameController.Instance.PLAYER_DEFAULT_SPEED);
         _animator.SetInteger("state", (int)AnimationStates.Idle);
         CanBeDamaged = true;
     }
 }
    private void CalculateRemainingDistance()
    {
        if (currentNavEntity == null)
        {
            distanceRemaining = Mathf.Infinity;
            return;
        }
        float distance = 0;

        distance += (destination - (Vector2)transform.position).magnitude;
        NavEntity navEntity = currentNavEntity;

        while (navEntity.GetNext() != null)
        {
            distance += (navEntity.GetNext().transform.position - navEntity.transform.position).magnitude;
            navEntity = navEntity.GetNext();
        }
        distanceRemaining = distance;
    }
Пример #21
0
        protected override void Update()
        {
            if (IsDead)
            {
                return;
            }

            if (Input.GetKeyDown(KeyCode.Space) && SkillIsReady && !PowerUpIsActive)
            {
                //perform dodge
                CanBeDamaged = false;
                NavEntity.SetSpeed(
                    GameController.Instance.PLAYER_DEFAULT_SPEED *
                    GameController.Instance.ELF_DODGE_SPEED);
                _animator.SetInteger("state", (int)AnimationStates.Action1);
                Invoke("CompleteDodge", GameController.Instance.ELF_DODGE_DURATION);

                MovementDirections oppositeDirection = MovementDirections.None;
                switch (MovementDirection)
                {
                case MovementDirections.Up:
                    oppositeDirection = MovementDirections.Down;
                    break;

                case MovementDirections.Down:
                    oppositeDirection = MovementDirections.Up;
                    break;

                case MovementDirections.Left:
                    oppositeDirection = MovementDirections.Right;
                    break;

                case MovementDirections.Right:
                    oppositeDirection = MovementDirections.Left;
                    break;
                }
                var arrow = Instantiate(_arrowPrefab).GetComponent <Arrow>();
                arrow.Initialize(transform.position, oppositeDirection);
                GameEvents.Instance.OnPlayerUsedSkill(new GameEvents.OnPlayerUsedSkillEventArgs());
            }

            base.Update();
        }
Пример #22
0
 public void UpdateWayPointIndex()
 {
     if (wayPointList != null)
     {
         if (nextWaypointIndex < wayPointList.Count)
         {
             if (Vector3.Distance(
                     NavEntity.GetCurrentPosition(entityID).XZ(),
                     wayPointList[nextWaypointIndex].XZ()
                     ) < CHECKED_DISTANCE)
             {
                 nextWaypointIndex++;
                 if (nextWaypointIndex == wayPointList.Count)
                 {
                     Debug.Log("寻路路径已经走完");
                 }
             }
         }
     }
 }
Пример #23
0
        protected virtual void ReceiveDamage()
        {
            if (!CanBeDamaged)
            {
                return;
            }

            Life--;
            if (Life == 0)
            {
                Die();
            }
            else
            {
                NavEntity.DisableMoving();
                CanBeDamaged = false;
                CanDamage    = false;
                _animator.SetInteger("state", (int)AnimationStates.Hurt);
                Invoke("RecoverFromDamage", GameController.Instance.RECOVER_INVERVAL);
            }
        }
Пример #24
0
        private void Flee()
        {
            if (NavEntity.CanMove && NavEntity.ReachedDestination && !IsDead)
            {
                //avoid player
                var currentNode =
                    GameController.Instance.LevelManager.NavGraph.GetNode(
                        NavEntity.LastIndexes.Item1, NavEntity.LastIndexes.Item2);

                var path =
                    GameController.Instance.LevelManager.NavGraph.FarthestPath(
                        currentNode, GameController.Instance.GetPlayerCurrentNode(),
                        new System.Collections.Generic.List <Pacman.NodeType>()
                {
                    Pacman.NodeType.NonWalkable
                });

                //moves a max of 2 nodes per time
                NavEntity.SetPath(path);
            }
        }
Пример #25
0
    public override void ProcessStep()
    {
        if (navHandleData.isGroup)
        {
            _cachedGroup = (navHandleData.entity as NavGroup);
            if (_cachedGroup == null)
            {
                Debug.LogError("组对象为空!");
                return;
            }

            Vector3 currentPosition = NavEntity.GetCurrentPosition(navHandleData.entityID);
            Vector3 nextPosition    = currentPosition + navHandleData.realVelocity * NavHandleData.NAV_TICK_TIME;
            _cachedGroup.AssignSlots(nextPosition, navHandleData.realVelocity.normalized);
            _childData = navHandleData.GetChildNavData();
            for (int i = 0; i < _childData.Count; i++)
            {
                _childData[i].slotPositionWhenAsChild = _cachedGroup.GetSlotInfoByEntityID(_childData[i].entityID).slotWorldPosition;
            }
            Debug.DrawLine(currentPosition, nextPosition, Color.magenta, NavHandleData.NAV_TICK_TIME);
        }
    }
Пример #26
0
    private bool CheckChildrenReached()
    {
        if (isGroup == false || _childEntityDataList.Count == 0)
        {
            return(true);
        }
        else
        {
            bool allReached = true;
            for (int i = 0; i < _childEntityDataList.Count; i++)
            {
                if (Vector3.Distance(NavEntity.GetCurrentPosition(_childEntityDataList[i].entityID).XZ(), _childEntityDataList[i].slotPositionWhenAsChild.XZ()) >
                    CHECKED_DISTANCE)
                {
                    allReached = false;
                    break;
                }
            }

            return(allReached);
        }
    }
Пример #27
0
        // Update is called once per frame
        protected override void Update()
        {
            base.Update();
            //player moves on tile per time

            if (!Initialized)
            {
                Debug.Log("Initialize player first");
                return;
            }

            //read player's input
            var inputDirection = MovementDirection;

            if (Input.GetKey(KeyCode.UpArrow))
            {
                inputDirection = MovementDirections.Up;
            }
            else if (Input.GetKey(KeyCode.DownArrow))
            {
                inputDirection = MovementDirections.Down;
            }
            else if (Input.GetKey(KeyCode.LeftArrow))
            {
                inputDirection = MovementDirections.Left;
            }
            else if (Input.GetKey(KeyCode.RightArrow))
            {
                inputDirection = MovementDirections.Right;
            }

            //it means the entity has stopped
            if (NavEntity.CanMove && NavEntity.ReachedDestination)
            {
                GameEvents.Instance.OnPlayerWalkedTile(
                    new GameEvents.PlayerWalkedTileEventArgs()
                {
                    indexX = NavEntity.LastIndexes.Item1,
                    indexY = NavEntity.LastIndexes.Item2
                });

                bool changedDirection = false;

                if (inputDirection != MovementDirection)
                {
                    int xIndexShiftNewDirection = 0;
                    int yIndexShiftNewDirection = 0;

                    //calc the direction on grid coordinates for new input
                    switch (inputDirection)
                    {
                    case MovementDirections.Up:
                        xIndexShiftNewDirection = 0;
                        yIndexShiftNewDirection = 1;
                        break;

                    case MovementDirections.Down:
                        xIndexShiftNewDirection = 0;
                        yIndexShiftNewDirection = -1;
                        break;

                    case MovementDirections.Left:
                        xIndexShiftNewDirection = -1;
                        yIndexShiftNewDirection = 0;
                        break;

                    case MovementDirections.Right:
                        xIndexShiftNewDirection = 1;
                        yIndexShiftNewDirection = 0;
                        break;
                    }

                    //try finding a walkable node in the new direction
                    Pacman.NavNode nodeInDirection =
                        GameController.Instance.LevelManager.NavGraph.GetNode(
                            (int)Math.Round(NavEntity.Position.Item1) + xIndexShiftNewDirection,
                            (int)Math.Round(NavEntity.Position.Item2) + yIndexShiftNewDirection);

                    //found a node, change player's path and direction
                    if (nodeInDirection != null && nodeInDirection.NodeType != Pacman.NodeType.NonWalkable)
                    {
                        changedDirection  = true;
                        MovementDirection = inputDirection;
                        NavEntity.SetPath(
                            new System.Collections.Generic.List <Pacman.NavNode>()
                        {
                            nodeInDirection
                        });
                    }
                }

                //try to move in same direction
                if (!changedDirection)
                {
                    int xIndexShiftOldDirection = 0;
                    int yIndexShiftOldDirection = 0;

                    //calc the direction on grid coordinates for new input
                    switch (MovementDirection)
                    {
                    case MovementDirections.Up:
                        xIndexShiftOldDirection = 0;
                        yIndexShiftOldDirection = 1;
                        break;

                    case MovementDirections.Down:
                        xIndexShiftOldDirection = 0;
                        yIndexShiftOldDirection = -1;
                        break;

                    case MovementDirections.Left:
                        xIndexShiftOldDirection = -1;
                        yIndexShiftOldDirection = 0;
                        break;

                    case MovementDirections.Right:
                        xIndexShiftOldDirection = 1;
                        yIndexShiftOldDirection = 0;
                        break;
                    }

                    //try finding a walkable node in the old direction
                    Pacman.NavNode nodeInDirection =
                        GameController.Instance.LevelManager.NavGraph.GetNode(
                            (int)Math.Round(NavEntity.Position.Item1) + xIndexShiftOldDirection,
                            (int)Math.Round(NavEntity.Position.Item2) + yIndexShiftOldDirection);

                    //found a node, change player's target
                    if (nodeInDirection != null && nodeInDirection.NodeType != Pacman.NodeType.NonWalkable)
                    {
                        NavEntity.SetPath(
                            new System.Collections.Generic.List <Pacman.NavNode>()
                        {
                            nodeInDirection
                        });
                    }
                }
            }

            if (MovementDirection == MovementDirections.Left)
            {
                transform.localScale = new Vector3(-1, 1, 1);
            }
            else if (MovementDirection == MovementDirections.Right)
            {
                transform.localScale = new Vector3(1, 1, 1);
            }
        }
Пример #28
0
 public void WalkOutsideSpawnZone(List <Pacman.NavNode> path)
 {
     NavEntity.SetPath(path);
     NavEntity.EnableMoving();
     StartCoroutine(WaitWalkingOutsideSpawnZone());
 }
Пример #29
0
        protected override void Update()
        {
            base.Update();
            if (Spawned)
            {
                switch (ActionState)
                {
                case EnemyActionState.Following:

                    //test condictions for firing the magi ball
                    if (CanDamage)
                    {
                        if (_magicTimer > GameController.Instance.NECRO_MAGIC_COOLDOWN)
                        {
                            _magicTimer = 0;
                            //fires the magic
                            var directions = System.Enum.GetValues(typeof(MovementDirections));
                            foreach (MovementDirections direction in directions)
                            {
                                if (direction != MovementDirections.None)
                                {
                                    var fireball = Instantiate(_magicPrefab).GetComponent <Fireball>();
                                    fireball.Initialize(transform.position, direction, GameController.Instance.NECRO_MAGIC_SPEED);
                                }
                            }
                        }
                        else
                        {
                            _magicTimer += Time.deltaTime;
                        }
                    }


                    if (NavEntity.CanMove && NavEntity.ReachedDestination && !IsDead)
                    {
                        //follow player
                        var currentNode =
                            GameController.Instance.LevelManager.NavGraph.GetNode(
                                NavEntity.LastIndexes.Item1, NavEntity.LastIndexes.Item2);

                        var path =
                            GameController.Instance.LevelManager.NavGraph.ShortestPath(
                                currentNode, GameController.Instance.GetPlayerCurrentNode(),
                                new System.Collections.Generic.List <Pacman.NodeType>()
                        {
                            Pacman.NodeType.NonWalkable
                        });

                        //moves a max of 3 nodes per time
                        NavEntity.SetPath(path.GetRange(0, Mathf.Min(3, path.Count)));
                    }
                    break;

                case EnemyActionState.Fleeing:
                    if (NavEntity.CanMove && NavEntity.ReachedDestination && !IsDead)
                    {
                        //avoid player
                        var currentNode =
                            GameController.Instance.LevelManager.NavGraph.GetNode(
                                NavEntity.LastIndexes.Item1, NavEntity.LastIndexes.Item2);

                        var path =
                            GameController.Instance.LevelManager.NavGraph.FarthestPath(
                                currentNode, GameController.Instance.GetPlayerCurrentNode(),
                                new System.Collections.Generic.List <Pacman.NodeType>()
                        {
                            Pacman.NodeType.NonWalkable
                        });

                        NavEntity.SetPath(path);
                    }
                    break;
                }
            }
        }
Пример #30
0
 public override void DisablePowerUp()
 {
     base.DisablePowerUp();
     _animator.SetInteger("state", (int)AnimationStates.Idle);
     NavEntity.SetSpeed(GameController.Instance.PLAYER_DEFAULT_SPEED);
 }