home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 02 Useful Techniques / 04 Orkin / AnimController.cpp next >
Encoding:
C/C++ Source or Header  |  2001-06-27  |  2.1 KB  |  76 lines

  1. #include "AnimController.h"
  2.  
  3. // --------------------------------------------------------------------------
  4.  
  5. bool CAnimController::PlayAnim(AnimFileStruct* pRequestedAnim)
  6. {
  7.     bool bPlayAnim = true;
  8.  
  9.     ANIM_INSTANCE_MAP::iterator it;
  10.     for(it = m_mapAnimInstances.begin(); it != m_mapAnimInstances.end(); ++it) 
  11.     {
  12.         EnumAnimCategory ePlayingCategory    = (EnumAnimCategory)it->first;
  13.         AnimFileStruct*     pPlayingAnim        = it->second.pAnimFileStruct;
  14.  
  15.         // Check if animation is already playing.
  16.         if(pRequestedAnim == pPlayingAnim) 
  17.         {
  18.             bPlayAnim = false;
  19.             break;
  20.         }
  21.  
  22.         // Check if categories clash.
  23.         else if(ePlayingCategory & pRequestedAnim->eCategory) 
  24.         {    
  25.             unsigned long nPlayingPriority = pPlayingAnim->nPriority;
  26.             
  27.             // If an animation of the same or lower priority, and the 
  28.             // exact same category is playing, stop the currently playing
  29.             // animation.  The requested animation will replace it.
  30.             if(ePlayingCategory == pRequestedAnim->eCategory) 
  31.             {
  32.                 if(nPlayingPriority > pRequestedAnim->nPriority)
  33.                 {
  34.                     bPlayAnim = false;
  35.                     break;
  36.                 }
  37.                 else {
  38.                     m_mapAnimInstances.erase(it);
  39.                 }
  40.                 break;
  41.             }
  42.  
  43.             // If requested animation has a higher priority, and encompasses the currently
  44.             // playing animation, stop the currently playing animation.
  45.             // For example, UpperBody encompasses LeftArm.
  46.             if( ((unsigned long)ePlayingCategory < (unsigned long)pRequestedAnim->eCategory)
  47.                 && (nPlayingPriority < pRequestedAnim->nPriority) ) 
  48.             {
  49.                 m_mapAnimInstances.erase(it);
  50.                 break;
  51.             }
  52.         }
  53.     }
  54.  
  55.     // No conflicts were found, so play the requested animation.
  56.     if(bPlayAnim)
  57.     {
  58.         m_mapAnimInstances.insert( ANIM_INSTANCE_MAP::value_type( (unsigned long)(pRequestedAnim->eCategory), AnimInstanceStruct(pRequestedAnim) ) );
  59.     }
  60.  
  61.     return bPlayAnim;
  62. }
  63.  
  64. // --------------------------------------------------------------------------
  65.  
  66. void CAnimController::Print()
  67. {
  68.     printf("\nCurrently Playing:\n");
  69.  
  70.     ANIM_INSTANCE_MAP::iterator it;
  71.     for(it = m_mapAnimInstances.begin(); it != m_mapAnimInstances.end(); ++it)
  72.     {
  73.         printf("  %s\n", it->second.pAnimFileStruct->szFileName);
  74.     }
  75. }
  76.