Skip to content

Commit 3cc731b

Browse files
authored
first pass at MusicPlayer (#137)
* first pass at MusicPlayer * clean up of StartUp scene to not include UI or dynamic light * PR feedback and re-applied scene changes after latest dev merge
1 parent 4f049b0 commit 3cc731b

File tree

12 files changed

+203
-8
lines changed

12 files changed

+203
-8
lines changed

Assets/BossRoom/Prefabs/Character/ImpBoss.prefab

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,20 @@ MonoBehaviour:
1919
m_NetworkHealthState: {fileID: 848206395698055335}
2020
m_BaseHP: {fileID: 11400000, guid: f393c51c2bce455478a0b995b8d4e3dd, type: 2}
2121
m_TransformToTrack: {fileID: 2561745155600296936}
22+
m_VerticalWorldOffset: 0
2223
m_VerticalScreenOffset: -50
24+
--- !u!114 &8073394427373638825
25+
MonoBehaviour:
26+
m_ObjectHideFlags: 0
27+
m_CorrespondingSourceObject: {fileID: 0}
28+
m_PrefabInstance: {fileID: 0}
29+
m_PrefabAsset: {fileID: 0}
30+
m_GameObject: {fileID: 3688950541947916333}
31+
m_Enabled: 1
32+
m_EditorHideFlags: 0
33+
m_Script: {fileID: 11500000, guid: a6b1447f40f499c4a82ca50d40c76051, type: 3}
34+
m_Name:
35+
m_EditorClassIdentifier:
2336
--- !u!1001 &2203142923062114684
2437
PrefabInstance:
2538
m_ObjectHideFlags: 0
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:f0b60321c4c961b6a5db48bf178f613e6764cee4d041ff51ad3fa447eb67192e
3-
size 179653
2+
oid sha256:7809388324e550b23320d6674038d42c653a29d5e915fcf409607190e1efde48
3+
size 180878
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:464c71232b4435ea90aa137c2bb609ea933b8a97b2e95cd9fa46f67b17832783
3-
size 37518
2+
oid sha256:8bfaec7eab23696295a495e54bbab784a26d89ec04a2f6143b30d442be657893
3+
size 38743
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:9098c2eefd072d47d755154288491d3b3a2774f7225c5d6eeb1521758d70ca3d
3-
size 25129
2+
oid sha256:d9cc8bb46d086cc1a1f1671211de0a8f6ce38ce96a9accc5f03ca41b2f836f84
3+
size 20090

Assets/BossRoom/Scripts/Client/Audio.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using UnityEngine;
2+
3+
4+
namespace BossRoom.Client
5+
{
6+
/// <summary>
7+
/// Simple class to restart game theme on main menu load
8+
/// </summary>
9+
public class BossMusicStarter : MonoBehaviour
10+
{
11+
private bool m_Won = false;
12+
13+
void Start()
14+
{
15+
var netState = GetComponent<NetworkCharacterState>();
16+
netState.NetworkLifeState.OnValueChanged += OnLifeStateChanged;
17+
netState.HealthState.HitPoints.OnValueChanged += OnHealthChanged;
18+
}
19+
20+
private void OnLifeStateChanged(LifeState previousValue, LifeState newValue)
21+
{
22+
if (newValue!= LifeState.Alive)
23+
{
24+
// players won! Start victory theme
25+
ClientMusicPlayer.Instance.PlayVictoryMusic();
26+
m_Won = true;
27+
}
28+
}
29+
30+
private void OnHealthChanged(int previousValue, int newValue)
31+
{
32+
// don't do anything if battle is over
33+
if (m_Won) { return; }
34+
35+
// make sure battle music started anytime boss is hurt
36+
if (previousValue>newValue)
37+
{
38+
ClientMusicPlayer.Instance.PlayBossMusic();
39+
}
40+
}
41+
}
42+
}

Assets/BossRoom/Scripts/Client/Audio/BossMusicStarter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.Audio;
5+
6+
namespace BossRoom.Client
7+
{
8+
/// <summary>
9+
/// Music player that handles start of boss battle, victory and restart
10+
/// </summary>
11+
[RequireComponent(typeof(AudioSource))]
12+
public class ClientMusicPlayer : MonoBehaviour
13+
{
14+
[SerializeField]
15+
private AudioClip m_ThemeMusic;
16+
17+
[SerializeField]
18+
private AudioClip m_BossMusic;
19+
20+
[SerializeField]
21+
private AudioClip m_VictoryMusic;
22+
23+
[SerializeField]
24+
private AudioSource m_source;
25+
26+
/// <summary>
27+
/// static accessor for ClientMusicPlayer
28+
/// </summary>
29+
public static ClientMusicPlayer Instance { get; private set; }
30+
31+
void Start()
32+
{
33+
34+
}
35+
36+
public void PlayThemeMusic(bool restart)
37+
{
38+
PlayTrack(m_ThemeMusic, true, restart);
39+
}
40+
41+
public void PlayBossMusic()
42+
{
43+
// this can be caled multiple times - play with restart = false
44+
PlayTrack(m_BossMusic, true, false);
45+
}
46+
47+
public void PlayVictoryMusic()
48+
{
49+
PlayTrack(m_VictoryMusic, false, false);
50+
}
51+
52+
private void PlayTrack(AudioClip clip, bool looping, bool restart)
53+
{
54+
if (m_source.isPlaying)
55+
{
56+
// if we dont want to restart the clip, do nothing if it is playing
57+
if (!restart && m_source.clip==clip) { return; }
58+
m_source.Stop();
59+
}
60+
m_source.clip = clip;
61+
m_source.loop = looping;
62+
m_source.time = 0;
63+
m_source.Play();
64+
}
65+
66+
private void Awake()
67+
{
68+
m_source = GetComponent<AudioSource>();
69+
70+
if (Instance != null)
71+
{
72+
throw new System.Exception("Multiple ClientMuscPlayers!");
73+
}
74+
m_source = GetComponent<AudioSource>();
75+
DontDestroyOnLoad(gameObject);
76+
Instance = this;
77+
}
78+
}
79+
}

Assets/BossRoom/Scripts/Client/Audio/ClientMusicPlayer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using UnityEngine;
2+
3+
4+
namespace BossRoom.Client
5+
{
6+
/// <summary>
7+
/// Simple class to play game theme on scene load
8+
/// </summary>
9+
public class MainMenuMusicStarter : MonoBehaviour
10+
{
11+
// set whether theme should restart if already playing
12+
[SerializeField]
13+
bool m_Restart;
14+
15+
void Start()
16+
{
17+
ClientMusicPlayer.Instance.PlayThemeMusic(m_Restart);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)