-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnimatedObject.cpp
executable file
·137 lines (109 loc) · 4.2 KB
/
AnimatedObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// ************************************************************************
//
// File: AnimatedObject.cpp
// Programmer: T.J. Eason
// Project: Game Engine
// Description: Get scene object to support mesh animation
// Date: 3-31-10
// Revision 1: 4-2-10
//
// *************************************************************************
#include "Engine.h"
// Animated object class constructor.
AnimatedObject::AnimatedObject( char *meshName, char *meshPath, unsigned long type ) : SceneObject( type, meshName, meshPath, false )
{
// Create a clone of the mesh's animation controller.
if( GetMesh() != NULL )
GetMesh()->CloneAnimationController( &m_animationController );
// No mesh exist, so clear pointer
else
m_animationController = NULL;
// Set the track speed on both tracks to full speed.
if( m_animationController != NULL )
{
m_animationController->SetTrackSpeed( 0, 1.0f );
m_animationController->SetTrackSpeed( 1, 1.0f );
}
// Clear the variables used for animation.
m_currentTrack = 0;
m_currentTime = 0.0f;
}
// Animated object class destructor.
AnimatedObject::~AnimatedObject()
{
if( m_animationController )
{
m_animationController ->Release();
m_animationController = NULL;
}
}
// Updates the object.
void AnimatedObject::Update( float elapsed, bool addVelocity )
{
// Allow the base scene object to update.
SceneObject::Update( elapsed, addVelocity );
// Check if the object has an animation controller.
if( m_animationController )
{
// Advance the object's animation controller.
m_animationController->AdvanceTime( elapsed, this );
// Keep track of the current time for animation purposes.
m_currentTime += elapsed;
}
// Update the mesh.
if( GetMesh() != NULL )
GetMesh()->Update();
}
// Plays the given animation with the given transition time.-
void AnimatedObject::PlayAnimation( unsigned int animation, float transitionTime, bool loop )
{
// Ensure object has a valid animation controller.
if( m_animationController == NULL )
return;
// Ensure the transition time is always greater than zero.
if( transitionTime <= 0.0f )
transitionTime = 0.000001f;
// Find which track to play the new animation on.
unsigned int newTrack = ( m_currentTrack == 0 ? 1 : 0 );
// Get a pointer to the animation set to play.
ID3DXAnimationSet *as;
m_animationController->GetAnimationSet( animation, &as );
// Set the animation set to the new track.
m_animationController->SetTrackAnimationSet( newTrack, as );
// Clear all the events that are currently set on the tracks.
m_animationController->UnkeyAllTrackEvents( m_currentTrack );
m_animationController->UnkeyAllTrackEvents( newTrack );
// Check if this animation should be looped or only played once.
if( loop == true )
{
// Transition the new track in over the specified transition time period.
m_animationController->KeyTrackEnable( m_currentTrack, false, m_currentTime + transitionTime );
m_animationController->KeyTrackWeight( m_currentTrack, 0.0f, m_currentTime, transitionTime, D3DXTRANSITION_LINEAR );
m_animationController->SetTrackEnable( newTrack, true );
m_animationController->KeyTrackWeight( newTrack, 1.0f, m_currentTime, transitionTime, D3DXTRANSITION_LINEAR );
}
else
{
// Stop the current track, and start the new track without transitioning.
m_animationController->SetTrackEnable( m_currentTrack, false );
m_animationController->SetTrackWeight( m_currentTrack, 0.0f );
m_animationController->SetTrackEnable( newTrack, true );
m_animationController->SetTrackWeight( newTrack, 1.0f );
m_animationController->SetTrackPosition( newTrack, 0.0f );
m_animationController->KeyTrackEnable( newTrack, false, m_currentTime + as->GetPeriod() );
}
// Release the pointer to the animation set.
as->Release();
// The new track is now the current track.
m_currentTrack = newTrack;
}
// Returns a pointer to the object's animation controller.
ID3DXAnimationController *AnimatedObject::GetAnimationController()
{
return m_animationController;
}
// Animation callback handler.
HRESULT CALLBACK AnimatedObject::HandleCallback( THIS_ UINT Track, LPVOID pCallbackData )
{
return S_OK;
}