VActorAnimationController.cpp
Engine/source/Verve/VActor/VActorAnimationController.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Verve 4// Copyright (C) 2014 - Violent Tulip 5// 6// Permission is hereby granted, free of charge, to any person obtaining a copy 7// of this software and associated documentation files (the "Software"), to 8// deal in the Software without restriction, including without limitation the 9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10// sell copies of the Software, and to permit persons to whom the Software is 11// furnished to do so, subject to the following conditions: 12// 13// The above copyright notice and this permission notice shall be included in 14// all copies or substantial portions of the Software. 15// 16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22// IN THE SOFTWARE. 23//----------------------------------------------------------------------------- 24#include "VActorAnimationController.h" 25 26#include "VActor.h" 27#include "VActorData.h" 28#include "VActorAnimationStates.h" 29 30//----------------------------------------------------------------------------- 31 32VActorAnimationController::VActorAnimationController( void ) : 33 mObject( NULL ) 34{ 35 // Void. 36} 37 38VActorAnimationController::~VActorAnimationController( void ) 39{ 40 // Clear Table. 41 mAnimationTable.clear(); 42} 43 44 45 46 47//----------------------------------------------------------------------------- 48// 49// Initialisation Methods. 50// 51//----------------------------------------------------------------------------- 52 53//----------------------------------------------------------------------------- 54// 55// VActorAnimationController::initAnimationTable(); 56// 57// ... 58// 59//----------------------------------------------------------------------------- 60bool VActorAnimationController::initAnimationTable( void ) 61{ 62 // Valid Object? 63 if ( !isValidObject() ) 64 { 65 // No, Quit Now. 66 return false; 67 } 68 69 // Clear the Table. 70 mAnimationTable.clear(); 71 72 // Fetch Sequence List. 73 VActorData::tAnimationSequenceVector *sequenceList = getObject()->getDataBlock()->getAnimationList(); 74 75 // Initialise the Animation States. 76 for ( VActorData::tAnimationSequenceVector::iterator itr = sequenceList->begin(); 77 itr != sequenceList->end(); 78 itr++ ) 79 { 80 // Fetch Sequence Definition. 81 const VActorData::sAnimationSequence &animSequence = ( *itr ); 82 83 // Valid State? 84 if ( animSequence.State ) 85 { 86 // Register Animation. 87 mAnimationTable.registerState( animSequence.State, animSequence.Priority ); 88 } 89 } 90 91 // Sort the Table. 92 mAnimationTable.sort(); 93 94 // Valid. 95 return true; 96} 97 98//----------------------------------------------------------------------------- 99// 100// VActorAnimationController::initAnimation( pThread, pIndex, pPosition, pTimeScale ); 101// 102// ... 103// 104//----------------------------------------------------------------------------- 105bool VActorAnimationController::initAnimation( sAnimationRef &pAnimation, const U32 &pIndex, const F32 &pPosition, const F32 &pTimeScale ) 106{ 107 // Valid Object & Sequence? 108 if ( !isValidObject() || !isAnimationSequence( pIndex ) ) 109 { 110 // No, Quit Now. 111 return false; 112 } 113 114 // Store as Current Animation. 115 pAnimation.Index = pIndex; 116 117 // Initialise Thread. 118 return initAnimationThread( pAnimation.Thread, pAnimation.Index, pPosition, pTimeScale ); 119} 120 121//----------------------------------------------------------------------------- 122// 123// VActorAnimationController::initAnimationThread( pThread, pIndex, pPosition, pTimeScale ); 124// 125// ... 126// 127//----------------------------------------------------------------------------- 128bool VActorAnimationController::initAnimationThread( TSThread *&pThread, const U32 &pIndex, const F32 &pPosition, const F32 &pTimeScale ) 129{ 130 // Valid Object & Sequence? 131 if ( !isValidObject() || !isAnimationSequence( pIndex ) ) 132 { 133 // No, Quit Now. 134 return false; 135 } 136 137 // Valid Thread? 138 if ( !pThread ) 139 { 140 // Create a Thread. 141 pThread = getShapeInstance()->addThread(); 142 } 143 144 // Init the Sequence. 145 getShapeInstance()->setSequence( pThread, getAnimationSequence( pIndex ), pPosition ); 146 147 // Set Initial Time Scale. 148 getShapeInstance()->setTimeScale( pThread, pTimeScale ); 149 150 // Valid. 151 return true; 152} 153 154//----------------------------------------------------------------------------- 155// 156// VActorAnimationController::initBaseAnimation( pThread, pIndex, pPosition, pTimeScale ); 157// 158// ... 159// 160//----------------------------------------------------------------------------- 161bool VActorAnimationController::initBaseAnimation( const U32 &pIndex, const F32 &pPosition, const F32 &pTimeScale ) 162{ 163 return initAnimation( mBaseAnimation, pIndex, pPosition, pTimeScale ); 164} 165 166 167 168 169//----------------------------------------------------------------------------- 170// 171// Accessor Methods. 172// 173//----------------------------------------------------------------------------- 174 175//----------------------------------------------------------------------------- 176// 177// VActorAnimationController::isValidObject(); 178// 179// ... 180// 181//----------------------------------------------------------------------------- 182bool VActorAnimationController::isValidObject( void ) 183{ 184 return ( mObject != NULL && mObject->getDataBlock() != NULL ); 185} 186 187//----------------------------------------------------------------------------- 188// 189// VActorAnimationController::getObject(); 190// 191// ... 192// 193//----------------------------------------------------------------------------- 194VActor *VActorAnimationController::getObject( void ) 195{ 196 return mObject; 197} 198 199//----------------------------------------------------------------------------- 200// 201// VActorAnimationController::setObject( pObject ); 202// 203// ... 204// 205//----------------------------------------------------------------------------- 206void VActorAnimationController::setObject( VActor *pObject ) 207{ 208 // Set Object. 209 mObject = pObject; 210 211 // Set Table's Reference. 212 mAnimationTable.setObject( pObject ); 213} 214 215//----------------------------------------------------------------------------- 216// 217// VActorAnimationController::getShape(); 218// 219// ... 220// 221//----------------------------------------------------------------------------- 222const TSShape *VActorAnimationController::getShape( void ) 223{ 224 if ( !isValidObject() ) 225 { 226 return NULL; 227 } 228 229 return mObject->getShape(); 230} 231 232//----------------------------------------------------------------------------- 233// 234// VActorAnimationController::getShapeInstance(); 235// 236// ... 237// 238//----------------------------------------------------------------------------- 239TSShapeInstance *VActorAnimationController::getShapeInstance( void ) 240{ 241 if ( !isValidObject() ) 242 { 243 return NULL; 244 } 245 246 return mObject->getShapeInstance(); 247} 248 249 250 251 252//----------------------------------------------------------------------------- 253// 254// Animation Methods 255// 256//----------------------------------------------------------------------------- 257 258void VActorAnimationController::update( const F32 &pDelta ) 259{ 260 // Valid Objects? 261 if ( !isValidObject() ) 262 { 263 // No, Quit Now. 264 return; 265 } 266 267 // Update Animation State. 268 mAnimationTable.execute(); 269 270 // Advance Threads. 271 getShapeInstance()->advanceTime( pDelta, mBaseAnimation.Thread ); 272} 273 274//----------------------------------------------------------------------------- 275// 276// VActorAnimationController::isAnimationSequence( pIndex ); 277// 278// ... 279// 280//----------------------------------------------------------------------------- 281bool VActorAnimationController::isAnimationSequence( const U32 &pIndex ) 282{ 283 return ( getAnimationSequence( pIndex ) != -1 ); 284} 285 286//----------------------------------------------------------------------------- 287// 288// VActorAnimationController::getAnimationSequence( pIndex ); 289// 290// ... 291// 292//----------------------------------------------------------------------------- 293S32 VActorAnimationController::getAnimationSequence( const U32 &pIndex ) 294{ 295 // Valid Object? 296 if ( !mObject || !mObject->getDataBlock() ) 297 { 298 // No, Invalid Sequence. 299 return -1; 300 } 301 302 // Return Sequence. 303 return mObject->getDataBlock()->getAnimationSequence( pIndex ); 304} 305 306//----------------------------------------------------------------------------- 307// 308// VActorAnimationController::getAnimation( pIndex ); 309// 310// ... 311// 312//----------------------------------------------------------------------------- 313U32 VActorAnimationController::getAnimation( void ) 314{ 315 // Base Animation Initialised? 316 if ( !mBaseAnimation.Thread ) 317 { 318 // Null. 319 return U32_MAX; 320 } 321 322 // Return Current Animation. 323 return mBaseAnimation.Index; 324} 325 326//----------------------------------------------------------------------------- 327// 328// VActorAnimationController::setAnimation( pIndex ); 329// 330// ... 331// 332//----------------------------------------------------------------------------- 333void VActorAnimationController::setAnimation( const U32 &pIndex ) 334{ 335 // Base Animation Initialised? 336 if ( !mBaseAnimation.Thread || mBaseAnimation.Index == pIndex ) 337 { 338 // Don't Update. 339 return; 340 } 341 342 // Store as Current Animation. 343 mBaseAnimation.Index = pIndex; 344 345 // Fetch the Sequence. 346 const S32 &sequence = getAnimationSequence( pIndex ); 347 348 // Valid? 349 if ( sequence != -1 ) 350 { 351 // Play the Sequence. 352 getShapeInstance()->transitionToSequence( mBaseAnimation.Thread, sequence, 0.f, 0.15f, true ); 353 //getShapeInstance()->setSequence( mBaseAnimation.Thread, sequence, 0.f ); 354 } 355} 356