Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Feb 13th, 2012  |  syntax: Objective C  |  size: 3.10 KB  |  hits: 76  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #import "ActionLayer.h"
  2. #import "SimpleAudioEngine.h"
  3.  
  4. @implementation ActionLayer
  5.  
  6. + (id)scene {
  7.     CCScene *scene = [CCScene node];    
  8.    
  9.     ActionLayer *layer = [ActionLayer node];
  10.     [scene addChild:layer];    
  11.    
  12.     return scene;
  13. }
  14.  
  15. - (void)setupWorld {    
  16.     b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
  17.     bool doSleep = false;
  18.     _world = new b2World(b2Vec2(0,0), doSleep);
  19. }
  20.  
  21. - (void)setupLevelHelper {
  22.     _lhelper = [[LevelHelperLoader alloc] initWithContentOfFile:@"TestLevel"];
  23.     [_lhelper addObjectsToWorld:_world cocos2dLayer:self];
  24.     [_lhelper createWorldBoundaries:_world];
  25.     [_lhelper createGravity:_world];
  26. }
  27.  
  28. - (void)setupDebugDraw {    
  29.     _debugDraw = new GLESDebugDraw([_lhelper pixelsToMeterRatio] *
  30.                                    [[CCDirector sharedDirector] contentScaleFactor]);
  31.     _world->SetDebugDraw(_debugDraw);
  32.     _debugDraw->SetFlags(b2DebugDraw::e_shapeBit |
  33.                          b2DebugDraw::e_jointBit);
  34. }
  35.  
  36. - (void)setupAudio {
  37.     [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"Raycast.m4a"];
  38.     [[SimpleAudioEngine sharedEngine] preloadEffect:@"ground.wav"];
  39.     [[SimpleAudioEngine sharedEngine] preloadEffect:@"laser.wav"];
  40.     [[SimpleAudioEngine sharedEngine] preloadEffect:@"wing.wav"];
  41.     [[SimpleAudioEngine sharedEngine] preloadEffect:@"whine.wav"];
  42.     [[SimpleAudioEngine sharedEngine] preloadEffect:@"lose.wav"];
  43.     [[SimpleAudioEngine sharedEngine] preloadEffect:@"win.wav"];
  44. }
  45.  
  46. - (id)init {
  47.     if ((self = [super init])) {
  48.         [self setupWorld];
  49.         [self setupLevelHelper];
  50.         [self setupDebugDraw];
  51.         [self setupAudio];
  52.         [self scheduleUpdate];
  53.     }
  54.     return self;
  55. }
  56.  
  57. - (void)updateLevelHelper:(ccTime)dt {
  58.     [_lhelper update:dt];
  59. }
  60.  
  61. - (void)updateBox2D:(ccTime)dt {
  62.     _world->Step(dt, 1, 1);
  63.     _world->ClearForces();
  64. }
  65.  
  66. - (void)updateSprites:(ccTime)dt {
  67.    
  68.     for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext())
  69.     {
  70.         if (b->GetUserData() != NULL)
  71.         {
  72.             CCSprite *myActor = (CCSprite*)b->GetUserData();            
  73.             if(myActor != 0)
  74.             {
  75.                 //THIS IS VERY IMPORTANT - GETTING THE POSITION FROM BOX2D TO COCOS2D
  76.                 myActor.position = [_lhelper metersToPoints:b->GetPosition()];
  77.                 myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());      
  78.             }            
  79.         }  
  80.     }
  81. }
  82.  
  83. - (void)update:(ccTime)dt {
  84.     [self updateLevelHelper:dt];
  85.     [self updateBox2D:dt];
  86.     [self updateSprites:dt];
  87. }
  88.  
  89. -(void) draw {  
  90.    
  91.     glClearColor(98.0/255.0, 183.0/255.0, 214.0/255.0, 255.0/255.0);
  92.     glClear(GL_COLOR_BUFFER_BIT);      
  93.    
  94.     glDisable(GL_TEXTURE_2D);
  95.     glDisableClientState(GL_COLOR_ARRAY);
  96.     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  97.    
  98.     _world->DrawDebugData();
  99.    
  100.     glEnable(GL_TEXTURE_2D);
  101.     glEnableClientState(GL_COLOR_ARRAY);
  102.     glEnableClientState(GL_TEXTURE_COORD_ARRAY);       
  103. }
  104.  
  105. - (void)dealloc {
  106.     [_lhelper release];
  107.     _lhelper = nil;
  108.     delete _world;
  109.     [super dealloc];
  110. }
  111.  
  112. @end