Skip to content

Commit 2f31f3a

Browse files
committed
Add music
1 parent b117286 commit 2f31f3a

File tree

9 files changed

+81
-2
lines changed

9 files changed

+81
-2
lines changed

‎ps4/peggleclone.xcodeproj/project.pbxproj‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
6882BA8F27CAA31A009F3DAD /* GameEngineManager+GameLogicDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6882BA8E27CAA31A009F3DAD /* GameEngineManager+GameLogicDelegate.swift */; };
4848
6882BA9127CAA9EE009F3DAD /* CannonBallMagazineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6882BA9027CAA9EE009F3DAD /* CannonBallMagazineView.swift */; };
4949
6882BA9327CB2650009F3DAD /* TimerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6882BA9227CB264F009F3DAD /* TimerView.swift */; };
50+
6882BA9527CB4293009F3DAD /* MusicManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6882BA9427CB4293009F3DAD /* MusicManager.swift */; };
51+
6882BA9727CB467D009F3DAD /* background-music.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = 6882BA9627CB467D009F3DAD /* background-music.mp3 */; };
52+
6882BA9A27CB483D009F3DAD /* ball-bounce.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = 6882BA9927CB483C009F3DAD /* ball-bounce.mp3 */; };
53+
6882BA9C27CB490E009F3DAD /* ball-bounce.wav in Resources */ = {isa = PBXBuildFile; fileRef = 6882BA9B27CB490E009F3DAD /* ball-bounce.wav */; };
5054
68861F0627C92DB500D2AEF7 /* SLPhysicsTriangle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68861F0527C92DB500D2AEF7 /* SLPhysicsTriangle.swift */; };
5155
689A4A0027A2CD0D00A2E4E6 /* Level.swift in Sources */ = {isa = PBXBuildFile; fileRef = 689A49FF27A2CD0D00A2E4E6 /* Level.swift */; };
5256
689A4A0227A2CD7C00A2E4E6 /* AllLevelsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 689A4A0127A2CD7C00A2E4E6 /* AllLevelsManager.swift */; };
@@ -109,6 +113,10 @@
109113
6882BA8E27CAA31A009F3DAD /* GameEngineManager+GameLogicDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GameEngineManager+GameLogicDelegate.swift"; sourceTree = "<group>"; };
110114
6882BA9027CAA9EE009F3DAD /* CannonBallMagazineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CannonBallMagazineView.swift; sourceTree = "<group>"; };
111115
6882BA9227CB264F009F3DAD /* TimerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerView.swift; sourceTree = "<group>"; };
116+
6882BA9427CB4293009F3DAD /* MusicManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MusicManager.swift; sourceTree = "<group>"; };
117+
6882BA9627CB467D009F3DAD /* background-music.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = "background-music.mp3"; sourceTree = "<group>"; };
118+
6882BA9927CB483C009F3DAD /* ball-bounce.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = "ball-bounce.mp3"; sourceTree = "<group>"; };
119+
6882BA9B27CB490E009F3DAD /* ball-bounce.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = "ball-bounce.wav"; sourceTree = "<group>"; };
112120
68861F0527C92DB500D2AEF7 /* SLPhysicsTriangle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SLPhysicsTriangle.swift; sourceTree = "<group>"; };
113121
689A49FF27A2CD0D00A2E4E6 /* Level.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Level.swift; sourceTree = "<group>"; };
114122
689A4A0127A2CD7C00A2E4E6 /* AllLevelsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AllLevelsManager.swift; sourceTree = "<group>"; };
@@ -239,6 +247,17 @@
239247
path = StartGame;
240248
sourceTree = "<group>";
241249
};
250+
6882BA9827CB46F3009F3DAD /* Music */ = {
251+
isa = PBXGroup;
252+
children = (
253+
6882BA9B27CB490E009F3DAD /* ball-bounce.wav */,
254+
6882BA9927CB483C009F3DAD /* ball-bounce.mp3 */,
255+
6882BA9627CB467D009F3DAD /* background-music.mp3 */,
256+
6882BA9427CB4293009F3DAD /* MusicManager.swift */,
257+
);
258+
path = Music;
259+
sourceTree = "<group>";
260+
};
242261
689A4A0727A47FC800A2E4E6 /* Data */ = {
243262
isa = PBXGroup;
244263
children = (
@@ -289,6 +308,7 @@
289308
68C1B1DE279DCCCC002DF357 /* peggleclone */ = {
290309
isa = PBXGroup;
291310
children = (
311+
6882BA9827CB46F3009F3DAD /* Music */,
292312
68B6B8CF27C540A8004CE9E2 /* Persistance */,
293313
6831831327B4F8ED00732E00 /* SLGameEngine */,
294314
68686A9827B371B000B97394 /* SLPhysicsEngine */,
@@ -371,9 +391,12 @@
371391
isa = PBXResourcesBuildPhase;
372392
buildActionMask = 2147483647;
373393
files = (
394+
6882BA9A27CB483D009F3DAD /* ball-bounce.mp3 in Resources */,
395+
6882BA9727CB467D009F3DAD /* background-music.mp3 in Resources */,
374396
68C1B1E7279DCCD3002DF357 /* Preview Assets.xcassets in Resources */,
375397
68C1B1E4279DCCD3002DF357 /* Assets.xcassets in Resources */,
376398
689A4A0D27A4888600A2E4E6 /* Seed.json in Resources */,
399+
6882BA9C27CB490E009F3DAD /* ball-bounce.wav in Resources */,
377400
);
378401
runOnlyForDeploymentPostprocessing = 0;
379402
};
@@ -445,6 +468,7 @@
445468
68143565279EE77400A16058 /* ButtonsRowView.swift in Sources */,
446469
6834AD2927C76DB300F5A4E3 /* SpookyPegPersistance.swift in Sources */,
447470
68B6B8D727C558FE004CE9E2 /* PointPersistance.swift in Sources */,
471+
6882BA9527CB4293009F3DAD /* MusicManager.swift in Sources */,
448472
6834AD2527C762BD00F5A4E3 /* SpookyPeg.swift in Sources */,
449473
6882BA8B27CAA2C2009F3DAD /* GameLogicDelegate.swift in Sources */,
450474
6882BA8727CA94D4009F3DAD /* RemainingPegsView.swift in Sources */,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// MusicManager.swift
3+
// peggleclone
4+
//
5+
// Created by Stuart Long on 27/2/22.
6+
//
7+
8+
import Foundation
9+
import AVFoundation
10+
11+
class MusicManager {
12+
static let shared = MusicManager()
13+
private var backgroundMusicPlayer: AVAudioPlayer?
14+
private var ballBounceMusicPlayer: AVAudioPlayer?
15+
16+
private init() {
17+
}
18+
19+
func startBackgroundMusic() {
20+
if let bundle = Bundle.main.path(forResource: "background-music", ofType: "mp3") {
21+
let backgroundMusic = NSURL(fileURLWithPath: bundle)
22+
do {
23+
backgroundMusicPlayer = try AVAudioPlayer(contentsOf: backgroundMusic as URL)
24+
guard let audioPlayer = backgroundMusicPlayer else {
25+
return
26+
}
27+
audioPlayer.numberOfLoops = -1
28+
audioPlayer.prepareToPlay()
29+
audioPlayer.play()
30+
} catch {
31+
print(error)
32+
}
33+
}
34+
}
35+
36+
func ballBounceMusic() {
37+
if let bundle = Bundle.main.path(forResource: "ball-bounce", ofType: "wav") {
38+
let ballBounce = NSURL(fileURLWithPath: bundle)
39+
do {
40+
ballBounceMusicPlayer = try AVAudioPlayer(contentsOf: ballBounce as URL)
41+
guard let audioPlayer = ballBounceMusicPlayer else {
42+
return
43+
}
44+
audioPlayer.play()
45+
} catch {
46+
print(error)
47+
}
48+
}
49+
}
50+
}
1.96 MB
Binary file not shown.
51 KB
Binary file not shown.
51 KB
Binary file not shown.

‎ps4/peggleclone/SLPhysicsEngine/SLPhysicsWorld.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class SLPhysicsWorld {
7575
let nextBody = physicsBodies[secondIndex]
7676
let canIgnore = currentBody.canIgnore || nextBody.canIgnore
7777
if !canIgnore && currentBody.intersectWith(physicsBody: nextBody) {
78+
MusicManager.shared.ballBounceMusic()
7879
currentBody.setCollided()
7980
nextBody.setCollided()
8081
collisions.append(SLPhysicsCollision(firstBody: currentBody,

‎ps4/peggleclone/ViewModels/GameEngineManager+GameLogicDelegate.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension GameEngineManager: GameLogicDelegate {
4747

4848
func hasTimerEnded() -> Bool {
4949
let result = self.timer <= 0.0
50-
print(result)
50+
// print(result)
5151
return result
5252
}
5353
}

‎ps4/peggleclone/ViewModels/GameEngineManager.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class GameEngineManager: ObservableObject {
1414
@Published var isTimerUp: Bool
1515
@Published var points: Int = 0
1616
@Published var cannonBallAmmo = 0
17-
@Published var timer = 30.0
17+
@Published var timer = 100.0
1818
private var gameEngine: SLGameEngine
1919
var positionOfCannon: Point
2020

‎ps4/peggleclone/Views/ContentView.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct ContentView: View {
1313
@StateObject var keyboardResponder = KeyboardResponder()
1414
@State var gameState = GameState.menu
1515
@State var gameEngineManager: GameEngineManager
16+
var musicManager = MusicManager.shared
1617

1718
var body: some View {
1819
ZStack {
@@ -34,6 +35,9 @@ struct ContentView: View {
3435
.environmentObject(levelManager)
3536
}
3637
}
38+
.onAppear {
39+
musicManager.startBackgroundMusic()
40+
}
3741
}
3842
}
3943

0 commit comments

Comments
 (0)