Advertisement
Guest User

GameClass.m

a guest
Dec 12th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "Level01.h"
  2.  
  3. #import "Constants.h"
  4. #import "GameData.h"
  5. #import "GameDataParser.h"
  6. const float32 FIXED_TIMESTEP = 1.0f / 60.0f;
  7. const float32 MINIMUM_TIMESTEP = 1.0f / 600.0f;
  8. const int32 VELOCITY_ITERATIONS = 8;
  9. const int32 POSITION_ITERATIONS = 8;
  10. const int32 MAXIMUM_NUMBER_OF_STEPS = 25;
  11.  
  12. @interface Level01()
  13. -(void) initPhysics;
  14. @end
  15.  
  16. @implementation Level01
  17.  
  18. @synthesize iPad;
  19.  
  20. - (void)onBack: (id) sender
  21. {
  22.     [SceneManager goMainMenu];
  23. }
  24.  
  25. - (void)addBackButton
  26. {
  27.     if (self.iPad)
  28.     {
  29.         CCMenuItemImage *goBack = [CCMenuItemImage itemWithNormalImage:kBackButtonNormalIpad selectedImage:kBackButtonSelectedIpad target:self selector:@selector(onBack:)];
  30.         CCMenu *back = [CCMenu menuWithItems: goBack, nil];
  31.         back.position = ccp(64, 700);
  32.         [self addChild: back z:4];
  33.     }
  34.     else
  35.     {
  36.         CCMenuItemImage *goBack = [CCMenuItemImage itemWithNormalImage:kBackButtonNormalIphone selectedImage:kBackButtonSelectedIphone target:self selector:@selector(onBack:)];
  37.         CCMenu *back = [CCMenu menuWithItems: goBack, nil];
  38.         back.position = ccp(32, 300);
  39.         [self addChild: back z:4];
  40.     }
  41. }
  42.  
  43. - (id)init
  44. {
  45.     if( (self=[super init]))
  46.     {
  47.         //Determine if the device is an ipad
  48.         self.iPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
  49.         //GameData *gameData = [GameDataParser loadData];
  50.        
  51.         self.isTouchEnabled = YES;
  52.         CGSize screenSize = [CCDirector sharedDirector].winSize;
  53.         int largeFont = screenSize.height / kFontScaleLarge;
  54.        
  55.         CCLabelTTF *label = [CCLabelTTF labelWithString:@"JUEGO" fontName:@"Marker Felt" fontSize:largeFont];
  56.         label.position = ccp( screenSize.width/2, screenSize.height/1.05);
  57.         [self addChild:label z:4];
  58.        
  59.         [self addBackButton];
  60.         [self initPhysics];
  61.        
  62.         [self schedule: @selector(tick:) interval:1.0f/60.0f];
  63.        
  64.        
  65.         //create a LevelHelperLoader object that has the data of the specified level
  66.         loader = [[LevelHelperLoader alloc] initWithContentOfFile:@"juego1"];
  67.        
  68.         //create all objects from the level file and adds them to the cocos2d layer (self)
  69.         [loader addObjectsToWorld:world cocos2dLayer:self];
  70.        
  71.         //checks if the level has physics boundaries
  72.         if([loader hasPhysicBoundaries])
  73.         {
  74.             //if it does, it will create the physic boundaries
  75.             [loader createPhysicBoundaries:world];
  76.         }
  77.        
  78.         if(![loader isGravityZero])
  79.         {
  80.             [loader createGravity:world];
  81.         }
  82.        
  83.         [self retrieveRequiredObjects];
  84.         //[self scheduleUpdate];
  85.        
  86.     }
  87.    
  88.     return self;
  89. }
  90.  
  91. -(void) retrieveRequiredObjects
  92. {
  93.     //Retrieve pointers to parallax node and player sprite.
  94.     paralaxNode = [loader parallaxNodeWithUniqueName:@"Parallax_1"];
  95.     NSAssert(paralaxNode!=nil, @"Couldn't find the parallax!");
  96.    
  97.     player = [loader spriteWithUniqueName:@"personaje"];
  98.     NSAssert(player!=nil, @"Couldn't find the player!");
  99.    
  100.     playerBody = [player body];
  101.     NSAssert(playerBody!=nil, @"Error taking the body from the player LHSprite.");
  102.  
  103. }
  104.  
  105.  
  106. - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  107. {
  108.    
  109.     playerVelocity = 0.5f;
  110.     playerShouldFly = true;
  111.    
  112.     //[player startAnimationNamed:@"mouseFly"];
  113. }
  114.  
  115.  
  116. - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  117. {
  118.    
  119. }
  120.  
  121. -(void) cancelPlayerFly
  122. {
  123.     playerShouldFly = false;
  124.     playerWasFlying = true;
  125.     playerVelocity = 0.0f;
  126. }
  127.  
  128. - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  129. {
  130.     [self cancelPlayerFly];
  131. }
  132. - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  133. {
  134.     [self cancelPlayerFly];
  135. }
  136.  
  137.  
  138. -(void)step:(ccTime)dt {
  139.     float32 frameTime = dt;
  140.     int stepsPerformed = 0;
  141.     while ( (frameTime > 0.0) && (stepsPerformed < MAXIMUM_NUMBER_OF_STEPS) ){
  142.         float32 deltaTime = std::min( frameTime, FIXED_TIMESTEP );
  143.         frameTime -= deltaTime;
  144.         if (frameTime < MINIMUM_TIMESTEP) {
  145.             deltaTime += frameTime;
  146.             frameTime = 0.0f;
  147.         }
  148.         world->Step(deltaTime,VELOCITY_ITERATIONS,POSITION_ITERATIONS);
  149.         stepsPerformed++;
  150.         [self afterStep]; // process collisions and result from callbacks called by the step
  151.     }
  152.     world->ClearForces ();
  153. }
  154. -(void)afterStep {
  155.     // process collisions and result from callbacks called by the step
  156. }
  157. -(void) tick: (ccTime) dt
  158. {
  159.     [self step:dt];
  160.    
  161.     if(playerShouldFly)
  162.     {
  163.         playerBody->ApplyLinearImpulse(b2Vec2(0, playerVelocity), playerBody->GetWorldCenter());
  164.        
  165.         playerVelocity += 0.01f;
  166.        
  167.         if(playerVelocity > 1.5f)
  168.             playerVelocity = 1.5f;
  169.     }
  170. }
  171.  
  172.  
  173. -(void) dealloc
  174. {
  175.     delete world;
  176.     world = NULL;
  177.    
  178.     delete m_debugDraw;
  179.     m_debugDraw = NULL;
  180.    
  181.     [loader release];
  182.     loader = nil;
  183.    
  184.    
  185.     [super dealloc];
  186. }
  187.  
  188. -(void) initPhysics
  189. {
  190.    
  191.     CGSize s = [[CCDirector sharedDirector] winSize];
  192.    
  193.     b2Vec2 gravity;
  194.     gravity.Set(0.0f, -10.0f);
  195.     world = new b2World(gravity);
  196.    
  197.    
  198.    
  199.     // Do we want to let bodies sleep?
  200.     world->SetAllowSleeping(true);
  201.    
  202.     world->SetContinuousPhysics(true);
  203.    
  204.     m_debugDraw = new GLESDebugDraw( PTM_RATIO );
  205.     world->SetDebugDraw(m_debugDraw);
  206.    
  207.     uint32 flags = 0;
  208.     flags += b2Draw::e_shapeBit;
  209.     //      flags += b2Draw::e_jointBit;
  210.     //      flags += b2Draw::e_aabbBit;
  211.     //      flags += b2Draw::e_pairBit;
  212.     //      flags += b2Draw::e_centerOfMassBit;
  213.     m_debugDraw->SetFlags(flags);
  214.    
  215.    
  216.     // Define the ground body.
  217.     b2BodyDef groundBodyDef;
  218.     groundBodyDef.position.Set(0, 0); // bottom-left corner
  219.    
  220.     // Call the body factory which allocates memory for the ground body
  221.     // from a pool and creates the ground box shape (also from a pool).
  222.     // The body is also added to the world.
  223.     b2Body* groundBody = world->CreateBody(&groundBodyDef);
  224.    
  225.     // Define the ground box shape.
  226.     b2EdgeShape groundBox;
  227.    
  228.     // bottom
  229.    
  230.     groundBox.Set(b2Vec2(0,0), b2Vec2(s.width/PTM_RATIO,0));
  231.     groundBody->CreateFixture(&groundBox,0);
  232.    
  233.     // top
  234.     groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO));
  235.     groundBody->CreateFixture(&groundBox,0);
  236.    
  237.     // left
  238.     groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(0,0));
  239.     groundBody->CreateFixture(&groundBox,0);
  240.    
  241.     // right
  242.     groundBox.Set(b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,0));
  243.     groundBody->CreateFixture(&groundBox,0);
  244. }
  245.  
  246.  
  247.  
  248. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement