//NEW CODE
		public Actor(ContentRegister content, MaterialLightCollection lights)
		{
			//A ModelInstance can be created without any content...
			//However it cannot be used until the content is set

			model = new ModelInstance();
			model.LightCollection = lights;	//this class is reused by later tutorials, which require lights

			//get and create the animation controller for this model.
			animationController = model.GetAnimationController();

			//NOTE: Animations cannot be played until the model data has been loaded...

			content.Add(this);

			//At this point in this tutorial, the model is now loaded.

			//get the index of the walk animation
			//this model has 4 animations, Wave, Jog, Walk and Loiter
			//The animations are stored in model.ModelData.Animations
			int animationIndex = animationController.AnimationIndex("Walk");

			//begin playing the animation, looping
			animation = animationController.PlayLoopingAnimation(animationIndex);

			//as many animations as you want can be played at any one time
			//to blend between animations, adjust their weighting with:
			//animation.Weighting = ...;
			//Combined weightings usually should add up to 1.0
			//A weighting of 0 means the animation has no effect, 1 has normal effect.
			//Values outside the 0-1 range usually produces undesirable results.

			//Note:
			//Animations in xen are lossy compressed.
			//For the model used here, the animation data is reduced from nearly 2mb
			//down to around 200kb. (The model geometry is less than 300kb)
			//The amount of compression change can be configured in the content's properties
			//The 'Animation Compression Tolerance' value is a percentage
			//The default is .5%. This means the animation will always be within .5%
			//of the source. Setting this value to 0 will save a lossless animation.
		}
		public Actor(ContentRegister content, Vector3 position, float animationSpeed, int animationIndex)
		{
			Matrix.CreateRotationZ(1-(float)animationIndex, out this.worldMatrix);
			this.worldMatrix.Translation = position;

			model = new ModelInstance();
			this.animationController = model.GetAnimationController();

			content.Add(this);

			this.animation = this.animationController.PlayLoopingAnimation(animationIndex);
			this.animation.PlaybackSpeed = animationSpeed;
		}
		private void InitaliseAnimations(UpdateManager updateManager)
		{
			//create the controller as an asynchronous controller.
			//this will process animations as a thread task
			//This occurs between the update loop and the draw loop,
			//which is why the UpdateManager must be provided.
			control = model.GetAsyncAnimationController(updateManager);

			//these perform a linear search to find the animation index
			int idleAnimationIndex	= control.AnimationIndex("Loiter");
			int jogAnimationIndex	= control.AnimationIndex("Jog");
			int walkAnimationIndex	= control.AnimationIndex("Walk");

			//create the idle animation
			idle = control.PlayLoopingAnimation(idleAnimationIndex);

			//give it a random speed
			idle.PlaybackSpeed = (float)random.NextDouble() * 0.5f + 0.6f;


			if (moveSpeed > 0.75)
			{
				//run animation
				move = control.PlayLoopingAnimation(jogAnimationIndex); // play a jogging animation
				move.PlaybackSpeed = 0.5f;
			}
			else
			{
				//walk animation
				move = control.PlayLoopingAnimation(walkAnimationIndex); // play a walking animation
			}
			//initially don't want the move animation being visible
			move.Weighting = 0;
		}
		protected override void Update(UpdateState state)
		{
			configEditor.Instance = this.renderConfig;

			//lerp the lens exposure to the curent target exposure, this allows nice exposure transitions
			this.renderConfig.LensExposure = this.renderConfig.LensExposure * 0.95f + this.renderConfig.TargetLensExposure * 0.05f;

			//make sure the model is animating if the user wants it to...
			if ((modelAnimation.ValidAnimation & !modelAnimation.AnimationFinished) == this.renderConfig.PauseModelAnimation)
			{
				//user has changed the animating property..

				//the animated model is 180 degrees rotated, so...
				this.modelRotation.RotationAngle += MathHelper.Pi; //hack!

				if (this.renderConfig.PauseModelAnimation)
				{
					//stop animation
					this.modelAnimation.StopAnimation();
				}
				else
				{
					//play the first animation
					AnimationController anims = this.model.GetAnimationController();
					if (anims.AnimationCount > 0)
						this.modelAnimation = anims.PlayLoopingAnimation(0);
				}
			}

			if (!this.renderConfig.PauseModelRotation)
			{
				this.modelRotation.RotationAngle += state.DeltaTimeSeconds * 0.25f;
			}

			if (state.PlayerInput[PlayerIndex.One].InputState.Buttons.Back.OnPressed)
				this.Shutdown();
		}
		public Actor(ContentRegister content, UpdateManager updateManager)
		{
			//load the model
			model = new ModelInstance();
			content.Add(this);

			//create the animation controller
			animationController = model.GetAsyncAnimationController(updateManager);

			//NEW CODE
			//create the animation modifier

			int rotateBoneIndex = model.ModelData.Skeleton.GetBoneIndexByName("Bip01_Spine2");
			this.animationModifer = new AnimationModifier(rotateBoneIndex, model.ModelData);

			//set the modifier on the animation controller
			this.animationController.AnimationBoneModifier = this.animationModifer;

			//play the run animation
			int animationIndex = animationController.AnimationIndex("Jog");
			this.animation = animationController.PlayLoopingAnimation(animationIndex);
		}
		//update...
		public UpdateFrequency Update(UpdateState state)
		{
			//when pressing A, create a new avatar description
			if (state.PlayerInput[PlayerIndex.One].InputState.Buttons.A.OnPressed)
			{
				this.avatar.AvatarDescription = Microsoft.Xna.Framework.GamerServices.AvatarDescription.CreateRandom();
			}

			//if pressing B, play the cheer animation
			if (state.PlayerInput[PlayerIndex.One].InputState.Buttons.B.OnPressed)
			{
				//start or stop the cheer animation (which is a built in animation in XNA)
				//use a fadein / fadeout
				if (!cheerAnimation.AnimationFinished)
					cheerAnimation.StopAnimation(0.5f);
				else
					cheerAnimation = animationController.PlayPresetAnimation(AvatarAnimationPreset.Celebrate, true, 0.5f, 0.5f);
			}

			//when the cheer animation is playing, fade the walking animation out,
			//do this based on the opposite weighitng of the cheer animation (taking fading into account)
			this.walkAnimation.Weighting = 1 - this.cheerAnimation.GetFadeScaledWeighting(state);

			return UpdateFrequency.FullUpdate60hz;
		}
Пример #7
0
        protected override void Update(UpdateState state)
        {
            // update mouse position for terrain picking
            simpleTerrain.MousePosition = new Vector2(state.MouseState.X, state.MouseState.Y);
            //this.rootElement.Update();

            JVector targetVelocity = JVector.Zero;
            if (state.KeyboardState.IsKeyDown(Keys.Down)) targetVelocity += JVector.Left;
            if (state.KeyboardState.IsKeyDown(Keys.Up)) targetVelocity += JVector.Right;
            if (state.KeyboardState.IsKeyDown(Keys.Left)) targetVelocity += JVector.Backward;
            if (state.KeyboardState.IsKeyDown(Keys.Right)) targetVelocity += JVector.Forward;

            if (targetVelocity.LengthSquared() > 0.0f) targetVelocity.Normalize();
            targetVelocity *= 10.0f;

            characterController.TryJump = state.KeyboardState.IsKeyDown(Keys.Space);
            characterController.TargetVelocity = targetVelocity;

            // update physics
            world.Step(state.DeltaTimeSeconds,
                false); // multithreaded
            modelPhysics.Position = world.RigidBodies[0].Position.ToVector3();
            //modelPhysics.Position = simpleTerrain.PickingDrawer.position; // test
            modelPhysics.Orientation = world.RigidBodies[0].Orientation;

            // mouse picking
            float fraction;
            JVector hitNormal;
            Vector3 hitPoint;
            RigidBody grabBody;
            bool result = world.CollisionSystem.Raycast(viewCamera.Position.ToJVector(),
                simpleTerrain.RayFromMouse.ToJVector(), RaycastCallback, out grabBody, out hitNormal, out fraction);
            if (result && grabBody == simpleTerrain.RigidBody)
            {
                hitPoint = viewCamera.Position + fraction * simpleTerrain.RayFromMouse;
                simpleTerrain.PickingDrawer.position = hitPoint;
            }

            configEditor.Instance = this.renderConfig;

            //lerp the lens exposure to the curent target exposure, this allows nice exposure transitions
            this.renderConfig.LensExposure = this.renderConfig.LensExposure * 0.95f + this.renderConfig.TargetLensExposure * 0.05f;

            //make sure the model is animating if the user wants it to...
            if ((modelAnimation.ValidAnimation & !modelAnimation.AnimationFinished) == this.renderConfig.PauseModelAnimation)
            {
                //user has changed the animating property..

                //the animated model is 180 degrees rotated, so...
                this.modelRotation.RotationAngle += MathHelper.Pi; //hack!

                if (this.renderConfig.PauseModelAnimation)
                {
                    //stop animation
                    this.modelAnimation.StopAnimation();
                }
                else
                {
                    //play the first animation
                    AnimationController anims = this.model.GetAnimationController();
                    if (anims.AnimationCount > 0)
                        this.modelAnimation = anims.PlayLoopingAnimation(0);
                }
            }

            if (!this.renderConfig.PauseModelRotation)
            {
                this.modelRotation.RotationAngle += state.DeltaTimeSeconds * 0.25f;
            }

            if (state.PlayerInput[PlayerIndex.One].InputState.Buttons.Back.OnPressed)
                this.Shutdown();

            // unlock mouse cursor while left control is down
            if (state.KeyboardState.IsKeyDown(Keys.LeftControl))
            {
                if (state.PlayerInput[PlayerIndex.One].InputMapper.CentreMouseToWindow)
                {
                    state.PlayerInput[PlayerIndex.One].InputMapper.CentreMouseToWindow = false;
                    state.PlayerInput[PlayerIndex.One].InputMapper.MouseVisible = true;
                    if (viewCamera.RotationSensitivity != Vector2.Zero)
                        this.lastCameraRotationSensitivity = viewCamera.RotationSensitivity;
                    viewCamera.RotationSensitivity = Vector2.Zero;
                }
            }
            else
            {
                if (!state.PlayerInput[PlayerIndex.One].InputMapper.CentreMouseToWindow)
                {
                    state.PlayerInput[PlayerIndex.One].InputMapper.CentreMouseToWindow = true;
                    state.PlayerInput[PlayerIndex.One].InputMapper.MouseVisible = false;
                    viewCamera.RotationSensitivity = this.lastCameraRotationSensitivity;
                }
            }
        }