Advertisement
GrebeniukLA

ConnectItViewController

Aug 4th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ConnectItViewController.m
  3. //  ConnectIt
  4. //
  5. //  Created by Леонид Гребенюк on 7/8/13.
  6. //  Copyright (c) 2013 Леонид Гребенюк. All rights reserved.
  7. //
  8.  
  9. #import "ConnectItViewController.h"
  10. #import "GameViewController.h"
  11.  
  12.  
  13. #import "Settings.h"
  14. #import "LAShip.h"
  15. #import "LAFiledForSettingsShip.h"
  16. #import "Constants.h"
  17. #import "MultyEnemy.h"
  18.  
  19. #import "ImageMaker.h" // Содание картинок в отдельном классе
  20.  
  21.  
  22. @interface ConnectItViewController () <UIGestureRecognizerDelegate>
  23.  
  24. @property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *numberShipsImage;
  25. @property (strong, nonatomic) NSMutableArray * arrayNumberShip;
  26.  
  27. @property (weak, nonatomic) IBOutlet UIImageView *iconImage;
  28.  
  29. @property (weak, nonatomic) IBOutlet UIButton *startButon;
  30.  
  31. @property (weak, nonatomic) IBOutlet UIImageView *poleImage;
  32.  
  33. @property (assign, nonatomic) CGPoint touchOffset;
  34.  
  35. @property (assign, nonatomic) CGPoint touchOnDotButton;
  36.  
  37. @property (assign, nonatomic) CGPoint firstPositionCenter;
  38.  
  39. @property (weak, nonatomic) LAShip *shipButton;
  40. @property (strong, nonatomic) LAFiledForSettingsShip* filedForSettingsShip;
  41.  
  42. @property (strong, nonatomic) NSArray *arrayDots;
  43.  
  44. @property (strong, nonatomic) IBOutletCollection(LAShip) NSArray *shipImages;
  45.  
  46. @property (weak, nonatomic) IBOutlet UILabel *rankLabel;
  47.  
  48. @end
  49.  
  50.  
  51.  
  52. @implementation ConnectItViewController
  53.  
  54.  
  55.  
  56. - (LAFiledForSettingsShip*) filedForSettingsShip {
  57.     if (!_filedForSettingsShip) {
  58.         _filedForSettingsShip = [[LAFiledForSettingsShip alloc] init];
  59.         _filedForSettingsShip.delegate = self;
  60.     }
  61.     return _filedForSettingsShip;
  62. }
  63.  
  64. #pragma mark -
  65. #pragma mark touch
  66.  
  67.  
  68.  
  69. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  70.  
  71. {
  72.     UITouch *touch = [[event allTouches]anyObject];
  73.    
  74.     for (LAShip* ship in self.shipImages) {
  75.         if([touch view]==ship) {
  76.             long taps = [touch tapCount];
  77.             self.shipButton = ship;
  78.             if (taps==1) {
  79.                
  80.                 [self.shipButton setChoisenShip:YES];
  81.                 self.firstPositionCenter = self.shipButton.center;
  82.                
  83.                 CGPoint touchPoint = [touch locationInView:ship];
  84.                
  85.                 self.touchOffset = CGPointMake(CGRectGetMidX(ship.bounds) - touchPoint.x,
  86.                                                CGRectGetMidY(ship.bounds) - touchPoint.y);
  87.                
  88.                 [self.view bringSubviewToFront:self.shipButton];
  89.             }
  90.             if (taps==2&&ship.x>-1) {
  91.                
  92.                 [self.filedForSettingsShip changeDiraction:self.shipButton];
  93.                
  94.             }
  95.            
  96.         }
  97.     }
  98. }
  99.  
  100. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
  101.    
  102.     if (self.shipButton) {
  103.        
  104.         UITouch* touch = [touches anyObject];
  105.        
  106.         CGPoint pointOnMainView = [touch locationInView:self.view];
  107.        
  108.         CGPoint correction = CGPointMake(pointOnMainView.x + self.touchOffset.x,
  109.                                          pointOnMainView.y + self.touchOffset.y);
  110.        
  111.         self.shipButton.center = correction;
  112.     }
  113.    
  114. }
  115. - (void) onTouchesEnded {
  116.    
  117.     [self.shipButton setChoisenShip:NO];
  118.     self.shipButton = nil;
  119.    
  120. }
  121. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  122. {
  123.     if (self.shipButton) {
  124.    
  125.         [self.filedForSettingsShip setShip:self.shipButton];
  126.        
  127.        
  128.         [self onTouchesEnded];
  129.        
  130.         if ([self allShipsSetted]) {
  131.             [self.startButon setEnabled:YES];
  132.         }
  133.        
  134.        
  135.     }
  136. }
  137. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
  138.     if (self.shipButton) {
  139.        
  140.         [self onTouchesEnded];
  141.     }
  142.    
  143. }
  144.  
  145.  
  146. #pragma mark -
  147. #pragma mark logic
  148.  
  149. -(BOOL) allShipsSetted {
  150.     for (LAShip * ship in self.shipImages) {
  151.         if (ship.x==-1) {
  152.             return NO;
  153.         }
  154.     }
  155.     return YES;
  156. }
  157.  
  158. -(NSInteger) numberUnSettedShipsSize: (NSInteger) size  {
  159.     NSInteger number=0;
  160.     for (LAShip * ship in self.shipImages) {
  161.         if ((ship.size==size)&&(ship.x==-1)) {
  162.             number++;
  163.         }
  164.     }
  165.     return number;
  166. }
  167.  
  168.  
  169.  
  170.  
  171. #pragma mark -
  172. #pragma mark load
  173. CGSize sizeScreen;
  174. -(void) viewDidAppear:(BOOL)animated {
  175.     [super viewDidAppear:YES];
  176.    
  177.     [self allShipsSetted]? [self.startButon setEnabled:YES] :  [self.startButon setEnabled:NO];
  178.  
  179.    
  180. }
  181. - (void)viewDidLoad
  182. {
  183.     [super viewDidLoad];
  184.    
  185.     UIFont *font;
  186.    
  187.     if (iPad) {
  188.        
  189.         font = [UIFont fontWithName:@"BauhausCTT"size:40];
  190.        
  191.     } else
  192.     {
  193.         font = [UIFont fontWithName:@"BauhausCTT"size:25];
  194.     }
  195.    
  196.     [self.rankLabel setFont:font];
  197.    
  198.     self.filedForSettingsShip.frame = self.poleImage.frame;
  199.     for (LAShip* ship in self.shipImages) {
  200.         ship.width= [self sizeShip:ship.tag-1];
  201.         ship.height = 1;
  202.         ship.x=-1;
  203.        
  204.     }
  205.     [self rankSetting];
  206. }
  207. -(void) rankSetting {
  208.    
  209.     [self.iconImage setImage: [ImageMaker makeMyIcon]];
  210.     self.rankLabel.text = [StringMaker makeMyRank];
  211.    
  212. }
  213.  
  214. -(NSInteger) sizeShip: (NSInteger) numberShip {
  215.     NSArray* array = @[@"1",@"1",@"1",@"1",@"2",@"2", @"2", @"3", @"3", @"4" ];
  216.     return [array[numberShip] integerValue];
  217. }
  218.  
  219. #pragma mark LAFieldDelegate
  220.  
  221. - (void)moveShipBack{
  222.     self.shipButton.center = self.firstPositionCenter;
  223.     [[Settings data] playSoundIShoot];
  224. }
  225.  
  226. -(void)moveShipToX:(NSInteger)x Y:(NSInteger)y {
  227.     [self.shipButton setFrame:CGRectMake(x+1, y+1, self.shipButton.frame.size.width, self.shipButton.frame.size.height)];
  228.     [[Settings data] playSoundShip:MAX(self.shipButton.width, self.shipButton.height) ];
  229.    
  230.     NSInteger size = [self.shipButton size];
  231.    
  232.     for (UIImageView *numberIMG in self.numberShipsImage) {
  233.         if (numberIMG.tag==size) {
  234.             NSInteger number = [self numberUnSettedShipsSize:size];
  235.             NSString *strNumber = [NSString stringWithFormat:@"%li",(long)number];
  236.            
  237.             [numberIMG setImage:[UIImage imageNamed:strNumber]];
  238.         }
  239.     }
  240. }
  241.  
  242.  
  243. -(void)flipShip {
  244.     [self.shipButton flip];
  245.     [[Settings data] playSoundDoubleShip];
  246. }
  247.  
  248.  
  249. #pragma mark -
  250. #pragma mark bottom Button
  251.  
  252. - (IBAction)goBack:(id)sender {
  253.     [[Settings data] playSoundKlick];
  254.     [self.navigationController popViewControllerAnimated:YES];
  255.     [self bigBannerShow];
  256. }
  257.  
  258.  
  259. - (IBAction)start:(id)sender {
  260.    
  261.     [[Settings data] playSoundKlick];
  262.    
  263. }
  264.  
  265. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  266.    
  267.     if ([[segue identifier] isEqualToString:@"GameViewController"])
  268.     {
  269.         GameViewController *controller = [segue destinationViewController];
  270.         controller.shipsArray = self.shipImages;
  271.  
  272.     }
  273.    
  274. }
  275.  
  276.  
  277. - (IBAction)pushButton:(id)sender {
  278.     [self bigBannerShow];
  279.     [[Settings data] playSoundKlick];
  280. }
  281.  
  282.  
  283. - (IBAction)goShopAction:(id)sender {
  284.     [[Settings data] playSoundKlick];
  285.     [LAUserDefaults boolForKey:@"Kids"] ? [self askParents:KidsShoping]: [self rightAnswerShoping];
  286. }
  287.  
  288.  
  289. #pragma mark LAKidsDelegate
  290.  
  291. - (void)rightAnswerShoping{
  292.     [self performSegueWithIdentifier:@"goShop" sender:self];
  293.     [self bigBannerShow];
  294.    
  295. }
  296.  
  297.  
  298. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement