Clone of PhatAC @ https://github.com/floaterxk/PhatAC

TurbineAnimation.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "StdAfx.h"
  2. #include "TurbineAnimation.h"
  3. TurbineAnimationFrame::TurbineAnimationFrame()
  4. {
  5. m_pTranslations = NULL;
  6. m_pEvents = NULL;
  7. }
  8. TurbineAnimationFrame::~TurbineAnimationFrame()
  9. {
  10. SafeDeleteArray(m_pTranslations);
  11. SafeDeleteArray(m_pEvents);
  12. }
  13. BYTE* TurbineAnimationFrame::InitializeTranslations(BYTE *pbFrame, int iPartCount)
  14. {
  15. m_pTranslations = new PartOrientation[iPartCount];
  16. for (int i = 0; i < iPartCount; i++)
  17. pbFrame = m_pTranslations[i].ReadData(pbFrame);
  18. return pbFrame;
  19. }
  20. BYTE* TurbineAnimationFrame::InitializeEvents(BYTE *pbFrame)
  21. {
  22. int iEventCount = *((DWORD *)pbFrame);
  23. pbFrame += 4;
  24. if (iEventCount)
  25. {
  26. m_pEvents = new PartEvent[iEventCount];
  27. for (int i = 0; i < iEventCount; i++)
  28. pbFrame = m_pEvents[i].ReadData(pbFrame);
  29. }
  30. return pbFrame;
  31. }
  32. void TurbineAnimationFrame::ExecuteFrame(CPhysicsObj* pWeenie, AnimationPackage* pAnimation)
  33. {
  34. }
  35. TurbineAnimation::TurbineAnimation(DWORD dwID) : TurbineObject(dwID)
  36. {
  37. m_iType = 0;
  38. m_iPartCount = 0;
  39. m_iFrameCount = 0;
  40. }
  41. TurbineAnimation::~TurbineAnimation()
  42. {
  43. SafeDeleteArray(m_pFrames);
  44. }
  45. BYTE* TurbineAnimation::InitializeFrames(BYTE *pbFrames, unsigned int iType, unsigned int iPartCount, unsigned int iFrameCount)
  46. {
  47. m_iType = iType;
  48. m_iPartCount = iPartCount;
  49. m_iFrameCount = iFrameCount;
  50. m_pFrames = new ANIMATIONFRAME[m_iFrameCount];
  51. if (iType == 1 || iType == 3)
  52. pbFrames += sizeof(PartOrientation) * iPartCount;
  53. for (unsigned int i = 0; i < iFrameCount; i++)
  54. {
  55. pbFrames = m_pFrames[i].InitializeTranslations(pbFrames, iPartCount);
  56. pbFrames = m_pFrames[i].InitializeEvents(pbFrames);
  57. }
  58. return pbFrames;
  59. }
  60. void TurbineAnimation::Initialize(BYTE *pbData, DWORD dwLength)
  61. {
  62. if (!pbData)
  63. return;
  64. DWORD *header = (DWORD *)pbData;
  65. // Animation Header (4 dwords, ie: 16 bytes)
  66. DWORD dwFileID = header[0];
  67. int iType = header[1];
  68. int iPartCount = header[2];
  69. int iFrameCount = header[3];
  70. BYTE* frames = pbData + 16;
  71. InitializeFrames(frames, iType, iPartCount, iFrameCount);
  72. }
  73. unsigned int TurbineAnimation::GetFrameCount()
  74. {
  75. return m_iFrameCount;
  76. }
  77. ANIMATIONFRAME* TurbineAnimation::GetFrame(int iFrame)
  78. {
  79. // Returns a frame by index from the array.
  80. return &m_pFrames[iFrame];
  81. }
  82. long TurbineAnimation::GetFrameByTime(float fTime, float fSpeed)
  83. {
  84. if (!fSpeed)
  85. fSpeed = 30.0f;
  86. return (long)(fTime * fSpeed);
  87. }
  88. bool TurbineAnimation::Execute(CPhysicsObj* pWeenie, AnimationPackage* pAnimation)
  89. {
  90. // The is the workhorse for animations.
  91. if (!pAnimation)
  92. return false;
  93. if (!GetFrameCount())
  94. return false;
  95. // Calculate the number of frames intended to be executed.
  96. DWORD dwFrameCount =
  97. min(GetFrameCount() - 1, pAnimation->m_dwEndFrame) -
  98. min(GetFrameCount() - 1, pAnimation->m_dwStartFrame);
  99. //The offset from the starting frame.
  100. float fTime = (float)(g_pGlobals->Time() - pAnimation->m_fStartTime);
  101. long lFrame = GetFrameByTime(fTime, pAnimation->m_fSpeed);
  102. //The offset extends beyond the desired frames.
  103. if (unsigned(abs(lFrame)) >= dwFrameCount)
  104. return false;
  105. DWORD dwCurrentFrame = pAnimation->GetBaseFrame() + lFrame;
  106. if (dwCurrentFrame != pAnimation->m_dwCurrentFrame)
  107. {
  108. //Time to launch some events!
  109. if (lFrame > 0) {
  110. while (1)
  111. {
  112. //Execute all frames until we are up to date.
  113. if (0x80000000 == pAnimation->m_dwCurrentFrame) //Null frame.
  114. {
  115. pAnimation->m_dwCurrentFrame = pAnimation->GetBaseFrame();
  116. GetFrame(pAnimation->m_dwCurrentFrame)->ExecuteFrame(pWeenie, pAnimation);
  117. }
  118. else if (dwCurrentFrame > pAnimation->m_dwCurrentFrame)
  119. {
  120. GetFrame(++pAnimation->m_dwCurrentFrame)->ExecuteFrame(pWeenie, pAnimation);
  121. }
  122. else
  123. break;
  124. }
  125. }
  126. else if (lFrame < 0)
  127. {
  128. while (1)
  129. {
  130. if (0x80000000 == pAnimation->m_dwCurrentFrame) //Null frame.
  131. {
  132. pAnimation->m_dwCurrentFrame = pAnimation->GetBaseFrame();
  133. GetFrame(pAnimation->m_dwCurrentFrame)->ExecuteFrame(pWeenie, pAnimation);
  134. }
  135. else if (dwCurrentFrame < pAnimation->m_dwCurrentFrame)
  136. {
  137. GetFrame(--pAnimation->m_dwCurrentFrame)->ExecuteFrame(pWeenie, pAnimation);
  138. }
  139. else
  140. break;
  141. }
  142. }
  143. }
  144. return true;
  145. }