@@ -10,6 +10,7 @@ namespace Game.Scripts.Network
1010 public class GameNetworkPlayer : NetworkBehaviour
1111 {
1212 public static GameNetworkPlayer LocalPlayer ;
13+ public static int PlayerCount ;
1314
1415 public const float StartHealth = 100 ;
1516
@@ -27,25 +28,25 @@ public class GameNetworkPlayer : NetworkBehaviour
2728 [ SyncVar ( hook = nameof ( OnSyncHealth ) ) ] public float health ;
2829
2930 private NetworkIdentity _identity ;
30- private ArenaUi _arenaUi ;
3131 private Camera _spectatorCamera ;
3232 private HoveringDetails _hoveringDetails ;
3333 private bool _inCarSelection ;
3434
3535 [ SyncVar ] private GameObject _car ;
3636 private int _carIndex ;
3737 private VehiclePhysics _vehiclePhysics ;
38+ private bool _quitting ;
3839
3940 public GameObject Car => _car ;
4041
4142 public void Start ( )
4243 {
43- CmdInitialize ( MainMenu . PlayerName ) ;
44+ PlayerCount ++ ;
4445 _identity = GetComponent < NetworkIdentity > ( ) ;
45- _arenaUi = GameObject . Find ( "ArenaUI" ) . GetComponent < ArenaUi > ( ) ;
4646 // If this is the local player, then the Start means he has just has just connected, so he will pick a car
4747 if ( isLocalPlayer )
4848 {
49+ CmdInitialize ( MainMenu . PlayerName ) ;
4950 LocalPlayer = this ;
5051 _spectatorCamera = GameObject . Find ( "SpectatorCamera" ) . GetComponent < Camera > ( )
5152 ?? throw new NullReferenceException ( $ "Missing SpectatorCamera") ;
@@ -60,10 +61,27 @@ public void Start()
6061 // If the car wasn't spawned yet, then wait for the RPC callback
6162 }
6263
64+ private void OnDestroy ( )
65+ {
66+ if ( ! _quitting && ! isLocalPlayer )
67+ {
68+ DisplayPositiveEvent ( $ "{ playerName } : Disconnected", true ) ;
69+ }
70+
71+ PlayerCount -- ;
72+ }
73+
74+ private void OnApplicationQuit ( )
75+ {
76+ _quitting = true ;
77+ }
78+
6379 [ Command ]
6480 public void CmdInitialize ( string playerName )
6581 {
6682 this . playerName = playerName ;
83+ // RPC doesn't have access to the playerName yet
84+ RpcDisplayPositiveEvent ( $ "{ playerName } : Connected", true ) ;
6785 }
6886
6987 /// <summary>
@@ -79,7 +97,7 @@ public void ChangeCarByLocalPlayer()
7997
8098 _spectatorCamera . gameObject . SetActive ( false ) ;
8199 vehicleCamera . gameObject . SetActive ( false ) ;
82- _arenaUi . DisableUi ( ) ;
100+ ArenaUi . Instance . DisableUi ( ) ;
83101 // Ensure that the car selection scene is never loaded twice
84102 if ( ! _inCarSelection )
85103 {
@@ -164,7 +182,7 @@ public void RpcOnSpawnedCar(GameObject car, bool byNewRound)
164182 health = StartHealth ;
165183 if ( isLocalPlayer )
166184 {
167- _arenaUi . EnableUi ( this ) ;
185+ ArenaUi . Instance . EnableUi ( this ) ;
168186 _spectatorCamera . gameObject . SetActive ( false ) ;
169187 vehicleCamera . gameObject . SetActive ( true ) ;
170188 vehicleCamera . playerCar = car . transform ;
@@ -259,13 +277,13 @@ public void RpcDisplayHealth(float displayHealth)
259277 [ ClientRpc ]
260278 public void RpcDisplayPlayerHitEvent ( string player2 , float health , int score )
261279 {
262- _arenaUi . DisplayPlayerHitEvent ( playerName , player2 , health , score ) ;
280+ ArenaUi . Instance . DisplayPlayerHitEvent ( playerName , player2 , health , score ) ;
263281 }
264282
265283 [ ClientRpc ]
266284 public void RpcDisplayObjectHitEvent ( string by , float hp )
267285 {
268- _arenaUi . DisplayObjectHitEvent ( playerName , by , hp ) ;
286+ ArenaUi . Instance . DisplayObjectHitEvent ( playerName , by , hp ) ;
269287 }
270288
271289 [ ClientRpc ]
@@ -276,7 +294,17 @@ public void RpcDisplayPositiveEvent(string positiveMessage, bool announcement)
276294
277295 public void DisplayPositiveEvent ( string positiveMessage , bool announcement )
278296 {
279- _arenaUi . DisplayPositiveEvent ( ! announcement ? $ "{ playerName } : { positiveMessage } " : positiveMessage ) ;
297+ ArenaUi . Instance . DisplayPositiveEvent ( ! announcement ? $ "{ playerName } : { positiveMessage } " : positiveMessage ) ;
298+ }
299+
300+ [ Command ]
301+ public void CmdRestartServer ( string remoteAdminPassword )
302+ {
303+ Debug . Log ( $ "Attempt of { playerName } to restart server using password { remoteAdminPassword } ") ;
304+ if ( MainMenu . RemoteAdminPassword != null && MainMenu . RemoteAdminPassword . Equals ( remoteAdminPassword ) )
305+ {
306+ GameObject . Find ( "NetworkManager" ) . GetComponent < NetworkManager > ( ) . StopHost ( ) ;
307+ }
280308 }
281309 }
282310}
0 commit comments