Introduction:
This update focuses on integrating a server for data management, enabling the upload of match data and tracking scores. Additionally, it implements end-of-match conditions and a scoreboard display to provide players with comprehensive feedback on their performance.
ScoreManager:
Initialization: Initializes scores from room properties, retrieving attack and defense scores from the server if available.
Update Score UI: Updates the UI elements in the match manager to reflect the current scores.
Update Score to Room: Updates the server's room properties with the latest scores to synchronize them across all clients.
Check Match Over: Determines if the match is over based on the scores, triggering appropriate actions such as loading the next level or displaying a game-over screen.
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class ScoreManager : MonoBehaviour
{
public MatchManager matchManager;
public int TeamCount, EnemyCount;
private const string AttackScoreKey = "AttackScore";
private const string DefenseScoreKey = "DefenseScore";
public int attackScore = 0;
public int defenseScore = 0;
public PhotonView PV;
// Start is called before the first frame update
void Start()
{
PV = GetComponent<PhotonView>();
matchManager = FindObjectOfType<MatchManager>();
InitializeScoresFromRoomProperties();
}
// Update is called once per frame
void Update()
{
}
private void InitializeScoresFromRoomProperties()
{
if (PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue(AttackScoreKey, out object attackScoreObj))
{
attackScore = (int)attackScoreObj;
Debug.Log("Attack Score: " + attackScore);
}
if (PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue(DefenseScoreKey, out object defenseScoreObj))
{
defenseScore = (int)defenseScoreObj;
Debug.Log("Defense Score: " + defenseScore);
}
UpdateScoreUI();
}
public void UpdateScoreUI()
{
matchManager.UIScoreUpdate();
}
public void UpdateScoreToRoom()
{
if (PhotonNetwork.IsMasterClient)
{
ExitGames.Client.Photon.Hashtable roomProps = new ExitGames.Client.Photon.Hashtable();
roomProps[AttackScoreKey] = attackScore;
roomProps[DefenseScoreKey] = defenseScore;
PhotonNetwork.CurrentRoom.SetCustomProperties(roomProps);
Debug.Log("Updated in room custom properties");
}
}
public void CheckMatchOver()
{
if(attackScore >= 1 || defenseScore >= 1)
{
Debug.Log("Game over");
}
else
{
PhotonNetwork.LoadLevel(1);
}
}
[PunRPC]
public void RPC_LoadLevelForAll()
{
PhotonNetwork.LoadLevel(1);
}
}
MatchManager:
End Timer Start: Initiates the end timer when the match phase ends, determining the winning team and updating scores accordingly.
Buy Phase: Manages the buy phase, allowing players to purchase weapons and equipment before the match begins.
Match Phase: Controls the match phase, where players engage in combat and attempt to achieve the match objectives.
Defuse Phase: Handles the defuse phase, enabling players to defuse planted spikes within a specific time frame.
Spectator Mode: Implements functionality for spectating, allowing players to cycle through available cameras for observation.
RPCs: Utilizes remote procedure calls (RPCs) to synchronize game state changes across all clients, such as declaring the winning team and updating UI elements.
Conclusion:
With the integration of server capabilities for data management and score tracking, players can now participate in matches with enhanced feedback on their performance. The end-of-match conditions provide closure to each game session, while the scoreboard display offers a comprehensive overview of the match outcome. This update brings the multiplayer experience closer to completion, paving the way for further refinement and optimization in future iterations.