kevryan

CMPartAidansCooler.cpp

Apr 22nd, 2016
32
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "CMPartAidansCooler.h"
  2. #include "contraptions/managers/CMLevelManager.h"
  3. #include "cmcocos/util/CCOldTypes.h"
  4.  
  5. NS_CC_BEGIN
  6.  
  7. CMPartAidansCooler::CMPartAidansCooler(int id, CMLevel *level, int type, TmPointInt32 pos, int partProcessFlags, int partActionFlags, int state, int rot)
  8.  : CMPartContainer(id,level,type,pos,partProcessFlags,partActionFlags,state,rot)
  9. {
  10. }
  11.  
  12. void CMPartAidansCooler::onCreation(bool fromLoad)
  13. {
  14.     mHandleBody = getBodyById(1);
  15.     mWheelBody = getBodyById(2);
  16.  
  17.     mWheelCpBody = mWheelBody->mCpBody;
  18.     mHandleCpBody = mHandleBody->mCpBody;
  19.  
  20.     mNormalCoolerSprite = static_cast<CMPartArtSpriteRect*>(getArtById(0))->mSprite;
  21.     mFlippedCoolerSprite = static_cast<CMPartArtSpriteRect*>(getArtById(1))->mSprite;
  22.  
  23.     mNormalCoolerSprite->setVisible(!mFlippedX);
  24.     mFlippedCoolerSprite->setVisible(mFlippedX);
  25.     if (mFlippedX)
  26.         mLevel->addArtToList(getArtById(1));
  27.     else
  28.         mLevel->addArtToList(getArtById(0));
  29.  
  30.  
  31.     CMPart::onCreation(fromLoad);
  32. }
  33.  
  34. void CMPartAidansCooler::postPhysicsUpdate(int time, int levelFrame)
  35. {
  36.     // Keep wheel from rotating forever
  37.     if (mWheelCpBody->w != 0)
  38.     {
  39.         if (mWheelCpBody->w > -INT32_HALF && mWheelCpBody->w < INT32_HALF)
  40.             mWheelCpBody->w = 0;
  41.         else
  42.             mWheelCpBody->w = INT64_SHIFT_DOWN(mWheelCpBody->w * FLOAT_TO_INT64(0.98f));
  43.     }
  44.  
  45.     // Give handle pivot a little friction too
  46.     if (mHandleCpBody->w != 0)
  47.     {
  48.         if (mHandleCpBody->w > -(INT32_HALF>>2) && mHandleCpBody->w < (INT32_HALF>>2))
  49.             mHandleCpBody->w = 0;
  50.         else
  51.             mHandleCpBody->w = INT64_SHIFT_DOWN(mHandleCpBody->w * FLOAT_TO_INT64(0.96f));
  52.     }
  53.  
  54.     CMPart::postPhysicsUpdate(time,levelFrame);
  55. }
  56.  
  57. void CMPartAidansCooler::flip()
  58. {
  59.     for (int i=0;i<6;i++)
  60.         getShapeById(i)->flip();
  61.  
  62.     mHandleBody->flipX();
  63.     mWheelBody->flipX();
  64.  
  65.     cpPivotJoint *pivot = (cpPivotJoint*) getConstraintById(0)->mCpConstraint;
  66.     pivot->anchr1.x = -pivot->anchr1.x;
  67.     pivot->anchr2.x = -pivot->anchr2.x;
  68.  
  69.     pivot = (cpPivotJoint*) getConstraintById(1)->mCpConstraint;
  70.     pivot->anchr1.x = -pivot->anchr1.x;
  71.     pivot->anchr2.x = -pivot->anchr2.x;
  72.  
  73.     cpRotaryLimitJoint *limitJoint = (cpRotaryLimitJoint*) getConstraintById(2)->mCpConstraint;
  74.     int temp = limitJoint->max;
  75.     limitJoint->max = -limitJoint->min;
  76.     limitJoint->min = -temp;
  77.  
  78.     mFlippedX = !mFlippedX;
  79.     mInitFlippedX = mFlippedX;
  80.     mScaleX = -mScaleX;
  81.  
  82.     mNormalCoolerSprite->setVisible(!mFlippedX);
  83.     mFlippedCoolerSprite->setVisible(mFlippedX);
  84.  
  85.     if (mFlippedX)
  86.     {
  87.         mLevel->removeArtFromList(getArtById(0));
  88.         mLevel->addArtToList(getArtById(1));
  89.     }
  90.     else
  91.     {
  92.         mLevel->removeArtFromList(getArtById(1));
  93.         mLevel->addArtToList(getArtById(0));
  94.     }
  95.  
  96.     updatePosAndRot();
  97. }
  98.  
  99. NS_CC_END
RAW Paste Data