private void ChangeRoleTest(PlayerEntity playerEntity, IAdaptiveContainer <IFsmInputCommand> commands)
        {
            if (playerEntity.hasGamePlay &&
                playerEntity.gamePlay.IsVariant() &&
                playerEntity.hasStateInterface)
            {
                var state = playerEntity.stateInterface.State.GetNextPostureState();
                if (state == PostureInConfig.Stand)
                {
                    var   characterInfo = playerEntity.characterInfo.CharacterInfoProviderContext;
                    float targetHeight  = characterInfo.GetStandCapsule().Height;

                    GameObject gameObject = playerEntity.RootGo();
                    Vector3    startPoint = gameObject.transform.position;
                    RaycastHit outHit;
                    startPoint.y += CastRadius;

                    if (PhysicsCastHelper.SphereCast(startPoint, CastRadius, Vector3.up, out outHit, targetHeight - 2 * CastRadius, UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, LiftUp))
                    {
                        //var item = commands.GetAvailableItem(command => { return command.Type == FsmInput.None; });
                        //item.Type = FsmInput.Crouch;
                        playerEntity.stateInterface.State.SetPostureCrouch();
                    }
                }
            }
        }
        private static bool IsHitGround(PlayerEntity playerEntity, float dist)
        {
            var gameObject = playerEntity.RootGo();
            var prevLayer  = gameObject.layer;

            IntersectionDetectTool.SetColliderLayer(gameObject, UnityLayerManager.GetLayerIndex(EUnityLayerName.User));
            var startPoint  = gameObject.transform.position;
            var valueRadius = playerEntity.characterContoller.Value.radius;

            //UnityLayers.
            // a shift lift up
            startPoint = startPoint + gameObject.transform.rotation * new Vector3(0f, valueRadius, 0f);
            RaycastHit outHit;

//            DebugDraw.DebugWireSphere(startPoint, Color.red, CastRadius, 1f);
//            DebugDraw.DebugWireSphere(startPoint + new Vector3(0,targetHeight - CastRadius - LiftUp,0), Color.magenta, CastRadius, 1f);
            var isHit = PhysicsCastHelper.SphereCast(startPoint, CastRadius, Vector3.down, out outHit, dist,
                                                     UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, 0.1f);

            //if (!isHit)
            {
                //DebugDraw.DebugWireSphere(startPoint,isHit ? Color.green : (playerEntity.characterContoller.Value.isGrounded ? Color.magenta : Color.red) ,CastRadius, isHit ? 0f:60f);
            }
            IntersectionDetectTool.SetColliderLayer(gameObject, prevLayer);
            return(isHit);
        }
