Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "CMPartEgg.h"
- #include "contraptions/managers/CMLevelManager.h"
- #include "cmcocos/util/CCOldTypes.h"
- NS_CC_BEGIN
- static CMPartAttractsInfo sDogAttractsInfo(
- FLOAT_TO_INT32(0),FLOAT_TO_INT32(230), 0,0,
- 0,0, 0,0,
- ATTRACTS_PRIORITY_ONE,ATTRACTS_PRIORITY_ONE,
- ATTRACTS_CANT_LOOK_BEHIND,ATTRACTS_SHOULDNT_RUN,ATTRACTS_EDIBLE);
- CMPartEgg::CMPartEgg(int id, CMLevel *level, int type, TmPointInt32 pos, int partProcessFlags, int partActionFlags, int state, int rot)
- : CMPart(id,level,type,pos,partProcessFlags,partActionFlags,state,rot),
- mCpEggBody(NULL),
- mShouldCrack(false),
- mCracked(false)
- {
- for (int i = 0; i < MAX_EGG_SPRITES;i++)
- mEggSprites[i] = NULL;
- }
- void CMPartEgg::onCreation(bool fromLoad)
- {
- // Hide broken egg art
- static_cast<CMPartArtSpriteRect*>(getArtById(11))->setVisible(false);
- mCpEggBody = getBodyById(0)->mCpBody;
- CMPartArtSpriteRect *partArt;
- for (int i=0;i<11;i++)
- {
- partArt = static_cast<CMPartArtSpriteRect*>(getArtById(i));
- mEggSprites[i] = partArt->mSprite;
- mEggSprites[i]->setVisible(false);
- }
- mEggSprites[mState]->setVisible(true);
- mLevel->addArtToList(getArtById(mState));
- CMPart::onCreation(fromLoad);
- mSoundResource = getSoundResource("EggBreak");
- }
- void CMPartEgg::setVisible(bool visible)
- {
- if (mCracked)
- return;
- for (int i=0;i<MAX_EGG_SPRITES;i++)
- {
- if (mEggSprites[i])
- mEggSprites[i]->setVisible(false);
- }
- if (mState >= 0 && mState < MAX_EGG_SPRITES && mEggSprites[mState])
- mEggSprites[mState]->setVisible(visible);
- }
- void CMPartEgg::setInitialState(int state, bool alwaysUpdate)
- {
- if (state != mState)
- {
- mEggSprites[mState]->setVisible(false);
- mLevel->removeArtFromList(getArtById(mState));
- mState = state;
- mEggSprites[mState]->setVisible(true);
- mLevel->addArtToList(getArtById(mState));
- CMPart::setInitialState(state,true);
- }
- }
- void CMPartEgg::postPhysicsUpdate(int time, int levelFrame)
- {
- // Do nothing if egg is cracked or destroyed
- if (mCracked || mInactivated || mMakeInactive)
- return;
- if (mShouldCrack)
- crackEgg();
- CMPart::postPhysicsUpdate(time,levelFrame);
- }
- void CMPartEgg::crackEgg()
- {
- mGoalState = 1;
- mCracked = true;
- // Hide intact egg and show broken egg
- mEggSprites[mState]->setVisible(false);
- mLevel->removeArtFromList(getArtById(mState));
- static_cast<CMPartArtSpriteRect*>(getArtById(11))->setVisible(true);
- mLevel->addArtToList(getArtById(11));
- // Delete the intact egg shapes
- CMPartShape *handleShape = getShapeById(0);
- deleteShape(handleShape);
- handleShape = getShapeById(1);
- deleteShape(handleShape);
- // Infinity moment of interia so Egg can't rotate
- mCpEggBody->a = 0;
- mCpEggBody->w = 0;
- cpBodySetMoment(mCpEggBody, INT32_INFINITY);
- // Shape for egg to be detected to be eaten
- 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);
- playSound(INT32_TO_FLOAT(mPos.x), INT32_TO_FLOAT(mPos.y), mSoundResource, 0, 0);
- }
- void CMPartEgg::message(int messageType, CMPart *otherPart)
- {
- // Energy bursts will crack the egg
- if (messageType == PART_MSG_HIT_ENERGY_BURST)
- mShouldCrack = true;
- else
- CMPart::message(messageType,otherPart);
- }
- void CMPartEgg::postHandleCollision(CMPart *otherPart, cpArbiter *arb, bool isB)
- {
- // Only process on first contact
- if (mCracked || mShouldCrack || otherPart->mNitroSafe || !cpArbiterIsFirstContact(arb))
- return;
- // Get a couple of collision shapes that we will be using
- cpShape *otherShape;
- cpShape *myShape;
- if (isB)
- cpArbiterGetShapes(arb, &otherShape, &myShape);
- else
- cpArbiterGetShapes(arb, &myShape, &otherShape);
- // Egg won't break if bouncing off trampoline bouncy material
- if (otherShape->shapeProperty == SP_NORMAL_TRAMPOLINE || otherShape->shapeProperty == SP_MECHANICAL_TRAMPOLINE)
- return;
- // Also want to get egg body and other part's body
- cpBody *otherCpBody;
- cpBody *myCpBody;
- if (isB)
- cpArbiterGetBodies(arb,&otherCpBody,&myCpBody);
- else
- cpArbiterGetBodies(arb,&myCpBody,&otherCpBody);
- // Mass of the other body
- int oMass = otherCpBody->m;
- // Protecting from overflow by shifting down by 4
- // -- to keep everything running the same, adjusting the checks below to ((val * val) / 256 ) * 65536 which is (val * val) << 8
- cpVect64 tiVect = cpArbiterTotalImpulse(arb);
- tiVect.x = INT64_SHIFT_DOWN_BY(tiVect.x,4);
- tiVect.y = INT64_SHIFT_DOWN_BY(tiVect.y,4);
- tmInt64 totImp = INT64_SHIFT_DOWN(tiVect.x * (tmInt64)tiVect.x + tiVect.y * (tmInt64)tiVect.y);
- // Mass inverse value of zero mean infinite mass
- if (otherCpBody->m_inv == 0)
- {
- // Hit a static object so only Egg velocity/impulse into shape matters
- if (totImp > ((500LL*500LL)<<8))
- mShouldCrack = true;
- }
- else if (oMass > FLOAT_TO_INT32(1.5f))
- {
- // Different masses require different impulses to crack egg
- if (oMass > FLOAT_TO_INT32(80.0f))
- mShouldCrack = (totImp > ((1400LL*1400LL)<<8));
- else if (oMass > FLOAT_TO_INT32(40.0f))
- mShouldCrack = (totImp > ((2800LL*2800LL)<<8));
- else if (oMass > FLOAT_TO_INT32(20.0f))
- mShouldCrack = (totImp > ((2200LL*2200LL)<<8));
- else if (oMass > FLOAT_TO_INT32(6.0f))
- mShouldCrack = (totImp > ((1800LL*1800LL)<<8));
- else if (oMass > FLOAT_TO_INT32(4.0f))
- mShouldCrack = (totImp > ((1400LL*1400LL)<<8));
- else
- mShouldCrack = (totImp > ((740LL*740LL)<<8));
- }
- }
- int CMPartEgg::sensorBeginHandleCollision(CMPart *otherPart, cpArbiter *arb)
- {
- // Dog wants to eat egg
- if (otherPart->mType == PART_DOG)
- otherPart->tryToEat(this);
- return 0;
- }
- void CMPartEgg::processExplosion(TmPointInt32 explosionSource, int maxDistSquared, int impulseAtSource, int impulseAtEdge)
- {
- if (!mShouldCrack)
- {
- // See if explosion is close enough to crack egg
- int deltaX = mBodies->mCpBody->p.x - explosionSource.x;
- int deltaY = mBodies->mCpBody->p.y - explosionSource.y;
- tmInt64 distSquared = INT64_SHIFT_DOWN(deltaX * (tmInt64)deltaX + deltaY * (tmInt64)deltaY);
- mShouldCrack = (distSquared < maxDistSquared/2);
- }
- }
- void CMPartEgg::flipGroup(int flags, TmPointInt32 centerPos)
- {
- mRot = 0 - mRot;
- CMPart::flipGroup(flags,centerPos);
- }
- int CMPartEgg::eaten()
- {
- if (mCracked && !mInactivated && !mMakeInactive)
- {
- mMakeInactive = true;
- // Return how long it takes to eat
- return FLOAT_TO_INT32(1.4f);
- }
- return 0;
- }
- CMPartAttractsInfo *CMPartEgg::getAttractsInfo(CMPart *part)
- {
- // Only dogs are attracted to broken eggs right now
- if (mCracked && (part->mType == PART_DOG || part->mType == PART_SCAREDY_DOG))
- return &sDogAttractsInfo;
- return NULL;
- }
- NS_CC_END
Add Comment
Please, Sign In to add comment