NEED HELP ASP
GAMEMANAGER.CS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
[RequireComponent(typeof(PhotonView))] // Ensure that the GameObject has a PhotonView component
public class GameManager : MonoBehaviourPunCallbacks
{
public static GameManager Instance;
private bool gameStarted = false;
private void Awake()
{
if (Instance)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
private void Start()
{
Debug.Log("GameManager Start called.");
// Check if connected and ready before initializing the game
if (PhotonNetwork.IsConnectedAndReady)
{
if (PhotonNetwork.IsMasterClient)
{
// Wait for all players to join before assigning teams
StartCoroutine(WaitForPlayersAndAssignTeams());
}
}
else
{
Debug.LogWarning("Photon not connected and ready. Game initialization cannot proceed.");
}
}
private IEnumerator WaitForPlayersAndAssignTeams()
{
// Wait for a short duration to ensure all players have joined
yield return new WaitForSeconds(2f);
// Assign teams after waiting
AssignTeams();
SpawnPlayers();
// Check if there is at least one seeker
if (!IsSeekerInGame())
{
// If no seeker, stop the game or take any appropriate action
Debug.LogWarning("No seeker found. The game won't start.");
// You can implement your own logic here, such as ending the game or restarting the process.
}
else
{
// Start the game
gameStarted = true;
Debug.Log("Game started!");
}
}
private void AssignTeams()
{
Photon.Realtime.Player[] players = PhotonNetwork.PlayerList;
// Shuffle the player array to randomize the order
ShuffleArray(players);
// Assign the first player as the seeker and others as hiders
for (int i = 0; i < players.Length; i++)
{
bool isSeeker = (i == 0); // The first player is the seeker
photonView.RPC("SetPlayerTeam", RpcTarget.All, players[i], isSeeker);
}
}
private void SpawnPlayers()
{
if (SpawnManager.Instance != null)
{
Photon.Realtime.Player[] players = PhotonNetwork.PlayerList;
foreach (Photon.Realtime.Player player in players)
{
bool isSeeker = (bool)player.CustomProperties["IsSeeker"];
if (isSeeker)
{
SpawnManager.Instance.SpawnSeeker(); // Modify this line according to your SpawnManager script
}
else
{
SpawnManager.Instance.SpawnHider(); // Modify this line according to your SpawnManager script
}
}
}
else
{
Debug.LogError("SpawnManager.Instance is null. Make sure SpawnManager is properly initialized.");
}
}
// Helper method to shuffle an array
private void ShuffleArray<T>(T[] array)
{
int n = array.Length;
for (int i = 0; i < n; i++)
{
int r = i + Random.Range(0, n - i);
T temp = array[r];
array[r] = array[i];
array[i] = temp;
}
}
public static bool IsSeeker()
{
object isSeeker;
if (PhotonNetwork.LocalPlayer.CustomProperties.TryGetValue("IsSeeker", out isSeeker))
{
return (bool)isSeeker;
}
return false;
}
private bool IsSeekerInGame()
{
Photon.Realtime.Player[] players = PhotonNetwork.PlayerList;
foreach (Photon.Realtime.Player player in players)
{
bool isSeeker = (bool)player.CustomProperties["IsSeeker"];
if (isSeeker)
{
return true; // At least one seeker found
}
}
return false; // No seeker found
}
[PunRPC]
private void SetPlayerTeam(Photon.Realtime.Player player, bool isSeeker)
{
player.SetCustomProperties(new ExitGames.Client.Photon.Hashtable { { "IsSeeker", isSeeker } });
}
}
SPAWNMANAGER.CS
using Photon.Pun;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
public static SpawnManager Instance;
private List<SpawnPoint> spawnPoints = new List<SpawnPoint>();
void Awake()
{
Debug.Log("SpawnManager Awake called."); // Add this line
if (Instance)
{
Destroy(gameObject);
return;
}
Instance = this;
// Find all spawn points in the scene
spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint")
.Select(spawnPoint => spawnPoint.GetComponent<SpawnPoint>())
.ToList();
}
public GameObject SpawnSeeker()
{
// Get a random unoccupied spawn point
SpawnPoint randomSpawnPoint = GetRandomSpawnPoint();
if (randomSpawnPoint != null)
{
// Mark the spawn point as occupied
randomSpawnPoint.IsOccupied = true;
// Instantiate the seeker prefab at the chosen spawn point
return PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "SeekerPrefab"), randomSpawnPoint.transform.position, Quaternion.identity);
}
// If no available spawn points are found, return null
return null;
}
public GameObject SpawnHider()
{
// Get a random unoccupied spawn point
SpawnPoint randomSpawnPoint = GetRandomSpawnPoint();
if (randomSpawnPoint != null)
{
// Mark the spawn point as occupied
randomSpawnPoint.IsOccupied = true;
// Instantiate the hider prefab at the chosen spawn point
return PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "HiderPrefab"), randomSpawnPoint.transform.position, Quaternion.identity);
}
// If no available spawn points are found, return null
return null;
}
private SpawnPoint GetRandomSpawnPoint()
{
// Filter unoccupied spawn points
List<SpawnPoint> availableSpawnPoints = spawnPoints.Where(spawnPoint => !spawnPoint.IsOccupied).ToList();
if (availableSpawnPoints.Count > 0)
{
// Get a random unoccupied spawn point
int randomIndex = Random.Range(0, availableSpawnPoints.Count);
return availableSpawnPoints[randomIndex];
}
// If no available spawn points are found, return null
return null;
}
private void ReleaseSpawnPoint(SpawnPoint spawnPoint)
{
if (spawnPoint != null)
{
spawnPoint.ResetSpawnPoint();
}
}
}