77
88import GameplayKit
99import SpriteKit
10+ import GameKit
1011
1112func randomizer( min: Int , max: Int ) -> Int {
1213 let randomizer = GKRandomDistribution ( lowestValue: min, highestValue: max)
@@ -41,6 +42,8 @@ class GameSceneController: NSObject, SKPhysicsContactDelegate {
4142 var pausedTime : TimeInterval = 0
4243 var buttonsPause = UITapGestureRecognizer ( )
4344 var buttonTvOS = UITapGestureRecognizer ( )
45+ let grandmaTexture = SKTexture ( imageNamed: " vovo " )
46+ var coinsInRun : Int = 0
4447
4548 private func calculateScore( currentTime: TimeInterval ) {
4649 if timeScore == 0 {
@@ -49,6 +52,10 @@ class GameSceneController: NSObject, SKPhysicsContactDelegate {
4952 let deltaTime = ( currentTime - timeScore)
5053 if deltaTime >= 1 {
5154 score += 1
55+ if score == 80 && coinsInRun == 0 {
56+ GameCenterService . shared. showAchievements ( achievementID: " noCoinsInRunID " )
57+ }
58+
5259 gameDelegate? . drawScore ( score: score)
5360 timeScore = currentTime
5461 }
@@ -89,13 +96,13 @@ class GameSceneController: NSObject, SKPhysicsContactDelegate {
8996 }
9097
9198 func passedObstacles( node: SKNode ) -> Bool {
92- if let sprite = node as? SKSpriteNode {
93- return sprite. position. x < ( - 1 * ( sprite. size. width/ 2 + 20 ) )
94- } else {
95- return false
96- }
99+ if let sprite = node as? SKSpriteNode {
100+ return sprite. position. x < ( - 1 * ( sprite. size. width/ 2 + 20 ) )
101+ } else {
102+ return false
103+ }
97104 }
98-
105+
99106 func setSwipeGesture( ) -> [ UISwipeGestureRecognizer ] {
100107 let swipeUp : UISwipeGestureRecognizer = UISwipeGestureRecognizer ( target: self , action: #selector( self . respondToSwipeGesture) )
101108 swipeUp. direction = . up
@@ -114,13 +121,18 @@ class GameSceneController: NSObject, SKPhysicsContactDelegate {
114121 if gameDelegate? . pausedStatus ( ) == false {
115122 gameDelegate? . movePlayer ( direction: direction)
116123 }
124+ }
125+
126+ #if os(tvOS)
127+ func addTargetToTapGestureRecognizer( ) -> UITapGestureRecognizer {
128+ buttonsPause. addTarget ( self , action: #selector( clicked) )
117129
118- defaults . set ( true , forKey : " playerFirstTime " )
130+ return buttonsPause
119131 }
120132
121- #if os(tvOS)
122- func addTargetToTapGestureRecognizer ( ) -> UITapGestureRecognizer {
123- buttonsPause . addTarget ( self , action : #selector ( clicked ) )
133+ @ objc func clicked ( ) {
134+ if gameDelegate ? . getResumeButton ( ) . isFocused == true {
135+ gameDelegate ? . resumeGame ( )
124136
125137 return buttonsPause
126138 }
@@ -142,33 +154,34 @@ class GameSceneController: NSObject, SKPhysicsContactDelegate {
142154 gameDelegate? . musicAction ( )
143155 }
144156 }
145- #endif
157+ }
158+ #endif
146159
147160 func calculateObstacleMovement( allObstacles: [ SKNode ] ) {
148161 for obstacle in allObstacles {
149162 if gameDelegate? . pausedStatus ( ) == true {
150163 obstacle. position. x -= 0
151164 }
152165 else {
153- #if os(iOS)
154- let moveObstAction = SKAction . moveTo ( x: ( - 100000 ) , duration: duration*100)
155- #elseif os(tvOS)
156- let moveObstAction = SKAction . moveTo ( x: ( - 100000 ) , duration: durationTV*100)
157- #endif
166+ #if os(iOS)
167+ let moveObstAction = SKAction . moveTo ( x: ( - 100000 ) , duration: duration*100)
168+ #elseif os(tvOS)
169+ let moveObstAction = SKAction . moveTo ( x: ( - 100000 ) , duration: durationTV*100)
170+ #endif
158171
159172 obstacle. run ( moveObstAction)
160173 }
161174 }
162175 }
163176
164- #if os(tvOS)
165- func addTargetToPauseActionToTV( ) -> UITapGestureRecognizer {
166- self . buttonTvOS. addTarget ( self , action: #selector( self . tvOSAction) )
167- self . buttonTvOS. allowedPressTypes = [ NSNumber ( value: UIPress . PressType. playPause. rawValue) ]
168-
169- return buttonTvOS
170- }
171- #endif
177+ #if os(tvOS)
178+ func addTargetToPauseActionToTV( ) -> UITapGestureRecognizer {
179+ self . buttonTvOS. addTarget ( self , action: #selector( self . tvOSAction) )
180+ self . buttonTvOS. allowedPressTypes = [ NSNumber ( value: UIPress . PressType. playPause. rawValue) ]
181+
182+ return buttonTvOS
183+ }
184+ #endif
172185
173186 // MARK: - Função de clicar no botão com tvRemote
174187 @objc private func tvOSAction( ) {
@@ -237,7 +250,7 @@ class GameSceneController: NSObject, SKPhysicsContactDelegate {
237250 }
238251 }
239252 }
240-
253+
241254 func chooseObstacle( ) -> [ Obstacle ] {
242255 /*
243256 1. decidir peso
@@ -328,28 +341,25 @@ class GameSceneController: NSObject, SKPhysicsContactDelegate {
328341 lastCoinTimeCreated = currentTime
329342 }
330343 let pastTime = ( currentTime - lastCoinTimeCreated)
331- #if os(iOS)
344+ #if os(iOS)
332345 if pastTime >= coinDelayIOS {
333346 gameDelegate? . createCoin ( )
334347 lastCoinTimeCreated = currentTime
335348 if coinDelayIOS > 1.5 { // limite minimo do delay
336349 coinDelayIOS -= 0.05 // cada vez que o update é chamado diminui o delay
337350 }
338351 }
339- #elseif os (tvOS)
352+ #elseif os (tvOS)
340353 if pastTime >= coinDelayTV {
341354 gameDelegate? . createCoin ( )
342355 lastCoinTimeCreated = currentTime
343356 if coinDelayTV > 4 { // limite minimo do delay
344357 coinDelayTV -= 0.085 // cada vez que o update é chamado diminui o delay
345358 }
346359 }
347- #endif
348- }
349- // MARK: Primera vez do player no jogo
350- func firstTime( ) {
351- defaults. set ( false , forKey: " playerFirstTime " )
360+ #endif
352361 }
362+
353363 // MARK: TearDown
354364 func tearDown( ) {
355365 timeCounter = 0
@@ -358,7 +368,7 @@ class GameSceneController: NSObject, SKPhysicsContactDelegate {
358368 score = 0
359369 }
360370
361- func contact( contact: SKPhysicsContact , nodeA: SKNode , nodeB: SKNode ) {
371+ func contact( contact: SKPhysicsContact , nodeA: SKSpriteNode , nodeB: SKSpriteNode ) {
362372 if contact. bodyB. node? . name == " Fly " || contact. bodyA. node? . name == " Fly " {
363373 collisionBetween ( player: nodeA, enemy: nodeB)
364374 }
@@ -384,11 +394,26 @@ class GameSceneController: NSObject, SKPhysicsContactDelegate {
384394 }
385395 }
386396
387- func collisionBetween( player: SKNode , enemy: SKNode ) {
397+ func collisionBetween( player: SKSpriteNode , enemy: SKSpriteNode ) {
388398 if player. name == " Coin " || enemy. name == " Coin " {
389399 AudioService . shared. soundManager ( with: . coin, soundAction: . play)
390400 increaseScore ( player: player, enemy: enemy)
401+ coinsInRun += 1
402+ print ( coinsInRun)
403+ if coinsInRun == 5 {
404+ GameCenterService . shared. showAchievements ( achievementID: " firstCoinsInRunID " )
405+ }
406+ if coinsInRun == 12 {
407+ GameCenterService . shared. showAchievements ( achievementID: " secondCoinsInRunID " )
408+ }
391409 } else {
410+ guard let playerTexture = player. texture else { return }
411+ guard let enemyTexture = enemy. texture else { return }
412+
413+ if playerTexture. description == grandmaTexture. description || enemyTexture. description == grandmaTexture. description {
414+ GameCenterService . shared. showAchievements ( achievementID: " crashedGrandmaID " )
415+ }
416+
392417 gameDelegate? . goToGameOverScene ( )
393418 }
394419 }
0 commit comments