public static Map ReadMap(string json) { List<Map.Tile> tiles = new List<Map.Tile>(); List<Map.Town> towns = new List<Map.Town>(); LitJson.JsonData data = LitJson.JsonMapper.ToObject(json); Vector2 min = new Vector2(); Vector2 max = new Vector2(); min.x = float.Parse(data["min"]["x"].ToString()); min.y = float.Parse(data["min"]["y"].ToString()); max.x = float.Parse(data["max"]["x"].ToString()); max.y = float.Parse(data["max"]["y"].ToString()); for (int i = 0; i < data["tiles"].Count; i++) { LitJson.JsonData s = data["tiles"][i]; int x = int.Parse(s["key"]["x"].ToString()); int y = int.Parse(s["key"]["y"].ToString()); Map.HexCoord hc = Map.HexCoord.HexCoordXY(x, y); Map.TileType tt = (Map.TileType)Enum.Parse(typeof(Map.TileType), s["value"]["tileType"].ToString()); Map.Penalty penalty = (Map.Penalty)Enum.Parse(typeof(Map.Penalty), s["value"]["penalty"].ToString()); Map.Visibility v = (Map.Visibility)Enum.Parse(typeof(Map.Visibility), s["value"]["visibility"].ToString()); Map.Tile tile = new Map.Tile(); tile.hexCoord = hc; tile.spriteType = tt; tile.penalty = penalty; tile.visibility = v; tiles.Add(tile); } for (int i = 0; i < data["towns"].Count; i++) { LitJson.JsonData s = data["towns"][i]; int x = int.Parse(s["key"]["x"].ToString()); int y = int.Parse(s["key"]["y"].ToString()); Map.HexCoord hc = Map.HexCoord.HexCoordXY(x, y); int team = int.Parse(s["value"]["playerSide"].ToString()); Map.Town town = new Map.Town(); town.hexCoord = hc; town.team = team; towns.Add(town); } return new Map(tiles, towns, min, max); }
public static NetworkComm.GameUpdate ReadUpdate(string json) { Debug.Log ("JSON: ReadUpdate"); NetworkComm.GameUpdate gu = new NetworkComm.GameUpdate(); LitJson.JsonData data = LitJson.JsonMapper.ToObject(json); Debug.Log ("JSON: Reading units"); List <NetworkComm.UnitUpdate> unitUpdates = new List <NetworkComm.UnitUpdate>(); for (int i = 0; i < data["units"].Count; i++) { Debug.Log ("JSON: Unit " + i.ToString()); LitJson.JsonData s = data["units"][i]; NetworkComm.UnitUpdate uUpdate = new NetworkComm.UnitUpdate(); Debug.Log ("JSON: Reading unit data"); Debug.Log ("JSON: Key"); int id = int.Parse (s["key"].ToString()); Debug.Log ("JSON: Owner: " + s["value"]["playerSide"].ToString()); int team = int.Parse(s["value"]["playerSide"].ToString()); Debug.Log ("JSON: Unittype"); string type = s["value"]["unitType"].ToString(); Debug.Log ("JSON: Food"); int food = int.Parse (s["value"]["food"].ToString()); Debug.Log ("JSON: Coords - x1"); int x1 = int.Parse (s["value"]["coords"][0]["x"].ToString()); Debug.Log ("JSON: Coords - y1"); int y1 = int.Parse (s["value"]["coords"][0]["y"].ToString()); Map.HexCoord coord1 = Map.HexCoord.HexCoordXY(x1, y1); Map.HexCoord coord2; if (s["value"]["coords"].Count > 1) { Debug.Log ("JSON: We have another coord"); Debug.Log ("JSON: Coords - x2"); int x2 = int.Parse (s["value"]["coords"][1]["x"].ToString()); Debug.Log ("JSON: Coords - y2"); int y2 = int.Parse (s["value"]["coords"][1]["y"].ToString()); coord2 = Map.HexCoord.HexCoordXY(x2, y2); } else { coord2 = coord1; } Debug.Log ("JSON: Assembling unit data"); uUpdate.uId = id; uUpdate.food = food; uUpdate.hexCoord = coord1; uUpdate.targetHexCoord = coord2; uUpdate.team = team; switch (type) { case "Cavalry": uUpdate.uType = Map.UnitType.Cavalry; break; case "Infantry": uUpdate.uType = Map.UnitType.Infantry; break; case "Artillery": uUpdate.uType = Map.UnitType.Artillery; break; case "Scout": uUpdate.uType = Map.UnitType.Scout; break; case "Kart": uUpdate.uType = Map.UnitType.Kart; break; default: Debug.Log ("JSON: Unknown unit type " + type); break; } Debug.Log ("JSON: Adding unit data to the list"); unitUpdates.Add(uUpdate); } Debug.Log ("JSON: Reqding towns"); List <Map.Town> townList = new List<Map.Town>(); for (int i = 0; i < data["towns"].Count; i++) { LitJson.JsonData s = data["towns"][i]; Map.Town town = new Map.Town(); int x = int.Parse (s["key"]["x"].ToString()); int y = int.Parse (s["key"]["y"].ToString()); int food = int.Parse (s["value"]["food"].ToString()); int team = int.Parse (s["value"]["playerSide"].ToString()); Map.HexCoord hc = Map.HexCoord.HexCoordXY(x, y); town.hexCoord = hc; town.team = team; town.food = food; townList.Add(town); } Debug.Log ("JSON: Reading coords"); List <Map.HexCoord> visibleCords = new List<Map.HexCoord>(); for (int i = 0; i < data["coords"].Count; i++) { LitJson.JsonData s = data["coords"][i]; int x = int.Parse (s["x"].ToString()); int y = int.Parse (s["y"].ToString()); Map.HexCoord newHc = Map.HexCoord.HexCoordXY(x, y); visibleCords.Add(newHc); } Debug.Log ("JSON: Assembling GameUpdate"); gu.towns = townList; gu.unitUpdates = unitUpdates; gu.visibleTiles = visibleCords; return gu; }