this is part of my code
my heroy (pickachu) digging ground.
and eating red mushrooms
I use colision to check which tile of gound need to be delete
Code: Select all
int groundCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
//speed /2
HelloWorld* helscene=(HelloWorld*)data;
CGPoint checkedTiled=[helscene coordinatesAtPosition:b->body->p];
TMXLayer *orthoLayer=[helscene->ortho layerNamed:@"Layer 0"];
[orthoLayer removeTileAt:ccp(checkedTiled.x,checkedTiled.y)]
cpSpaceRemoveBody(helscene->space,b->body);
if (b->body->m == INFINITY)
cpSpaceRemoveStaticShape(helscene->space,b);
else
cpSpaceRemoveShape(helscene->space ,b);
cpBodyFree(b->body);
cpShapeFree(b);
return 1;
}
-(void)setupChipmunk{
cpInitChipmunk();
space =cpSpaceNew();
//space->gravity = cpv(0,-200);
space->elasticIterations = 1;
[self schedule: @selector(tick:) interval: 1.0f/60.0f];
pickachuBody=cpBodyNew(100.0, INFINITY);
pickachuBody->p= cpv(112,144);
cpSpaceAddBody(space, pickachuBody);
cpVect squared[] = { cpv(-16, -16), cpv(-16, 16), cpv(16, 16), cpv(16,-16) };
cpShape* pickachuShape=cpPolyShapeNew(pickachuBody, 4, squared, cpvzero);
pickachuShape->e=0.8;
pickachuShape->u =0.8;
pickachuShape->data=sprite_h;
pickachuShape->collision_type =7;
cpSpaceAddShape(space, pickachuShape);
//*-*-*-*-*-*-*-*-*
TMXLayer *orthoLayer=[ortho layerNamed:@"Layer 0"];
CGSize s=[orthoLayer layerSize];
cpBody *staticBody;
staticBody = cpBodyNew(INFINITY, INFINITY);
for (int y=0; y<s.height; y++) {
for (int x=0; x<s.width; x++) {
unsigned int tilegid=[orthoLayer tileGIDAt:ccp(x,y)];
//NSLog(@"(%d,%d)= %d",x,y,tilegid);
if(tilegid>0)
{
/* if(tilegid<=8) {
int reversDighit[10]={9,8,7,6,5,4,3,2,1,0};
cpFloat xpos=x*32;
cpFloat ypos=reversDighit[y]*32;
cpShape* floorShape = cpSegmentShapeNew(staticBody, cpv(xpos,ypos+32), cpv(xpos+32,ypos+32), 0);
floorShape->e = 0.5;
floorShape->u = 0.1;
floorShape->collision_type = 0;
cpSpaceAddShape(space, floorShape);
}*/
//50 гриб можно сьесть
if(tilegid==50) {
int reversDighit[10]={9,8,7,6,5,4,3,2,1,0};
cpFloat xpos=x*32;
cpFloat ypos=reversDighit[y]*32;
cpBody* mushroom2Body = cpBodyNew(INFINITY, INFINITY);
mushroom2Body->p = cpv(xpos+16, ypos+16);
cpVect squared[] = { cpv(-16, -16), cpv(-16, 16), cpv(16, 16), cpv(16,-16) };
//cpSpaceAddBody(space, mushroomBody);
cpShape* mushroom2Shape = cpPolyShapeNew(mushroom2Body, 4, squared, cpvzero);
mushroom2Shape->e = 0.5;
mushroom2Shape->u = 0.1;
//mushroomShape->data=[orthoLayer tileAt:ccp(x,y)];
mushroom2Shape->collision_type = 5;
cpSpaceAddStaticShape(space, mushroom2Shape);
}
if(tilegid==40) {
int reversDighit[10]={9,8,7,6,5,4,3,2,1,0};
cpFloat xpos=x*32;
cpFloat ypos=reversDighit[y]*32;
cpBody* groundBody = cpBodyNew(INFINITY, INFINITY);
groundBody->p = cpv(xpos+16, ypos+16);
cpVect squared[] = { cpv(-16, -16), cpv(-16, 16), cpv(16, 16), cpv(16,-16) };
//cpSpaceAddBody(space, mushroomBody);
cpShape* groundShape = cpPolyShapeNew(groundBody, 4, squared, cpvzero);
groundShape->e = 0;
groundShape->u = 1;
//mushroomShape->data=[orthoLayer tileAt:ccp(x,y)];
groundShape->collision_type = 3;
cpSpaceAddStaticShape(space, groundShape);
}
//51 гриб можно двигать
/*if(tilegid==51) {
int reversDighit[10]={9,8,7,6,5,4,3,2,1,0};
cpFloat xpos=x*32;
cpFloat ypos=reversDighit[y]*32;
//AtlasSprite* tmpSprite=[orthoLayer tileAt:ccp(x,y)];
//-Texture2D* textureForMushroom=[orthoLayer spriteManagerWithTexture:<#(Texture2D *)tex#>][tmpSprite.textureRect]
//Sprite* tmpSp=[orthoLayer createSpriteWithRect:tmpSprite.textureRect];
//--Sprite* tmpSp=[Sprite spriteWithTexture:[tmpSprite->textureAtlas_]];
Sprite* moosh=[Sprite spriteWithFile:@"pickachu.png"];
[moosh setPosition:CGPointMake(xpos+16, ypos+16)];
tagindex++;
[self addChild:moosh z:1 tag:tagindex];
cpBody* mushroomBody = cpBodyNew(100, INFINITY);
mushroomBody->p = cpv(xpos+16, ypos+16);
cpSpaceAddBody(space, mushroomBody);
cpVect squared[] = { cpv(-16, -16), cpv(-16, 16), cpv(16, 16), cpv(16,-16) };
cpShape* mushroomShape = cpPolyShapeNew(mushroomBody, 4, squared, cpvzero);
mushroomShape->e = 0.5;
mushroomShape->u = 0.1;
mushroomShape->data=moosh;
mushroomShape->collision_type = 6;
cpSpaceAddShape(space, mushroomShape);
[orthoLayer removeTileAt:ccp(x,y)];
}*/
}
}
}
cpSpaceAddCollisionPairFunc(space, 7, 5, &mushroomCollision, self);
cpSpaceAddCollisionPairFunc(space, 7, 3, &groundCollision, self);
//cpSpaceAddCollisionPairFunc(space, 7, 6, &mushroomCollisionP, self);
}