Skip to content

Commit 9b203ee

Browse files
committed
Integration plus bucket letterboxing
1 parent 63fbf7c commit 9b203ee

26 files changed

+198
-123
lines changed

‎ps4/peggleclone.xcodeproj/project.pbxproj‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
68686A9C27B373EC00B97394 /* SLPhysicsBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68686A9B27B373EC00B97394 /* SLPhysicsBody.swift */; };
3535
68686AA827B3A1E700B97394 /* SLPhysicsCircle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68686AA727B3A1E700B97394 /* SLPhysicsCircle.swift */; };
3636
687C2B3F27B61ABF00D1BB1E /* GameEngineManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 687C2B3E27B61ABF00D1BB1E /* GameEngineManager.swift */; };
37-
687C2B4127B61CF500D1BB1E /* PegColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 687C2B4027B61CF500D1BB1E /* PegColor.swift */; };
37+
687C2B4127B61CF500D1BB1E /* PegState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 687C2B4027B61CF500D1BB1E /* PegState.swift */; };
3838
687C2B4327B6509200D1BB1E /* SLPhysicsCollision.swift in Sources */ = {isa = PBXBuildFile; fileRef = 687C2B4227B6509200D1BB1E /* SLPhysicsCollision.swift */; };
3939
688064E927B8E01400EC1501 /* LevelLoaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 688064E827B8E01400EC1501 /* LevelLoaderView.swift */; };
4040
688064EB27B8FFD200EC1501 /* PeggleHomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 688064EA27B8FFD100EC1501 /* PeggleHomeView.swift */; };
41+
6882BA8127CA520C009F3DAD /* GameState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6882BA8027CA520C009F3DAD /* GameState.swift */; };
4142
68861F0627C92DB500D2AEF7 /* SLPhysicsTriangle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68861F0527C92DB500D2AEF7 /* SLPhysicsTriangle.swift */; };
4243
689A4A0027A2CD0D00A2E4E6 /* Level.swift in Sources */ = {isa = PBXBuildFile; fileRef = 689A49FF27A2CD0D00A2E4E6 /* Level.swift */; };
4344
689A4A0227A2CD7C00A2E4E6 /* AllLevelsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 689A4A0127A2CD7C00A2E4E6 /* AllLevelsManager.swift */; };
@@ -87,10 +88,11 @@
8788
68686A9B27B373EC00B97394 /* SLPhysicsBody.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SLPhysicsBody.swift; sourceTree = "<group>"; };
8889
68686AA727B3A1E700B97394 /* SLPhysicsCircle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SLPhysicsCircle.swift; sourceTree = "<group>"; };
8990
687C2B3E27B61ABF00D1BB1E /* GameEngineManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameEngineManager.swift; sourceTree = "<group>"; };
90-
687C2B4027B61CF500D1BB1E /* PegColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PegColor.swift; sourceTree = "<group>"; };
91+
687C2B4027B61CF500D1BB1E /* PegState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PegState.swift; sourceTree = "<group>"; };
9192
687C2B4227B6509200D1BB1E /* SLPhysicsCollision.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SLPhysicsCollision.swift; sourceTree = "<group>"; };
9293
688064E827B8E01400EC1501 /* LevelLoaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LevelLoaderView.swift; sourceTree = "<group>"; };
9394
688064EA27B8FFD100EC1501 /* PeggleHomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeggleHomeView.swift; sourceTree = "<group>"; };
95+
6882BA8027CA520C009F3DAD /* GameState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameState.swift; sourceTree = "<group>"; };
9496
68861F0527C92DB500D2AEF7 /* SLPhysicsTriangle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SLPhysicsTriangle.swift; sourceTree = "<group>"; };
9597
689A49FF27A2CD0D00A2E4E6 /* Level.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Level.swift; sourceTree = "<group>"; };
9698
689A4A0127A2CD7C00A2E4E6 /* AllLevelsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AllLevelsManager.swift; sourceTree = "<group>"; };
@@ -133,8 +135,9 @@
133135
6834AD3027C8074A00F5A4E3 /* TriangleView.swift */,
134136
68143562279EE71700A16058 /* BackgroundView.swift */,
135137
6814356D279FD9C900A16058 /* PegView.swift */,
136-
687C2B4027B61CF500D1BB1E /* PegColor.swift */,
138+
687C2B4027B61CF500D1BB1E /* PegState.swift */,
137139
688064EA27B8FFD100EC1501 /* PeggleHomeView.swift */,
140+
6882BA8027CA520C009F3DAD /* GameState.swift */,
138141
);
139142
path = Views;
140143
sourceTree = "<group>";
@@ -379,7 +382,7 @@
379382
files = (
380383
6814356E279FD9C900A16058 /* PegView.swift in Sources */,
381384
689A4A1027A6AE2100A2E4E6 /* LevelDesignerView.swift in Sources */,
382-
687C2B4127B61CF500D1BB1E /* PegColor.swift in Sources */,
385+
687C2B4127B61CF500D1BB1E /* PegState.swift in Sources */,
383386
68143567279EE7D700A16058 /* PegsRowView.swift in Sources */,
384387
68B6B8D527C54138004CE9E2 /* PeggleObjectPersistance.swift in Sources */,
385388
6834AD2D27C7D0A000F5A4E3 /* PowerUpHandler.swift in Sources */,
@@ -398,6 +401,7 @@
398401
688064E927B8E01400EC1501 /* LevelLoaderView.swift in Sources */,
399402
6834AD3127C8074A00F5A4E3 /* TriangleView.swift in Sources */,
400403
6834AD3527C8231300F5A4E3 /* TrianglePegPersistance.swift in Sources */,
404+
6882BA8127CA520C009F3DAD /* GameState.swift in Sources */,
401405
689A4A1727A6D72E00A2E4E6 /* KeyboardResponder.swift in Sources */,
402406
689A4A0427A2DBD700A2E4E6 /* LevelSelectorView.swift in Sources */,
403407
6834AD1E27C684E100F5A4E3 /* SLPhysicsBucket.swift in Sources */,

‎ps4/peggleclone/Models/KaboomPeg.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class KaboomPeg: Peg {
5353
color = .greenGlow
5454
} else if color != .greenGlow {
5555
explosionState += 1
56-
guard let newColor = PegColor(rawValue: "explosion-\(explosionState)") else {
56+
guard let newColor = PegState(rawValue: "explosion-\(explosionState)") else {
5757
color = .greenGlow
5858
return
5959
}

‎ps4/peggleclone/Models/Level.swift‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ struct Level: Identifiable {
1818
self.peggleObjects = peggleObjects
1919
}
2020

21+
init() {
22+
self.id = UUID()
23+
self.name = "empty"
24+
self.peggleObjects = []
25+
}
26+
2127
init(id: UUID, name: String, peggleObjects: [PeggleObject]) {
2228
self.id = id
2329
self.name = name
@@ -80,7 +86,6 @@ struct Level: Identifiable {
8086

8187
extension Level: Equatable {
8288
static func == (lhs: Level, rhs: Level) -> Bool {
83-
let isEqual = lhs.id == rhs.id && lhs.name == rhs.name && lhs.peggleObjects == rhs.peggleObjects
84-
return isEqual
89+
lhs.id == rhs.id && lhs.name == rhs.name
8590
}
8691
}

‎ps4/peggleclone/Models/Peg.swift‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import SwiftUI
1111
class Peg: PeggleObject {
1212
static let pegMinRadius = 25.0
1313
static let pegMaxRadius = 50.0
14-
var color: PegColor
14+
var color: PegState
1515
var radius: Double
1616
var shadow: Color = .white
1717
var shadowRadius: Double = 0.0
1818

19-
init(color: PegColor, center: Point, radius: Double = 25) {
19+
init(color: PegState, center: Point, radius: Double = 25) {
2020
self.color = color
2121
self.radius = radius
2222
super.init(center: center)
2323
}
2424

25-
init(id: UUID, center: Point, color: PegColor, radius: Double = 25) {
25+
init(id: UUID, center: Point, color: PegState, radius: Double = 25) {
2626
self.color = color
2727
self.radius = radius
2828
super.init(id: id, center: center)
@@ -48,10 +48,10 @@ class Peg: PeggleObject {
4848
}
4949

5050
func glow() {
51-
if color == PegColor.bluePeg {
52-
self.color = PegColor.blueGlow
53-
} else if color == PegColor.orangePeg {
54-
self.color = PegColor.orangeGlow
51+
if color == PegState.bluePeg {
52+
self.color = PegState.blueGlow
53+
} else if color == PegState.orangePeg {
54+
self.color = PegState.orangeGlow
5555
}
5656
}
5757

‎ps4/peggleclone/PeggleCloneApp.swift‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ import SwiftUI
99

1010
@main
1111
struct PeggleCloneApp: App {
12+
@StateObject var allLevelsManager = AllLevelsManager()
1213

1314
var body: some Scene {
1415
WindowGroup {
15-
ContentView()
16+
GeometryReader { geometry in
17+
ContentView(levelManager: allLevelsManager
18+
.initialiseLevelManager(canvasDimension: geometry.frame(in: .global)),
19+
gameEngineManager: GameEngineManager(canvasDimension: geometry.frame(in: .global)))
20+
.environmentObject(allLevelsManager)
21+
}
1622
}
1723
}
1824
}

‎ps4/peggleclone/Persistance/KaboomPegPersistance.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class KaboomPegPersistance: PegPersistance {
2323

2424
required init(from decoder: Decoder) throws {
2525
let container = try decoder.container(keyedBy: CodingKeys.self)
26-
let color = try container.decode(PegColor.self, forKey: .color)
26+
let color = try container.decode(PegState.self, forKey: .color)
2727
let radius = try container.decode(Double.self, forKey: .radius)
2828
let id = try container.decode(UUID.self, forKey: .id)
2929
let center = try container.decode(PointPersistance.self, forKey: .center)

‎ps4/peggleclone/Persistance/PegPersistance.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99
import SwiftUI
1010

1111
class PegPersistance: PeggleObjectPersistance {
12-
var color: PegColor
12+
var color: PegState
1313
var radius: Double
1414
var shadow: Color = .white
1515
var shadowRadius: Double = 0.0
@@ -20,7 +20,7 @@ class PegPersistance: PeggleObjectPersistance {
2020
super.init(peg)
2121
}
2222

23-
init(id: UUID, center: PointPersistance, color: PegColor, radius: Double) {
23+
init(id: UUID, center: PointPersistance, color: PegState, radius: Double) {
2424
self.color = color
2525
self.radius = radius
2626
super.init(id: id, center: center)
@@ -35,7 +35,7 @@ class PegPersistance: PeggleObjectPersistance {
3535

3636
required init(from decoder: Decoder) throws {
3737
let container = try decoder.container(keyedBy: CodingKeys.self)
38-
self.color = try container.decode(PegColor.self, forKey: .color)
38+
self.color = try container.decode(PegState.self, forKey: .color)
3939
self.radius = try container.decode(Double.self, forKey: .radius)
4040
self.shadow = .white
4141
self.shadowRadius = 0.0

‎ps4/peggleclone/Persistance/SpookyPegPersistance.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SpookyPegPersitance: PegPersistance {
2323

2424
required init(from decoder: Decoder) throws {
2525
let container = try decoder.container(keyedBy: CodingKeys.self)
26-
let color = try container.decode(PegColor.self, forKey: .color)
26+
let color = try container.decode(PegState.self, forKey: .color)
2727
let radius = try container.decode(Double.self, forKey: .radius)
2828
let id = try container.decode(UUID.self, forKey: .id)
2929
let center = try container.decode(PointPersistance.self, forKey: .center)

‎ps4/peggleclone/SLGameEngine/SLGameEngine.swift‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SLGameEngine {
4545
let physicsBody = SLPhysicsCircle(position: peg.center, isDynamic: false, radius: peg.radius)
4646
physicsObjects.append(physicsBody)
4747
mappings[peg] = physicsBody
48-
if peg.color == PegColor.orangePeg {
48+
if peg.color == PegState.orangePeg {
4949
numOfOrangePegs += 1
5050
}
5151
} else if let triangle = peggleObject as? TriangleBlock {
@@ -65,7 +65,12 @@ class SLGameEngine {
6565
mappings[bucket] = bucketPhysicsBody
6666
physicsObjects.append(bucketPhysicsBody)
6767
physicsEngine.load(physicsBodies: physicsObjects, canvasDimensions: canvasDimensions)
68+
print(level.peggleObjects)
6869
addCannonBall()
70+
guard let cannonBall = cannonBall else {
71+
return
72+
}
73+
print(cannonBall)
6974
if numOfOrangePegs == 0 {
7075
gameLogicDelegate.gameWin()
7176
}
@@ -76,9 +81,13 @@ class SLGameEngine {
7681
return
7782
}
7883
let middleOfTopScreen = CGPoint(x: 400, y: 50)
79-
let cannonBall = Peg(color: PegColor.cannonPeg, center: toPoint(point: middleOfTopScreen))
84+
let cannonBall = Peg(color: PegState.cannonPeg, center: toPoint(point: middleOfTopScreen))
8085
self.cannonBall = cannonBall
81-
86+
guard let cannonBallCheck = self.cannonBall else {
87+
print("failed to add")
88+
return
89+
}
90+
print("cannon ball added \(cannonBallCheck)")
8291
gameLogicDelegate.didAddCannonBall(cannonBall: cannonBall)
8392
self.mostRecentPosition = cannonBall.center
8493
}
@@ -262,14 +271,18 @@ class SLGameEngine {
262271
}
263272

264273
func fireCannonBall(directionOf: Point) {
274+
print("fire cannon ball")
265275
guard let cannonBall = cannonBall else {
276+
print("no cannon ball")
266277
return
267278
}
268279

269280
if mappings[cannonBall] != nil {
281+
print("no cannon ball mapping")
270282
return
271283
}
272284

285+
print("fired cannon ball")
273286
self.numOfCannonBallsLeft -= 1
274287
print(numOfCannonBallsLeft)
275288
var modifiedDirection = directionOf
@@ -287,4 +300,16 @@ class SLGameEngine {
287300
mappings[cannonBall] = cannonBallPhysics
288301
physicsEngine.addCannonBall(cannonBall: cannonBallPhysics)
289302
}
303+
304+
func gameEnd() {
305+
physicsEngine.removeBodies()
306+
mappings = [:]
307+
cannonBall = nil
308+
similarPositionCounter = 0
309+
mostRecentPosition = nil
310+
bucket = nil
311+
numOfCannonBallsLeft = 0
312+
numOfOrangePegs = 0
313+
cannonBallInBucket = false
314+
}
290315
}

‎ps4/peggleclone/SLPhysicsEngine/SLPhysicsCollision.swift‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,21 @@ class SLPhysicsCollision {
4343
// - triangle.position.xCoordinate,
4444
// yDirection: circle.position.yCoordinate
4545
// - triangle.position.yCoordinate)
46-
// let distance = distanceBetween(firstPoint: triangle.position, secondPoint: circle.position)
46+
// let distance = 2 * max(triangle.width / 2, circle.radius)
4747
// let collisionNormalVector = Vector(xDirection: collisionVector.xDirection / (distance),
4848
// yDirection: collisionVector.yDirection / (distance))
4949
// let relativeVelocity = triangle.velocity.subtract(vector: circle.velocity)
5050
// let tempSpeed = (relativeVelocity.xDirection * collisionVector.xDirection +
5151
// relativeVelocity.yDirection * collisionVector.yDirection)
5252
// let speedAfterScale = Double(tempSpeed) * speedScale
5353
// let speedAfterRestitution = speedAfterScale * restitution
54-
// let impulse = 2 * speedAfterRestitution / (triangle.mass + circle.mass)
5554
// if speedAfterRestitution >= 0 {
5655
// triangle.forces.append(collisionNormalVector
57-
// .multiplyWithScalar(scalar: -1 * impulse * circle.mass))
56+
// .multiplyWithScalar(scalar: -1 * speedAfterRestitution))
5857
// circle.forces.append(collisionNormalVector
59-
// .multiplyWithScalar(scalar: impulse * triangle.mass))
58+
// .multiplyWithScalar(scalar: speedAfterRestitution))
6059
// }
61-
let restitution = 1.25
60+
let restitution = 2.5
6261

6362
if circleIntersectEdge(circle: circle, vertexOne: triangle.vertexOne, vertexTwo: triangle.vertexTwo) {
6463
// rebound counter clockwise
@@ -146,15 +145,14 @@ class SLPhysicsCollision {
146145
- firstCircle.position.xCoordinate,
147146
yDirection: secondCircle.position.yCoordinate
148147
- firstCircle.position.yCoordinate)
149-
let distance = 2 * max(firstCircle.radius, secondCircle.radius) // distanceBetween(firstPoint: firstCircle.position, secondPoint: secondCircle.position)
148+
let distance = 2 * max(firstCircle.radius, secondCircle.radius)
150149
let collisionNormalVector = Vector(xDirection: collisionVector.xDirection / (distance),
151150
yDirection: collisionVector.yDirection / (distance))
152151
let relativeVelocity = firstCircle.velocity.subtract(vector: secondCircle.velocity)
153152
let tempSpeed = (relativeVelocity.xDirection * collisionVector.xDirection +
154153
relativeVelocity.yDirection * collisionVector.yDirection)
155154
let speedAfterScale = Double(tempSpeed) * speedScale
156155
let speedAfterRestitution = speedAfterScale * restitution
157-
let impulse = 2 * speedAfterRestitution / (firstCircle.mass + secondCircle.mass)
158156
if speedAfterRestitution >= 0 {
159157
firstCircle.forces.append(collisionNormalVector
160158
.multiplyWithScalar(scalar: -1 * speedAfterRestitution))

0 commit comments

Comments
 (0)