@@ -11,6 +11,7 @@ import SwiftUI
1111class SLGameEngine {
1212 private var physicsEngine = SLPhysicsWorld ( )
1313 private var mappings : [ PeggleObject : SLPhysicsBody ] = [ : ]
14+ private var trianglesRemovedTemporarily : [ TriangleBlock : SLPhysicsBody ] = [ : ]
1415 private let msPerUpdate = TimeInterval ( 0.016 )
1516 private var lag = 0.0
1617 private var previous : Date ?
@@ -71,12 +72,10 @@ class SLGameEngine {
7172 mappings [ bucket] = bucketPhysicsBody
7273 physicsObjects. append ( bucketPhysicsBody)
7374 physicsEngine. load ( physicsBodies: physicsObjects, canvasDimensions: canvasDimensions)
74- print ( level. peggleObjects)
7575 addCannonBall ( )
76- guard let cannonBall = cannonBall else {
76+ guard cannonBall != nil else {
7777 return
7878 }
79- print ( cannonBall)
8079 if numOfOrangePegs == 0 {
8180 gameLogicDelegate. gameWin ( )
8281 }
@@ -91,11 +90,6 @@ class SLGameEngine {
9190 let cannonBall = Peg ( color: PegState . cannonPeg, center: toPoint ( point: middleOfTopScreen) ,
9291 radius: Peg . pegMinRadiusRatio * canvasDimensions. width)
9392 self . cannonBall = cannonBall
94- guard let cannonBallCheck = self . cannonBall else {
95- print ( " failed to add " )
96- return
97- }
98- print ( " cannon ball added \( cannonBallCheck) " )
9993 gameDisplayDelegate. didAddCannonBall ( cannonBall: cannonBall)
10094 self . mostRecentPosition = cannonBall. center
10195 }
@@ -134,20 +128,19 @@ class SLGameEngine {
134128 render ( cannonBallCount: numberOfCannonBalls)
135129 }
136130
137- private func handleCannonBallMovement( _ cannonBallCount: Int , _ currentCollisions: [ Peg ] ) {
131+ private func handleCannonBallMovement( _ cannonBallCount: Int , _ currentCollisions: [ PeggleObject ] ) {
138132 guard let cannonBall = cannonBall,
139133 let cannonBallPhysicsBody = mappings [ cannonBall] ,
140134 let gameDisplayDelegate = gameDisplayDelegate,
141135 let gameLogicDelegate = gameLogicDelegate else {
142136 return
143137 }
144-
145138 let similarPositionLimit = 75
146139
147140 if cannonBallCount == 0 {
148141 cannonBallPhysicsBody. ignore ( )
149142 mappings. removeValue ( forKey: cannonBall)
150- gameDisplayDelegate. didRemove ( peg : cannonBall)
143+ gameDisplayDelegate. didRemove ( peggleObject : cannonBall)
151144 if cannonBallInBucket {
152145 numOfCannonBallsLeft += 1
153146 gameLogicDelegate. addCannonball ( )
@@ -158,13 +151,17 @@ class SLGameEngine {
158151 } else {
159152 if isCannonBallSamePosition ( ) {
160153 if similarPositionCounter > similarPositionLimit {
161- for peg in currentCollisions {
162- guard let pegInTouch = mappings [ peg ] else {
154+ for peggleObject in currentCollisions {
155+ guard let peggleObjInTouch = mappings [ peggleObject ] else {
163156 continue
164157 }
165- if pegInTouch. hasCollided {
166- pegInTouch. ignore ( )
167- gameDisplayDelegate. didRemove ( peg: peg)
158+ if peggleObjInTouch. hasCollided {
159+ peggleObjInTouch. ignore ( )
160+ gameDisplayDelegate. didRemove ( peggleObject: peggleObject)
161+ if let triangle = peggleObject as? TriangleBlock {
162+ // handle them -> put in an array or whatever
163+ trianglesRemovedTemporarily [ triangle] = peggleObjInTouch
164+ }
168165 }
169166 }
170167 } else {
@@ -206,12 +203,9 @@ class SLGameEngine {
206203 moveCannonBall ( cannonBall, cannonBallPhysicsBody, gameDisplayDelegate)
207204
208205 let collisions = cannonBallPhysicsBody. collisionsWith
209- let currentCollisions = handleCollisions ( collisions,
210- gameDisplayDelegate,
211- gameLogicDelegate,
212- cannonBallCount,
213- cannonBall)
206+ let currentCollisions = handleCollisions ( collisions, cannonBallCount)
214207
208+ checkCollidedTriangles ( )
215209 handleCannonBallMovement ( cannonBallCount, currentCollisions)
216210
217211 if numOfCannonBallsLeft == 0 && cannonBallCount == 0 {
@@ -224,17 +218,38 @@ class SLGameEngine {
224218 }
225219 }
226220
221+ private func checkCollidedTriangles( ) {
222+ for (key, value) in trianglesRemovedTemporarily {
223+ if !touchingCannonBall( value) {
224+ gameDisplayDelegate? . didAdd ( peggleObject: key)
225+ mappings [ key] = value
226+ value. unignore ( )
227+ trianglesRemovedTemporarily. removeValue ( forKey: key)
228+ }
229+ }
230+ }
231+
232+ private func touchingCannonBall( _ physicsBody: SLPhysicsBody ) -> Bool {
233+ guard let cannonBall = cannonBall, let cannonBallMapping = mappings [ cannonBall] else {
234+ return false
235+ }
236+ return cannonBallMapping. intersectWith ( physicsBody: physicsBody)
237+ }
238+
227239 private func moveBucket(
228240 _ bucket: Bucket , _ bucketPhysicsBody: SLPhysicsBody , _ gameDisplayDelegate: GameDisplayDelegate ) {
229241 bucket. center = bucketPhysicsBody. position
230242 gameDisplayDelegate. didMove ( peggleObject: bucket, newLocation: bucketPhysicsBody. position)
231243 }
232244
233- private func handleCollisions(
234- _ collisions: [ SLPhysicsBody ] , _ gameDisplayDelegate: GameDisplayDelegate ,
235- _ gameLogicDelegate: GameLogicDelegate ,
236- _ cannonBallCount: Int , _ cannonBall: Peg ) -> [ Peg ] {
237- var currentCollisions : [ Peg ] = [ ]
245+ private func handleCollisions( _ collisions: [ SLPhysicsBody ] , _ cannonBallCount: Int ) -> [ PeggleObject ] {
246+ guard let gameLogicDelegate = gameLogicDelegate,
247+ let gameDisplayDelegate = gameDisplayDelegate,
248+ let cannonBall = cannonBall else {
249+ return [ ]
250+ }
251+
252+ var currentCollisions : [ PeggleObject ] = [ ]
238253
239254 for (key, value) in mappings where value. hasCollided {
240255 if let peg = key as? Peg {
@@ -246,8 +261,8 @@ class SLGameEngine {
246261 powerUpHandler. handlePowerUp ( powerPeg: peg, mappings: mappings,
247262 cannonBall: cannonBall, gameDisplayDelegate: gameDisplayDelegate)
248263 }
249- if cannonBallCount == 0 {
250- gameDisplayDelegate. didRemove ( peg : peg)
264+ if cannonBallCount == 0 && peg != cannonBall {
265+ gameDisplayDelegate. didRemove ( peggleObject : peg)
251266 mappings. removeValue ( forKey: peg)
252267 if peg. color != cannonBall. color {
253268 gameLogicDelegate. didAddPoints ( peg. points)
@@ -261,6 +276,10 @@ class SLGameEngine {
261276 }
262277 }
263278 }
279+ } else if let triangle = key as? TriangleBlock {
280+ if contains ( arr: collisions, physicsBody: value) {
281+ currentCollisions. append ( triangle)
282+ }
264283 }
265284 }
266285
@@ -320,20 +339,15 @@ class SLGameEngine {
320339 }
321340
322341 func fireCannonBall( directionOf: Point ) -> Bool {
323- print ( " fire cannon ball " )
324342 guard let cannonBall = cannonBall else {
325- print ( " no cannon ball " )
326343 return false
327344 }
328345
329346 if mappings [ cannonBall] != nil {
330- print ( " no cannon ball mapping " )
331347 return false
332348 }
333349
334- print ( " fired cannon ball " )
335350 self . numOfCannonBallsLeft -= 1
336- print ( numOfCannonBallsLeft)
337351 var modifiedDirection = directionOf
338352 if directionOf. yCoordinate < cannonBall. center. yCoordinate {
339353 modifiedDirection = Point ( xCoordinate: directionOf. xCoordinate,
0 commit comments