Created
August 23, 2016 07:13
-
-
Save arun02139/38b3a7fcca78d5c2186501c37ae91fad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Assertions; | |
using UnityEngine.Networking; | |
using Prototype.NetworkLobby; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System; | |
using System.Reflection; | |
using Random = UnityEngine.Random; | |
using UnityEngine.UI; | |
using UnityEngine.Events; | |
public class NetworkTimeManager : NetworkBehaviour | |
{ | |
static public NetworkTimeManager i = null; | |
public IntUnityEvent UEventColorFlip = new IntUnityEvent(); | |
[SyncVar] | |
public int secondsRemaining; | |
float _timeLeft; // how many msec left in the game | |
TimeSpan _timeSpan; | |
bool _clockRunning = false; | |
void Awake() | |
{ | |
i = this; | |
} | |
public override void OnStartServer () | |
{ | |
// empty stub as of Unity 5.4 (NOTE: called by NetworkIdentity.UNetStaticUpdate – since all | |
// NetworkBehaviours must have a NetworkIdentity component, NetworkIdentity must implement a | |
// static Update function that finds (all?) NetworkBehaviour scripts on it's game object and | |
// makes all the callbacks at the appropriate times) | |
base.OnStartServer (); | |
_timeLeft = Constants.MATCH_LENGTH_SEC; | |
_clockRunning = true; | |
Debug.Log (string.Format("NetworkTimeManager.OnStartServer: ", "")); | |
ServerDebugUI.i.Log (string.Format("NetworkTimeManager.OnStartServer: player count = {0}", | |
NetworkGameManager.i.players.Count)); | |
} | |
[ServerCallback] | |
void Update() | |
{ | |
if (!_clockRunning) | |
return; | |
// server keeps track of time | |
_timeLeft -= Time.unscaledDeltaTime; | |
_timeLeft = _timeLeft < 0f ? 0f : _timeLeft; // QUESTION: necessary safty-check? | |
var sec = Mathf.FloorToInt (_timeLeft); | |
// update secondsRemaining SyncVar | |
if (sec != secondsRemaining) { | |
// QUESTION: if we assign the same value to an int syncvar, will it become marked dirty? | |
secondsRemaining = sec; | |
// update colorIndex SyncVar every 60 sec (test network functionality via the GameTimeUI which will change its color) | |
if (secondsRemaining % 10 == 0) | |
{ | |
int newIndex = Random.Range (0, 6); | |
ServerDebugUI.i.Log (string.Format ("NetworkTimeManager.Update: invoking EventColorFlip ({0})", newIndex)); | |
// do with Rpc call | |
RpcColorFlip(newIndex); | |
} | |
} | |
// no more time updates required; for now, auto-shutdown the network when this happens | |
if (secondsRemaining == 0) | |
{ | |
_clockRunning = false; | |
// TODO, QUESTION: move below call into NetworkGameManager which should manage the entire game life-cycle | |
StartCoroutine(NetworkGameManager.i.ReturnToLobby()); | |
} | |
} | |
void ClientColorFlip(int colorIndex) | |
{ | |
UEventColorFlip.Invoke(colorIndex); | |
} | |
// NOTE: jsut use Rpc calls instead of SyncEvents; per a Unity employee on their forums, | |
// "SyncEvent has turned out to not be very useful" (LINK: http://goo.gl/Jv5KXe) | |
[ClientRpc] | |
void RpcColorFlip(int colorIndex) | |
{ | |
if(UEventColorFlip != null) | |
{ | |
UEventColorFlip.Invoke(colorIndex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment