public static GridInfo ReadGrid(Deserializer reader) { GridInfo gridInfo = new GridInfo(); gridInfo.min = reader.ReadLVector2Int(); gridInfo.max = reader.ReadLVector2Int(); gridInfo.cellSize = reader.ReadLVector3(); gridInfo.cellGap = reader.ReadLVector3(); gridInfo.cellLayout = reader.ReadInt32(); gridInfo.cellSwizzle = reader.ReadInt32(); int num = reader.ReadInt32(); gridInfo.tileMaps = new TileInfos[num]; string[] array = new string[num]; int[] array2 = new int[num]; for (int i = 0; i < num; i++) { string text = reader.ReadString(); array[i] = text; } for (int j = 0; j < num; j++) { int num2 = reader.ReadInt32(); array2[j] = num2; } for (int k = 0; k < num; k++) { TileInfos tileInfos = TileMapDeserializer.ReadMap(reader); gridInfo.tileMaps[k] = tileInfos; tileInfos.name = array[k]; } return(gridInfo); }
public Color[] colors = new Color[1]; // background public ScreenInfos() { for (int i = 0; i < colors.Length; i++) { colors[i] = new Color(0.9f, 0.5f, 0.8f); } for (int x = 0; x < editorManager.nbTilesX; x++) { for (int y = 0; y < editorManager.nbTilesY; y++) { for (int l = 0; l < editorManager.nbLayers; l++) { tiles[x, y, l] = new TileInfos(0, 0, 0); } } } }
// Use this for initialization void Start() { Populate(); roomInfos = JsonUtility.FromJson <RoomInfos>(JSON("rooms.json")); roomInfos.rooms.ToList().ForEach(x => { x.Init(x); //Debug.Log(x.id); }); tileInfos = JsonUtility.FromJson <TileInfos>(JSON("tiles.json")); tileInfos.Tiles.ToList().ForEach(x => { //x.Init(x); //Debug.Log(x.Name); }); Room(12, 12, 8, 8, "Saloon"); }
void updateDisplayedLevel() { if (screens [screenPosX, screenPosY] == null) { screens [screenPosX, screenPosY] = new ScreenInfos(); } cam.backgroundColor = screens [screenPosX, screenPosY].colors[0]; for (int i = 0; i < gradients.Length; i++) { gradients[i].GetComponent <gradient>().updateColors(); } for (int x = 0; x < nbTilesX; x++) { for (int y = 0; y < nbTilesY; y++) { for (int l = 0; l < nbLayers; l++) { TileInfos thisTileInfos = screens[screenPosX, screenPosY].tiles[x, y, l]; setTile(x, y, l, thisTileInfos.type, thisTileInfos.shape, thisTileInfos.rotation); } } } }
static TileInfos ReadMap(BinaryReader reader) { //read var pos = reader.BaseStream.Position; var len = reader.BaseStream.Length; if (len - pos < 8) { throw new FileContentException(string.Format("FileSpace is not enough for head!! pos = {0} len = {1} ", pos, len)); } var magicNum = reader.ReadBytes(4); if (!CheckMagicNumber(magicNum)) { throw new FileContentException("MagicNumberError"); return(null); } var needSize = reader.ReadInt32(); //CheckSize if (len - pos < needSize) { throw new FileContentException(string.Format("FileSpace is not enough!! pos = {0} len = {1} needSize = {2}", pos, len, needSize)); return(null); } var totalCount = reader.ReadInt32(); var isUseByteForPos = reader.ReadBoolean(); var isSaveWithOutPos = reader.ReadBoolean(); var minx = reader.ReadInt32(); var miny = reader.ReadInt32(); var sizex = reader.ReadInt32(); var sizey = reader.ReadInt32(); var notNullCount = reader.ReadInt32(); if (notNullCount > totalCount) { throw new FileContentException(string.Format("notNullCount {0} > count {1}", notNullCount, totalCount)); return(null); } var tiles = new ushort[totalCount]; var retVal = new TileInfos(); retVal.tileIDs = tiles; retVal.min = new Vector2Int(minx, miny); retVal.size = new Vector2Int(sizex, sizey); var diffCount = reader.ReadInt32(); ushort[] tileRawIDs = new ushort[diffCount]; for (int i = 0; i < diffCount; i++) { tileRawIDs[i] = reader.ReadUInt16(); } Dictionary <ushort, ushort> id2Tile = new Dictionary <ushort, ushort>(); for (ushort i = 0; i < diffCount; i++) { id2Tile[(ushort)(i + 1)] = tileRawIDs[i]; } if (isSaveWithOutPos) { for (int i = 0; i < totalCount; i++) { var val = reader.ReadByte(); if (val == 0) { tiles[i] = 0; } else { if (id2Tile.TryGetValue(val, out ushort tile)) { tiles[i] = tile; } } } } else { if (isUseByteForPos) { for (int idx = 0; idx < notNullCount; idx++) { var x = reader.ReadByte(); var y = reader.ReadByte(); var val = reader.ReadByte(); if (id2Tile.TryGetValue(val, out ushort tile)) { tiles[y * sizex + x] = tile; } Debug.Assert(tiles[y * sizex + x] != 0, ""); } } else { for (int idx = 0; idx < notNullCount; idx++) { var x = reader.ReadUInt16(); var y = reader.ReadUInt16(); var val = reader.ReadByte(); if (id2Tile.TryGetValue(val, out ushort tile)) { tiles[y * sizex + x] = tile; } Debug.Assert(tiles[y * sizex + x] != 0, ""); } } } return(retVal); }
public static GridInfo LoadLevel(int level) { CheckLoadTileIDMap(); var path = LevelManager.GetMapPathFull(level); if (!File.Exists(path)) { Debug.LogError("Have no map file" + level); return(null); } var bytes = File.ReadAllBytes(path); var reader = new BinaryReader(new MemoryStream(bytes)); var info = TileMapSerializer.ReadGrid(reader); var go = GameObject.FindObjectOfType <Grid>(); Debug.Assert(go != null, "Can not find Grid in scene"); //init tilemap info var maps = go.GetComponentsInChildren <Tilemap>(); for (int i = 0; i < maps.Length; i++) { var tileMap = maps[i]; var tileMapInfo = info.GetMapInfo(tileMap.name); if (tileMapInfo == null) { continue; } tileMapInfo.tilemap = tileMap; tileMap.ClearAllTiles(); tileMap.SetTiles(tileMapInfo.GetAllPositions(), tileMapInfo.GetAllTiles()); if (Application.isPlaying) { if (tileMap.name == TILE_MAP_NAME_BORN_POS) { tileMap.GetComponent <TilemapRenderer>().enabled = false; } } if (tileMap.name == TILE_MAP_NAME_BORN_POS || tileMap.name == TILE_MAP_NAME_GRASS) { tileMapInfo.hasCollider = false; } if (tileMap.name == TILE_MAP_NAME_BORN_POS) { tileMapInfo.isTagMap = true; } } // resort tilemaps by layer int count = maps.Length; var tempMaps = go.GetComponentsInChildren <TileMapHelper>().ToList(); tempMaps.Sort((a, b) => { return(b.layer - a.layer); }); //重排优先级 Debug.Assert(tempMaps.Count == count, "Some tilemap do not have component!!" + typeof(TileMapHelper).ToString()); for (int i = 0; i < count; i++) { maps[i] = tempMaps[i].GetComponent <Tilemap>(); } var tempTileMaps = new TileInfos[count]; var tempNames = new string[count]; for (int i = 0; i < count; i++) { var name = tempMaps[i].name; tempNames[i] = name; tempTileMaps[i] = info.GetMapInfo(name); } info.tileMaps = tempTileMaps; info.names = tempNames; var min = new Vector2Int(int.MaxValue, int.MaxValue); var max = new Vector2Int(int.MinValue, int.MinValue); foreach (var tempInfo in info.tileMaps) { var tileMap = tempInfo.tilemap; var mapMin = tileMap.cellBounds.min; if (mapMin.x < min.x) { min.x = mapMin.x; } if (mapMin.y < min.y) { min.y = mapMin.y; } var mapMax = tileMap.cellBounds.max; if (mapMax.x > max.x) { max.x = mapMax.x; } if (mapMax.y > max.y) { max.y = mapMax.y; } } GameManager.Instance.min = min; GameManager.Instance.max = max; return(info); }
private static TileInfos ReadMap(Deserializer reader) { int position = reader.Position; int rawDataSize = reader.RawDataSize; bool flag = rawDataSize - position < 9; if (flag) { //throw new BaseTileMapSerializer.FileContentException(string.Format("FileSpace is not enough for head!! pos = {0} len = {1} ", position, rawDataSize)); } byte[] bs = reader.ReadBytes_255(); bool flag2 = !CheckMagicNumber(bs); if (flag2) { //throw new BaseTileMapSerializer.FileContentException("MagicNumberError"); } int num = reader.ReadInt32(); int num2 = rawDataSize - reader.Position; bool flag3 = num2 < num; if (flag3) { //throw new BaseTileMapSerializer.FileContentException(string.Format("FileSpace is not enough!! reader.Position = {0} remainSize = {1} needSize = {2}", reader.Position, num2, num)); } bool isTagMap = reader.ReadBoolean(); bool hasCollider = reader.ReadBoolean(); int num3 = reader.ReadInt32(); bool flag4 = reader.ReadBoolean(); bool flag5 = reader.ReadBoolean(); int x = reader.ReadInt32(); int y = reader.ReadInt32(); int num4 = reader.ReadInt32(); int y2 = reader.ReadInt32(); int num5 = reader.ReadInt32(); bool flag6 = num5 > num3; if (flag6) { //throw new BaseTileMapSerializer.FileContentException(string.Format("notNullCount {0} > count {1}", num5, num3)); } ushort[] array = new ushort[num3]; TileInfos tileInfos = new TileInfos(); tileInfos.isTagMap = isTagMap; tileInfos.hasCollider = hasCollider; tileInfos.tileIDs = array; tileInfos.min = new LVector2Int(x, y); tileInfos.size = new LVector2Int(num4, y2); int num6 = reader.ReadInt32(); ushort[] array2 = new ushort[num6]; for (int i = 0; i < num6; i++) { array2[i] = reader.ReadUInt16(); } Dictionary <ushort, ushort> dictionary = new Dictionary <ushort, ushort>(); ushort num7 = 0; while ((int)num7 < num6) { dictionary[(ushort)(num7 + 1)] = array2[(int)num7]; num7 += 1; } bool flag7 = flag5; if (flag7) { for (int j = 0; j < num3; j++) { byte b = reader.ReadByte(); bool flag8 = b == 0; if (flag8) { array[j] = 0; } else { ushort num8; bool flag9 = dictionary.TryGetValue((ushort)b, out num8); if (flag9) { array[j] = num8; } } } } else { bool flag10 = flag4; if (flag10) { for (int k = 0; k < num5; k++) { byte b2 = reader.ReadByte(); byte b3 = reader.ReadByte(); byte key = reader.ReadByte(); ushort num9; bool flag11 = dictionary.TryGetValue((ushort)key, out num9); if (flag11) { array[(int)b3 * num4 + (int)b2] = num9; } Debug.Assert(array[(int)b3 * num4 + (int)b2] > 0, ""); } } else { for (int l = 0; l < num5; l++) { ushort num10 = reader.ReadUInt16(); ushort num11 = reader.ReadUInt16(); byte key2 = reader.ReadByte(); ushort num12; bool flag12 = dictionary.TryGetValue((ushort)key2, out num12); if (flag12) { array[(int)num11 * num4 + (int)num10] = num12; } Debug.Assert(array[(int)num11 * num4 + (int)num10] > 0, ""); } } } //tileInfos.tileIDs = array; return(tileInfos); }