public TMXTileSet GetTileSetByGID(int gid) { for (int i = 0; i < tileSets.Count; i++) { TMXTileSet set = tileSets[i]; if (set.Contains(gid)) { return(set); } } return(null); }
public TMXTileSet FindTileSet(int gid) { for (int i = 0; i < tileSets.Count; i++) { TMXTileSet set = (TMXTileSet)tileSets[i]; if (set.Contains(gid)) { return(set); } } return(null); }
/// <summary> /// 设置指定位置的瓦片ID /// </summary> /// /// <param name="x"></param> /// <param name="y"></param> /// <param name="tile"></param> public void SetTileID(int x, int y, int tile) { if (tile == 0) { data[x, y, 0] = -1; data[x, y, 1] = 0; data[x, y, 2] = 0; } else { TMXTileSet set = tmx.FindTileSet(tile); data[x, y, 0] = set.index; data[x, y, 1] = tile - set.firstGID; data[x, y, 2] = tile; } }
public string GetTileProperty(int tileID, string propertyName, string def) { if (tileID == 0) { return(def); } TMXTileSet set = FindTileSet(tileID); TMXProperty props_0 = set.GetProperties(tileID); if (props_0 == null) { return(def); } return(props_0.GetProperty(propertyName, def)); }
public LTexture GetTileImage(int x, int y, int layerIndex) { TMXLayer layer = layers[layerIndex]; int tileSetIndex = layer.data[x, y, 0]; if ((tileSetIndex >= 0) && (tileSetIndex < tileSets.Count)) { TMXTileSet tileSet = tileSets[tileSetIndex]; int sheetX = tileSet.GetTileX(layer.data[x, y, 1]); int sheetY = tileSet.GetTileY(layer.data[x, y, 1]); return(tileSet.tiles.GetSubImage(sheetX, sheetY)); } return(null); }
/// <summary> /// 根据TMX地图描述创建一个新层 /// </summary> /// /// <param name="map"></param> /// <param name="element"></param> /// <exception cref="System.Exception"></exception> public TMXLayer(TMXTiledMap map, XMLElement element) { this.tmx = map; this.name = element.GetAttribute("name", ""); this.width = element.GetIntAttribute("width", 0); this.height = element.GetIntAttribute("height", 0); this.data = new int[width, height, 3]; this.MaxLightSize(width, height); // 获得当前图层属性 XMLElement propsElement = element.GetChildrenByName("properties"); if (propsElement != null) { props = new TMXProperty(); List <XMLElement> properties = propsElement.List("property"); for (int i = 0; i < properties.Count; i++) { XMLElement propElement = properties[i]; string name_0 = propElement.GetAttribute("name", null); string value_ren = propElement.GetAttribute("value", null); props.SetProperty(name_0, value_ren); } } XMLElement dataNode = element.GetChildrenByName("data"); string encoding = dataNode.GetAttribute("encoding", null); string compression = dataNode.GetAttribute("compression", null); // 进行base64的压缩解码 if ("base64".Equals(encoding) && "gzip".Equals(compression)) { try { byte[] sdec = Base64Coder.DecodeBase64(dataNode.GetContents().Trim().ToCharArray()); ByteArrayInputStream mask0 = new ByteArrayInputStream(sdec); GZipInputStream dis = new GZipInputStream(mask0); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int tileId = 0; tileId |= dis.ReadByte(); tileId |= dis.ReadByte() << 8; tileId |= dis.ReadByte() << 16; tileId |= dis.ReadByte() << 24; if (tileId == 0) { data[x, y, 0] = -1; data[x, y, 1] = 0; data[x, y, 2] = 0; } else { TMXTileSet set = map.FindTileSet(tileId); if (set != null) { data[x, y, 0] = set.index; data[x, y, 1] = tileId - set.firstGID; } data[x, y, 2] = tileId; } } } } catch (Exception e) { Loon.Utils.Debug.Log.Exception(e); throw new Exception("Unable to decode base64 !"); } } else { throw new Exception("Unsupport tiled map type " + encoding + "," + compression + " only gzip base64 Support !"); } }
/// <summary> /// 渲染当前层画面到LGraphics之上 /// </summary> /// /// <param name="g"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="sx"></param> /// <param name="sy"></param> /// <param name="width"></param> /// <param name="ty"></param> /// <param name="isLine"></param> /// <param name="mapTileWidth"></param> /// <param name="mapTileHeight"></param> public void Draw(GLEx g, int x, int y, int sx, int sy, int width, int height, bool isLine, int mapTileWidth, int mapTileHeight) { if (width == 0 || height == 0) { return; } if (lightingOn) { GLUtils.SetShadeModelSmooth(GLEx.GL); } this.tmxTileSet = null; this.mapTileSet = null; for (int tileset = 0; tileset < tmx.GetTileSetCount(); tileset++) { keyHashCode = 1; keyHashCode = LSystem.Unite(keyHashCode, tileset); keyHashCode = LSystem.Unite(keyHashCode, sx); keyHashCode = LSystem.Unite(keyHashCode, sy); keyHashCode = LSystem.Unite(keyHashCode, width); keyHashCode = LSystem.Unite(keyHashCode, height); keyHashCode = LSystem.Unite(keyHashCode, mapTileWidth); keyHashCode = LSystem.Unite(keyHashCode, mapTileHeight); keyHashCode = LSystem.Unite(keyHashCode, lightingOn); mapTileSet = (MapTileSet)CollectionUtils.Get(lazyMaps, keyHashCode); if (!isLightDirty && mapTileSet != null) { mapTileSet.cache.x = x; mapTileSet.cache.y = y; LTextureBatch.Commit(mapTileSet.texture, mapTileSet.cache); if (isLine) { tmx.Draw(g, x, y, sx, sy, width, height, index); } if (lightingOn) { GLUtils.SetShadeModelSmooth(GLEx.GL); } return; } for (int ty = 0; ty < height; ty++) { for (int tx = 0; tx < width; tx++) { if ((sx + tx < 0) || (sy + ty < 0)) { continue; } if ((sx + tx >= this.width) || (sy + ty >= this.height)) { continue; } if (data[sx + tx, sy + ty, 0] == tileset) { if (tmxTileSet == null) { tmxTileSet = tmx.GetTileSet(tileset); tmxTileSet.tiles.GLBegin(); } int sheetX = tmxTileSet .GetTileX(data[sx + tx, sy + ty, 1]); int sheetY = tmxTileSet .GetTileY(data[sx + tx, sy + ty, 1]); int tileOffsetY = tmxTileSet.tileHeight - mapTileHeight; cx = tx * mapTileWidth; cy = ty * mapTileHeight - tileOffsetY; if (lightingOn) { SetLightColor(cx / mapTileWidth, cy / mapTileHeight); } tmxTileSet.tiles .Draw(g, cx, cy, sheetX, sheetY, colors); } } } if (tmxTileSet != null) { tmxTileSet.tiles.GLEnd(); if (mapTileSet == null) { mapTileSet = new TMXLayer.MapTileSet(); } else { mapTileSet.texture = null; mapTileSet.cache.Dispose(); mapTileSet.cache = null; } mapTileSet.texture = tmxTileSet.tiles.GetTarget(); mapTileSet.cache = tmxTileSet.tiles.NewCache(); mapTileSet.cache.x = x; mapTileSet.cache.y = y; CollectionUtils.Put(lazyMaps, keyHashCode, mapTileSet); if (lightingOn) { GLUtils.SetShadeModelFlat(GLEx.GL); } if (isLine) { tmx.Draw(g, x, y, sx, sy, width, height, index); } isLightDirty = false; tmxTileSet = null; } } }
/// <summary> /// 渲染当前层画面到LGraphics之上 /// </summary> /// /// <param name="g"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="sx"></param> /// <param name="sy"></param> /// <param name="width"></param> /// <param name="ty"></param> /// <param name="isLine"></param> /// <param name="mapTileWidth"></param> /// <param name="mapTileHeight"></param> public void Draw(GLEx g, int x, int y, int sx, int sy, int width, int height, bool isLine, int mapTileWidth, int mapTileHeight) { if (width == 0 || height == 0) { return; } if (lightingOn) { GLUtils.SetShadeModelSmooth(GLEx.GL); } this.tmxTileSet = null; this.mapTileSet = null; for (int tileset = 0; tileset < tmx.GetTileSetCount(); tileset++) { keyHashCode = 1; keyHashCode = LSystem.Unite(keyHashCode, tileset); keyHashCode = LSystem.Unite(keyHashCode, sx); keyHashCode = LSystem.Unite(keyHashCode, sy); keyHashCode = LSystem.Unite(keyHashCode, width); keyHashCode = LSystem.Unite(keyHashCode, height); keyHashCode = LSystem.Unite(keyHashCode, mapTileWidth); keyHashCode = LSystem.Unite(keyHashCode, mapTileHeight); keyHashCode = LSystem.Unite(keyHashCode, lightingOn); mapTileSet = (MapTileSet)CollectionUtils.Get(lazyMaps, keyHashCode); if (!isLightDirty && mapTileSet != null) { mapTileSet.cache.x = x; mapTileSet.cache.y = y; LTextureBatch.Commit(mapTileSet.texture, mapTileSet.cache); if (isLine) { tmx.Draw(g, x, y, sx, sy, width, height, index); } if (lightingOn) { GLUtils.SetShadeModelSmooth(GLEx.GL); } return; } for (int ty = 0; ty < height; ty++) { for (int tx = 0; tx < width; tx++) { if ((sx + tx < 0) || (sy + ty < 0)) { continue; } if ((sx + tx >= this.width) || (sy + ty >= this.height)) { continue; } if (data[sx + tx,sy + ty,0] == tileset) { if (tmxTileSet == null) { tmxTileSet = tmx.GetTileSet(tileset); tmxTileSet.tiles.GLBegin(); } int sheetX = tmxTileSet .GetTileX(data[sx + tx,sy + ty,1]); int sheetY = tmxTileSet .GetTileY(data[sx + tx,sy + ty,1]); int tileOffsetY = tmxTileSet.tileHeight - mapTileHeight; cx = tx * mapTileWidth; cy = ty * mapTileHeight - tileOffsetY; if (lightingOn) { SetLightColor(cx / mapTileWidth, cy / mapTileHeight); } tmxTileSet.tiles .Draw(g, cx, cy, sheetX, sheetY, colors); } } } if (tmxTileSet != null) { tmxTileSet.tiles.GLEnd(); if (mapTileSet == null) { mapTileSet = new TMXLayer.MapTileSet(); } else { mapTileSet.texture = null; mapTileSet.cache.Dispose(); mapTileSet.cache = null; } mapTileSet.texture = tmxTileSet.tiles.GetTarget(); mapTileSet.cache = tmxTileSet.tiles.NewCache(); mapTileSet.cache.x = x; mapTileSet.cache.y = y; CollectionUtils.Put(lazyMaps, keyHashCode, mapTileSet); if (lightingOn) { GLUtils.SetShadeModelFlat(GLEx.GL); } if (isLine) { tmx.Draw(g, x, y, sx, sy, width, height, index); } isLightDirty = false; tmxTileSet = null; } } }
/// <summary> /// 渲染当前层画面到LGraphics之上 /// </summary> /// /// <param name="g"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="sx"></param> /// <param name="sy"></param> /// <param name="width0"></param> /// <param name="ty"></param> /// <param name="isLine"></param> /// <param name="mapTileWidth"></param> /// <param name="mapTileHeight"></param> public void Draw(GLEx g, int x, int y, int sx, int sy, int width0, int height0, bool isLine, int mapTileWidth, int mapTileHeight) { if (width0 == 0 || height0 == 0) { return; } if (lightingOn) { } this.tmxTileSet = null; for (int tileset = 0; tileset < tmx.GetTileSetCount(); tileset++) { for (int ty = 0; ty < height0; ty++) { for (int tx = 0; tx < width0; tx++) { if ((sx + tx < 0) || (sy + ty < 0)) { continue; } if ((sx + tx >= this.width) || (sy + ty >= this.height)) { continue; } if (data[sx + tx, sy + ty, 0] == tileset) { if (tmxTileSet == null) { tmxTileSet = tmx.GetTileSet(tileset); tmxTileSet.tiles.GLBegin(); } int sheetX = tmxTileSet .GetTileX(data[sx + tx, sy + ty, 1]); int sheetY = tmxTileSet .GetTileY(data[sx + tx, sy + ty, 1]); int tileOffsetY = tmxTileSet.tileHeight - mapTileHeight; cx = tx * mapTileWidth; cy = ty * mapTileHeight - tileOffsetY; if (lightingOn) { SetLightColor(cx / mapTileWidth, cy / mapTileHeight); } tmxTileSet.tiles .Draw(g, cx, cy, sheetX, sheetY); } } } if (tmxTileSet != null) { tmxTileSet.tiles.GLEnd(); if (lightingOn) { } if (isLine) { tmx.Draw(g, x, y, sx, sy, width0, height0, index); } isLightDirty = false; tmxTileSet = null; } } }
private void Load(Stream ins0, string tileSetsLocation) { screenRect = LSystem.screenRect; tilesLocation = tileSetsLocation; try { XMLDocument doc = XMLParser.Parse(ins0); XMLElement docElement = doc.GetRoot(); string orient = docElement.GetAttribute("orientation", ""); if (!"orthogonal".Equals(orient)) { throw new Exception( "Only orthogonal maps supported, found " + orient); } width = docElement.GetIntAttribute("width", 0); height = docElement.GetIntAttribute("height", 0); tileWidth = docElement.GetIntAttribute("tilewidth", 0); tileHeight = docElement.GetIntAttribute("tileheight", 0); XMLElement propsElement = (XMLElement)docElement .GetChildrenByName("properties"); if (propsElement != null) { props = new TMXProperty(); List<XMLElement> property = propsElement.List("property"); for (int i = 0; i < property.Count; i++) { XMLElement propElement = property[i]; string name = propElement.GetAttribute("name", null); string value_ren = propElement.GetAttribute("value", null); props.SetProperty(name, value_ren); } } if (loadTileSets) { TMXTileSet tileSet = null; TMXTileSet lastSet = null; List<XMLElement> setNodes = docElement.List("tileset"); for (int i_0 = 0; i_0 < setNodes.Count; i_0++) { XMLElement current = setNodes[i_0]; tileSet = new TMXTileSet(this, current, true); tileSet.index = i_0; if (lastSet != null) { lastSet.SetLimit(tileSet.firstGID - 1); } lastSet = tileSet; CollectionUtils.Add(tileSets, tileSet); } } List<XMLElement> layerNodes = docElement.List("layer"); for (int i_1 = 0; i_1 < layerNodes.Count; i_1++) { XMLElement current_2 = layerNodes[i_1]; TMXLayer layer = new TMXLayer(this, current_2); layer.index = i_1; CollectionUtils.Add(layers, layer); } List<XMLElement> objectGroupNodes = docElement .List("objectgroup"); for (int i_3 = 0; i_3 < objectGroupNodes.Count; i_3++) { XMLElement current_4 = objectGroupNodes[i_3]; TMXTileGroup objectGroup = new TMXTileGroup(current_4); objectGroup.index = i_3; CollectionUtils.Add(objectGroups, objectGroup); } defWidth = (int)(screenRect.GetWidth() / tileWidth); defHeight = (int)(screenRect.GetHeight() / tileHeight); } catch (Exception ex) { Console.Error.WriteLine(ex.StackTrace); throw new Exception("Failed to parse map", ex); } }
private void Load(Stream ins, string tileSetsLocation) { screenRect = LSystem.screenRect; tilesLocation = tileSetsLocation; try { XMLDocument doc = XMLParser.Parse(ins); XMLElement docElement = doc.GetRoot(); string orient = docElement.GetAttribute("orientation", ""); if (!"orthogonal".Equals(orient)) { throw new Exception( "Only orthogonal maps supported, found " + orient); } width = docElement.GetIntAttribute("width", 0); height = docElement.GetIntAttribute("height", 0); tileWidth = docElement.GetIntAttribute("tilewidth", 0); tileHeight = docElement.GetIntAttribute("tileheight", 0); XMLElement propsElement = docElement .GetChildrenByName("properties"); if (propsElement != null) { props = new TMXProperty(); List <XMLElement> property = propsElement.List("property"); for (int i = 0; i < property.Count; i++) { XMLElement propElement = property[i]; string name = propElement.GetAttribute("name", null); string value_ren = propElement.GetAttribute("value", null); props.SetProperty(name, value_ren); } } if (loadTileSets) { TMXTileSet tileSet = null; TMXTileSet lastSet = null; List <XMLElement> setNodes = docElement.List("tileset"); for (int i_0 = 0; i_0 < setNodes.Count; i_0++) { XMLElement current = setNodes[i_0]; tileSet = new TMXTileSet(this, current, true); tileSet.index = i_0; if (lastSet != null) { lastSet.SetLimit(tileSet.firstGID - 1); } lastSet = tileSet; CollectionUtils.Add(tileSets, tileSet); } } List <XMLElement> layerNodes = docElement.List("layer"); for (int i_1 = 0; i_1 < layerNodes.Count; i_1++) { XMLElement current_2 = layerNodes[i_1]; TMXLayer layer = new TMXLayer(this, current_2); layer.index = i_1; CollectionUtils.Add(layers, layer); } List <XMLElement> objectGroupNodes = docElement .List("objectgroup"); for (int i_3 = 0; i_3 < objectGroupNodes.Count; i_3++) { XMLElement current_4 = objectGroupNodes[i_3]; TMXTileGroup objectGroup = new TMXTileGroup(current_4); objectGroup.index = i_3; CollectionUtils.Add(objectGroups, objectGroup); } defWidth = (int)(screenRect.GetWidth() / tileWidth); defHeight = (int)(screenRect.GetHeight() / tileHeight); } catch (Exception ex) { Console.Error.WriteLine(ex.StackTrace); throw new Exception("Failed to parse map", ex); } }
/// <summary> /// ��Ⱦ��ǰ�㻭�浽LGraphics֮�� /// </summary> /// /// <param name="g"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="sx"></param> /// <param name="sy"></param> /// <param name="width0"></param> /// <param name="ty"></param> /// <param name="isLine"></param> /// <param name="mapTileWidth"></param> /// <param name="mapTileHeight"></param> public void Draw(GLEx g, int x, int y, int sx, int sy, int width0, int height0, bool isLine, int mapTileWidth, int mapTileHeight) { if (width0 == 0 || height0 == 0) { return; } if (lightingOn) { } this.tmxTileSet = null; for (int tileset = 0; tileset < tmx.GetTileSetCount(); tileset++) { for (int ty = 0; ty < height0; ty++) { for (int tx = 0; tx < width0; tx++) { if ((sx + tx < 0) || (sy + ty < 0)) { continue; } if ((sx + tx >= this.width) || (sy + ty >= this.height)) { continue; } if (data[sx + tx,sy + ty,0] == tileset) { if (tmxTileSet == null) { tmxTileSet = tmx.GetTileSet(tileset); tmxTileSet.tiles.GLBegin(); } int sheetX = tmxTileSet .GetTileX(data[sx + tx,sy + ty,1]); int sheetY = tmxTileSet .GetTileY(data[sx + tx,sy + ty,1]); int tileOffsetY = tmxTileSet.tileHeight - mapTileHeight; cx = tx * mapTileWidth; cy = ty * mapTileHeight - tileOffsetY; if (lightingOn) { SetLightColor(cx / mapTileWidth, cy / mapTileHeight); } tmxTileSet.tiles .Draw(g, cx, cy, sheetX, sheetY); } } } if (tmxTileSet != null) { tmxTileSet.tiles.GLEnd(); if (lightingOn) { } if (isLine) { tmx.Draw(g, x, y, sx, sy, width0, height0, index); } isLightDirty = false; tmxTileSet = null; } } }