diff --git a/Assets/Game1.unity b/Assets/Game1.unity index c50531b..731bdab 100644 Binary files a/Assets/Game1.unity and b/Assets/Game1.unity differ diff --git a/Assets/Levels.cs b/Assets/Levels.cs index ecd62fe..d1fe60f 100644 --- a/Assets/Levels.cs +++ b/Assets/Levels.cs @@ -17,6 +17,8 @@ void Start () void NextLevel() { + Debug.Log("Current level " + level); + lastCount = RockSphere.count; if (RockSphere.count == 0) { @@ -26,6 +28,7 @@ void NextLevel() level = 1; } + Debug.Log("Next level " + level); SceneManager.LoadScene(level); } } @@ -33,6 +36,7 @@ void NextLevel() [ClientRpc] void RpcNextLevel() { + Debug.Log("RpcNextLevel " + level); NextLevel(); } @@ -47,8 +51,11 @@ void Update () { if (RockSphere.count != lastCount) { + lastCount = RockSphere.count; + Debug.Log("Rocks " + lastCount + " " + Random.Range(0.0f, 1.0f).ToString()); if (RockSphere.count == 0) { + Debug.Log("Rocks " + lastCount); RpcNextLevel(); } } diff --git a/Assets/RockField1.prefab b/Assets/RockField1.prefab index d48bef1..3c3d6ab 100644 Binary files a/Assets/RockField1.prefab and b/Assets/RockField1.prefab differ diff --git a/Assets/RockField2.prefab b/Assets/RockField2.prefab index c2589a0..5825879 100644 Binary files a/Assets/RockField2.prefab and b/Assets/RockField2.prefab differ diff --git a/Assets/RockSphere.cs b/Assets/RockSphere.cs index 64dad71..ba69c63 100644 --- a/Assets/RockSphere.cs +++ b/Assets/RockSphere.cs @@ -86,7 +86,8 @@ void Start() } count++; - } + Debug.Log("Add Rocks " + count + " " + Random.Range(0.0f, 1.0f).ToString()); + } [ServerCallback] void OnTriggerEnter(Collider other) @@ -99,6 +100,7 @@ void OnTriggerEnter(Collider other) destroyed = true; NetworkServer.Destroy(transform.gameObject); count--; + Debug.Log("Destroy Rocks " + count + " " + Random.Range(0.0f, 1.0f).ToString()); if (rockSpherePrefab) { diff --git a/Assets/RockSphere1.prefab b/Assets/RockSphere1.prefab index 93e420c..705b1a4 100644 Binary files a/Assets/RockSphere1.prefab and b/Assets/RockSphere1.prefab differ diff --git a/Assets/RocketSphere.cs b/Assets/RocketSphere.cs index 9138079..8b4458c 100644 --- a/Assets/RocketSphere.cs +++ b/Assets/RocketSphere.cs @@ -41,9 +41,13 @@ public class RocketSphere : NetworkBehaviour AudioSource hyperspaceSound; AudioSource engineSound; public GameObject explosionPrefab; - public GameObject spawnPrefab; Rigidbody rb; [SyncVar] Color RocketColor = Color.white; + [SyncVar] float hue = 0.0f; + [SyncVar] bool visible = false; + // [SyncVar] Vector3 posSave = Vector3.zero; + // [SyncVar] Quaternion rotSave = Quaternion.identity; + [SyncVar] Quaternion rot2Save = Quaternion.identity; // Unity makes a clone of the Material every time GetComponent().material is used. // Cache it here and Destroy it in OnDestroy to prevent a memory leak. @@ -60,30 +64,38 @@ public override void OnStartServer() base.OnStartServer(); // create a random color for each player as they are created on the server - RocketColor = new Color(Random.Range(0.3f, 1.0f), Random.Range(0.3f, 1.0f), Random.Range(0.3f, 1.0f)); + //RocketColor = new Color(Random.Range(0.3f, 1.0f), Random.Range(0.3f, 1.0f), Random.Range(0.3f, 1.0f)); + + float hue = Random.Range(0.0f, 1.0f); + + RocketSphere[] players = FindObjectsOfType(); + + for (int i = 0; i < players.Length; i++) + { + // 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) + { + // generate a new hue if it is too close to this other players color + hue = Random.Range(0.0f, 1.0f); + + // restart check with new hue until we find a hue that is different enough from all the other players. + i = 0; + } + } + + // rocket color is now set for this player until it is destroyed + RocketColor = Color.HSVToRGB(hue, 1.0f, 1.0f); } // Use this for initialization void Start() { - // get the current radius from position - //float radius = transform.position.magnitude; - //Vector3 pos = transform.position; - - //reset the position back to the center - transform.position = Vector3.zero; - transform.rotation = Quaternion.identity; - - // Move rocket child gameobject out to radius - Vector3 pos = Vector3.forward * radius; - Quaternion rot = Quaternion.FromToRotation(Vector3.forward, pos); - // Find the rocket child object rocket = transform.Find("Rocket").gameObject; - rocket.transform.position = pos; - rocket.transform.rotation = rot; - // find the engine child object and attach the particle generator GameObject engine = transform.Find("Rocket").Find("Engine").gameObject; engineParticleSystem = engine.GetComponent(); @@ -95,6 +107,14 @@ void Start() // find the rb so we can apply torque during the Update() rb = transform.GetComponent(); + //reset the position back to the center + transform.position = Vector3.zero; + transform.rotation = Quaternion.identity; + + // Move rocket child gameobject out to radius + rocket.transform.position = Vector3.forward * radius; + rocket.transform.rotation = Quaternion.FromToRotation(Vector3.forward, rocket.transform.position); ; + // Keep the player objects through level changes DontDestroyOnLoad(this); @@ -107,8 +127,20 @@ void Start() cachedMaterial = rocket.GetComponent().material; cachedMaterial.color = RocketColor; - // spawn the ship on the server - CmdSpawnShipDelay(); + if (isLocalPlayer) + { + // spawn the ship on the client in the direction the player is currently looking after 5 seconds + // which will enable the ship and then enable it on the other player clients through the server + Invoke("SpawnShip", 5); + } + else + { + // not our rocket so we need to update the state based on the server sync var state + rocket.SetActive(visible); + //rocket.transform.position = posSave; + //rocket.transform.rotation = rotSave; + transform.rotation = rot2Save; + } } private void OnLevelWasLoaded(int level) @@ -130,17 +162,21 @@ void MySetActive(bool active, Quaternion rotation) // make the rocket invisible rocket.SetActive(false); + visible = false; } else { - // put initial rotation of new spacecraft at current camera rotation so player can orient spawn position to a safe spot + // put initial rotation of new spacecraft at current camera rotation so player can orient spawn position to a safe spot, + // save it so new players know where to create the ship, if the player doesn't move transform.rotation = rotation; + rot2Save = rotation; // restart rb movement rb.isKinematic = false; // make the rocket visible again rocket.SetActive(true); + visible = true; // play the hyperspace sound on enable hyperspaceSound.Play(); @@ -159,12 +195,6 @@ void CmdMySetActive(bool active, Quaternion rotation) RpcMySetActive(active, rotation); } - [Command] - void CmdSpawnShipDelay() - { - rpcSpawnShipDelay(); - } - [Client] void SpawnShip() { @@ -339,6 +369,12 @@ void CmdHyperspace() void Update() { + // make sure the state matches the server state + if (visible != rocket.activeSelf) + { + rocket.SetActive(visible); + } + // only allow input for local player if (!isLocalPlayer) return; @@ -382,5 +418,11 @@ void Update() { CmdHyperspace(); } + + if (Input.GetButtonDown("Submit")) + { + + } + } } \ No newline at end of file diff --git a/ProjectSettings/GraphicsSettings.asset b/ProjectSettings/GraphicsSettings.asset index 9861984..4794c37 100644 Binary files a/ProjectSettings/GraphicsSettings.asset and b/ProjectSettings/GraphicsSettings.asset differ diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index e2ec525..e5233de 100644 Binary files a/ProjectSettings/ProjectSettings.asset and b/ProjectSettings/ProjectSettings.asset differ