Beispiel #3
0
        private static bool IsHitGround(PlayerEntity playerEntity, float dist)
        {
            var        gameObject     = playerEntity.RootGo();
            var        prevLayer      = gameObject.layer;
            var        prev           = IntersectionDetectTool.SetColliderDisable(gameObject);
            var        playerPosition = gameObject.transform.position;
            var        playerRotation = gameObject.transform.rotation;
            var        controller     = playerEntity.characterContoller.Value;
            var        valueRadius    = playerEntity.characterContoller.Value.radius;
            var        isHit          = false;
            RaycastHit outHit;

            if (UseSphereCast(controller))
            {
                //use sphere cast
                var position = playerPosition + playerRotation * new Vector3(0f, valueRadius, 0f);

//            DebugDraw.DebugWireSphere(startPoint, Color.red, CastRadius, 1f);
//            DebugDraw.DebugWireSphere(startPoint + new Vector3(0,targetHeight - CastRadius - LiftUp,0), Color.magenta, CastRadius, 1f);
                isHit = PhysicsCastHelper.SphereCast(position, CastRadius, Vector3.down, out outHit, dist,
                                                     UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, 0.1f);
                if (!isHit)
                {
                    //DebugDraw.DebugWireSphere(position + (isHit ? Vector3.down * outHit.distance : Vector3.down * dist),isHit ? Color.green : (playerEntity.characterContoller.Value.isGrounded ? Color.magenta : Color.red) ,CastRadius, isHit ? 0f:60f);
                }
            }
            else
            {
                Vector3 point1, point2;
                float   radius;
                var     height = controller.height;
                radius = controller.radius;
                var center = controller.center;
                PhysicsCastHelper.GetCapsule(controller.transform.position, controller.transform.rotation, height, radius, center, controller.direction, out point1, out point2);
                isHit = PhysicsCastHelper.CapsuleCast(point1, point2, radius, Vector3.down, out outHit, dist,
                                                      UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, 0.1f);
                if (!isHit && false)
                {
                    PhysicsCastHelper.GetDebugDrawTypeCapsule(controller.transform.position, controller.transform.rotation, height, radius, center, controller.direction, out point1, out point2);
                    DebugDraw.DebugCapsule(point1 + (isHit ? Vector3.down * outHit.distance : Vector3.down * dist),
                                           point2 + (isHit ? Vector3.down * outHit.distance : Vector3.down * dist),
                                           isHit ? Color.green : (playerEntity.characterContoller.Value.isGrounded ? Color.magenta : Color.red),
                                           radius,
                                           isHit ? 0f:60f
                                           );
                }
            }


            IntersectionDetectTool.RestoreCollider(gameObject, prev);
            return(isHit);
        }
        private static bool IsHitGround(PlayerEntity playerEntity, float dist)
        {
            var gameObject = playerEntity.RootGo();
            var prevLayer  = gameObject.layer;

            IntersectionDetectTool.SetColliderDisable(gameObject, IntersectionDetectTool.ColliderEnableState);
            var        playerPosition = gameObject.transform.position;
            var        playerRotation = gameObject.transform.rotation;
            var        controller     = playerEntity.characterContoller.Value;
            var        valueRadius    = playerEntity.characterContoller.Value.radius;
            var        isHit          = false;
            RaycastHit outHit;

            if (UseSphereCast(controller))
            {
                //use sphere cast
                var position = playerPosition + playerRotation * new Vector3(0f, valueRadius, 0f);

                isHit = PhysicsCastHelper.SphereCast(position, CastRadius, Vector3.down, out outHit, dist,
                                                     UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, 0.1f);
            }
            else
            {
                Vector3 point1, point2;
                float   radius;
                var     height = controller.height;
                radius = controller.radius;
                var center = controller.center;
                PhysicsCastHelper.GetCapsule(controller.transform.position, controller.transform.rotation, height, radius, center, controller.direction, out point1, out point2);
                isHit = PhysicsCastHelper.CapsuleCast(point1, point2, radius, Vector3.down, out outHit, dist,
                                                      UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, 0.1f);
            }


            IntersectionDetectTool.RestoreCollider(gameObject, IntersectionDetectTool.ColliderEnableState);
            return(isHit);
        }
        /// <summary>
        /// <p>角色切换姿势会引起包围盒变化,当切换后的包围盒大于当前空间时,会无法切换姿势</p>
        /// 如:玩家站立175cm,当前空间只有160cm则无法站立。蹲趴同理
        /// </summary>
        /// <param name="playerEntity"></param>
        /// <param name="commandsContainer"></param>
        private void StandCrouchDisableTest(PlayerEntity playerEntity,
                                            IAdaptiveContainer <IFsmInputCommand> commandsContainer)
        {
            var state         = playerEntity.stateInterface.State.GetNextPostureState();
            var characterInfo = playerEntity.characterInfo.CharacterInfoProviderContext;

            if (!(state == PostureInConfig.Crouch || state == PostureInConfig.Prone))
            {
                return;
            }

            // crouchDisable
            testCondition.Clear();
            if (state == PostureInConfig.Crouch)
            {
                // to stand
                testCondition.Add(FsmInput.Jump);
                testCondition.Add(FsmInput.Crouch);
            }
            else
            {
                // to stand
                testCondition.Add(FsmInput.Jump);
                testCondition.Add(FsmInput.Prone);
                // to crouch
                testCondition.Add(FsmInput.Crouch);
            }

            testCommand.Clear();
            for (int i = 0; i < commandsContainer.Length; i++)
            {
                var v         = commandsContainer[i];
                int keyLength = testCondition.Count;
                for (int j = 0; j < keyLength; ++j)
                {
                    if (testCondition[j] == v.Type)
                    {
                        testCommand.Add(commandsContainer[i]);
                        _logger.DebugFormat("match type:{0}, state:{1}", v.Type, state);
                        break;
                    }
                }
            }

            if (testCommand.Count == 0)
            {
                return;
            }

            float targetHeight   = 0.0f;
            bool  containsCrouch = false;

            foreach (IFsmInputCommand command in testCommand)
            {
                if (command.Type == FsmInput.Crouch)
                {
                    containsCrouch = true;
                }
            }

            bool toStand = false;

            // to stand
            if (state == PostureInConfig.Crouch || (state == PostureInConfig.Prone && !containsCrouch))
            {
                targetHeight = characterInfo.GetStandCapsule().Height;
                toStand      = true;
            }
            // to crouch
            else
            {
                targetHeight = characterInfo.GetCrouchCapsule()
                               .Height;
                toStand = false;
            }

            var gameObject = playerEntity.RootGo();
            var prevLayer  = gameObject.layer;

            IntersectionDetectTool.SetColliderDisable(gameObject, IntersectionDetectTool.ColliderEnableState);
            var startPoint = gameObject.transform.position;

            //UnityLayers.
            // a shift lift up
            startPoint.y += CastRadius;
            RaycastHit outHit;

//            DebugDraw.DebugWireSphere(startPoint, Color.red, CastRadius, 1f);
//            DebugDraw.DebugWireSphere(startPoint + new Vector3(0,targetHeight - CastRadius - LiftUp,0), Color.magenta, CastRadius, 1f);

            if (PhysicsCastHelper.SphereCast(startPoint, CastRadius, Vector3.up, out outHit, targetHeight - 2
                                             * CastRadius,
                                             UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, LiftUp))
            {
                foreach (IFsmInputCommand command in testCommand)
                {
                    _logger.InfoFormat(
                        "chang command:{0} to none, because current state:{1} can not stand up!, collider name:{2}, collid point:{3}, collider normal:{4}",
                        command.Type,
                        state,
                        outHit.collider.gameObject.name,
                        outHit.point,
                        outHit.normal);
                    command.Type = FsmInput.None;
                }

                if (toStand)
                {
                    playerEntity.tip.TipType = ETipType.CanNotStand;
                }
                else
                {
                    playerEntity.tip.TipType = ETipType.CanNotToCrouch;
                }

                //Debug.DrawLine(outHit.point, outHit.normal, Color.red, 5000.0f);
            }

            IntersectionDetectTool.RestoreCollider(gameObject, IntersectionDetectTool.ColliderEnableState);
            testCommand.Clear();
            testCondition.Clear();
        }