kevryan

CMPartEgg.cpp

May 16th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.26 KB | None | 0 0
  1. #include "CMPartEgg.h"
  2. #include "contraptions/managers/CMLevelManager.h"
  3. #include "cmcocos/util/CCOldTypes.h"
  4.  
  5. NS_CC_BEGIN
  6.  
  7. static CMPartAttractsInfo sDogAttractsInfo(
  8.    FLOAT_TO_INT32(0),FLOAT_TO_INT32(230), 0,0,
  9.    0,0, 0,0,
  10.    ATTRACTS_PRIORITY_ONE,ATTRACTS_PRIORITY_ONE,
  11.    ATTRACTS_CANT_LOOK_BEHIND,ATTRACTS_SHOULDNT_RUN,ATTRACTS_EDIBLE);
  12.  
  13. CMPartEgg::CMPartEgg(int id, CMLevel *level, int type, TmPointInt32 pos, int partProcessFlags, int partActionFlags, int state, int rot)
  14.  : CMPart(id,level,type,pos,partProcessFlags,partActionFlags,state,rot),
  15.    mCpEggBody(NULL),
  16.    mShouldCrack(false),
  17.    mCracked(false)
  18. {
  19.     for (int i = 0; i < MAX_EGG_SPRITES;i++)
  20.         mEggSprites[i] = NULL;
  21. }
  22.  
  23. void CMPartEgg::onCreation(bool fromLoad)
  24. {
  25.     // Hide broken egg art
  26.     static_cast<CMPartArtSpriteRect*>(getArtById(11))->setVisible(false);
  27.  
  28.     mCpEggBody = getBodyById(0)->mCpBody;
  29.  
  30.     CMPartArtSpriteRect *partArt;
  31.     for (int i=0;i<11;i++)
  32.     {
  33.         partArt = static_cast<CMPartArtSpriteRect*>(getArtById(i));
  34.         mEggSprites[i] = partArt->mSprite;
  35.         mEggSprites[i]->setVisible(false);
  36.     }
  37.  
  38.     mEggSprites[mState]->setVisible(true);
  39.     mLevel->addArtToList(getArtById(mState));
  40.  
  41.     CMPart::onCreation(fromLoad);
  42.    
  43.     mSoundResource = getSoundResource("EggBreak");
  44. }
  45.  
  46. void CMPartEgg::setVisible(bool visible)
  47. {
  48.     if (mCracked)
  49.         return;
  50.  
  51.     for (int i=0;i<MAX_EGG_SPRITES;i++)
  52.     {
  53.         if (mEggSprites[i])
  54.             mEggSprites[i]->setVisible(false);
  55.     }
  56.  
  57.     if (mState >= 0 && mState < MAX_EGG_SPRITES && mEggSprites[mState])
  58.         mEggSprites[mState]->setVisible(visible);
  59. }
  60.  
  61.  
  62. void CMPartEgg::setInitialState(int state, bool alwaysUpdate)
  63. {
  64.     if (state != mState)
  65.     {
  66.         mEggSprites[mState]->setVisible(false);
  67.         mLevel->removeArtFromList(getArtById(mState));
  68.  
  69.         mState = state;
  70.         mEggSprites[mState]->setVisible(true);
  71.         mLevel->addArtToList(getArtById(mState));
  72.  
  73.         CMPart::setInitialState(state,true);
  74.     }
  75. }
  76.  
  77. void CMPartEgg::postPhysicsUpdate(int time, int levelFrame)
  78. {
  79.     // Do nothing if egg is cracked or destroyed
  80.     if (mCracked || mInactivated || mMakeInactive)
  81.         return;
  82.  
  83.     if (mShouldCrack)
  84.         crackEgg();
  85.  
  86.     CMPart::postPhysicsUpdate(time,levelFrame);
  87. }
  88.  
  89. void CMPartEgg::crackEgg()
  90. {
  91.     mGoalState = 1;
  92.     mCracked = true;
  93.  
  94.     // Hide intact egg and show broken egg
  95.     mEggSprites[mState]->setVisible(false);
  96.     mLevel->removeArtFromList(getArtById(mState));
  97.     static_cast<CMPartArtSpriteRect*>(getArtById(11))->setVisible(true);
  98.     mLevel->addArtToList(getArtById(11));
  99.  
  100.     // Delete the intact egg shapes
  101.     CMPartShape *handleShape = getShapeById(0);
  102.     deleteShape(handleShape);
  103.     handleShape = getShapeById(1);
  104.     deleteShape(handleShape);
  105.  
  106.     // Infinity moment of interia so Egg can't rotate
  107.     mCpEggBody->a = 0;
  108.     mCpEggBody->w = 0;
  109.     cpBodySetMoment(mCpEggBody, INT32_INFINITY);
  110.  
  111.     // Shape for egg to be detected to be eaten
  112.     new CMPartShapeCircle(3,this,CC_TO_TMPOINTINT32(0,2.5f),getBodyById(0), SP_SENSOR,SP_SFX_SILENT, FLOAT_TO_INT32(11.0f), COL_GROUP_DEFAULT,COL_TYPE_SENSOR_BEGIN_SEPARATE,COL_LAYER_DOG);
  113.  
  114.     playSound(INT32_TO_FLOAT(mPos.x), INT32_TO_FLOAT(mPos.y), mSoundResource, 0, 0);
  115. }
  116.  
  117. void CMPartEgg::message(int messageType, CMPart *otherPart)
  118. {
  119.     // Energy bursts will crack the egg
  120.     if (messageType == PART_MSG_HIT_ENERGY_BURST)
  121.         mShouldCrack = true;
  122.     else
  123.         CMPart::message(messageType,otherPart);
  124. }
  125.  
  126. void CMPartEgg::postHandleCollision(CMPart *otherPart, cpArbiter *arb, bool isB)
  127. {
  128.     // Only process on first contact
  129.     if (mCracked || mShouldCrack || otherPart->mNitroSafe || !cpArbiterIsFirstContact(arb))
  130.         return;
  131.  
  132.     // Get a couple of collision shapes that we will be using
  133.     cpShape *otherShape;
  134.     cpShape *myShape;
  135.     if (isB)
  136.         cpArbiterGetShapes(arb, &otherShape, &myShape);
  137.     else
  138.         cpArbiterGetShapes(arb, &myShape, &otherShape);
  139.  
  140.     // Egg won't break if bouncing off trampoline bouncy material
  141.     if (otherShape->shapeProperty == SP_NORMAL_TRAMPOLINE || otherShape->shapeProperty == SP_MECHANICAL_TRAMPOLINE)
  142.         return;
  143.  
  144.     // Also want to get egg body and other part's body
  145.     cpBody *otherCpBody;
  146.     cpBody *myCpBody;
  147.     if (isB)
  148.         cpArbiterGetBodies(arb,&otherCpBody,&myCpBody);
  149.     else
  150.         cpArbiterGetBodies(arb,&myCpBody,&otherCpBody);
  151.  
  152.     // Mass of the other body
  153.     int oMass = otherCpBody->m;
  154.  
  155.     // Protecting from overflow by shifting down by 4
  156.     //  -- to keep everything running the same, adjusting the checks below to ((val * val) / 256 ) * 65536 which is (val * val) << 8
  157.     cpVect64 tiVect = cpArbiterTotalImpulse(arb);
  158.     tiVect.x = INT64_SHIFT_DOWN_BY(tiVect.x,4);
  159.     tiVect.y = INT64_SHIFT_DOWN_BY(tiVect.y,4);
  160.     tmInt64 totImp = INT64_SHIFT_DOWN(tiVect.x * (tmInt64)tiVect.x + tiVect.y * (tmInt64)tiVect.y);
  161.  
  162.     // Mass inverse value of zero mean infinite mass
  163.     if (otherCpBody->m_inv == 0)
  164.     {
  165.         // Hit a static object so only Egg velocity/impulse into shape matters
  166.         if (totImp > ((500LL*500LL)<<8))
  167.             mShouldCrack = true;
  168.     }
  169.     else if (oMass > FLOAT_TO_INT32(1.5f))
  170.     {
  171.         // Different masses require different impulses to crack egg
  172.         if (oMass > FLOAT_TO_INT32(80.0f))
  173.             mShouldCrack = (totImp > ((1400LL*1400LL)<<8));
  174.         else if (oMass > FLOAT_TO_INT32(40.0f))
  175.             mShouldCrack = (totImp > ((2800LL*2800LL)<<8));
  176.         else if (oMass > FLOAT_TO_INT32(20.0f))
  177.             mShouldCrack = (totImp > ((2200LL*2200LL)<<8));
  178.         else if (oMass > FLOAT_TO_INT32(6.0f))
  179.             mShouldCrack = (totImp > ((1800LL*1800LL)<<8));
  180.         else if (oMass > FLOAT_TO_INT32(4.0f))
  181.             mShouldCrack = (totImp > ((1400LL*1400LL)<<8));
  182.         else
  183.             mShouldCrack = (totImp > ((740LL*740LL)<<8));
  184.     }
  185. }
  186.  
  187. int CMPartEgg::sensorBeginHandleCollision(CMPart *otherPart, cpArbiter *arb)
  188. {
  189.    // Dog wants to eat egg
  190.    if (otherPart->mType == PART_DOG)
  191.       otherPart->tryToEat(this);
  192.  
  193.    return 0;
  194. }
  195.  
  196. void CMPartEgg::processExplosion(TmPointInt32 explosionSource, int maxDistSquared, int impulseAtSource, int impulseAtEdge)
  197. {
  198.     if (!mShouldCrack)
  199.     {
  200.         // See if explosion is close enough to crack egg
  201.         int deltaX = mBodies->mCpBody->p.x - explosionSource.x;
  202.         int deltaY = mBodies->mCpBody->p.y - explosionSource.y;
  203.         tmInt64 distSquared = INT64_SHIFT_DOWN(deltaX * (tmInt64)deltaX + deltaY * (tmInt64)deltaY);
  204.         mShouldCrack = (distSquared < maxDistSquared/2);
  205.     }
  206. }
  207.  
  208. void CMPartEgg::flipGroup(int flags, TmPointInt32 centerPos)
  209. {
  210.     mRot = 0 - mRot;
  211.  
  212.     CMPart::flipGroup(flags,centerPos);
  213. }
  214.  
  215. int CMPartEgg::eaten()
  216. {
  217.     if (mCracked && !mInactivated && !mMakeInactive)
  218.     {
  219.         mMakeInactive = true;
  220.  
  221.         // Return how long it takes to eat
  222.         return FLOAT_TO_INT32(1.4f);
  223.     }
  224.  
  225.     return 0;
  226. }
  227.  
  228. CMPartAttractsInfo *CMPartEgg::getAttractsInfo(CMPart *part)
  229. {
  230.     // Only dogs are attracted to broken eggs right now
  231.     if (mCracked && (part->mType == PART_DOG || part->mType == PART_SCAREDY_DOG))
  232.         return &sDogAttractsInfo;
  233.  
  234.     return NULL;
  235. }
  236.  
  237. NS_CC_END
Add Comment
Please, Sign In to add comment