priore

cocos2d: menu level in Angry Birds style

Jun 18th, 2012
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  Created by Danilo Priore on 10/06/12.
  3. //  Copyright 2012 Prioregroup.com. All rights reserved.
  4. //
  5. //  Permission is hereby granted, free of charge, to any person obtaining a copy
  6. //  of this software and associated documentation files (the "Software"), to deal
  7. //  in the Software without restriction, including without limitation the rights
  8. //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. //  copies of the Software, and to permit persons to whom the Software is
  10. //  furnished to do so, subject to the following conditions:
  11. //
  12. //  The above copyright notice and this permission notice shall be included in
  13. //  all copies or substantial portions of the Software.
  14. //
  15. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. //  THE SOFTWARE.
  22. //
  23. // how to use :
  24. //
  25. // #import "MenuLevel.h"
  26. // declare delegate in you layer (yourlager.h)
  27. // @interface YourLager : CCLayer <MenuLevelDelegate> { }
  28. //
  29. // add menu in your layer (yourlayer.m)
  30. // MenuLevel *menu = [[MenuLevel alloc] init];
  31. // menu.delegate = self;
  32. // [self addChild:menu];
  33. //
  34. // level buttons
  35. // CGFloat x = 100;
  36. // [menu createMenuItemLevel:1 position:ccp(x, size.height / 2) life:5 locked:NO];
  37. // [menu createMenuItemLevel:2 position:ccp(x + 70, size.height / 2) life:2 locked:NO];
  38. // [menu createMenuItemLevel:3 position:ccp(x + 140, size.height / 2) life:0 locked:YES];
  39. // [menu createMenuItemLevel:4 position:ccp(x + 210, size.height / 2) life:0 locked:YES];
  40. //
  41. // complete source code and example on http://www.prioregroup.com/iphone/menulevel.aspx
  42. //
  43.  
  44. #import "cocos2d.h"
  45.  
  46. @protocol MenuLevelDelegate;
  47.  
  48. @interface MenuLevel : CCMenu {
  49.    
  50. }
  51.  
  52. @property (nonatomic, retain) NSString *lockFileName;
  53. @property (nonatomic, retain) NSString *backgroundFileName;
  54. @property (nonatomic, retain) NSString *starFileName;
  55. @property (nonatomic, retain) NSString *starLowFileName;
  56. @property (nonatomic, retain) NSString *fontName;
  57. @property (nonatomic, assign) CGFloat fontSize;
  58. @property (nonatomic, assign) ccColor3B fontColor;
  59. @property (nonatomic, assign) NSInteger maxLifes;
  60. @property (nonatomic, assign) id<MenuLevelDelegate>delegate;
  61.  
  62. - (CCMenuItem*)createMenuItemLevel:(NSInteger)level
  63.                           position:(CGPoint)position
  64.                               life:(NSInteger)life
  65.                             locked:(BOOL)locked;
  66.  
  67. - (CCMenuItem*)createMenuItemLevel:(NSInteger)level
  68.                           position:(CGPoint)position
  69.                               life:(NSInteger)life
  70.                             locked:(BOOL)locked
  71.                             target:(id)target
  72.                           selector:(SEL)selector;
  73.  
  74. @end
  75.  
  76. @protocol MenuLevelDelegate <NSObject>
  77.  
  78. @optional
  79.  
  80. - (void)menuLevel:(MenuLevel*)menuLevel buttonSelected:(id)source;
  81.  
  82. @end
  83.  
  84. @implementation MenuLevel
  85.  
  86. @synthesize lockFileName, backgroundFileName;
  87. @synthesize starFileName, starLowFileName;
  88. @synthesize fontName, fontSize, fontColor;
  89. @synthesize maxLifes, delegate;
  90.  
  91. - (id)init
  92. {
  93.     if (self = [super initWithItems:nil vaList:nil])
  94.     {
  95.         // default values
  96.         maxLifes = 5;                       // number of lifes (showed in stars)
  97.         lockFileName = @"lock.png";         // image for level locked
  98.         backgroundFileName = @"button.png"; // image for button level
  99.         starFileName = @"star.png";         // image for lifes
  100.         starLowFileName = @"star_low.png";  // image for lose lifes
  101.         fontName = @"Marker Felt";          // font for level number
  102.         fontSize = 40.0f;                   // font size for level number
  103.         fontColor = ccWHITE;                // font color for level number
  104.         self.position = CGPointZero;
  105.     }
  106.    
  107.     return self;
  108. }
  109.  
  110. - (CCMenuItem*)createMenuItemLevel:(NSInteger)level
  111.                           position:(CGPoint)position
  112.                               life:(NSInteger)life
  113.                             locked:(BOOL)locked
  114. {
  115.     return [self createMenuItemLevel:level
  116.                             position:position
  117.                                 life:life
  118.                               locked:locked
  119.                               target:self
  120.                             selector:@selector(buttonSelected:)];
  121. }
  122.    
  123. - (CCMenuItem*)createMenuItemLevel:(NSInteger)level
  124.                           position:(CGPoint)position
  125.                               life:(NSInteger)life
  126.                             locked:(BOOL)locked
  127.                             target:(id)target
  128.                           selector:(SEL)selector
  129. {
  130.     // retrieve menu level scene
  131.     CCScene *scene = (CCScene*)delegate;
  132.     if (scene == nil)
  133.         NSAssert(YES, @"Invalid delegate for MenuLevel!");
  134.    
  135.     // create button with background
  136.     CCMenuItemSprite *item = [CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithFile:backgroundFileName]
  137.                                                       selectedSprite:nil target:target selector:selector];
  138.     item.anchorPoint = ccp(0, 1);
  139.     item.position = position;
  140.     [self addChild:item];
  141.    
  142.     // set button tag same level
  143.     item.tag = level;
  144.    
  145.     // level text
  146.     CGPoint pos = ccp(item.position.x + item.boundingBox.size.width / 2 ,
  147.                       item.position.y - item.boundingBox.size.height / 2);
  148.     CCLabelTTF *levelText = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d", level]
  149.                                            fontName:fontName fontSize:fontSize];
  150.     levelText.position = pos;
  151.     levelText.color = fontColor;
  152.     levelText.visible = !locked;
  153.     [scene addChild:levelText];  
  154.    
  155.     // lock image
  156.     CCSprite *lock = [CCSprite spriteWithFile:lockFileName];
  157.     lock.position = pos;
  158.     lock.visible = locked;
  159.     [scene addChild:lock];
  160.    
  161.     // life's
  162.     for (int c = 0; c < maxLifes; c++) {
  163.        
  164.         // star
  165.         CCSprite *star = [CCSprite spriteWithFile:c < life ? starFileName : starLowFileName];
  166.         star.anchorPoint = ccp(0, 1);
  167.         star.position = ccp(item.position.x - 10 + (star.boundingBox.size.width * c) + (1 * c),
  168.                             item.position.y - item.boundingBox.size.height - 2);
  169.         [scene addChild:star];
  170.     }
  171.    
  172.     return item;    
  173. }
  174.  
  175. - (void)buttonSelected:(id)sender
  176. {
  177.     // call delegate when button is selected
  178.     if (delegate != nil && [delegate respondsToSelector:@selector(menuLevel:buttonSelected:)])
  179.         [delegate menuLevel:self buttonSelected:sender];
  180. }
  181.  
  182. @end
Advertisement
Add Comment
Please, Sign In to add comment