Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2012
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include "Animation.h"
  2.  
  3. Animation::Animation(void)
  4. {
  5. }
  6.  
  7. Animation::Animation(int cFrame, int numOfFrames, float timePerFrame[], bool animateOnlyOnce, bool setIndividualFrameTimes)
  8. {
  9.     //Set initial values
  10.     Timer = timer();
  11.     currentTime = 0.0f;
  12.     currentFrame = cFrame;
  13.     oldFrame = cFrame;
  14.     numberOfFrames = numOfFrames;
  15.  
  16.     //Set Individual Maximum Times per frame
  17.     for (int n = 0; n < numOfFrames; n++)
  18.         a_maxTimePerFrame[n] = timePerFrame[n];
  19.  
  20.     animateOnce = animateOnlyOnce;
  21.     animating = true;
  22. }
  23.  
  24. Animation::Animation(int cFrame, int numOfFrames, float timePerFrame, bool animateOnlyOnce)
  25. {
  26.     //Set initial values
  27.     Timer = timer();
  28.     currentTime = 0.0f;
  29.     currentFrame = cFrame;
  30.     oldFrame = cFrame;
  31.     numberOfFrames = numOfFrames;
  32.     maxTimePerFrame = timePerFrame;
  33.     animateOnce = animateOnlyOnce;
  34.     animating = true;
  35.     individualFrameTimes = false;
  36. }
  37.  
  38. void Animation::Update(SDLib& lib)
  39. {
  40.     if (lib.pause)
  41.         Timer.pause();
  42.  
  43.     if (Timer.is_paused() && lib.play)
  44.         Timer.unpause();
  45.  
  46.     if (!Timer.is_started())
  47.         Timer.start();
  48.  
  49.     if (animating && numberOfFrames > 0 && maxTimePerFrame > 0 && !lib.pause && !individualFrameTimes) //We have some frames to animate with
  50.     {
  51.         float accumlator = 1000 * Timer.getDelta();
  52.         currentTime += accumlator;
  53.         float maxTime = 0;
  54.  
  55.         //Half The Reload-Time if we want faster gamplay
  56.         if (lib.speedUp)
  57.             maxTime = maxTimePerFrame / 2;
  58.         else
  59.             maxTime = maxTimePerFrame;
  60.  
  61.         if (currentTime > maxTime)
  62.         {
  63.             currentTime = 0.0f;
  64.             currentFrame++;
  65.             Timer.stop();
  66.  
  67.             if (currentFrame >= numberOfFrames && !animateOnce)
  68.             {
  69.                 currentFrame = oldFrame;
  70.             }
  71.             else if (currentFrame >= numberOfFrames && animateOnce)
  72.             {
  73.                 animating = false;
  74.                 currentFrame = numberOfFrames - 1;
  75.             }
  76.         }
  77.     }
  78.     else if (animating && numberOfFrames > 0 && a_maxTimePerFrame[currentFrame] > 0 && individualFrameTimes && !lib.pause) //We have some frames to animate with
  79.     {
  80.         float accumlator = 1000 * Timer.getDelta();
  81.         currentTime += accumlator;
  82.         float maxTime = 0;
  83.  
  84.         //Half The Reload-Time if we want faster gamplay
  85.         if (lib.speedUp)
  86.             maxTime = a_maxTimePerFrame[currentFrame] / 2;
  87.         else
  88.             maxTime = a_maxTimePerFrame[currentFrame];
  89.  
  90.         if (currentTime > maxTime)
  91.         {
  92.             currentTime = 0.0f;
  93.             currentFrame++;
  94.             Timer.stop();
  95.  
  96.             if (currentFrame >= numberOfFrames && !animateOnce)
  97.             {
  98.                 currentFrame = oldFrame;
  99.             }
  100.             else if (currentFrame >= numberOfFrames && animateOnce)
  101.             {
  102.                 animating = false;
  103.                 currentFrame = numberOfFrames - 1;
  104.             }
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement