Advertisement
Guest User

Untitled

a guest
Nov 13th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.41 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // CircleJump
  4. //
  5. // Created by Aleksandar Angelov on 10/27/14.
  6. // Copyright (c) 2014 Aleksandar Angelov. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import iAd
  11. class ViewController: UIViewController,ADBannerViewDelegate,ADInterstitialAdDelegate,GADBannerViewDelegate, GADInterstitialDelegate,UIActionSheetDelegate {
  12. //MARK: - Variables -
  13. @IBOutlet var _playButton:UIButton!;
  14.  
  15. @IBOutlet var _frontCircleImg:UIImageView!;
  16. @IBOutlet var _backCircleImg:UIImageView!;
  17. @IBOutlet var _firstLineImg:UIImageView!;
  18. @IBOutlet var _scoreBackgroundImg:UIImageView!;
  19.  
  20. @IBOutlet var _leaderboardButton:UIButton!;
  21. @IBOutlet var _rateButton:UIButton!;
  22. @IBOutlet var _musicButton:UIButton!;
  23. @IBOutlet var _replayButton:UIButton!;
  24. @IBOutlet var _shareButton:UIButton!;
  25. @IBOutlet var noAds:UIButton!;
  26.  
  27. @IBOutlet var _titleLbl:UILabel!;
  28. @IBOutlet var _tapLbl:UILabel!;
  29. @IBOutlet var _scoreLbl:UILabel!;
  30. @IBOutlet var _bestScoreTextLbl:UILabel!;
  31. @IBOutlet var _bestScoreLbl:UILabel!;
  32. @IBOutlet var _gameoverLbl:UILabel!;
  33. @IBOutlet var _gameoverScoreLbl:UILabel!;
  34.  
  35. var _iAdView:UIView!;
  36.  
  37. var _iAdInterstitial:ADInterstitialAd!;
  38. var _googleInterstitial:GADInterstitial!;
  39.  
  40. var _iAdBannerView:ADBannerView!;
  41.  
  42. var _winSize:CGSize!;
  43.  
  44. var _isGoingUp:Bool=false;
  45. var _isFirstTap:Bool=true;
  46. var _isInitialJump:Bool=true;
  47. var _isGameOver:Bool=false;
  48.  
  49. var _jumpNo:Int=0;
  50. var _colorIndex:Int=0;
  51.  
  52. var _fallVelocity:CGPoint!;
  53. var _jumpVelocity:CGPoint!;
  54. var _lineMoveSpeed:CGPoint!;
  55. var _fallAcceleration:CGPoint!;
  56. var _initialCirclePosition:CGPoint!;
  57.  
  58. var _updateTimer:NSTimer!;
  59.  
  60. var _lineBoundaries:LineBoundaries!;
  61.  
  62. var _lineList:NSMutableArray!;
  63. var _pointList:NSMutableArray!;
  64.  
  65. //MARK:- Init -
  66. override func viewDidLoad() {
  67. super.viewDidLoad();
  68. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseCircleBounceAnimation"), name: ADBackgroundStateChangeNotification, object: nil);
  69. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("resumeCircleBounceAnimation"), name: ADForegroundStateChangeNotification, object: nil);
  70. GamecenterUtils.sharedGamecenterUtils.authenticateLocalUserOnViewController(self);
  71. _winSize = self.view.frame.size;
  72. _frontCircleImg.layer.zPosition = 10;
  73. _backCircleImg.layer.zPosition = -10;
  74. _firstLineImg.layer.zPosition = -5;
  75.  
  76. var circleImgPosition = _frontCircleImg.layer.position;
  77. _lineBoundaries = LineBoundaries(topLimit: kTopLimit, bottomLimit:kBottomLimit, minimimLength: kMinimumLength, maximumLength: kMaximumLength);
  78.  
  79. _initialCirclePosition = circleImgPosition;
  80. _fallVelocity = isIpad() ? CGPointMake(0, 0.4): CGPointMake(0, 0.2);
  81. _jumpVelocity = isIpad() ? CGPointMake(0, -1) : CGPointMake(0, -0.5);
  82. _lineMoveSpeed = kLineMoveSpeed;
  83. _fallAcceleration = isIpad() ? CGPointMake(0, 0.04): CGPointMake(0, 0.03);
  84.  
  85. _bestScoreLbl.text = String(UserDefaultsUtils.sharedUserDefaultsUtils.highscore());
  86.  
  87. if(UserDefaultsUtils.sharedUserDefaultsUtils.musicOn()) {
  88. _musicButton.setImage(UIImage(named: "musicButtonOn.png"), forState: UIControlState.Normal);
  89. }
  90. else {
  91. _musicButton.setImage(UIImage(named: "musicButtonOff.png"), forState: UIControlState.Normal);
  92. }
  93.  
  94. var children = self.view.subviews;
  95. for child in children {
  96. if let subview = child as? UIView {
  97. if (subview.tag == 1) {
  98. subview.layer.opacity = 0;
  99. }
  100. }
  101. }
  102.  
  103. // Remove Ads
  104. if NSUserDefaults.standardUserDefaults().boolForKey("noAds") {
  105.  
  106. admobBannerView.removeFromSuperview()
  107. }
  108.  
  109. UIView.animateWithDuration(0.5, animations: {
  110. for child in children {
  111. if let subview = child as? UIView {
  112. if (subview.tag == 1) {
  113. subview.layer.opacity = 1;
  114. }
  115. }
  116. }
  117. });
  118.  
  119. self.initLines();
  120. self.initialCircleBounce();
  121. if (kBanner != .NoBanner) {
  122. self.createBanner();
  123. }
  124. if (kInterstitial != .NoInterstitial) {
  125. self.createInterstitial();
  126. }
  127. }
  128. func initLines(){
  129.  
  130. let lineHeight:CGFloat = _firstLineImg.frame.size.height;
  131. var startPosition:CGPoint = CGPointMake(_firstLineImg.layer.position.x+_firstLineImg.frame.size.width/2, _firstLineImg.layer.position.y+lineHeight/2);
  132.  
  133. var circleDotImg:UIImageView = UIImageView(image: UIImage(named: "circleDot.png"));
  134. circleDotImg.frame.size = kCircleSize;
  135. circleDotImg.layer.position = CGPointMake(startPosition.x, startPosition.y-circleDotImg.frame.size.height/2);
  136. circleDotImg.layer.zPosition = -5;
  137. self.view.addSubview(circleDotImg);
  138.  
  139. _lineList = NSMutableArray(objects: _firstLineImg);
  140. _pointList = NSMutableArray(object: circleDotImg);
  141.  
  142. for (var i=0;i<kLinesCount;i++) {
  143.  
  144. var positionOffset_x = Int(arc4random()%UInt32(_lineBoundaries.maximumLength-_lineBoundaries.minimimLength+1))+Int(_lineBoundaries.minimimLength);
  145. var positionOffset_y = self.verticalOffset();
  146.  
  147. var nextPosition:CGPoint = CGPointMake(startPosition.x+CGFloat(positionOffset_x), startPosition.y+positionOffset_y);
  148. circleDotImg = UIImageView(image: UIImage(named: "circleDot.png"));
  149. circleDotImg.frame.size = kCircleSize;
  150. circleDotImg.layer.position = CGPointMake(nextPosition.x, nextPosition.y-circleDotImg.frame.size.height/2);
  151. circleDotImg.layer.zPosition = -5;
  152. self.view.addSubview(circleDotImg);
  153.  
  154.  
  155. var diff:CGPoint = CGPointMake(startPosition.x-nextPosition.x, startPosition.y-nextPosition.y);
  156. var rads:Float = Float(M_PI)-atan2(Float(diff.y), Float(diff.x));
  157. //var deg:Float=rads*(Float(180)/Float(M_PI));
  158.  
  159. var lineWidth = pointsDistance(startPosition, pointB: nextPosition);
  160. var newLineImg:UIImageView = UIImageView(frame: CGRectMake(startPosition.x-lineWidth/2,startPosition.y-lineHeight, lineWidth, lineHeight));
  161. newLineImg.layer.anchorPoint = CGPointMake(0, 0.5);
  162. newLineImg.image = UIImage(named: "line.png");
  163. if(diff.y != 0) {
  164. newLineImg.transform = CGAffineTransformMakeRotation(CGFloat(-rads));
  165. }
  166.  
  167. startPosition = nextPosition;
  168. newLineImg.layer.zPosition = -5;
  169. self.view.addSubview(newLineImg);
  170. _lineList.addObject(newLineImg);
  171. _pointList.addObject(circleDotImg);
  172. }
  173. }
  174. //MARK:- Touch handler -
  175. override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
  176. var touch = touches.anyObject() as UITouch;
  177. var touchPoint = touch.locationInView(self.view);
  178. if (_isGameOver) {
  179. return;
  180. }
  181. if (_iAdBannerView != nil) {
  182. if (CGRectContainsPoint(_iAdBannerView.frame, touchPoint)) {
  183. return;
  184. }
  185. }
  186. if (_iAdView != nil) {
  187. if(_iAdView.alpha != 0) {
  188. return;
  189. }
  190. }
  191. AudioUtils.sharedAudioUtils.playJumpSound();
  192. if(_isFirstTap){
  193. self.fadeOutAnimation();
  194. var frontCircleLayer:CALayer = _frontCircleImg.layer.presentationLayer() as CALayer;
  195. _frontCircleImg.frame = frontCircleLayer.frame;
  196. var backCircleLayer:CALayer = _backCircleImg.layer.presentationLayer() as CALayer;
  197. _backCircleImg.frame = backCircleLayer.frame;
  198. _frontCircleImg.layer.removeAllAnimations();
  199. _backCircleImg.layer.removeAllAnimations();
  200. _isFirstTap = false;
  201. _updateTimer = NSTimer.scheduledTimerWithTimeInterval( 0.01, target: self, selector: Selector("update"), userInfo: nil, repeats: true);
  202. _scoreLbl.hidden = false;
  203. _isGoingUp = true;
  204. }
  205. else {
  206. if(!_isGoingUp) {
  207. _isGoingUp = true;
  208.  
  209. }
  210. _jumpNo = 0;
  211. }
  212. var score:Int = _scoreLbl.text!.toInt()!;
  213. score+=1;
  214. if ((score%kColorChangeClicks)==0 && score > 0) {
  215. var rgbColor = kColorList[_colorIndex];
  216. _colorIndex++;
  217. if (_colorIndex == kColorList.count) {
  218. _colorIndex = 0;
  219. }
  220. var red:CGFloat = CGFloat(rgbColor.Red/255);
  221. var green:CGFloat = CGFloat(rgbColor.Green/255);
  222. var blue:CGFloat = CGFloat(rgbColor.Blue/255);
  223. UIView.animateWithDuration(0.5, animations: { () -> Void in
  224. self.view.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1);
  225. });
  226. }
  227. _scoreLbl.text = String(score);
  228.  
  229. }
  230. //MARK:- Animations -
  231. func initialCircleBounce(){
  232. var offset:CGFloat = isIpad() ? 40 : 20;
  233. _frontCircleImg.layer.position = _initialCirclePosition;
  234. _backCircleImg.layer.position = _initialCirclePosition;
  235. UIView.animateWithDuration(1.3,
  236. delay: 0,
  237. options: .CurveEaseInOut | .Repeat | .Autoreverse,
  238. animations: {
  239. self._frontCircleImg.center = CGPointMake(self._frontCircleImg.center.x, self._frontCircleImg.center.y+offset);
  240. self._backCircleImg.center = CGPointMake(self._backCircleImg.center.x, self._backCircleImg.center.y+offset);
  241.  
  242. }, completion: nil);
  243. }
  244. func resumeCircleBounceAnimation(){
  245. _frontCircleImg.layer.position = _initialCirclePosition;
  246. _backCircleImg.layer.position = _initialCirclePosition;
  247. self.initialCircleBounce();
  248. }
  249. func pauseCircleBounceAnimation(){
  250. var frontCircleImgLayer = _frontCircleImg.layer.presentationLayer() as CALayer;
  251. var backCircleImgLayer = _backCircleImg.layer.presentationLayer() as CALayer;
  252. _frontCircleImg.frame = frontCircleImgLayer.frame;
  253. _backCircleImg.frame = backCircleImgLayer.frame;
  254. frontCircleImgLayer.removeAllAnimations();
  255. backCircleImgLayer.removeAllAnimations();
  256.  
  257. }
  258. func fadeOutAnimation(){
  259. UIView.animateWithDuration(0.5, animations:{
  260. self._titleLbl.layer.opacity = 0;
  261. self._tapLbl.layer.opacity = 0;
  262. self._leaderboardButton.layer.opacity = 0;
  263. self._rateButton.layer.opacity = 0;
  264. self._musicButton.layer.opacity = 0;
  265. self._bestScoreTextLbl.layer.opacity = 0;
  266. self._bestScoreLbl.layer.opacity = 0;
  267. self.noAds.layer.opacity = 0;
  268. });
  269. }
  270. func fadeInAnimation(){
  271. UIView.animateWithDuration(0.5, animations:{
  272. self._frontCircleImg.layer.opacity = 0;
  273. self._backCircleImg.layer.opacity = 0;
  274. self._scoreLbl.layer.opacity = 0;
  275. self._titleLbl.layer.opacity = 1;
  276. self._gameoverLbl.layer.opacity = 1;
  277. self._scoreBackgroundImg.layer.opacity = 1;
  278. self._gameoverScoreLbl.layer.opacity = 1;
  279. self._replayButton.layer.opacity = 1;
  280. self._shareButton.layer.opacity = 1;
  281. self._leaderboardButton.layer.opacity = 1;
  282. self._rateButton.layer.opacity = 1;
  283. self._musicButton.layer.opacity = 1;
  284. self._bestScoreTextLbl.layer.opacity = 1;
  285. self._bestScoreLbl.layer.opacity = 1;
  286. self.noAds.layer.opacity = 1;
  287. });
  288. }
  289. func errorAnimation(){
  290.  
  291. var errorBackCircle:UIImageView = UIImageView(frame: _backCircleImg.frame);
  292. errorBackCircle.layer.zPosition = -10;
  293. errorBackCircle.image = UIImage(named: "errorBackCircle.png");
  294. errorBackCircle.alpha = 0;
  295.  
  296. var errorFrontCircle:UIImageView = UIImageView(frame: _frontCircleImg.frame);
  297. errorFrontCircle.layer.zPosition = 10;
  298. errorFrontCircle.image = UIImage(named: "errorFrontCircle.png");
  299. errorFrontCircle.alpha = 0;
  300.  
  301. self.view.addSubview(errorBackCircle);
  302. self.view.addSubview(errorFrontCircle);
  303.  
  304. UIView.animateWithDuration(kBlinkAnimationDuration, delay:0, options:UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse, animations: { () -> Void in
  305. UIView.setAnimationRepeatCount(3);
  306. errorBackCircle.alpha = 1;
  307. errorFrontCircle.alpha = 1;
  308. }, completion: { finished in
  309. errorBackCircle.alpha = 0;
  310. errorFrontCircle.alpha = 0;
  311. self._frontCircleImg.alpha = 0;
  312. self._backCircleImg.alpha = 0;
  313. self.fadeInAnimation();
  314. });
  315. }
  316. //MARK:- Button handlers -
  317. @IBAction func repeatButtonClicked(x:UIButton) {
  318. if (_iAdBannerView != nil) {
  319. _iAdBannerView.delegate = nil;
  320. }
  321. if (_iAdInterstitial != nil) {
  322. _iAdInterstitial.delegate = nil;
  323. }
  324. if (_googleInterstitial != nil) {
  325. _googleInterstitial.delegate = nil;
  326. }
  327. NSNotificationCenter.defaultCenter().removeObserver(self);
  328. AudioUtils.sharedAudioUtils.playClickSound();
  329. UIView.animateWithDuration(0.5, animations: {
  330. var children = self.view.subviews;
  331. for child in children {
  332. if let subview = child as? UIView {
  333. subview.layer.opacity = 0;
  334. }
  335. }
  336. }, completion: { finished in
  337. AppDelegate.reset();
  338. })
  339. }
  340. @IBAction func leaderboardButtonClicked(x:UIButton) {
  341. AudioUtils.sharedAudioUtils.playClickSound();
  342. GamecenterUtils.sharedGamecenterUtils.showLeaderboardOnViewController(self,leaderboardID:kLeaderboardID);
  343. }
  344. @IBAction func musicButtonClicked(x:UIButton) {
  345. AudioUtils.sharedAudioUtils.playClickSound();
  346. var musicState:Bool?;
  347. if(UserDefaultsUtils.sharedUserDefaultsUtils.musicOn()) {
  348. _musicButton.setImage(UIImage(named: "musicButtonOff.png"), forState: UIControlState.Normal);
  349. musicState = false;
  350. }
  351. else {
  352. _musicButton.setImage(UIImage(named: "musicButtonOn.png"), forState: UIControlState.Normal);
  353. musicState = true;
  354. }
  355. UserDefaultsUtils.sharedUserDefaultsUtils.setMusic(musicState!);
  356. }
  357. @IBAction func noAdsClicked(x:UIButton) {
  358.  
  359. /*var alert = UIAlertController(title: "Coming Soon!", message: "In-App Purchases to remove ads are coming soon!", preferredStyle: UIAlertControllerStyle.Alert)
  360. alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
  361. self.presentViewController(alert, animated: true, completion: nil)*/
  362.  
  363. PFPurchase.buyProduct("com.develop.ring.noads", block: { (error:NSError?) -> Void in
  364. if error != nil{
  365. let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
  366.  
  367. alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
  368.  
  369. self.presentViewController(alert, animated: true, completion: nil)
  370. }
  371. })
  372. }
  373. @IBAction func rateButtonClicked(x:UIButton) {
  374. AudioUtils.sharedAudioUtils.playClickSound();
  375. var iTunesLink:NSString = "itms-apps://itunes.apple.com/app/id\(kRateAppID)";
  376. var url = NSURL(string: iTunesLink);
  377. UIApplication.sharedApplication().openURL(url!);
  378. }
  379. @IBAction func shareButtonClicked(x:UIButton) {
  380. AudioUtils.sharedAudioUtils.playClickSound();
  381. var message = kSocialMessage;
  382. var url = NSURL(string: kItunesLink);
  383. var image = UIImage(named: kSocialImage);
  384. var activityItems:NSArray = [message,url!,image!];
  385.  
  386. var contr = UIActivityViewController(activityItems: activityItems, applicationActivities: nil);
  387. if (isIpad()){
  388. contr.popoverPresentationController?.sourceView = self.view;
  389. var sourceRect = contr.popoverPresentationController?.sourceRect;
  390. contr.popoverPresentationController?.sourceRect = CGRectMake(0, _winSize.height, _winSize.width, sourceRect!.size.height);
  391. }
  392. self.presentViewController(contr, animated: true, completion: nil);
  393.  
  394. }
  395. @IBAction func closeAd(x:UIButton) {
  396. AudioUtils.sharedAudioUtils.playClickSound();
  397. UIView.animateWithDuration(0.5, animations: { () -> Void in
  398. self._iAdView.alpha = 0;
  399. }, completion: { finished in
  400. self._iAdView.removeFromSuperview();
  401. self._iAdInterstitial.delegate = nil;
  402. self._iAdInterstitial = nil;
  403. });
  404. }
  405. //MARK:- Timers -
  406. func update(){
  407. self.jump();
  408. self.fall();
  409. self.moveLines();
  410. self.movePoints();
  411. self.checkForCollision();
  412.  
  413. }
  414. func checkForCollision() {
  415. for imgView in _lineList{
  416. let lineImgView = imgView as UIImageView;
  417. var touchPoint = CGPointZero;//touch.locationInView(imgView as UIImageView);
  418. var bottomCollidePoint:CGPoint = CGPointMake(_backCircleImg.layer.position.x, _backCircleImg.layer.position.y+_backCircleImg.frame.size.height/2-(isIpad() ? 16 : 7.5));
  419. var topCollidePoint:CGPoint = CGPointMake(_backCircleImg.layer.position.x, _backCircleImg.layer.position.y-_backCircleImg.frame.size.height/2+(isIpad() ? 15 : 9));
  420. if(CGRectContainsPoint(lineImgView.bounds, lineImgView.convertPoint(topCollidePoint, fromView: self.view)) || CGRectContainsPoint(lineImgView.bounds, lineImgView.convertPoint(bottomCollidePoint, fromView: self.view))){
  421. _updateTimer.invalidate();
  422. UserDefaultsUtils.sharedUserDefaultsUtils.setHighscore(_scoreLbl.text!.toInt()!);
  423. GamecenterUtils.sharedGamecenterUtils.reportScore(UserDefaultsUtils.sharedUserDefaultsUtils.highscore(),leaderboardID:kLeaderboardID);
  424. _gameoverScoreLbl.text = _scoreLbl?.text;
  425. _bestScoreLbl.text = String(UserDefaultsUtils.sharedUserDefaultsUtils.highscore());
  426. AudioUtils.sharedAudioUtils.playErrorSound();
  427. self.errorAnimation();
  428. _isGameOver = true;
  429. }
  430. }
  431. }
  432. func moveLines(){
  433. for line in _lineList{
  434. let lineImgView = line as UIImageView;
  435. lineImgView.layer.position = self.addPoints(lineImgView.layer.position, pointB: _lineMoveSpeed);
  436. let lineImgPos:CGPoint = lineImgView.frame.origin;
  437. if((lineImgPos.x+lineImgView.frame.width) < 0) {
  438. if (lineImgView == _firstLineImg) {
  439. _lineList.removeObject(_firstLineImg);
  440. }
  441. else {
  442. var lastLineImg = _lineList.lastObject as UIImageView;
  443. var lastPointImg = _pointList.lastObject as UIImageView;
  444. var preLastPointImg = _pointList.objectAtIndex(_pointList.count-2) as UIImageView;
  445.  
  446. _lineList.removeObjectAtIndex(0);
  447. _lineList.addObject(lineImgView);
  448.  
  449. lineImgView.transform = CGAffineTransformMakeRotation(0);
  450.  
  451. var nextPosition:CGPoint = lastPointImg.layer.position;
  452.  
  453. var diff:CGPoint = CGPointMake(preLastPointImg.layer.position.x-nextPosition.x, preLastPointImg.layer.position.y-nextPosition.y);
  454. var rads:Float = Float(M_PI)-atan2(Float(diff.y), Float(diff.x));
  455. var lineWidth = pointsDistance(preLastPointImg.layer.position, pointB: nextPosition);
  456. var lineHeight = lineImgView.frame.size.height;
  457.  
  458. lineImgView.frame = CGRectMake(preLastPointImg.layer.position.x,preLastPointImg.layer.position.y-lineHeight/2, lineWidth, lineHeight);
  459. if(diff.y != 0) {
  460. lineImgView.transform = CGAffineTransformMakeRotation(CGFloat(-rads));
  461. }
  462.  
  463. }
  464.  
  465. }
  466. }
  467. }
  468. func movePoints(){
  469. for line in _pointList{
  470. let pointImgView = line as UIImageView;
  471. pointImgView.layer.position = self.addPoints(pointImgView.layer.position, pointB: _lineMoveSpeed);
  472. let pointImgPos:CGPoint = pointImgView.layer.position;
  473. if((pointImgPos.x+pointImgView.frame.width/2) < 0) {
  474. var lastPointImg = _pointList.lastObject as UIImageView;
  475. var lastPointPosition = lastPointImg.layer.position;
  476.  
  477. var positionOffset_x = Int(arc4random()%UInt32(_lineBoundaries.maximumLength-_lineBoundaries.minimimLength+1))+Int(_lineBoundaries.minimimLength);
  478. var positionOffset_y = self.verticalOffset();
  479. pointImgView.layer.position = CGPointMake(lastPointPosition.x+CGFloat(positionOffset_x), lastPointPosition.y+positionOffset_y);
  480. _pointList.addObject(pointImgView);
  481. _pointList.removeObjectAtIndex(0);
  482.  
  483. }
  484. }
  485. }
  486. func jump(){
  487. if(_isGoingUp) {
  488. _frontCircleImg.layer.position = self.addPoints(_frontCircleImg.layer.position, pointB: _jumpVelocity);
  489. _backCircleImg.layer.position = self.addPoints(_backCircleImg.layer.position, pointB: _jumpVelocity);
  490. //_jumpVelocity = self.addPoints(_jumpVelocity, pointB: CGPointMake(0, 0));
  491. _jumpNo++;
  492. if(_jumpNo == 30) {
  493. _jumpNo = 0;
  494. _isGoingUp = false;
  495. //_jumpVelocity = CGPointMake(0, -0.7);
  496. _fallVelocity = isIpad() ? CGPointMake(0, 0.4): CGPointMake(0, 0.2);
  497. if(_isInitialJump) {
  498. _isInitialJump = false;
  499. _jumpVelocity = isIpad() ? CGPointMake(0, -2.3) : CGPointMake(0, -1.2);
  500. }
  501. }
  502. }
  503.  
  504. }
  505. func fall(){
  506. if(!_isGoingUp) {
  507. _frontCircleImg.layer.position = self.addPoints(_frontCircleImg.layer.position, pointB: _fallVelocity);
  508. _backCircleImg.layer.position = self.addPoints(_backCircleImg.layer.position, pointB: _fallVelocity);
  509. _fallVelocity = self.addPoints(_fallVelocity, pointB:_fallAcceleration);
  510. }
  511. }
  512. //MARK:- Lines Manipulation
  513. func verticalOffset()->CGFloat{
  514. var randomSideIndex = arc4random()%101;
  515. var offset = isIpad() ? Int((arc4random()%40)+50) : Int((arc4random()%20)+30);
  516. if(randomSideIndex < 50) {
  517. if(_lineBoundaries.topLimit > offset){
  518. _lineBoundaries.topLimit -= offset;
  519. if((_lineBoundaries.bottomLimit+offset)>kBottomLimit){
  520. _lineBoundaries.bottomLimit = kBottomLimit;
  521. }
  522. else {
  523. _lineBoundaries.bottomLimit += offset;
  524. }
  525. offset *= -1;
  526.  
  527. }
  528. else {
  529. _lineBoundaries.bottomLimit -= offset;
  530. if((_lineBoundaries.topLimit+offset)>kTopLimit){
  531. _lineBoundaries.topLimit = kTopLimit;
  532. }
  533. else {
  534. _lineBoundaries.topLimit += offset;
  535. }
  536.  
  537. }
  538. }
  539. else {
  540. if(_lineBoundaries.bottomLimit > offset){
  541. _lineBoundaries.bottomLimit -= offset;
  542. _lineBoundaries.topLimit += offset;
  543. if((_lineBoundaries.topLimit+offset)>kTopLimit){
  544. _lineBoundaries.topLimit = kTopLimit;
  545. }
  546. else {
  547. _lineBoundaries.topLimit += offset;
  548. }
  549. }
  550. else {
  551. _lineBoundaries.topLimit -= offset;
  552. _lineBoundaries.bottomLimit += offset;
  553. if((_lineBoundaries.bottomLimit+offset)>kBottomLimit){
  554. _lineBoundaries.bottomLimit = kBottomLimit;
  555. }
  556. else {
  557. _lineBoundaries.bottomLimit += offset;
  558. }
  559. offset *= -1;
  560. }
  561. }
  562. return CGFloat(offset);
  563. }
  564. //MARK:- ADS -
  565. //MARK:Interstitial
  566. func createInterstitial() {
  567. if(UserDefaultsUtils.sharedUserDefaultsUtils.adsCounter()==0){
  568. if (kInterstitial == .GoogleInterstitial) {
  569. _googleInterstitial = GADInterstitial();
  570. _googleInterstitial.delegate = self;
  571. _googleInterstitial.adUnitID = kGoogleInterstitialAppUnitID;
  572. var request = GADRequest();
  573. //request.testDevices = [GAD_SIMULATOR_ID];
  574. _googleInterstitial.loadRequest(request);
  575. }
  576. else if (kInterstitial == .iAdInterstitial) {
  577. _iAdInterstitial = ADInterstitialAd();
  578. _iAdInterstitial.delegate = self;
  579. }
  580. }
  581. UserDefaultsUtils.sharedUserDefaultsUtils.incrementAdsCounter(2);
  582. }
  583. func interstitialDidReceiveAd(ad: GADInterstitial!) {
  584. self.pauseCircleBounceAnimation();
  585. ad.presentFromRootViewController(self);
  586. }
  587. func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
  588. ad.delegate = nil;
  589. }
  590. func interstitialDidDismissScreen(ad: GADInterstitial!){
  591. self.resumeCircleBounceAnimation();
  592. }
  593. func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
  594. _iAdView = UIView(frame: CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height));
  595. _iAdView.layer.zPosition = 20;
  596. _iAdView.alpha = 0;
  597. self.view.addSubview(_iAdView);
  598. interstitialAd.presentInView(_iAdView);
  599. var closeButton = UIButton(frame: CGRectMake(0, 0, 40, 40));
  600. closeButton.addTarget(self, action: Selector("closeAd:"), forControlEvents: UIControlEvents.TouchDown);
  601. closeButton.setBackgroundImage(UIImage(named: "closeButton.png"), forState: UIControlState.Normal);
  602. _iAdView.addSubview(closeButton);
  603. UIView.animateWithDuration(0.5, animations: { () -> Void in
  604. self._iAdView.alpha = 1;
  605. })
  606. }
  607. func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
  608. UIView.animateWithDuration(0.5, animations: { () -> Void in
  609. self._iAdView.alpha = 0;
  610. }, completion: { finished in
  611. self._iAdView.removeFromSuperview();
  612. self._iAdInterstitial.delegate = nil;
  613. self._iAdInterstitial = nil;
  614. });
  615. }
  616. func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
  617. interstitialAd.cancelAction();
  618. }
  619.  
  620.  
  621.  
  622. //MARK:Banner
  623. func createBanner() {
  624. if (kBanner == .GoogleBanner) {
  625. var admobBannerView = GADBannerView(adSize: isIpad() ? kGADAdSizeSmartBannerPortrait : kGADAdSizeBanner);
  626. admobBannerView.rootViewController = self;
  627. admobBannerView.adUnitID = kGoogleBannerAppUnitID;
  628. admobBannerView.delegate = self;
  629. self.view.addSubview(admobBannerView);
  630. var request = GADRequest();
  631. //request.testDevices = ["600ecccd6a1c2a4750952facf253ea2f"];
  632. admobBannerView.loadRequest(request);
  633. admobBannerView.frame = CGRectMake(0, _winSize.height, _winSize.width, admobBannerView.frame.size.height);
  634. }
  635. else if (kBanner == .iAdBanner) {
  636. _iAdBannerView = ADBannerView(adType: ADAdType.Banner);
  637. _iAdBannerView.frame = CGRectMake(0, _winSize.height, _iAdBannerView.frame.width, _iAdBannerView.frame.height);
  638. _iAdBannerView.delegate = self;
  639. self.view.addSubview(_iAdBannerView);
  640. }
  641.  
  642.  
  643. }
  644. func bannerViewDidLoadAd(banner: ADBannerView!) {
  645. UIView.animateWithDuration(1, animations: { () -> Void in
  646. banner.frame = CGRectMake(0, self._winSize.height-banner.frame.height, banner.frame.width, banner.frame.height);
  647. });
  648. }
  649. func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
  650. UIView.animateWithDuration(1, animations: { () -> Void in
  651. banner.frame = CGRectMake(0, self._winSize.height, banner.frame.width, banner.frame.height);
  652. })
  653. }
  654. func adViewDidReceiveAd(view: GADBannerView!) {
  655. UIView.animateWithDuration(1, animations: { () -> Void in
  656. view.frame = CGRectMake(0, self._winSize.height-view.frame.size.height, view.frame.size.width, view.frame.size.height);
  657. });
  658. }
  659. func adView(view: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
  660. UIView.animateWithDuration(1, animations: { () -> Void in
  661. view.frame = CGRectMake(0, self._winSize.height, view.frame.size.width, view.frame.size.height);
  662. });
  663. }
  664.  
  665. //MARK:- Other -
  666. override func prefersStatusBarHidden() -> Bool {
  667. return true;
  668. }
  669. override func didReceiveMemoryWarning() {
  670. super.didReceiveMemoryWarning()
  671. }
  672. func pointsDistance(pointA:CGPoint,pointB:CGPoint)->CGFloat{
  673. var xDist = (pointB.x - pointA.x);
  674. var yDist = (pointB.y - pointA.y);
  675. var distance = sqrt((xDist * xDist) + (yDist * yDist));
  676. return distance;
  677. }
  678. func addPoints(pointA:CGPoint,pointB:CGPoint)->CGPoint{
  679. return CGPointMake(pointA.x+pointB.x, pointA.y+pointB.y);
  680. }
  681.  
  682. }
  683. func isIpad()->Bool{
  684. return UIDevice.currentDevice().userInterfaceIdiom == .Pad;
  685. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement