Skip to content

Commit

Permalink
Levels are now work again, better color differentiation between playe…
Browse files Browse the repository at this point in the history
…rs, initial client connect now properly enables the server player ship, orientation is still not right until the other player moves however. Currently just one rock per level for testing. PC back to windowed mode for debugging instead of screen and video capture.
  • Loading branch information
plaidpants committed Jan 31, 2022
1 parent c63ef79 commit 1851785
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 27 deletions.
Binary file modified Assets/Game1.unity
Binary file not shown.
7 changes: 7 additions & 0 deletions Assets/Levels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ void Start ()

void NextLevel()
{
Debug.Log("Current level " + level);

lastCount = RockSphere.count;
if (RockSphere.count == 0)
{
Expand All @@ -26,13 +28,15 @@ void NextLevel()
level = 1;
}

Debug.Log("Next level " + level);
SceneManager.LoadScene(level);
}
}

[ClientRpc]
void RpcNextLevel()
{
Debug.Log("RpcNextLevel " + level);
NextLevel();
}

Expand All @@ -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();
}
}
Expand Down
Binary file modified Assets/RockField1.prefab
Binary file not shown.
Binary file modified Assets/RockField2.prefab
Binary file not shown.
4 changes: 3 additions & 1 deletion Assets/RockSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ void Start()
}

count++;
}
Debug.Log("Add Rocks " + count + " " + Random.Range(0.0f, 1.0f).ToString());
}

[ServerCallback]
void OnTriggerEnter(Collider other)
Expand All @@ -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)
{
Expand Down
Binary file modified Assets/RockSphere1.prefab
Binary file not shown.
94 changes: 68 additions & 26 deletions Assets/RocketSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Renderer>().material is used.
// Cache it here and Destroy it in OnDestroy to prevent a memory leak.
Expand All @@ -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<RocketSphere>();

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<ParticleSystem>();
Expand All @@ -95,6 +107,14 @@ void Start()
// find the rb so we can apply torque during the Update()
rb = transform.GetComponent<Rigidbody>();

//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);

Expand All @@ -107,8 +127,20 @@ void Start()
cachedMaterial = rocket.GetComponent<Renderer>().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)
Expand All @@ -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();
Expand All @@ -159,12 +195,6 @@ void CmdMySetActive(bool active, Quaternion rotation)
RpcMySetActive(active, rotation);
}

[Command]
void CmdSpawnShipDelay()
{
rpcSpawnShipDelay();
}

[Client]
void SpawnShip()
{
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -382,5 +418,11 @@ void Update()
{
CmdHyperspace();
}

if (Input.GetButtonDown("Submit"))
{

}

}
}
Binary file modified ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file modified ProjectSettings/ProjectSettings.asset
Binary file not shown.

0 comments on commit 1851785

Please sign in to comment.