Skip to content

Commit

Permalink
Move MusicHandler to separate network object, important move rpc call…
Browse files Browse the repository at this point in the history
…s before destroy call or they don't happen on the clients. Argh! tough bug to find out why my rpc were not happening.
  • Loading branch information
plaidpants committed Feb 6, 2022
1 parent 657e70b commit b289bc1
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 13 deletions.
Binary file modified Assets/Game1.unity
Binary file not shown.
Binary file modified Assets/Game2.unity
Binary file not shown.
Binary file modified Assets/Game3.unity
Binary file not shown.
Binary file modified Assets/Game4.unity
Binary file not shown.
Binary file modified Assets/Game5.unity
Binary file not shown.
Binary file modified Assets/Game6.unity
Binary file not shown.
11 changes: 9 additions & 2 deletions Assets/MusicHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class MusicHandler : MonoBehaviour
public class MusicHandler : NetworkBehaviour
{
public int totalNumberOfRocksInLevel = 0;

Expand All @@ -18,9 +19,10 @@ public class MusicHandler : MonoBehaviour
public int musicProgression = 0;
bool outroQueued = false;

bool initialized = false;

// Start is called before the first frame update
void Start()
public override void OnStartClient()
{
musicLoops = new AudioSource[musicClipLoops.Length];

Expand All @@ -46,11 +48,16 @@ void Start()
// queue the first loop playing and looping after the intro
musicLoops[0].loop = true;
musicLoops[0].PlayScheduled(AudioSettings.dspTime + introMusic.clip.length);

initialized = true;
}

// Update is called once per frame
void Update()
{
if (!initialized)
return;

//Debug.Log("Rocks " + RockSphere.currentRocks + " total " + RockSphere.totalRocks + " destoyed " + RockSphere.destroyedRocks);

// are we done shooting rocks and have not started the outro music
Expand Down
6 changes: 4 additions & 2 deletions Assets/NetworkManagerRocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ void NextLevel()
level = 1;
}

MusicHandler mh = FindObjectOfType<MusicHandler>();

// wait for outro music + last music loop + 5 seconds before switching to the next level
Invoke("SwitchScenes", Camera.main.transform.gameObject.GetComponent<MusicHandler>().outroMusicClip.length +
Camera.main.transform.gameObject.GetComponent<MusicHandler>().musicClipLoops[Camera.main.transform.gameObject.GetComponent<MusicHandler>().musicClipLoops.Length - 1].length
Invoke("SwitchScenes", mh.outroMusicClip.length +
mh.musicClipLoops[mh.musicClipLoops.Length - 1].length
+ 5);
}

Expand Down
19 changes: 10 additions & 9 deletions Assets/RockSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public override void OnStartServer()
totalRocks++;
currentRocks++;

Debug.Log("current rocks " + currentRocks + " Rocks " + currentRocks + " total " + totalRocks + " destoyed " + destroyedRocks);
Debug.Log("OnStartServer current rocks " + currentRocks + " Rocks " + currentRocks + " total " + totalRocks + " destoyed " + destroyedRocks);
rpcSetRockStats(currentRocks, totalRocks, destroyedRocks);
}

Expand All @@ -97,12 +97,18 @@ void OnTriggerEnter(Collider other)
GameObject explosion = Instantiate(explosionPrefab, rock.transform.position, Quaternion.identity) as GameObject;
NetworkServer.Spawn(explosion);

destroyed = true;
NetworkServer.Destroy(transform.gameObject);

destroyedRocks++;
currentRocks--;

Debug.Log("OntriggerEnter Rocks " + currentRocks + " total " + totalRocks + " destoyed " + destroyedRocks);

// let all the clients know the current rock counts for music
// IMPORTANT!!!!, this must be called before the detroy or the rpc will never go out.
rpcSetRockStats(currentRocks, totalRocks, destroyedRocks);

destroyed = true;
NetworkServer.Destroy(transform.gameObject);

if (rockSpherePrefab)
{
for (int i = 0; i < pieces; i++)
Expand All @@ -111,11 +117,6 @@ void OnTriggerEnter(Collider other)
NetworkServer.Spawn(newRock);
}
}

Debug.Log("Rocks " + currentRocks + " total " + totalRocks + " destoyed " + destroyedRocks);

// let all the clients know the current rock counts for music
rpcSetRockStats(currentRocks, totalRocks, destroyedRocks);
}
}

Expand Down
Binary file modified ProjectSettings/GraphicsSettings.asset
Binary file not shown.

0 comments on commit b289bc1

Please sign in to comment.