#region File Description
//-----------------------------------------------------------------------------
// GameAnimateModel.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using RobotGameData.Render;
using RobotGameData.GameObject;
using RobotGameData.Resource;
using RobotGameData.Collision;
#endregion
namespace RobotGameData.GameObject
{
///
/// It has the Model’s animation data and processes the animate function
/// of the Model.
///
public class GameAnimateModel : GameModel
{
#region Fields
protected List animationBlenderList =
new List();
protected List animationList =
new List();
protected bool traceAnimation = false;
#endregion
#region Properties
public int AnimationCount
{
get { return animationList.Count; }
}
#endregion
///
/// Constructor.
///
/// model resource
public GameAnimateModel(GameResourceModel resource)
: base(resource) {}
///
/// Constructor.
///
/// model file name
public GameAnimateModel(string fileName)
: base( fileName) {}
public override void Initialize()
{
base.Initialize();
}
protected override void UnloadContent()
{
base.UnloadContent();
}
///
/// animates the skeleton of bones using stored key frame
/// in the AnimationBlender.
///
protected override void OnUpdate(GameTime gameTime)
{
// Reset the bone's transform by source transform
this.ModelData.model.CopyBoneTransformsFrom(this.ModelData.boneTransforms);
if (animationList.Count > 0)
{
for(int i=0; i
/// processes the necessary steps for binding a model.
/// It creates AnimationBlender to the number of the model's bone.
///
public override void BindModel(ModelData modelData)
{
base.BindModel(modelData);
if (animationBlenderList.Count == 0)
{
// Insert all bones in the AnimationBinder
for (int i = 0; i < modelData.model.Bones.Count; i++)
{
AnimationBlender animationBlender = new AnimationBlender();
animationBlender.Name = modelData.model.Bones[i].Name;
animationBlenderList.Add(animationBlender);
}
}
}
///
/// adds an animation to the list.
///
/// animation structure
/// index of the added animation
public int AddAnimation(AnimationSequence animation)
{
if (animation == null)
{
throw new ArgumentNullException("animation");
}
animationList.Add(animation);
return animationList.IndexOf(animation);
}
///
/// adds an animation to the list.
///
/// animation file (.Animation)
/// index of the added animation
public int AddAnimation(string fileName)
{
// Load and find an animation resource
GameResourceAnimation resource =
FrameworkCore.ResourceManager.LoadAnimation(fileName);
return AddAnimation(resource.Animation);
}
///
/// removes the animation by index.
///
public bool RemoveAnimation(int index)
{
animationList.RemoveAt(index);
return false;
}
///
/// remove all stored animations.
///
public void ClearAnimationAll()
{
animationList.Clear();
}
///
/// gets the animation structure by index.
///
/// stored animation index
/// animation structure
public AnimationSequence GetAnimation(int index)
{
return animationList[index];
}
public AnimationBlender FindAnimationBlenderByBoneName(string boneName)
{
for (int i = 0; i < animationBlenderList.Count; i++)
{
if (animationBlenderList[i].Name == boneName)
return animationBlenderList[i];
}
return null;
}
///
/// plays the bone animation by index.
///
/// stored animation index
/// animation play mode
///
public bool PlayAnimation(int index, AnimPlayMode playMode)
{
return PlayAnimation(index, 0.0f, 0.0f, 1.0f, playMode);
}
///
/// plays the bone animation by index.
///
/// stored animation index
/// begin time of the animation
/// blending time of the animation
///
/// time scale of the animation (default is 1.0)
///
/// animation play mode
///
public bool PlayAnimation(int index, float startTime, float blendTime,
float timeScaleFactor, AnimPlayMode playMode)
{
AnimationSequence animation = GetAnimation(index);
if (animation != null)
{
// Binding the playable AnimationSequence to AnimationBinder
for( int i=0; i
/// gets animation playing time of the bone
///
/// bone's name
/// current playing time
public float GetBonePlayTime(string boneName)
{
for (int i = 0; i < animationBlenderList.Count; i++)
{
if (animationBlenderList[i].Name == boneName)
{
return animationBlenderList[i].AnimationBinder.LocalTime;
}
}
return 0.0f;
}
}
}