Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualmrmohadnan

Posted 18 May 2013 - 02:00 PM

I have used  XNA Animation Component Library to load ".x" files with multiple animations clips , now there is a sample called dwarfmodel and here is the code used to animate it :

private void UpdateEnemyState(GameTime gameTime, KeyboardState keyState, KeyboardState prebiousKeyState,
            MouseState mouseState)
        {
            BonePoseCollection poses = dwarfAnimator.BonePoses;
            if (stateModel == "idle")
            {
                currentSpeed = 0;
                // Add this to the beginning of the if (state=="idle") block in the UpdateState method
                // Remove the old if (keyState.IsKeyDown(Keys.W)) block
                if (keyState.IsKeyDown(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "idleToWalk";
                }
                // Add this in the if (state=="idle") block of the UpdateState method
                if (keyState.IsKeyDown(Keys.Space))
                {
                    crouch.ElapsedTime = 0;
                    crouch.IsLooping = false;
                    stateModel = "crouchDown";
                }

                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    stateModel = "attack0";
                }

                RunController(dwarfAnimator, idle);

            }
            else if (stateModel == "walk")
            {
                currentSpeed = WALK_SPEED;
                // Add this to the beginning of the if (state=="walk") block in the UpdateState method
                // Remove the old if (keyState.IsKeyUp(Keys.W)) block
                if (keyState.IsKeyUp(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "walkToIdle";
                }
                if (keyState.IsKeyDown(Keys.LeftShift) && keyState.IsKeyDown(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "walkToRun";
                    run.SpeedFactor = 0;
                }
                RunController(dwarfAnimator, crouch );
            }
            // Add this to the UpdateState method
            else if (stateModel == "idleToWalk")
            {
                blendFactor += .1f;
                currentSpeed = blendFactor * WALK_SPEED;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    stateModel = "walk";
                }
                foreach (BonePose p in poses)
                {
                    p.CurrentController = idle;
                    p.CurrentBlendController = walk;
                    p.BlendFactor = blendFactor;
                }
            }
            // Add this to the UpdateState method
            else if (stateModel == "walkToIdle")
            {
                blendFactor += .1f;
                currentSpeed = (1f - blendFactor) * WALK_SPEED;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    stateModel = "idle";
                }
                foreach (BonePose p in poses)
                {
                    p.CurrentController = walk;
                    p.CurrentBlendController = idle;
                    p.BlendFactor = blendFactor;
                }
            }
            // Add this in the UpdateState method
            else if (stateModel == "crouchDown")
            {
                crouch.AnimationEnded += crouch_AnimationEnded;
                RunController(dwarfAnimator, crouch);
            }
            else if (stateModel == "stayCrouched")
            {
                // Add this to the if (state == "stayCrouched") block in the UpdateState method
                if (keyState.IsKeyDown(Keys.Space) && prebiousKeyState.IsKeyUp(Keys.Space))
                {
                    crouch.ElapsedTime = crouch.AnimationSource.Duration;
                    crouch.SpeedFactor = 0;
                    stateModel = "standUp";
                }
                RunController(dwarfAnimator, stayCrouched);
            }
            // Add this to the UpdateState method
            else if (stateModel == "standUp")
            {
                if (crouch.ElapsedTime - gameTime.ElapsedGameTime.Ticks <= 0)
                {
                    crouch.SpeedFactor = 1;
                    crouch.ElapsedTime = 0;
                    idle.ElapsedTime = 0;
                    stateModel = "idle";
                }
                else
                    crouch.ElapsedTime -= gameTime.ElapsedGameTime.Ticks;
                RunController(dwarfAnimator, crouch);
            }
            // Add this to the UpdateState method
            else if (stateModel == "walkToRun")
            {
                blendFactor += .05f;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    run.SpeedFactor = 1;
                    stateModel = "run";
                }
                double factor = (double)walk.ElapsedTime / walk.AnimationSource.Duration;
                run.ElapsedTime = (long)(factor * run.AnimationSource.Duration);
                currentSpeed = WALK_SPEED + blendFactor * (RUN_SPEED - WALK_SPEED);
                foreach (BonePose p in poses)
                {
                    p.CurrentController = walk;
                    p.CurrentBlendController = run;
                    p.BlendFactor = blendFactor;
                }
            }
            else if (stateModel == "run")
            {
                currentSpeed = RUN_SPEED;
                if (keyState.IsKeyUp(Keys.LeftShift))
                {
                    blendFactor = 0;
                    stateModel = "runToWalk";
                    walk.SpeedFactor = 0;
                }
                foreach (BonePose p in poses)
                {
                    p.CurrentController = run;
                    p.CurrentBlendController = null;
                }
            }
            else if (stateModel == "runToWalk")
            {
                blendFactor += .05f;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    walk.SpeedFactor = 1;
                    stateModel = "walk";
                }
                double factor = (double)run.ElapsedTime / run.AnimationSource.Duration;
                walk.ElapsedTime = (long)(factor * walk.AnimationSource.Duration);
                currentSpeed = WALK_SPEED + (1f - blendFactor) * (RUN_SPEED - WALK_SPEED);
                foreach (BonePose p in poses)
                {
                    p.CurrentController = run;
                    p.CurrentBlendController = walk;
                    p.BlendFactor = blendFactor;
                }

            }

            else if (stateModel == "attack0")
            {
                attak.SpeedFactor = 2;
                blendFactor += .5f;
                if (mouseState.LeftButton == ButtonState.Released)
                    stateModel = "idle";

                RunController(dwarfAnimator, attak);
            }

        }

 

 

Q1 : So , I want to change that , to become animated based on Machine state AI , for example if I am close enough then attack and so on !!
Q2 : I have problem with modifying the xml file used for the model  , as the animation for "beast.x" model not animated like the one used for "dwarfmodel.x" , I know it's hard to understand my questions smile.png but say what you want smile.png

 

How to change making my enemy animate based on key Input to become animate based on some AI behaviours ( for example if Hero close enough then attack not it key.w then attack !! )


#2mrmohadnan

Posted 18 May 2013 - 01:48 PM

I have used  XNA Animation Component Library to load ".x" files with multiple animations clips , now there is a sample called dwarfmodel and here is the code used to animate it :

private void UpdateEnemyState(GameTime gameTime, KeyboardState keyState, KeyboardState prebiousKeyState,
            MouseState mouseState)
        {
            BonePoseCollection poses = dwarfAnimator.BonePoses;
            if (stateModel == "idle")
            {
                currentSpeed = 0;
                // Add this to the beginning of the if (state=="idle") block in the UpdateState method
                // Remove the old if (keyState.IsKeyDown(Keys.W)) block
                if (keyState.IsKeyDown(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "idleToWalk";
                }
                // Add this in the if (state=="idle") block of the UpdateState method
                if (keyState.IsKeyDown(Keys.Space))
                {
                    crouch.ElapsedTime = 0;
                    crouch.IsLooping = false;
                    stateModel = "crouchDown";
                }

                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    stateModel = "attack0";
                }

                RunController(dwarfAnimator, idle);

            }
            else if (stateModel == "walk")
            {
                currentSpeed = WALK_SPEED;
                // Add this to the beginning of the if (state=="walk") block in the UpdateState method
                // Remove the old if (keyState.IsKeyUp(Keys.W)) block
                if (keyState.IsKeyUp(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "walkToIdle";
                }
                if (keyState.IsKeyDown(Keys.LeftShift) && keyState.IsKeyDown(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "walkToRun";
                    run.SpeedFactor = 0;
                }
                RunController(dwarfAnimator, crouch );
            }
            // Add this to the UpdateState method
            else if (stateModel == "idleToWalk")
            {
                blendFactor += .1f;
                currentSpeed = blendFactor * WALK_SPEED;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    stateModel = "walk";
                }
                foreach (BonePose p in poses)
                {
                    p.CurrentController = idle;
                    p.CurrentBlendController = walk;
                    p.BlendFactor = blendFactor;
                }
            }
            // Add this to the UpdateState method
            else if (stateModel == "walkToIdle")
            {
                blendFactor += .1f;
                currentSpeed = (1f - blendFactor) * WALK_SPEED;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    stateModel = "idle";
                }
                foreach (BonePose p in poses)
                {
                    p.CurrentController = walk;
                    p.CurrentBlendController = idle;
                    p.BlendFactor = blendFactor;
                }
            }
            // Add this in the UpdateState method
            else if (stateModel == "crouchDown")
            {
                crouch.AnimationEnded += crouch_AnimationEnded;
                RunController(dwarfAnimator, crouch);
            }
            else if (stateModel == "stayCrouched")
            {
                // Add this to the if (state == "stayCrouched") block in the UpdateState method
                if (keyState.IsKeyDown(Keys.Space) && prebiousKeyState.IsKeyUp(Keys.Space))
                {
                    crouch.ElapsedTime = crouch.AnimationSource.Duration;
                    crouch.SpeedFactor = 0;
                    stateModel = "standUp";
                }
                RunController(dwarfAnimator, stayCrouched);
            }
            // Add this to the UpdateState method
            else if (stateModel == "standUp")
            {
                if (crouch.ElapsedTime - gameTime.ElapsedGameTime.Ticks <= 0)
                {
                    crouch.SpeedFactor = 1;
                    crouch.ElapsedTime = 0;
                    idle.ElapsedTime = 0;
                    stateModel = "idle";
                }
                else
                    crouch.ElapsedTime -= gameTime.ElapsedGameTime.Ticks;
                RunController(dwarfAnimator, crouch);
            }
            // Add this to the UpdateState method
            else if (stateModel == "walkToRun")
            {
                blendFactor += .05f;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    run.SpeedFactor = 1;
                    stateModel = "run";
                }
                double factor = (double)walk.ElapsedTime / walk.AnimationSource.Duration;
                run.ElapsedTime = (long)(factor * run.AnimationSource.Duration);
                currentSpeed = WALK_SPEED + blendFactor * (RUN_SPEED - WALK_SPEED);
                foreach (BonePose p in poses)
                {
                    p.CurrentController = walk;
                    p.CurrentBlendController = run;
                    p.BlendFactor = blendFactor;
                }
            }
            else if (stateModel == "run")
            {
                currentSpeed = RUN_SPEED;
                if (keyState.IsKeyUp(Keys.LeftShift))
                {
                    blendFactor = 0;
                    stateModel = "runToWalk";
                    walk.SpeedFactor = 0;
                }
                foreach (BonePose p in poses)
                {
                    p.CurrentController = run;
                    p.CurrentBlendController = null;
                }
            }
            else if (stateModel == "runToWalk")
            {
                blendFactor += .05f;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    walk.SpeedFactor = 1;
                    stateModel = "walk";
                }
                double factor = (double)run.ElapsedTime / run.AnimationSource.Duration;
                walk.ElapsedTime = (long)(factor * walk.AnimationSource.Duration);
                currentSpeed = WALK_SPEED + (1f - blendFactor) * (RUN_SPEED - WALK_SPEED);
                foreach (BonePose p in poses)
                {
                    p.CurrentController = run;
                    p.CurrentBlendController = walk;
                    p.BlendFactor = blendFactor;
                }

            }

            else if (stateModel == "attack0")
            {
                attak.SpeedFactor = 2;
                blendFactor += .5f;
                if (mouseState.LeftButton == ButtonState.Released)
                    stateModel = "idle";

                RunController(dwarfAnimator, attak);
            }

        }

 

 

Q1 : So , I want to change that , to become animated based on Machine state AI , for example if I am close enough then attack and so on !!
Q2 : I have problem with modifying the xml file used for the model  , as the animation for "beast.x" model not animated like the one used for "dwarfmodel.x" , I know it's hard to understand my questions :) but say what you want :)

 

How to change making my enemy animate based on key Input to become animate based on some AI behaviours ( for example if Hero close enough then attack not it key.w then attack !! )


#1mrmohadnan

Posted 18 May 2013 - 12:24 PM

this is the code I used when I was moving enemy based on my keyboard input :

 

 private void UpdateEnemyState(GameTime gameTime, KeyboardState keyState, KeyboardState prebiousKeyState,
            MouseState mouseState)
        {
            BonePoseCollection poses = dwarfAnimator.BonePoses;
            if (stateModel == "idle")
            {
                currentSpeed = 0;
                // Add this to the beginning of the if (state=="idle") block in the UpdateState method
                // Remove the old if (keyState.IsKeyDown(Keys.W)) block
                if (keyState.IsKeyDown(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "idleToWalk";
                }
                // Add this in the if (state=="idle") block of the UpdateState method
                if (keyState.IsKeyDown(Keys.Space))
                {
                    crouch.ElapsedTime = 0;
                    crouch.IsLooping = false;
                    stateModel = "crouchDown";
                }

                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    stateModel = "attack0";
                }

                RunController(dwarfAnimator, idle);

            }
            else if (stateModel == "walk")
            {
                currentSpeed = WALK_SPEED;
                // Add this to the beginning of the if (state=="walk") block in the UpdateState method
                // Remove the old if (keyState.IsKeyUp(Keys.W)) block
                if (keyState.IsKeyUp(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "walkToIdle";
                }
                if (keyState.IsKeyDown(Keys.LeftShift) && keyState.IsKeyDown(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "walkToRun";
                    run.SpeedFactor = 0;
                }
                RunController(dwarfAnimator, walk);
            }
            // Add this to the UpdateState method
            else if (stateModel == "idleToWalk")
            {
                blendFactor += .1f;
                currentSpeed = blendFactor * WALK_SPEED;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    stateModel = "walk";
                }
                foreach (BonePose p in poses)
                {
                    p.CurrentController = idle;
                    p.CurrentBlendController = walk;
                    p.BlendFactor = blendFactor;
                }
            }
            // Add this to the UpdateState method
            else if (stateModel == "walkToIdle")
            {
                blendFactor += .1f;
                currentSpeed = (1f - blendFactor) * WALK_SPEED;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    stateModel = "idle";
                }
                foreach (BonePose p in poses)
                {
                    p.CurrentController = walk;
                    p.CurrentBlendController = idle;
                    p.BlendFactor = blendFactor;
                }
            }
            // Add this in the UpdateState method
            else if (stateModel == "crouchDown")
            {
                crouch.AnimationEnded += crouch_AnimationEnded;
                RunController(dwarfAnimator, crouch);
            }
            else if (stateModel == "stayCrouched")
            {
                // Add this to the if (state == "stayCrouched") block in the UpdateState method
                if (keyState.IsKeyDown(Keys.Space) && prebiousKeyState.IsKeyUp(Keys.Space))
                {
                    crouch.ElapsedTime = crouch.AnimationSource.Duration;
                    crouch.SpeedFactor = 0;
                    stateModel = "standUp";
                }
                RunController(dwarfAnimator, stayCrouched);
            }
            // Add this to the UpdateState method
            else if (stateModel == "standUp")
            {
                if (crouch.ElapsedTime - gameTime.ElapsedGameTime.Ticks <= 0)
                {
                    crouch.SpeedFactor = 1;
                    crouch.ElapsedTime = 0;
                    idle.ElapsedTime = 0;
                    stateModel = "idle";
                }
                else
                    crouch.ElapsedTime -= gameTime.ElapsedGameTime.Ticks;
                RunController(dwarfAnimator, crouch);
            }
            // Add this to the UpdateState method
            else if (stateModel == "walkToRun")
            {
                blendFactor += .05f;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    run.SpeedFactor = 1;
                    stateModel = "run";
                }
                double factor = (double)walk.ElapsedTime / walk.AnimationSource.Duration;
                run.ElapsedTime = (long)(factor * run.AnimationSource.Duration);
                currentSpeed = WALK_SPEED + blendFactor * (RUN_SPEED - WALK_SPEED);
                foreach (BonePose p in poses)
                {
                    p.CurrentController = walk;
                    p.CurrentBlendController = run;
                    p.BlendFactor = blendFactor;
                }
            }
            else if (stateModel == "run")
            {
                currentSpeed = RUN_SPEED;
                if (keyState.IsKeyUp(Keys.LeftShift))
                {
                    blendFactor = 0;
                    stateModel = "runToWalk";
                    walk.SpeedFactor = 0;
                }
                foreach (BonePose p in poses)
                {
                    p.CurrentController = run;
                    p.CurrentBlendController = null;
                }
            }
            else if (stateModel == "runToWalk")
            {
                blendFactor += .05f;
                if (blendFactor >= 1)
                {
                    blendFactor = 1;
                    walk.SpeedFactor = 1;
                    stateModel = "walk";
                }
                double factor = (double)run.ElapsedTime / run.AnimationSource.Duration;
                walk.ElapsedTime = (long)(factor * walk.AnimationSource.Duration);
                currentSpeed = WALK_SPEED + (1f - blendFactor) * (RUN_SPEED - WALK_SPEED);
                foreach (BonePose p in poses)
                {
                    p.CurrentController = run;
                    p.CurrentBlendController = walk;
                    p.BlendFactor = blendFactor;
                }

            }

            else if (stateModel == "attack0")
            {
                attak.SpeedFactor = 2;
                blendFactor += .5f;
                if (mouseState.LeftButton == ButtonState.Released)
                    stateModel = "idle";

                RunController(dwarfAnimator, attak);
            }

        }

 

How to change making my enemy animate based on key Input to become animate based on some AI behaviours ( for example if Hero close enough then attack not it key.w then attack !! )


PARTNERS