Created
April 5, 2017 16:29
Star
You must be signed in to star a gist
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.Events; | |
using UnityEngine.Assertions; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine.Networking; | |
using Prototype.NetworkLobby; | |
using System; | |
public enum BattleSMState { None=0, BattleSceneLoading, PreBattle, InBattle, PostBattle } | |
public class Battle : NetworkBehaviour | |
{ | |
static public Battle i; | |
public BattleSMState state = BattleSMState.None; | |
public Dictionary<uint, Unit> units = new Dictionary<uint, Unit> (); | |
public Dictionary<uint, Player> players = new Dictionary<uint, Player> (); | |
public uint localPlayerId; // the (net) id representing the local player on this device | |
// [SyncVar] | |
// public bool playersHaveIds; | |
[SyncVar] | |
public bool unitsReady; | |
string _debugString; | |
// NOTE: OnStartClient can be called before Start (observed by Arun 2017.03.29 in 1P-Editor) | |
// NOTE: OnStartClient can be called before OnStartServer (observed by Arun 2017.03.29 in 2P-Editor, Remote Client) | |
public override void OnStartClient () | |
{ | |
i = this; | |
base.OnStartClient (); | |
Field.i.GatherTiles (); | |
} | |
// NOTE: OnStartServer can be called before OnStartClient (observed by Arun 2017.03.29 in 1P-Editor) | |
// NOTE: OnStartServer can be called before Start (observed by Arun 2017.03.29 in 1P-Editor) | |
// NOTE: OnStartServer can be called before Start (observed by Arun 2017.03.29 in 1P-Editor) | |
public override void OnStartServer () | |
{ | |
i = this; | |
base.OnStartServer (); | |
StartCoroutine(Init()); | |
// sanity-check | |
if (!isServer) | |
ClientDebugUI.i.Log (string.Format ("Battle.OnStartServer: misleading!", ""), GameColor.Yellow); | |
} | |
// NOTE: Start can be called before OnStartServer (observed by Arun 2017.03.29 in 2P-Editor, Remote Client) | |
// NOTE: Start can be called before OnStartClient (observed by Arun 2017.03.29 in 2P-Editor, Remote Client) | |
void Start() | |
{ | |
i = this; | |
} | |
[Server] | |
IEnumerator Init() | |
{ | |
while(Battle.i.state == BattleSMState.BattleSceneLoading) { | |
_debugString = string.Format("Battle.Init: state={0}", Battle.i.state); | |
ServerDebugUI.i.Log (_debugString, GameColor.Magenta); | |
yield return new WaitForEndOfFrame(); | |
} | |
// players instantiating | |
while(Battle.i.players.Count < 1) | |
{ // *can* happen (observed by Arun 2017.03.29) | |
// _debugString = string.Format ("Battle.Init: players.Count is {0}", players.Count); | |
// ServerDebugUI.i.Log (_debugString, GameColor.Magenta); | |
yield return new WaitForEndOfFrame(); | |
} | |
// players waiting for ids | |
bool playerHaveIds = false; | |
while(!playerHaveIds) | |
{ | |
playerHaveIds = true; | |
foreach (KeyValuePair<uint, Player> kvp in Battle.i.players) | |
{ | |
if(kvp.Key < 1) { | |
playerHaveIds = false; | |
} | |
} | |
if(!playerHaveIds) { | |
_debugString = string.Format("Battle.Init: players.Count={0} but player ids are not ready", players); | |
ServerDebugUI.i.Log (_debugString, GameColor.Magenta); | |
yield return new WaitForEndOfFrame(); | |
} | |
} | |
// sanity-check: the local player id should be set by now | |
while(Battle.i.localPlayerId < 1) | |
{ | |
_debugString = string.Format("Battle.Init: Battle.i.localPlayerId is {0}", Battle.i.localPlayerId); | |
ServerDebugUI.i.Log (_debugString, GameColor.Magenta); | |
yield return new WaitForEndOfFrame(); | |
} | |
// do we have all our units | |
bool allPlayerHaveUnits = false; | |
while(!allPlayerHaveUnits) | |
{ | |
allPlayerHaveUnits = true; | |
foreach (KeyValuePair<uint, Player> kvp in players) | |
{ | |
if(kvp.Value.unitIds.Count < 1) { | |
allPlayerHaveUnits = false; | |
} | |
} | |
if(!allPlayerHaveUnits) | |
{ // *can* happen (observed by Arun 2017.03.29 in 1P-Editor) | |
// _debugString = string.Format("Battle.Init: player units not ready", ""); | |
// ServerDebugUI.i.Log (_debugString, GameColor.Magenta); | |
yield return new WaitForEndOfFrame(); | |
} | |
} | |
// do all units have ids? | |
bool allUnitsHaveIds = false; | |
while(!allUnitsHaveIds) | |
{ | |
allUnitsHaveIds = true; | |
foreach (KeyValuePair<uint, Unit> kvp in Battle.i.units) | |
{ | |
Unit unit = kvp.Value; | |
if(unit.netId.Value < 1) { | |
allUnitsHaveIds = false; | |
} | |
} | |
if(!allUnitsHaveIds) { | |
_debugString = string.Format("Battle.Init: not all units have ids ({0} total)", Battle.i.units.Count); | |
ServerDebugUI.i.Log (_debugString, GameColor.Magenta); | |
yield return new WaitForEndOfFrame(); | |
} | |
} | |
unitsReady = true; | |
CmdSetPhase( (int)BattleSMState.InBattle); | |
} | |
[Command] | |
public void CmdSetPhase(int phase) | |
{ | |
RpcSetBattlePhase (phase); | |
} | |
[ClientRpc] | |
public void RpcSetBattlePhase(int phase) | |
{ | |
Battle.i.GetComponent<Animator>().SetInteger("Phase", phase); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment