mirror of
https://github.com/Leahnaya/TheKingsRace.git
synced 2026-05-10 21:15:48 -05:00
105 lines
4.3 KiB
C#
105 lines
4.3 KiB
C#
using MLAPI;
|
|
using MLAPI.Messaging;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Pitfall : NetworkBehaviour
|
|
{
|
|
[SerializeField] private Vector3 RespawnPoint; //Where player will respawn to, set in GUI
|
|
|
|
public Transform runnerPrefab;
|
|
|
|
private GameObject _runner;
|
|
|
|
private bool cooldown = false;
|
|
|
|
private void OnTriggerEnter(Collider other) {
|
|
if (cooldown == false && other.transform.parent.gameObject.tag == "Player" && other.GetType() != typeof(BoxCollider) && other.gameObject.transform.parent.gameObject.GetComponent<NetworkObject>().OwnerClientId == NetworkManager.Singleton.LocalClientId) {
|
|
RespawnPlayerServerRPC(other.gameObject.transform.parent.gameObject.GetComponent<NetworkObject>().OwnerClientId);
|
|
cooldown = true;
|
|
StartCoroutine(CoolItDown());
|
|
}
|
|
}
|
|
|
|
IEnumerator CoolItDown()
|
|
{
|
|
yield return new WaitForSecondsRealtime(1f);
|
|
cooldown = false;
|
|
}
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
private void RespawnPlayerServerRPC(ulong clientID, ServerRpcParams serverRpcParams = default)
|
|
{
|
|
|
|
// Get all players in the scene
|
|
GameObject[] playableCharacters = GameObject.FindGameObjectsWithTag("Player");
|
|
|
|
Debug.LogError("# of players: " + playableCharacters.Length);
|
|
|
|
// Find our player first
|
|
foreach (GameObject character in playableCharacters)
|
|
{
|
|
if (character.GetComponent<NetworkObject>().OwnerClientId == clientID)
|
|
{
|
|
// Player found
|
|
|
|
// Despawn them
|
|
character.GetComponent<NetworkObject>().Despawn(true);
|
|
}
|
|
}
|
|
|
|
// Spawn the player
|
|
if (ServerGameNetPortal.Instance.clientIdToGuid.TryGetValue(clientID, out string clientGuid))
|
|
{
|
|
if (ServerGameNetPortal.Instance.clientData.TryGetValue(clientGuid, out PlayerData playerData))
|
|
{
|
|
// Spawn as player
|
|
_runner = Instantiate(runnerPrefab, RespawnPoint, Quaternion.Euler(0, -90, 0)).gameObject;
|
|
//Recreate Inventory
|
|
_runner.GetComponentInChildren<PlayerInventory>().UpdateEquips(playerData.pInv.NetworkItemList, this.gameObject.GetComponent<InventoryManager>().ItemDict);
|
|
_runner.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientID, null, true);
|
|
|
|
// Turn on camera if the player is the host
|
|
if (NetworkManager.Singleton.LocalClientId == clientID) {
|
|
GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent<Camera>().enabled = true;
|
|
GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent<AudioListener>().enabled = true;
|
|
GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent<PlayerCam>().enabled = true;
|
|
|
|
_runner.GetComponentInChildren<PlayerMovement>().enabled = true;
|
|
} else {
|
|
ClientRpcParams clientRpcParams = new ClientRpcParams
|
|
{
|
|
Send = new ClientRpcSendParams
|
|
{
|
|
TargetClientIds = new ulong[] { clientID }
|
|
}
|
|
};
|
|
// Spawn via RPC on the server
|
|
SpawnPlayerClientRpc(clientID, clientRpcParams);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Spawn in each player
|
|
[ClientRpc]
|
|
public void SpawnPlayerClientRpc(ulong clientId, ClientRpcParams clientRpcParams = default)
|
|
{
|
|
|
|
GameObject[] playableCharacters = GameObject.FindGameObjectsWithTag("Player");
|
|
|
|
foreach (GameObject character in playableCharacters)
|
|
{
|
|
if (character.GetComponent<NetworkObject>().OwnerClientId == clientId)
|
|
{
|
|
GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent<Camera>().enabled = true;
|
|
GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent<AudioListener>().enabled = true;
|
|
GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent<PlayerCam>().enabled = true;
|
|
|
|
character.GetComponentInChildren<PlayerMovement>().enabled = true;
|
|
}
|
|
}
|
|
}
|
|
}
|