Skip to content

Commit 01d0a44

Browse files
committed
Give ball an impulse when too horizontal
1 parent ca75a9d commit 01d0a44

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

TouchBreakout/GameScene.sks

-39 Bytes
Binary file not shown.

TouchBreakout/GameScene.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ class GameScene: SKScene {
4848
private let kBorderCategory : UInt32 = 0x1 << 4
4949
private let kHiddenCategory : UInt32 = 0x1 << 5
5050

51-
private let kBlockWidth: CGFloat = 90.0
52-
private let kBlockHeight: CGFloat = 25.0
53-
private let kBlockRows = 8
54-
private let kBlockColumns = 8
55-
private var kBlockRecoverTime = 10.0
51+
private let kBlockWidth : CGFloat = 90.0
52+
private let kBlockHeight : CGFloat = 25.0
53+
private let kBlockRows : Int = 8
54+
private let kBlockColumns : Int = 8
55+
private var kBlockRecoverTime = 10.0
5656

5757
private var velocityDx : CGFloat = 0.0
5858
private var velocityDy : CGFloat = 0.0
@@ -63,10 +63,10 @@ class GameScene: SKScene {
6363

6464
private var blocksNeedToBeDisplayed: [SKSpriteNode] = []
6565

66-
fileprivate var paddle: SKSpriteNode!
67-
fileprivate var ball: SKSpriteNode!
68-
fileprivate var bestLabel: SKLabelNode!
69-
fileprivate var scoreLabel: SKLabelNode!
66+
fileprivate var paddle : SKSpriteNode!
67+
fileprivate var ball : SKSpriteNode!
68+
fileprivate var bestLabel : SKLabelNode!
69+
fileprivate var scoreLabel : SKLabelNode!
7070
fileprivate var currentScore: Int = 0 {
7171
didSet {
7272
scoreLabel.text = "\(currentScore)"
@@ -75,10 +75,10 @@ class GameScene: SKScene {
7575
bestLabel.text = "Best: \(currentScore)"
7676
}
7777

78-
// increase velocity by score
78+
// increase velocity with score
7979
if currentScore > 0 {
8080
let currentVelocity = ball.physicsBody?.velocity.length ?? initialVelocityLength
81-
let velocityRatio = 1 + 5 / currentVelocity
81+
let velocityRatio: CGFloat = 1.0 + 5.0 / currentVelocity
8282
ball.physicsBody?.velocity.extend(by: velocityRatio)
8383
}
8484
}
@@ -216,7 +216,11 @@ class GameScene: SKScene {
216216
if velocityRatio < 1.0 {
217217
ball.physicsBody?.velocity.extend(by: 1 / velocityRatio)
218218
}
219-
// TODO: - if on a horizontal level...
219+
220+
// if too horizontal...
221+
if fabs(Double((ball.physicsBody?.velocity.dy)!)) < 30 {
222+
ball.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 2))
223+
}
220224
}
221225
}
222226

@@ -321,6 +325,7 @@ extension GameScene: TouchBarViewDelegate {
321325
guard gameState == .running else {
322326
return false
323327
}
328+
// 600.0 is touchbar's width
324329
var transformedX = CGFloat(locationX) / 600.0 * (scene?.size.width)! - (scene?.size.width)! / 2
325330
if transformedX - halfPaddleWidth < -halfScreenWidth {
326331
transformedX = -halfScreenWidth + halfPaddleWidth

0 commit comments

Comments
 (0)