はじめに
○最初に状態を制御するために、新しいメソッド UpdateState(GameTime time) を作成し、Update メソッドの始めに呼び出します。
  1. // Add this as a new method
  2. private void UpdateState(GameTime gameTime)
  3. {
  4. }
  5.  
  1. // Add this to the beginning of the Update method
  2. UpdateState(gameTime);
  3.  
○次のメンバ変数を追加して下さい。
  1. // Add these as member variables
  2. // This represents what the dwarf is doing
  3. string state = "idle";
  4. float currentSpeed = 0;
  5. const float WALK_SPEED = .115f;
  6.  

ドワーフを旋回させる
○ドワーフが何をしているかに関係なく、「Aキー」と「Dキー」を使用して彼を旋回させることができます。次のソースコードを Update メソッドに追加します。
  1. // Add this to the beginning of the Update method
  2. KeyboardState keyState = Keyboard.GetState();
  3. if (currentSpeed > 0)
  4. {
  5. dwarfPosition += (-Matrix.CreateTranslation(0, 0, currentSpeed)
  6. * rotation).Translation;
  7. }
  8. if (keyState.IsKeyDown(Keys.D))
  9. {
  10. rotation *=
  11. Matrix.CreateFromAxisAngle(Vector3.Up, -MathHelper.Pi / 25.0f);
  12.  
  13. }
  14. if (keyState.IsKeyDown(Keys.A))
  15. {
  16. rotation *=
  17. Matrix.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi / 25.0f);
  18. }
  19.  
○ドワーフの位置を増加させるとき、彼が向いている方向に前進します。通常とは逆順に行列が掛け合わされます。(平行移動 * 回転)

ドワーフを動き回らせる
○次のソースコードを UpdateState メソッドに追加して下さい。
  1. // Add this to the UpdateState method
  2. KeyboardState keyState = Keyboard.GetState();
  3. BonePoseCollection poses = dwarfAnimator.BonePoses;
  4. if (state == "idle")
  5. {
  6. currentSpeed = 0;
  7. if (keyState.IsKeyDown(Keys.W))
  8. state = "walk";
  9. RunController(dwarfAnimator, idle);
  10. }
  11. else if (state == "walk")
  12. {
  13. currentSpeed = WALK_SPEED;
  14. if (keyState.IsKeyUp(Keys.W))
  15. state = "idle";
  16. RunController(dwarfAnimator, walk);
  17. }
  18.  
○「Wキー」でドワーフを前進させ、「Wキー」を離すことで彼を立ち状態にします。
○もしドワーフが立ち状態であれば、現在のアニメーションに「立ち」状態を設定します。
○アニメーションブレンドが null でない場合、現在のアニメーションがブレンドされるものを決定します。
○「立ち」状態と「歩く」状態の間の移行に疑問が残ることに気づきます。これはアニメーションブレンドで解決できます。それについては次のセクションで記載します。

最終更新:2009年06月21日 12:48