Skip to content

Instantly share code, notes, and snippets.

@nathanclark
Created January 5, 2012 16:16
Show Gist options
  • Select an option

  • Save nathanclark/1565952 to your computer and use it in GitHub Desktop.

Select an option

Save nathanclark/1565952 to your computer and use it in GitHub Desktop.
#import "CCPageBox2dBase.h"
@implementation CCPageBox2dBase
@synthesize levelHelperName = _levelHelperName;
- (void)setupWorld {
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
bool doSleep = false;
_world = new b2World(b2Vec2(0,0), doSleep);
_world->SetContinuousPhysics(true);
}
- (void)setupLevelHelper {
_lhelper = [[LevelHelperLoader alloc] initWithContentOfFile:_levelHelperName];
[_lhelper addObjectsToWorld:_world cocos2dLayer:self];
[_lhelper createWorldBoundaries:_world];
[_lhelper createGravity:_world];
}
- (void)setupDebugDraw {
m_debugDraw = new GLESDebugDraw([_lhelper pixelsToMeterRatio] *
[[CCDirector sharedDirector] contentScaleFactor]);
_world->SetDebugDraw(m_debugDraw);
m_debugDraw->SetFlags(b2DebugDraw::e_shapeBit |
b2DebugDraw::e_jointBit);
}
- (void)setupAudio {
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
b2Vec2 gravity(-acceleration.y * 15, acceleration.x *15);
_world->SetGravity(gravity);
}
- (void)updateLevelHelper:(ccTime)dt {
[_lhelper update:dt];
}
- (void)updateBox2D:(ccTime)dt {
_world->Step(dt, 1, 1);
_world->ClearForces();
}
- (void)updateSprites:(ccTime)dt {
for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
CCSprite *myActor = (CCSprite*)b->GetUserData();
myActor.position = CGPointMake(b->GetPosition().x * [_lhelper pixelsToMeterRatio],
b->GetPosition().y * [_lhelper pixelsToMeterRatio]);
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
- (void)update:(ccTime)dt {
[self updateLevelHelper:dt];
[self updateBox2D:dt];
[self updateSprites:dt];
}
- (id)initWithLevelHelper:(NSString *)lhName
{
if ((self = [super init]) ) {
self.isTouchEnabled = YES;
self.isAccelerometerEnabled = YES;
[self setupWorld];
[self setupAudio];
[self scheduleUpdate];
_levelHelperName = lhName;
[self setupLevelHelper];
}
return self;
}
- (id)init {
if ((self = [super init])) {
}
return self;
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
CCLOG(@"dealloc: %@", self);
_levelHelperName = nil;
[_lhelper release];
_lhelper = nil;
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment