Skip to content

Instantly share code, notes, and snippets.

@arun02139
Created August 23, 2016 05:02
Show Gist options
  • Save arun02139/37cbd35c2c5c58051c3073a595ef1c56 to your computer and use it in GitHub Desktop.
Save arun02139/37cbd35c2c5c58051c3073a595ef1c56 to your computer and use it in GitHub Desktop.
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;
// QUESTION: these double events feel ugly >.<... with this much code, maybe using a
// [ClientRpc] instead of [SyncEvent] would make things read simpler
public delegate void ColorFlipDelegate( int colorIndex );
public class IntUnityEvent : UnityEvent<int> { }
public class NetworkTimeManager : NetworkBehaviour
{
static public NetworkTimeManager i = null;
[SyncEvent]
public event ColorFlipDelegate EventColorFlip; // TODO: make private?
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 OnStartClient ()
{
base.OnStartClient (); // empty stub as of Unity 5.4
EventColorFlip += ClientColorFlip;
}
void OnDestroy ()
{
EventColorFlip -= ClientColorFlip;
}
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) {
// will be null when there are no listeners
if (EventColorFlip != null) {
int newIndex = Random.Range (0, 6);
ServerDebugUI.i.Log (string.Format ("NetworkTimeManager.Update: invoking EventColorFlip ({0})", newIndex));
EventColorFlip.Invoke (newIndex); // invoke the phase change event (SyncEvent)
}
}
}
// 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)
{
if(UEventColorFlip != null)
{
UEventColorFlip.Invoke(colorIndex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment