protected override void Initialize() { CharacterTexture = Content.Load <Texture2D>("swat"); RifleTexture = Content.Load <Texture2D>("hk_mp5n"); CharacterMesh = new SkinnedModel(); CharacterMesh.GraphicsDevice = GraphicsDevice; CharacterMesh.FilePath = @"Content\Soldier.fbx"; CharacterMesh.Initialize(); CharacterMesh.Meshes[0].Texture = Content.Load <Texture2D>("swat"); AnimationIdle = new SkinnedModelAnimation(); AnimationIdle.FilePath = @"Content\Rifle Idle.dae"; AnimationIdle.Load(); AnimationRunning = new SkinnedModelAnimation(); AnimationRunning.FilePath = @"Content\Rifle Run.dae"; AnimationRunning.Load(); AnimationWalk = new SkinnedModelAnimation(); AnimationWalk.FilePath = @"Content\Rifle Walking.dae"; AnimationWalk.Load(); ModelRifle = new SimpleModel(); ModelRifle.GraphicsDevice = GraphicsDevice; ModelRifle.FilePath = @"Content\mp5.3ds"; ModelRifle.Initialize(); ModelInstance = new SkinnedModelInstance(); ModelInstance.Mesh = CharacterMesh; ModelInstance.SpeedTransitionSecond = 0.4f; ModelInstance.Initialize(); ModelInstance.SetAnimation(AnimationIdle); HeadBoneAnimationInstance = ModelInstance.GetBoneAnimationInstance("soldier_Head"); HandBoneAnimationInstance = ModelInstance.GetBoneAnimationInstance("soldier_RightHand"); SimpleModelEffect = this.Content.Load <Effect>("SimpleModelEffect"); SkinnedModelEffect = this.Content.Load <Effect>("SkinnedModelEffect"); var Position = new Vector3(500, 200, 0); var Direction = new Vector3(0, 200, 0); ViewMatrix = Matrix.CreateLookAt(Position, Direction, Vector3.Up); ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), GraphicsDevice.DisplayMode.AspectRatio, 0.1f, 5000f); base.Initialize(); }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (Keyboard.GetState().IsKeyDown(Keys.D1) && ModelInstance.Animation != AnimationIdle) { ModelInstance.SetAnimation(AnimationIdle, gameTime); HeadBoneAnimationInstance = ModelInstance.GetBoneAnimationInstance("soldier_Head"); HandBoneAnimationInstance = ModelInstance.GetBoneAnimationInstance("soldier_RightHand"); } else if (Keyboard.GetState().IsKeyDown(Keys.D2) && ModelInstance.Animation != AnimationRunning) { ModelInstance.SetAnimation(AnimationRunning, gameTime); HeadBoneAnimationInstance = ModelInstance.GetBoneAnimationInstance("soldier_Head"); HandBoneAnimationInstance = ModelInstance.GetBoneAnimationInstance("soldier_RightHand"); } else if (Keyboard.GetState().IsKeyDown(Keys.D3) && ModelInstance.Animation != AnimationWalk) { ModelInstance.SetAnimation(AnimationWalk, gameTime); HeadBoneAnimationInstance = ModelInstance.GetBoneAnimationInstance("soldier_Head"); HandBoneAnimationInstance = ModelInstance.GetBoneAnimationInstance("soldier_RightHand"); } var HeadYRotationFrame = 0; if (Keyboard.GetState().IsKeyDown(Keys.Left)) { HeadYRotation = 1; } else if (Keyboard.GetState().IsKeyDown(Keys.Right)) { HeadYRotation = -1; } else { if (HeadYRotation < -0.01) { HeadYRotation += 1; } else if (HeadYRotation > 0.01) { HeadYRotation = -1; } } HeadYRotation += HeadYRotationFrame * 1.1f * ((float)gameTime.ElapsedGameTime.TotalSeconds * 2.5f); HeadYRotation = MathHelper.Clamp(HeadYRotation, -1.1f, 1.1f); HeadBoneAnimationInstance.AdditionalTransform = Matrix.CreateRotationY(HeadYRotation); ModelInstance.Transformation = Matrix.CreateRotationY(2.5f); ModelInstance.UpdateBoneAnimations(gameTime); ModelInstance.UpdateBones(gameTime); RifleTransformation = Matrix.CreateScale(0.15f) * Matrix.CreateTranslation(10, 5, -4f) * Matrix.CreateRotationY(3.14f) * Matrix.CreateRotationX(3.14f / 2f) * ModelInstance.GetTransform(HandBoneAnimationInstance, gameTime) * ModelInstance.Transformation; base.Update(gameTime); }