Advertisement
Guest User

Even better

a guest
May 25th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void)placeBomb
  2. {
  3.     NSLog(@"Bomb placed");
  4.     _circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
  5.     CGPoint circle0position = ccp(_cat.position.x , _cat.position.y);
  6.     CGPoint c0TileCoordt = [self tileCoordForPosition:circle0position];
  7.     CGPoint c0TileCoord = [self positionForTileCoord:c0TileCoordt];
  8.     _circle.position = c0TileCoord;
  9.     [self addChild:_circle];
  10.     id fade = [CCScaleTo actionWithDuration:3.5 scale:0];
  11.     [_circle runAction:fade];
  12.    
  13.     double delayInSeconds = 3.0;
  14.     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
  15.     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  16.         [self explosionFromPoint:c0TileCoordt withSprite:_circle];
  17.     });
  18.    
  19.    
  20. }
  21.  
  22. - (BOOL)isLocationBombable:(CGPoint)tileCoord;
  23. {
  24.     if ([self isValidTileCoord:tileCoord] && ![self isWallAtTileCoord:tileCoord])
  25.     {
  26.         NSLog(@"Location bombable");
  27.         return YES;
  28.        
  29.     }
  30.     else
  31.     {
  32.         return NO;
  33.         NSLog(@"Location not bombable %@",CGPointCreateDictionaryRepresentation(tileCoord));//It works as supposed but this never returns so might be waste
  34.     }
  35. }
  36.  
  37. -(void)explosionFromPoint:(CGPoint)explosionPoint withSprite:(CCSprite*)sprite;
  38. {
  39.     //int
  40.     explosionLenght += 1;
  41.     if (explosionLenght >= 7) //Just for testing purposes, obviously
  42.     {
  43.         explosionLenght = 1;
  44.     }
  45.        
  46.     BOOL topB = YES;
  47.     BOOL leftB = YES;
  48.     BOOL bottomB = YES;
  49.     BOOL rightB = YES;
  50.    
  51.     int bombX =    (explosionPoint.x + 1);
  52.     int bombY =    (explosionPoint.y + 1);
  53.     int bombNegX = (explosionPoint.x - 1);
  54.     int bombNegY = (explosionPoint.y - 1);
  55.    
  56.     CGPoint top = ccp(explosionPoint.x, bombY);
  57.     CGPoint left = ccp(bombNegX, explosionPoint.y);
  58.     CGPoint bottom = ccp(explosionPoint.x, bombNegY);
  59.     CGPoint right = ccp(bombX, explosionPoint.y);
  60.    
  61.     if (![self isLocationBombable:top])
  62.     {topB = NO;}
  63.     if (![self isLocationBombable:left])
  64.     {leftB = NO;}
  65.     if (![self isLocationBombable:bottom])
  66.     {bottomB = NO;}
  67.     if (![self isLocationBombable:right])
  68.     {rightB = NO;}
  69.  
  70.     for (int i = 0; i <= explosionLenght; i++) {
  71.                
  72.         int bombX =    (explosionPoint.x + i);
  73.         int bombY =    (explosionPoint.y + i);
  74.         int bombNegX = (explosionPoint.x - i);
  75.         int bombNegY = (explosionPoint.y - i);
  76.        
  77.         CGPoint top = ccp(explosionPoint.x, bombY);
  78.         CGPoint left = ccp(bombNegX, explosionPoint.y);
  79.         CGPoint bottom = ccp(explosionPoint.x, bombNegY);
  80.         CGPoint right = ccp(bombX, explosionPoint.y);
  81.        
  82.         CCSprite *circleTop    = [[CCSprite alloc]initWithFile:@"Circle.png"];
  83.         CCSprite *circleLeft   = [[CCSprite alloc]initWithFile:@"Circle.png"];
  84.         CCSprite *circleBottom = [[CCSprite alloc]initWithFile:@"Circle.png"];
  85.         CCSprite *circleRight  = [[CCSprite alloc]initWithFile:@"Circle.png"];
  86.        
  87.         if ([self isLocationBombable:top] && topB == YES)
  88.         {
  89.             circleTop.position = [self positionForTileCoord:top];
  90.             [self addChild:circleTop];
  91.             id fadeTop = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleTop.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
  92.             [circleTop runAction:fadeTop];
  93.         }
  94.         if ([self isLocationBombable:left] && leftB == YES)
  95.         {
  96.             circleLeft.position = [self positionForTileCoord:left];
  97.             [self addChild:circleLeft];
  98.             id fadeLeft = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleLeft.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
  99.             [circleLeft runAction:fadeLeft];
  100.         }
  101.         if ([self isLocationBombable:bottom] && bottomB == YES)
  102.         {
  103.             circleBottom.position = [self positionForTileCoord:bottom];
  104.             [self addChild:circleBottom];
  105.             id fadeBottom = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleBottom.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
  106.             [circleBottom runAction:fadeBottom];
  107.         }
  108.         if ([self isLocationBombable:right] && rightB == YES)
  109.         {
  110.             circleRight.position = [self positionForTileCoord:right];
  111.             [self addChild:circleRight];
  112.             id fadeRight = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleRight.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
  113.             [circleRight runAction:fadeRight];
  114.         }
  115.     }
  116.     NSLog(@"Explosion done, call checkdamage");
  117.     [currentBombs addObject:sprite];
  118.     int cBCount = [currentBombs count];
  119.     NSLog(@"amount of bombs in array %i",cBCount);
  120.     [self schedule:@selector(checkDamageForBomb)];
  121. }
  122. -(void)stopCheckDamage{
  123.     NSLog(@"StopCheckDamage");
  124.     [self unschedule:@selector(checkDamageForBomb)];
  125.    
  126. }
  127.  
  128. -(void)checkDamageForBomb{
  129.     for (CCSprite* bomb in currentBombs)
  130.     {
  131.         CGPoint bombPos = [self tileCoordForPosition:bomb.position];
  132.        
  133.         for (int i = 0; i <= explosionLenght; i++) {
  134.            
  135.             CGPoint playerPos = [self tileCoordForPosition:_cat.position];
  136.            
  137.             int bombX =    (bombPos.x + i);
  138.             int bombY =    (bombPos.y + i);
  139.             int bombNegX = (bombPos.x - i);
  140.             int bombNegY = (bombPos.y - i);
  141.            
  142.             CGPoint centre = bombPos;
  143.             CGPoint top = ccp(centre.x, bombY);
  144.             CGPoint left = ccp(bombNegX, centre.y);
  145.             CGPoint bottom = ccp(centre.x, bombNegY);
  146.             CGPoint right = ccp(bombX, centre.y);
  147.        
  148.             //pastebin.com/biuQBfnv
  149.             if (CGPointEqualToPoint(top, playerPos) || CGPointEqualToPoint(left, playerPos) || CGPointEqualToPoint(bottom, playerPos) || CGPointEqualToPoint(right, playerPos))
  150.             {
  151.                 NSLog(@"Player hit");
  152.                 [currentBombs removeObject:bomb];
  153.                 if ([currentBombs count] == 0)
  154.                 {
  155.                     [self stopCheckDamage];
  156.                    
  157.                 }
  158.                 break;
  159.             }
  160.         }
  161.     }
  162.     double delayInSeconds = 3.0;
  163.     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
  164.     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  165.        
  166.         [self stopCheckDamage];
  167.        
  168.     });
  169.  
  170.    // [self performSelector:@selector(stopCheckDamage) withObject:nil afterDelay:5];
  171.                    
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement