Created
May 9, 2012 20:38
-
-
Save quique123/2648618 to your computer and use it in GitHub Desktop.
Chipmunk Hopper v6
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
static cpBool preSolveHG(cpArbiter *arb, cpSpace *space, CPHopper *hopper){/////////////pass it hopper reference | |
if(cpvdot(cpArbiterGetNormal(arb, 0), ccp(0,-1)) < 0){/////////////////////////////ignore bottom collision | |
cpArbiterIgnore(arb); | |
return cpFalse; | |
} | |
////////////////////////////////////////////////////////////////////////If collision from top, set to TRUE | |
hopper.isOnGround = TRUE; | |
return cpTrue; | |
} | |
-(void)updateStateWithDeltaTime:(ccTime)dt andListOfGameObjects:(CCArray*)listOfGameObjects { | |
//Add for debuglabel | |
[self setDebugLabelTextAndPosition]; | |
//Added to clamp for animation | |
CGPoint oldPosition = self.position; | |
//this is for when ole is in the air | |
[super updateStateWithDeltaTime:dt andListOfGameObjects:listOfGameObjects]; | |
//HOPPER IS IN THE AIR, CONTROL HIS JUMP | |
float jumpFactor; | |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { | |
jumpFactor = 300.0; | |
} else { | |
jumpFactor = 150.0; | |
} | |
CGPoint newVel = body->v; | |
if (self.isOnGround == FALSE) {//////////////////////////////////////////////////////////if in air | |
newVel = ccp(jumpFactor*accelerationFraction,body->v.y); | |
} | |
double timeJumping = CACurrentMediaTime()-jumpStartTime; | |
if (jumpStartTime != 0 && timeJumping < 0.25) { | |
newVel.y = jumpFactor*2; | |
} | |
cpBodySetVel(body, newVel); | |
//HOPPER IS ON THE GROUND, CONTROL HIS JUMP (X) | |
if (self.isOnGround == TRUE) {///////////////////////////////////////////////////////////if on ground | |
if (ABS(accelerationFraction) < 0.05) { | |
accelerationFraction = 0; | |
shape->surface_v = ccp(0, 0); | |
} else { | |
float maxSpeed = 200.0f; | |
shape->surface_v = ccp(-maxSpeed*accelerationFraction, 0); | |
cpBodyActivate(body); | |
} | |
} else { | |
shape->surface_v = cpvzero; | |
} | |
//prevent sprite from flying off the sides when in the air | |
float margin = 70; | |
CGSize winSize = [CCDirector sharedDirector].winSize; | |
if (body->p.x < margin) { | |
cpBodySetPos(body, ccp(margin, body->p.y)); | |
} | |
if (body->p.x > winSize.width - margin) { | |
cpBodySetPos(body, ccp(winSize.width - margin, body->p.y)); | |
} | |
// 1. call animations oldPosition | |
if(ABS(accelerationFraction) > 0.05) { | |
double diff = CACurrentMediaTime() - lastFlip; | |
if (diff > 0.1) { | |
lastFlip = CACurrentMediaTime(); | |
if (oldPosition.x > self.position.x) { | |
self.flipX = YES; | |
} else { | |
self.flipX = NO; | |
} | |
} | |
} | |
if (characterState == kStateIdle && accelerationFraction != 0) { | |
[self changeState:kStateWalking]; | |
} | |
if ([self numberOfRunningActions] == 0 && characterState != kStateIdle) { | |
[self changeState:kStateIdle]; | |
} | |
} | |
- (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, NULL, (cpCollisionPreSolveFunc)preSolveHG, NULL, NULL, self);///////////////////////////////////////call pre solve | |
//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; | |
} | |
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { | |
if (jetpack==TRUE) { | |
//run jetpack code | |
NSLog(@"this would be the jetpack true code"); | |
[self hopperJet2]; | |
} else if (self.isOnGround == TRUE) {///////////////////////////////////////////////if no jetpack effect | |
NSLog(@"this would be the jetpack false code, simply jump!"); | |
jumpStartTime = CACurrentMediaTime(); | |
} | |
return TRUE; | |
} |
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
- (void)update:(ccTime)dt { | |
////////////////////////////////////////////////////////Before simulating space, hopper isnt touching ground | |
hopper.isOnGround = FALSE; | |
static double MAX_TIME = 60; | |
double timeSoFar = CACurrentMediaTime() - startTime; | |
double remainingTime = MAX_TIME - timeSoFar; | |
[uiLayer displaySecs:remainingTime]; | |
static double UPDATE_INTERVAL = 1.0f/60.0f; | |
static double MAX_CYCLES_PER_FRAME = 5; | |
static double timeAccumulator = 0; | |
timeAccumulator += dt; | |
if (timeAccumulator > (MAX_CYCLES_PER_FRAME * UPDATE_INTERVAL)) { | |
timeAccumulator = UPDATE_INTERVAL; | |
} | |
while (timeAccumulator >= UPDATE_INTERVAL) { | |
timeAccumulator -= UPDATE_INTERVAL; | |
cpSpaceStep(space, UPDATE_INTERVAL); | |
} | |
//CPViking- calls the update method of gameobject's CPSprite...telling its sprites to update their position... | |
CCArray *listOfGameObjects = [sceneSpriteBatchNode children]; | |
for (GameCharacter *tempChar in listOfGameObjects) { | |
[tempChar updateStateWithDeltaTime:dt | |
andListOfGameObjects:listOfGameObjects]; | |
} | |
//call follow player | |
[self followPlayer:dt]; | |
//check for win/lose | |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { | |
if (remainingTime <= 0) { | |
[[GameManager sharedGameManager] setHasPlayerDied:YES]; | |
[[GameManager sharedGameManager] runSceneWithID:kLevelCompleteScene]; | |
} else if (hopper.position.y > 5600) { | |
[[GameManager sharedGameManager] setHasPlayerDied:NO]; | |
[[GameManager sharedGameManager] runSceneWithID:kLevelCompleteScene]; | |
} | |
} else { | |
if (remainingTime <= 0) { | |
[[GameManager sharedGameManager] setHasPlayerDied:YES]; | |
[[GameManager sharedGameManager] runSceneWithID:kLevelCompleteScene]; | |
} else if (hopper.position.y > 2600) { | |
[[GameManager sharedGameManager] setHasPlayerDied:NO]; | |
[[GameManager sharedGameManager] runSceneWithID:kLevelCompleteScene]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment