Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "Level01.h"
- #import "Constants.h"
- #import "GameData.h"
- #import "GameDataParser.h"
- const float32 FIXED_TIMESTEP = 1.0f / 60.0f;
- const float32 MINIMUM_TIMESTEP = 1.0f / 600.0f;
- const int32 VELOCITY_ITERATIONS = 8;
- const int32 POSITION_ITERATIONS = 8;
- const int32 MAXIMUM_NUMBER_OF_STEPS = 25;
- @interface Level01()
- -(void) initPhysics;
- @end
- @implementation Level01
- @synthesize iPad;
- - (void)onBack: (id) sender
- {
- [SceneManager goMainMenu];
- }
- - (void)addBackButton
- {
- if (self.iPad)
- {
- CCMenuItemImage *goBack = [CCMenuItemImage itemWithNormalImage:kBackButtonNormalIpad selectedImage:kBackButtonSelectedIpad target:self selector:@selector(onBack:)];
- CCMenu *back = [CCMenu menuWithItems: goBack, nil];
- back.position = ccp(64, 700);
- [self addChild: back z:4];
- }
- else
- {
- CCMenuItemImage *goBack = [CCMenuItemImage itemWithNormalImage:kBackButtonNormalIphone selectedImage:kBackButtonSelectedIphone target:self selector:@selector(onBack:)];
- CCMenu *back = [CCMenu menuWithItems: goBack, nil];
- back.position = ccp(32, 300);
- [self addChild: back z:4];
- }
- }
- - (id)init
- {
- if( (self=[super init]))
- {
- //Determine if the device is an ipad
- self.iPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
- //GameData *gameData = [GameDataParser loadData];
- self.isTouchEnabled = YES;
- CGSize screenSize = [CCDirector sharedDirector].winSize;
- int largeFont = screenSize.height / kFontScaleLarge;
- CCLabelTTF *label = [CCLabelTTF labelWithString:@"JUEGO" fontName:@"Marker Felt" fontSize:largeFont];
- label.position = ccp( screenSize.width/2, screenSize.height/1.05);
- [self addChild:label z:4];
- [self addBackButton];
- [self initPhysics];
- [self schedule: @selector(tick:) interval:1.0f/60.0f];
- //create a LevelHelperLoader object that has the data of the specified level
- loader = [[LevelHelperLoader alloc] initWithContentOfFile:@"juego1"];
- //create all objects from the level file and adds them to the cocos2d layer (self)
- [loader addObjectsToWorld:world cocos2dLayer:self];
- //checks if the level has physics boundaries
- if([loader hasPhysicBoundaries])
- {
- //if it does, it will create the physic boundaries
- [loader createPhysicBoundaries:world];
- }
- if(![loader isGravityZero])
- {
- [loader createGravity:world];
- }
- [self retrieveRequiredObjects];
- //[self scheduleUpdate];
- }
- return self;
- }
- -(void) retrieveRequiredObjects
- {
- //Retrieve pointers to parallax node and player sprite.
- paralaxNode = [loader parallaxNodeWithUniqueName:@"Parallax_1"];
- NSAssert(paralaxNode!=nil, @"Couldn't find the parallax!");
- player = [loader spriteWithUniqueName:@"personaje"];
- NSAssert(player!=nil, @"Couldn't find the player!");
- playerBody = [player body];
- NSAssert(playerBody!=nil, @"Error taking the body from the player LHSprite.");
- }
- - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- playerVelocity = 0.5f;
- playerShouldFly = true;
- //[player startAnimationNamed:@"mouseFly"];
- }
- - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- {
- }
- -(void) cancelPlayerFly
- {
- playerShouldFly = false;
- playerWasFlying = true;
- playerVelocity = 0.0f;
- }
- - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- {
- [self cancelPlayerFly];
- }
- - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- {
- [self cancelPlayerFly];
- }
- -(void)step:(ccTime)dt {
- float32 frameTime = dt;
- int stepsPerformed = 0;
- while ( (frameTime > 0.0) && (stepsPerformed < MAXIMUM_NUMBER_OF_STEPS) ){
- float32 deltaTime = std::min( frameTime, FIXED_TIMESTEP );
- frameTime -= deltaTime;
- if (frameTime < MINIMUM_TIMESTEP) {
- deltaTime += frameTime;
- frameTime = 0.0f;
- }
- world->Step(deltaTime,VELOCITY_ITERATIONS,POSITION_ITERATIONS);
- stepsPerformed++;
- [self afterStep]; // process collisions and result from callbacks called by the step
- }
- world->ClearForces ();
- }
- -(void)afterStep {
- // process collisions and result from callbacks called by the step
- }
- -(void) tick: (ccTime) dt
- {
- [self step:dt];
- if(playerShouldFly)
- {
- playerBody->ApplyLinearImpulse(b2Vec2(0, playerVelocity), playerBody->GetWorldCenter());
- playerVelocity += 0.01f;
- if(playerVelocity > 1.5f)
- playerVelocity = 1.5f;
- }
- }
- -(void) dealloc
- {
- delete world;
- world = NULL;
- delete m_debugDraw;
- m_debugDraw = NULL;
- [loader release];
- loader = nil;
- [super dealloc];
- }
- -(void) initPhysics
- {
- CGSize s = [[CCDirector sharedDirector] winSize];
- b2Vec2 gravity;
- gravity.Set(0.0f, -10.0f);
- world = new b2World(gravity);
- // Do we want to let bodies sleep?
- world->SetAllowSleeping(true);
- world->SetContinuousPhysics(true);
- m_debugDraw = new GLESDebugDraw( PTM_RATIO );
- world->SetDebugDraw(m_debugDraw);
- uint32 flags = 0;
- flags += b2Draw::e_shapeBit;
- // flags += b2Draw::e_jointBit;
- // flags += b2Draw::e_aabbBit;
- // flags += b2Draw::e_pairBit;
- // flags += b2Draw::e_centerOfMassBit;
- m_debugDraw->SetFlags(flags);
- // Define the ground body.
- b2BodyDef groundBodyDef;
- groundBodyDef.position.Set(0, 0); // bottom-left corner
- // Call the body factory which allocates memory for the ground body
- // from a pool and creates the ground box shape (also from a pool).
- // The body is also added to the world.
- b2Body* groundBody = world->CreateBody(&groundBodyDef);
- // Define the ground box shape.
- b2EdgeShape groundBox;
- // bottom
- groundBox.Set(b2Vec2(0,0), b2Vec2(s.width/PTM_RATIO,0));
- groundBody->CreateFixture(&groundBox,0);
- // top
- groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO));
- groundBody->CreateFixture(&groundBox,0);
- // left
- groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(0,0));
- groundBody->CreateFixture(&groundBox,0);
- // right
- groundBox.Set(b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,0));
- groundBody->CreateFixture(&groundBox,0);
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement