はじめに
○最初に状態を制御するために、新しいメソッド UpdateState(GameTime time) を作成し、Update メソッドの始めに呼び出します。
// Add this as a new method
private void UpdateState(GameTime gameTime)
{
}
// Add this to the beginning of the Update method
UpdateState(gameTime);
○次のメンバ変数を追加して下さい。
// Add these as member variables
// This represents what the dwarf is doing
string state = "idle";
float currentSpeed = 0;
const float WALK_SPEED = .115f;
ドワーフを旋回させる
○ドワーフが何をしているかに関係なく、「Aキー」と「Dキー」を使用して彼を旋回させることができます。次のソースコードを Update メソッドに追加します。
// Add this to the beginning of the Update method
KeyboardState keyState = Keyboard.GetState();
if (currentSpeed > 0)
{
dwarfPosition += (-Matrix.CreateTranslation(0, 0, currentSpeed)
* rotation).Translation;
}
if (keyState.IsKeyDown(Keys.D))
{
rotation *=
Matrix.CreateFromAxisAngle(Vector3.Up, -MathHelper.Pi / 25.0f);
}
if (keyState.IsKeyDown(Keys.A))
{
rotation *=
Matrix.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi / 25.0f);
}
○ドワーフの位置を増加させるとき、彼が向いている方向に前進します。通常とは逆順に行列が掛け合わされます。(平行移動 * 回転)
ドワーフを動き回らせる
○次のソースコードを UpdateState メソッドに追加して下さい。
// Add this to the UpdateState method
KeyboardState keyState = Keyboard.GetState();
BonePoseCollection poses = dwarfAnimator.BonePoses;
if (state == "idle")
{
currentSpeed = 0;
if (keyState.IsKeyDown(Keys.W))
state = "walk";
RunController(dwarfAnimator, idle);
}
else if (state == "walk")
{
currentSpeed = WALK_SPEED;
if (keyState.IsKeyUp(Keys.W))
state = "idle";
RunController(dwarfAnimator, walk);
}
○「Wキー」でドワーフを前進させ、「Wキー」を離すことで彼を立ち状態にします。
○もしドワーフが立ち状態であれば、現在のアニメーションに「立ち」状態を設定します。
○アニメーションブレンドが null でない場合、現在のアニメーションがブレンドされるものを決定します。
○「立ち」状態と「歩く」状態の間の移行に疑問が残ることに気づきます。これはアニメーションブレンドで解決できます。それについては次のセクションで記載します。
最終更新:2009年06月21日 12:48