Created
May 8, 2012 18:05
-
-
Save quique123/2638070 to your computer and use it in GitHub Desktop.
Chipmunk Hopper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation CPHopper | |
@synthesize groundShapes, jetpack, hasShield; | |
@synthesize myDebugLabel; | |
//methods for collision detec chipmunk | |
static cpBool beginHG(cpArbiter *arb, cpSpace *space, void *ignore) { | |
CP_ARBITER_GET_SHAPES(arb, hopperShape, groundShape); | |
CPHopper *hopper = (CPHopper *)hopperShape->data; | |
cpVect n = cpArbiterGetNormal(arb, 0); | |
if (n.y < 0.0f) { | |
cpArray *groundShapes = hopper.groundShapes; | |
cpArrayPush(groundShapes, groundShape); | |
} | |
return cpTrue; | |
} | |
static cpBool preSolveHG(cpArbiter *arb, cpSpace *space, void *ignore) { | |
if(cpvdot(cpArbiterGetNormal(arb, 0), ccp(0,-1)) < 0){ // 6 | |
cpArbiterIgnore(arb); | |
return cpFalse; | |
} | |
return cpTrue; | |
} | |
static void separateHG(cpArbiter *arb, cpSpace *space, void *ignore) { | |
CP_ARBITER_GET_SHAPES(arb, hopperShape, groundShape); // 7 | |
CPHopper *hopper = (CPHopper *)hopperShape->data; | |
cpArrayDeleteObj(hopper.groundShapes, groundShape); | |
} | |
- (id)initWithLocation:(CGPoint)location space:(cpSpace *)theSpace groundBody:(cpBody *)groundBody { | |
if ((self = [super initWithSpriteFrameName:@"70x70mega1.png"])) { | |
CGSize size; | |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { | |
size = CGSizeMake(60, 60); | |
self.anchorPoint = ccp(0.5, 30/self.contentSize.height); | |
} else { | |
size = CGSizeMake(30, 30); | |
self.anchorPoint = ccp(0.5, 15/self.contentSize.height); | |
} | |
[self addBoxBodyAndShapeWithLocation:location size:size space:theSpace mass:1.0 e:0.0 u:0.5 collisionType:kCollisionTypeViking canRotate:TRUE]; | |
} | |
//initialize groundarray | |
groundShapes = cpArrayNew(0); | |
cpSpaceAddCollisionHandler(space, kCollisionTypeViking,kCollisionTypeGround, beginHG, preSolveHG, NULL, separateHG, NULL); | |
//add constraint | |
cpConstraint *constraint = cpRotaryLimitJointNew(groundBody, body,CC_DEGREES_TO_RADIANS(-30), CC_DEGREES_TO_RADIANS(30)); | |
cpSpaceAddConstraint(space, constraint); | |
//call animations | |
[self initAnimations]; | |
//set jetpack to false | |
self.jetpack = FALSE; | |
self.hasShield = FALSE; | |
return self; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//methods for collision detec chipmunk | |
static cpBool beginJPG(cpArbiter *arb, cpSpace *space, void *ignore) { | |
CP_ARBITER_GET_SHAPES(arb, jetShape, groundShape); | |
JetPack *genBod = (JetPack *)jetShape->data; | |
cpVect n = cpArbiterGetNormal(arb, 0); | |
if (n.y < 0.0f) { | |
cpArray *groundShapes = genBod.groundShapes; | |
cpArrayPush(groundShapes, groundShape); | |
} | |
NSLog(@"shield picked up"); | |
return cpTrue; | |
} | |
static cpBool preSolveJPG(cpArbiter *arb, cpSpace *space, void *ignore) { | |
if(cpvdot(cpArbiterGetNormal(arb, 0), ccp(0,-1)) < 0){ // 6 | |
return cpFalse; | |
} | |
return cpTrue; | |
} | |
static void separateJPG(cpArbiter *arb, cpSpace *space, void *ignore) { | |
CP_ARBITER_GET_SHAPES(arb, hopperShape, groundShape); // 7 | |
JetPack *genBod = (JetPack *)hopperShape->data; | |
cpArrayDeleteObj(genBod.groundShapes, groundShape); | |
} | |
- (id)initWithLocation:(CGPoint)location space:(cpSpace *)theSpace | |
groundBody:(cpBody *)groundBody { | |
if ((self = [super initWithSpriteFrameName:@"jetpack.png"])) { | |
CGSize size; | |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { | |
size = CGSizeMake(60, 60); | |
self.anchorPoint = ccp(0.5, 30/self.contentSize.height); | |
} else { | |
size = CGSizeMake(30, 30); | |
self.anchorPoint = ccp(0.5, 15/self.contentSize.height); | |
} | |
screenSize = [CCDirector sharedDirector].winSize; | |
gameObjectType = kPowerUpTypeJetpack; | |
//[self initAnimations]; | |
[self addBoxBodyAndShapeWithLocation:location size:size space:theSpace mass:1.0 e:0.0 u:0.5 collisionType:kCollisionTypeJetpack canRotate:TRUE]; | |
[self changeState:kStateSpawning]; | |
} | |
//initialize groundarray | |
groundShapes = cpArrayNew(0); | |
cpSpaceAddCollisionHandler(space, kCollisionTypeJetpack,kCollisionTypeGround, beginJPG, preSolveJPG, NULL, separateJPG, NULL); | |
//add constraint | |
cpConstraint *constraint = cpRotaryLimitJointNew(groundBody, body,CC_DEGREES_TO_RADIANS(-30), CC_DEGREES_TO_RADIANS(30)); | |
cpSpaceAddConstraint(space, constraint); | |
//set jetpack to false | |
self.jetpack = TRUE; | |
return self; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Remove cpbody postStep-wise--hopper-shield collision | |
/** | |
static void postStepRemove(cpSpace *space, cpShape *shape, void *unused) | |
{ | |
NSLog(@"postStep remove running..."); | |
cpSpaceRemoveBody(space, shape->body); | |
cpBodyFree(shape->body); | |
cpSpaceRemoveShape(space, shape); | |
cpShapeFree(shape); | |
} | |
//Hopper-shield collision | |
static cpBool beginHS(cpArbiter *arb, cpSpace *space, Scene1ActionLayer *layer){//void *ignore) { | |
CP_ARBITER_GET_SHAPES(arb, hopperShape, shieldShape); | |
NSLog(@"player touched shield...calling postback"); | |
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemove, shieldShape, NULL); | |
[layer removeShield]; | |
return cpTrue; | |
} | |
**/ | |
//Meteor-ground collisions | |
static cpBool beginMG(cpArbiter *arb, cpSpace *space, void *ignore) { | |
CP_ARBITER_GET_SHAPES(arb, hopperShape, shieldShape); | |
NSLog(@"meteor-ground...calling postback"); | |
//cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemove, shieldShape, NULL); | |
//[layer removeShield]; | |
cpArbiterIgnore(arb); | |
return cpFalse; | |
} | |
//Meteor-hopper collisions | |
static cpBool beginMH(cpArbiter *arb, cpSpace *space, void *ignore) { | |
CP_ARBITER_GET_SHAPES(arb, hopperShape, meteorShape); | |
NSLog(@"meteor-hopper...push hopper back"); | |
//cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemove, shieldShape, NULL); | |
//[layer removeShield]; | |
//cpArbiterIgnore(arb); | |
return cpTrue; | |
} | |
- (void)createSpace { | |
space = cpSpaceNew(); | |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { | |
space->gravity = ccp(0, -1500); | |
} else { | |
space->gravity = ccp(0, -750); | |
} | |
cpSpaceResizeStaticHash(space, 400, 200); | |
cpSpaceResizeActiveHash(space, 200, 200); | |
//Register Collision Handlers | |
//2/4. BROUGHT IN FROM CPHOPPER | |
//hopper-shield | |
//cpSpaceAddCollisionHandler(space, kCollisionTypeViking, kCollisionTypeShield, (cpCollisionBeginFunc)beginHS, NULL, NULL, NULL, self); | |
//meteor-platform | |
cpSpaceAddCollisionHandler(space, kCollisionTypeMeteor, kCollisionTypeGround, (cpCollisionBeginFunc)beginMG, NULL, NULL, NULL, NULL); | |
//meteor-hopper | |
cpSpaceAddCollisionHandler(space, kCollisionTypeViking, kCollisionTypeMeteor, (cpCollisionBeginFunc)beginMH, NULL, NULL, NULL, NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment