bool sendData(JengaData data) { if (udp == null) { return(false); } try { BinaryFormatter bf = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream()) { bf.Serialize(ms, data); byte[] bytes = ms.ToArray(); int res = udp.Send(bytes, bytes.Length, ep); if (res != bytes.Length) { return(false); } return(true); } } catch (System.Exception e) { Debug.LogError(e.Message); return(false); } }
/// <summary> /// Tries to get the config if there is some. If you wanna load config call LoadJengaConfig. /// /// For bug prevention, LoadConfig sets to null the configuration so you have to explicitly load a config again. /// /// </summary> /// <returns>If there is no config, it will return null.</returns> public static JengaData GetJengaConfig() { JengaData data = _data; _data = null; return(data); }
void sendBlocks() { JengaData data = new JengaData(); fillBlocks(ref data); sendData(data); }
/// <summary> /// Will start saving the jenga game in another thread. /// </summary> /// <returns></returns> public static bool SaveJengaGame(GameObject[] pieces) { if (_instance._loadingData || _instance._loadingSavedGamesData != null) { Debug.Log("CAN'T SAVE"); return(false); } _instance._loadingData = true; if (_data == null) { _data = new JengaData { id = UnityEngine.Random.Range(-100000, 100000) }; } _data.jengas = new JengaPieceData[pieces.Length]; _data.date = DateTime.Now.Ticks; for (int i = 0; i < pieces.Length; ++i) { _data.jengas[i] = new JengaPieceData { position = pieces[i].transform.localPosition, rotation = pieces[i].transform.localRotation.eulerAngles, scale = pieces[i].transform.localScale }; } // We will keep with more login in KeepSavingJengaGame when this is finished loading _instance._loadingSavedGamesData = SaveSystem.LoadOnAnotherThread <SavedGamesData>("game_data.json"); return(true); }
public void sendLost() { JengaData data = new JengaData(); fillBlocks(ref data); data.lost = true; sendData(data); }
public void sendEndTurn() { lastData = Time.time; JengaData data = new JengaData(); fillBlocks(ref data); data.endTurn = true; sendData(data); }
public void ResetWithLoad(JengaData data) { _towerData.towerAlreadyBuilt = false; _piecePool.DeactivateAll(); _dataLoaded = data; Config.LoadJengaConfig(data); StartCoroutine(SpawnLoad()); FindObjectOfType <PlayerInGame>().Reset(false); }
void fillBlocks(ref JengaData data) { List <JengaBlockData> lblocks = new List <JengaBlockData>(); foreach (KeyValuePair <int, JengaBlock> b in dblocks) { if (b.Value != null) { lblocks.Add(new JengaBlockData(b.Value)); } } data.blocks = lblocks.ToArray(); }
private void Start() { _tower = new GameObject("Tower"); _tower.transform.parent = transform; _piecePool = GetComponent <Pool>(); _dataLoaded = Config.GetJengaConfig(); if (_dataLoaded == null) { StartCoroutine("BuildTower", buildInterval); return; } // Inject configuration again because // When you get jenga config it deletes // it because safety measures Config.LoadJengaConfig(_dataLoaded); StartCoroutine(SpawnLoad()); }
/// <summary> /// Loads jenga data so when we are in the gameplay scene /// we can have custom setups. if there is no config loaded it will spawn a standard tower. /// </summary> /// <param name="data"></param> public static void LoadJengaConfig(JengaData data) { _data = data; }