Skip to content

Commit

Permalink
Levels now work in multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
plaidpants committed Feb 1, 2022
1 parent 8893574 commit defca77
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 27 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.
48 changes: 33 additions & 15 deletions Assets/NetworkManagerRocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,40 @@ public class NetworkManagerRocket : NetworkManager

private void OnLevelWasLoaded(int level)
{
RockField1 = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "RockField1"));
RockField2 = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "RockField2"));
NetworkServer.Spawn(RockField1);
NetworkServer.Spawn(RockField2);
}

public override void OnStartHost()
{
RockField1 = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "RockField1"));
RockField2 = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "RockField2"));
NetworkServer.Spawn(RockField1);
NetworkServer.Spawn(RockField2);
}

public override void OnStopHost()
{
// destroy Rockfield
if (RockField1 != null)
NetworkServer.Destroy(RockField1);
if (RockField2 != null)
NetworkServer.Destroy(RockField2);
}

public static int level = 1;
public int lastCount = 0;

static bool preparing = false;

void SwitchScenes()
{
RocketSphere[] players = FindObjectsOfType<RocketSphere>();

for (int i = 0; i < players.Length; i++)
{
// deactivate all the ships on all clients
players[i].RpcMySetActive(false, Quaternion.identity, true);

// spawn ships, on all clients
players[i].rpcSpawnShipDelay();
}

// switch to the next level
ServerChangeScene("Game" + level);

preparing = false;
}

void Update()
{
if (RockSphere.count != lastCount)
Expand All @@ -51,9 +59,19 @@ void Update()
level = 1;
}

ServerChangeScene("Game" + level);
preparing = true;

// stop the music at the end of the level
Camera.main.transform.gameObject.GetComponent<AudioSource>().Stop();

// wait 10 seconds before switching to the next level
Invoke("SwitchScenes", 10);
}
}
}

if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
}
Binary file modified Assets/RockField1.prefab
Binary file not shown.
Binary file modified Assets/RockField2.prefab
Binary file not shown.
Binary file modified Assets/RockSphere1.prefab
Binary file not shown.
40 changes: 28 additions & 12 deletions Assets/RocketSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,21 @@ public override void OnStartServer()

RocketSphere[] players = FindObjectsOfType<RocketSphere>();

for (int i = 0; i < players.Length; i++)
int count = 0;
for (int i = 1; i < players.Length; i++)
{
count++;
if (count > 10)
{
Debug.Log("Give up finding a better color");
break; // give up
}

// check for difference between player and chosen hue,
// difference check must be small enough that we have enough colors for all players.
// if max player count is increased from 4, this value should be reassessed
// this could be an infinte check otherwise, might want a failsafe escape
if (Mathf.Abs(hue - players[i].hue) < 0.1f)
if ((Mathf.Abs(hue - players[i].hue) < 0.15f) && (players[i] != this))
{
// generate a new hue if it is too close to this other players color
hue = Random.Range(0.0f, 1.0f);
Expand All @@ -86,7 +94,7 @@ public override void OnStartServer()
}
}

// rocket color is now set for this player until it is destroyed
// rocket color is now set for this player until it is destroyed or disconnects
RocketColor = Color.HSVToRGB(hue, 1.0f, 1.0f);
}

Expand Down Expand Up @@ -150,7 +158,7 @@ private void OnLevelWasLoaded(int level)
Camera.main.GetComponent<CameraFollowRocket>().player = transform.gameObject.transform.Find("Rocket").gameObject.transform;
}

void MySetActive(bool active, Quaternion rotation)
void MySetActive(bool active, Quaternion rotation, bool playhyperspace)
{
if (active == false)
{
Expand Down Expand Up @@ -178,21 +186,25 @@ void MySetActive(bool active, Quaternion rotation)
rocket.SetActive(true);
visible = true;

// play the hyperspace sound on enable
}

if (playhyperspace == true)
{
// play the hyperspace sound
hyperspaceSound.Play();
}
}

[ClientRpc]
void RpcMySetActive(bool active, Quaternion rotation)
public void RpcMySetActive(bool active, Quaternion rotation, bool playhyperspace)
{
MySetActive(active, rotation);
MySetActive(active, rotation, playhyperspace);
}

[Command]
void CmdMySetActive(bool active, Quaternion rotation)
void CmdMySetActive(bool active, Quaternion rotation, bool playhyperspace)
{
RpcMySetActive(active, rotation);
RpcMySetActive(active, rotation, playhyperspace);
}

[Client]
Expand All @@ -201,14 +213,18 @@ void SpawnShip()
if (!isLocalPlayer) return;

// re-enable this rocket on all clients through server at the direction the user is looking at the moment
CmdMySetActive(true, Camera.main.transform.rotation);
CmdMySetActive(true, Camera.main.transform.rotation, true);
isSpawning = false;
}

bool isSpawning;

[ClientRpc]
void rpcSpawnShipDelay()
public void rpcSpawnShipDelay()
{
if (!isLocalPlayer) return;

isSpawning = true;
Invoke("SpawnShip", 5);
}

Expand All @@ -224,7 +240,7 @@ void OnTriggerEnter(Collider other)
NetworkServer.Spawn(explosion);

// deactivate the ship on all clients
RpcMySetActive(false, Quaternion.identity);
RpcMySetActive(false, Quaternion.identity, false);

// spawn a new ship, do this on the local player client and it will re-enable on all clients
rpcSpawnShipDelay();
Expand Down
Binary file modified ProjectSettings/EditorBuildSettings.asset
Binary file not shown.
Binary file modified ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file modified ProjectSettings/ProjectSettings.asset
Binary file not shown.

0 comments on commit defca77

Please sign in to comment.