public async Task SetTileMapState(JsonTileMapState jsonTileMapState) { await ConnectToContract(); TileMapState tileMapStateTx = new TileMapState(); tileMapStateTx.Data = JsonUtility.ToJson(jsonTileMapState); await this.contract.CallAsync("SetTileMapState", tileMapStateTx); }
private void ChainEventReceivedHandler(object sender, ChainEventArgs e) { if (e.EventName != "onTileMapStateUpdate") { return; } string stringData = Encoding.UTF8.GetString(e.Data); JsonTileMapState tileMapStateParsed = JsonUtility.FromJson <JsonTileMapState>(stringData); this.eventActions.Enqueue(() => { TileMapStateUpdated?.Invoke(tileMapStateParsed); }); }
public async Task <JsonTileMapState> GetTileMapState() { await ConnectToContract(); TileMapState result = await this.contract.StaticCallAsync <TileMapState>("GetTileMapState", new TileMapState()); if (result == null) { throw new Exception("Smart contract didn't return anything!"); } JsonTileMapState jsonTileMapState = JsonUtility.FromJson <JsonTileMapState>(result.Data); return(jsonTileMapState); }
private async Task ConnectClient() { ReconnectButton.gameObject.SetActive(false); StatusText.gameObject.SetActive(true); StatusText.text = "Connecting..."; try { await this.client.ConnectToContract(); JsonTileMapState jsonTileMapState = await this.client.GetTileMapState(); UpdateTileMap(jsonTileMapState); StatusText.gameObject.SetActive(false); } catch (Exception e) { StatusText.text = "Error: " + e.Message; ReconnectButton.gameObject.SetActive(true); Debug.LogException(e); } }
private void UpdateTileMap(JsonTileMapState jsonTileMapState) { this.jsonTileMapState = jsonTileMapState ?? new JsonTileMapState(); foreach (GameObject tile in this.tileGameObjects) { Destroy(tile); } this.tileGameObjects.Clear(); foreach (JsonTileMapState.Tile tile in this.jsonTileMapState.tiles) { GameObject go = new GameObject("Tile"); go.transform.localScale = Vector3.one * 16f; go.transform.position = new Vector3(tile.point.x, GameFieldSize.y - tile.point.y, 0); SpriteRenderer spriteRenderer = go.AddComponent <SpriteRenderer>(); spriteRenderer.sprite = PointSprite; spriteRenderer.color = new Color32((byte)tile.color.r, (byte)tile.color.g, (byte)tile.color.b, 255); this.tileGameObjects.Add(go); } }
private void ClientOnTileMapStateUpdated(JsonTileMapState obj) { UpdateTileMap(obj); }