Example #1
0
        public override void Update(GameTime gameTime)
        {
            overallTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (overallTime < path.Duration)
            {
                List <Pair <Pair <float, float?>, Pair <GameAction, bool> > > currentActions = path.Actions.FindAll(x => (x.First.First <overallTime && x.First.Second> overallTime));
                foreach (Pair <Pair <float, float?>, Pair <GameAction, bool> > action in path.Actions)
                {
                    if (action.First.First < overallTime && action.First.Second > overallTime && !action.Second.Second)
                    {
                        action.Second.Second = true;
                        AnimationController contr = Owner.GetComponent <AnimationController>();
                        if (contr != null)
                        {
                            string mov = (isCrouching ? "crouch" : (isRunning ? "run" : "walk"));
                            switch (action.Second.First)
                            {
                            case GameAction.CROUCH: SwitchMovement(true, isRunning); break;

                            case GameAction.JUMP: if (!isCrouching)
                                {
                                    contr.PlayAnimation("jump", 100, 0.2f);
                                }
                                break;

                            case GameAction.RUN: SwitchMovement(isCrouching, true); break;

                            case GameAction.MOVE_FORWARD: contr.PlayAnimation(mov + "Forward", 1, 0.2f); break;

                            case GameAction.MOVE_BACKWARD: contr.PlayAnimation(mov + "Backward", 1, 0.2f); break;

                            case GameAction.STRAFE_LEFT: contr.PlayAnimation(mov + "Left", 1, 0.2f); break;

                            case GameAction.STRAFE_RIGHT: contr.PlayAnimation(mov + "Right", 1, 0.2f); break;
                            }
                        }
                    }

                    if (action.First.Second < overallTime && action.Second.Second)
                    {
                        action.Second.Second = false;
                        AnimationController contr = Owner.GetComponent <AnimationController>();
                        if (contr != null)
                        {
                            string mov = (isCrouching ? "crouch" : (isRunning ? "run" : "walk"));
                            switch (action.Second.First)
                            {
                            case GameAction.CROUCH: SwitchMovement(false, isRunning); break;

                            case GameAction.JUMP: break;

                            case GameAction.RUN: SwitchMovement(isCrouching, false); break;

                            case GameAction.MOVE_FORWARD: contr.StopAnimation(mov + "Forward", 0.2f); break;

                            case GameAction.MOVE_BACKWARD: contr.StopAnimation(mov + "Backward", 0.2f); break;

                            case GameAction.STRAFE_LEFT: contr.StopAnimation(mov + "Left", 0.2f); break;

                            case GameAction.STRAFE_RIGHT: contr.StopAnimation(mov + "Right", 0.2f); break;
                            }
                        }
                    }
                }

                if (index < path.LocalPositions.Count - 1)
                {
                    time += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    Owner.LocalPosition = Vector3.LerpPrecise(path.LocalPositions[index], path.LocalPositions[index + 1], time / durationOfStep);
                    float rot1 = path.LocalRotations[index];
                    float rot2 = path.LocalRotations[index + 1];
                    if (Math.Abs(rot1 - rot2) > 180)
                    {
                        if (rot2 < 180)
                        {
                            rot2 += 360;
                        }
                        else
                        {
                            rot2 -= 360;
                        }
                    }
                    float newRot = MathHelper.Lerp(rot1, rot2, time / durationOfStep);
                    if (newRot > 360)
                    {
                        newRot -= 360;
                    }
                    if (newRot < 0)
                    {
                        newRot += 360;
                    }
                    Owner.LocalEulerRotation = Vector3.UnitX * newRot;
                    if (time > durationOfStep)
                    {
                        time = 0.0f;
                        index++;
                    }
                }
            }
            else
            {
                StopPlayback(null);
            }
        }