public TmxObjectGroup(XElement xObjectGroup) { Name = (string) xObjectGroup.Attribute("name") ?? String.Empty; Color = new TmxColor(xObjectGroup.Attribute("color")); Opacity = (double?) xObjectGroup.Attribute("opacity") ?? 1.0; Visible = (bool?) xObjectGroup.Attribute("visible") ?? true; OffsetX = (double?) xObjectGroup.Attribute("offsetx") ?? 0.0; OffsetY = (double?) xObjectGroup.Attribute("offsety") ?? 0.0; var drawOrderDict = new Dictionary<string, DrawOrderType> { {"unknown", DrawOrderType.UnknownOrder}, {"topdown", DrawOrderType.IndexOrder}, {"index", DrawOrderType.TopDown} }; var drawOrderValue = (string) xObjectGroup.Attribute("draworder"); if (drawOrderValue != null) DrawOrder = drawOrderDict[drawOrderValue]; Objects = new TmxList<TmxObject>(); foreach (var e in xObjectGroup.Elements("object")) Objects.Add(new TmxObject(e)); Properties = new PropertyDict(xObjectGroup.Element("properties")); }
public TmxMap(string filename) { XDocument xDoc = ReadXml(filename); var xMap = xDoc.Element("map"); Version = (string)xMap.Attribute("version"); Orientation = (OrientationType) Enum.Parse( typeof(OrientationType), xMap.Attribute("orientation").Value, true); Width = (int)xMap.Attribute("width"); Height = (int)xMap.Attribute("height"); TileWidth = (int)xMap.Attribute("tilewidth"); TileHeight = (int)xMap.Attribute("tileheight"); BackgroundColor = new TmxColor(xMap.Attribute("backgroundcolor")); Tilesets = new TmxList<TmxTileset>(); foreach (var e in xMap.Elements("tileset")) Tilesets.Add(new TmxTileset(e, TmxDirectory)); Layers = new TmxList<TmxLayer>(); foreach (var e in xMap.Elements("layer")) Layers.Add(new TmxLayer(e, Width, Height)); ObjectGroups = new TmxList<TmxObjectGroup>(); foreach (var e in xMap.Elements("objectgroup")) ObjectGroups.Add(new TmxObjectGroup(e)); ImageLayers = new TmxList<TmxImageLayer>(); foreach (var e in xMap.Elements("imagelayer")) ImageLayers.Add(new TmxImageLayer(e, TmxDirectory)); Properties = new PropertyDict(xMap.Element("properties")); }
public TmxMap() { Tilesets = new TmxList<TmxTileset>(); Layers = new TmxList<TmxLayer>(); ObjectGroups = new TmxList<TmxObjectGroup>(); ImageLayers = new TmxList<TmxImageLayer>(); Properties = new PropertyDict(); }
void Start() { var terrainDict = createTerrainDict(map.Tilesets); TmxList <TmxLayer> layers = map.Layers; foreach (var layer in map.Layers) { drawLayer(layer, terrainDict); } foreach (var objType in map.ObjectGroups) { foreach (var obj in objType.Objects) { Debug.Log(obj.Name); Debug.Log(obj.X / obj.Height); Debug.Log(obj.Y / obj.Width); createObject(obj.Name, obj.X, obj.Y, obj.Width, obj.Height); if (obj.Name == "start") { createObject("char", obj.X, obj.Y, obj.Width, obj.Height); } } } }
public List <Entity> loadEntities() { TmxList <TmxObject> elist = tmxmap.ObjectGroups["entity"].Objects; List <Entity> entities = new List <Entity>(); foreach (var entity_obj in elist) { string type = "bat"; int chiefId = 0; int numberMinion = 0; Vector2 entposition = new Vector2((float)entity_obj.X, (float)entity_obj.Y); if (entity_obj.Properties.Count > 0 && entity_obj.Properties["type"] != null) { type = entity_obj.Properties["type"]; if (type == "chief") { chiefId = Int32.Parse(entity_obj.Properties["id"]); numberMinion = Int32.Parse(entity_obj.Properties["numberMinion"]); } } if (type == "chief") { entities.Add(new Entity(entposition, type, chiefId, numberMinion)); } else { entities.Add(new Entity(entposition, type)); } } return(entities); }
public TmxMap(Stream stream) { XDocument xDoc = ReadXml(stream); XElement xMap = xDoc.Element("map"); Version = (string) xMap.Attribute("version"); Orientation = (OrientationType) Enum.Parse( typeof (OrientationType), xMap.Attribute("orientation").Value, true); Width = (int) xMap.Attribute("width"); Height = (int) xMap.Attribute("height"); TileWidth = (int) xMap.Attribute("tilewidth"); TileHeight = (int) xMap.Attribute("tileheight"); BackgroundColor = new TmxColor(xMap.Attribute("backgroundcolor")); Tilesets = new TmxList<TmxTileset>(); foreach (XElement e in xMap.Elements("tileset")) Tilesets.Add(new TmxTileset(e, TmxDirectory)); Layers = new TmxList<TmxLayer>(); foreach (XElement e in xMap.Elements("layer")) Layers.Add(new TmxLayer(e, Width, Height)); ObjectGroups = new TmxList<TmxObjectGroup>(); foreach (XElement e in xMap.Elements("objectgroup")) ObjectGroups.Add(new TmxObjectGroup(e)); if (xMap.Elements("imagelayer").Any()) throw new NotSupportedException( "Image layers are not supported in the current implementation. You can disable this warning at your own risk."); Properties = new PropertyDict(xMap.Element("properties")); }
private Dictionary <int, Tuple <IntRect, Texture> > ConvertGidDict(TmxList <TmxTileset> tilesets) { var gidDict = new Dictionary <int, Tuple <IntRect, Texture> >(); foreach (var tileset in tilesets) { var tilesetTexture = new Texture(tileset.Image.Source); var widthStart = tileset.Margin; var widthInc = tileset.TileWidth + tileset.Spacing; var widthEnd = tileset.Image.Width; var heightStart = tileset.Margin; var heightInc = tileset.TileHeight + tileset.Spacing; var heightEnd = tileset.Image.Height; var id = tileset.FirstGid; for (var height = heightStart; height < heightEnd; height += heightInc) { for (var width = widthStart; width < widthEnd; width += widthInc) { var rect = new IntRect(width, height, tileset.TileWidth, tileset.TileHeight); gidDict.Add(id, Tuple.Create(rect, tilesetTexture)); id += 1; } } } return(gidDict); }
// TMX tileset element constructor public TmxTileset(XElement xTileset, string tmxDir = "") { var xFirstGid = xTileset.Attribute("firstgid"); var source = (string) xTileset.Attribute("source"); if (source != null) { // Prepend the parent TMX directory if necessary source = Path.Combine(tmxDir, source); // source is always preceded by firstgid FirstGid = (int) xFirstGid; // Everything else is in the TSX file var xDocTileset = ReadXml(source); var ts = new TmxTileset(xDocTileset, TmxDirectory); Name = ts.Name; TileWidth = ts.TileWidth; TileHeight = ts.TileHeight; Spacing = ts.Spacing; Margin = ts.Margin; TileCount = ts.TileCount; TileOffset = ts.TileOffset; Image = ts.Image; Terrains = ts.Terrains; Tiles = ts.Tiles; Properties = ts.Properties; } else { // firstgid is always in TMX, but not TSX if (xFirstGid != null) FirstGid = (int) xFirstGid; Name = (string) xTileset.Attribute("name"); TileWidth = (int) xTileset.Attribute("tilewidth"); TileHeight = (int) xTileset.Attribute("tileheight"); Spacing = (int?) xTileset.Attribute("spacing") ?? 0; Margin = (int?) xTileset.Attribute("margin") ?? 0; TileCount = (int?) xTileset.Attribute("tilecount"); TileOffset = new TmxTileOffset(xTileset.Element("tileoffset")); Image = new TmxImage(xTileset.Element("image"), tmxDir); Terrains = new TmxList<TmxTerrain>(); var xTerrainType = xTileset.Element("terraintypes"); if (xTerrainType != null) { foreach (var e in xTerrainType.Elements("terrain")) Terrains.Add(new TmxTerrain(e)); } Tiles = new Collection<TmxTilesetTile>(); foreach (var xTile in xTileset.Elements("tile")) { var tile = new TmxTilesetTile(xTile, Terrains, tmxDir); Tiles.Add(tile); } Properties = new PropertyDict(xTileset.Element("properties")); } }
public List <Entry> loadEntries() { TmxList <TmxObject> elist = tmxmap.ObjectGroups["entry"].Objects; List <Entry> entries = new List <Entry>(); foreach (var entry_obj in elist) { if (!entry_obj.Points[0].Equals(null) && !entry_obj.Points[1].Equals(null)) { Vector2 evpoint1 = new Vector2((float)(entry_obj.X + entry_obj.Points[0].X), (float)(entry_obj.Y + entry_obj.Points[0].Y)); Vector2 evpoint2 = new Vector2((float)(entry_obj.X + entry_obj.Points[1].X), (float)(entry_obj.Y + entry_obj.Points[1].Y)); int itype = this.findEntryType(entry_obj.Type); if (itype != 4) { entryType type = (entryType)itype; Entry entry; if (evpoint1.X > evpoint2.X || evpoint1.Y > evpoint2.Y) { entry = new Entry(evpoint1, evpoint2, type); } else { entry = new Entry(evpoint2, evpoint1, type); } entries.Add(entry); } else { Debug.WriteLine("Type of entry [" + evpoint1.X + "/" + evpoint1.Y + "-" + evpoint2.X + "/" + evpoint2.Y + "] is undefined] => Fix it on Tiled"); } } } return(entries); }
private IEnumerable <Obstacle> Parse() { TmxList <TmxObject> tmxObjects = this.currentMap.ObjectGroups["Obstacles"].Objects; return(tmxObjects .Select(to => new Obstacle((int)to.X, (int)to.Y, (int)to.Width, (int)to.Height))); }
public TilesetManager(Graphics graphics, TmxList <TmxTileset> tilesets) { Graphics = graphics; foreach (var tileset in tilesets) { _tilesets.Add(tileset.Name, (Bitmap)Image.FromFile(tileset.Image.Source)); } }
public TmxObjectLayer(XMLReader xObjectGroup) : base(xObjectGroup, 0, 0) { Color = TmxHelpers.ParseTmxColor(xObjectGroup.Attribute("color")); DrawOrder = xObjectGroup.AttributeEnum <DrawOrder>("draworder"); Objects = new TmxList <TmxObject>(); foreach (XMLReader e in xObjectGroup.Elements("object")) { Objects.Add(new TmxObject(e)); } }
public void SetupTiles() { if (Core.Scene == null || enabled) { return; } //enabled = true; TmxMap map = Core.Scene.Content.LoadTiledMap("Assets/map.tmx"); Entity entity = Core.Scene.CreateEntity("tiled-map"); TiledMapRenderer tmr = entity.AddComponent(new TiledMapRenderer(map, "Collision", true)); TmxLayer CustomCollisionLayer = (TmxLayer)map.GetLayer("CustomCollision"); foreach (TmxLayerTile tile in CustomCollisionLayer.Tiles) { if (tile != null && tile.TilesetTile != null) { TmxList <TmxObjectGroup> objgl = tile.TilesetTile.ObjectGroups; if (objgl != null && objgl.Count > 0) { TmxObjectGroup objg = objgl[0]; if (objg.Objects != null && objg.Objects.Count > 0) { TmxObject obj = objg.Objects[0]; TmxObjectType type = obj.ObjectType; if (type == TmxObjectType.Ellipse) { //Draw Ellipse as collision Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2)) .AddComponent(new FSCollisionEllipse(obj.Width / 2, obj.Height / 2)) .AddComponent(new CircleCollider((obj.Width + obj.Height) / 4)); // have to get an average of sides, hence / 4 } else if (type == TmxObjectType.Polygon) { Vector2[] points = obj.Points; Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Tileset.TileWidth * tile.Position.X + obj.X, tile.Tileset.TileHeight * tile.Position.Y + obj.Y)) .AddComponent(new FSCollisionPolygon(points)) .AddComponent(new PolygonCollider(points)); } //basic is rectangle else if (type == TmxObjectType.Basic) { Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2)) .AddComponent(new FSCollisionBox(obj.Width, obj.Height)) .AddComponent(new BoxCollider(obj.Width, obj.Height)); } } } } } tmr.SetLayersToRender(new string[] { }); }
public TmxObjectGroup(XElement xObjectGroup) { Name = (string)xObjectGroup.Attribute("name"); Color = new TmxColor(xObjectGroup.Attribute("color")); Opacity = (double?)xObjectGroup.Attribute("opacity") ?? 1.0; Visible = (bool?)xObjectGroup.Attribute("visible") ?? true; Objects = new TmxList<TmxObject>(); foreach (var e in xObjectGroup.Elements("object")) Objects.Add(new TmxObject(e)); Properties = new PropertyDict(xObjectGroup.Element("properties")); }
private void LoadCollision() { TmxList <TmxObject> ObjectList = FindFloors(); if (ObjectList != null) { foreach (TmxObject thing in ObjectList) { Rectangle newR = new Rectangle((int)thing.X, (int)thing.Y, (int)thing.Width, (int)thing.Height); WallList.Add(newR); } } }
public void LoadMapNPCs(TileMap testMap) { TmxList <TmxObject> ObjectList = testMap.FindNPCs(); if (ObjectList != null) { foreach (TmxObject thing in ObjectList) { int adjustThingX = (int)(thing.X + (testMap._Postion.X + (thing.Width / 2))); int adjustThingY = (int)(thing.Y + (testMap._Postion.Y + (thing.Height / 2))); _NPCManager.CreateNPC(thing, new Vector2(adjustThingX, adjustThingY)); } } }
public void InitLevelEntites(cMapData level) { // this.monsters.Clear(); // this.allEntities.RemoveAll((cGameObject g) => g is cMonster ); TmxMap map = level.GetTmxMap(); TmxList <TmxObject> entityList = map.ObjectGroups["Entities"].Objects; foreach (var tmxEntity in entityList) { cMonster monster = new cMonster(this.refScene, new Vector2f((float)tmxEntity.X, (float)tmxEntity.Y)); this.AddMonster(monster); } }
public List <Rectangle> GetMapRectangles(string layerName) { TmxList <TmxObject> tmxObjects = _map.ObjectGroups[layerName].Objects; List <Rectangle> rectangles = new List <Rectangle>(); foreach (TmxObject tmxObject in tmxObjects) { Rectangle objectBox = new Rectangle((int)tmxObject.X, (int)tmxObject.Y, (int)tmxObject.Width, (int)tmxObject.Height); rectangles.Add(objectBox); } return(rectangles); }
/* creates dictionary for tiles */ private Dictionary <int, string> createTerrainDict(TmxList <TmxTileset> sets) { Dictionary <int, string> tdict = new Dictionary <int, string>(); foreach (var set in sets) { var firstGid = set.FirstGid; foreach (var terrain in set.Terrains) { tdict.Add(terrain.Tile + firstGid, terrain.Name); } } return(tdict); }
private void SetupCustomCollision() { TmxLayer CustomCollisionLayer = (TmxLayer)TileMap.GetLayer("CustomCollision"); foreach (TmxLayerTile tile in CustomCollisionLayer.Tiles) { if (tile != null && tile.TilesetTile != null) { TmxList <TmxObjectGroup> objgl = tile.TilesetTile.ObjectGroups; if (objgl != null && objgl.Count > 0) { TmxObjectGroup objg = objgl[0]; if (objg.Objects != null && objg.Objects.Count > 0) { TmxObject obj = objg.Objects[0]; TmxObjectType type = obj.ObjectType; if (type == TmxObjectType.Ellipse) { //Draw Ellipse as collision Entity.Scene.CreateEntity(obj.Name, new Vector2(Entity.Position.X + tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, Entity.Position.Y + tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2)) .AddComponent(new FSCollisionEllipse(obj.Width / 2, obj.Height / 2)) .AddComponent(new CircleCollider((obj.Width + obj.Height) / 4)); // have to get an average of sides, hence / 4 } else if (type == TmxObjectType.Polygon) { Vector2[] points = obj.Points; Entity.Scene.CreateEntity(obj.Name, new Vector2(Entity.Position.X + tile.Tileset.TileWidth * tile.Position.X + obj.X, Entity.Position.Y + tile.Tileset.TileHeight * tile.Position.Y + obj.Y)) .AddComponent(new FSCollisionPolygon(points)) .AddComponent(new PolygonCollider(points)); } //basic is rectangle else if (type == TmxObjectType.Basic) { Entity.Scene.CreateEntity(obj.Name, new Vector2(Entity.Position.X + tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, Entity.Position.Y + tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2)) .AddComponent(new FSCollisionBox(obj.Width, obj.Height)) .AddComponent(new BoxCollider(obj.Width, obj.Height)); Entity.AddComponent(new FSCollisionBox(obj.Width, obj.Height)).AddComponent(new BoxCollider(obj.Width, obj.Height)); } } } } } }
public TmxGroupedLayers(XMLReader xGroup, int width, int height) : base(xGroup, width, height) { Layers = new TmxList <TmxLayer>(); TileLayers = new TmxList <TmxLayer>(); ObjectGroups = new TmxList <TmxObjectLayer>(); ImageLayers = new TmxList <TmxImageLayer>(); Groups = new TmxList <TmxGroupedLayers>(); foreach (XMLReader e in xGroup.Elements().Where(x => x.Name == "layer" || x.Name == "objectgroup" || x.Name == "imagelayer" || x.Name == "group")) { TmxLayer layer; switch (e.Name) { case "layer": var tileLayer = new TmxLayer(e, width, height); layer = tileLayer; TileLayers.Add(tileLayer); break; case "objectgroup": var objectgroup = new TmxObjectLayer(e); layer = objectgroup; ObjectGroups.Add(objectgroup); break; case "imagelayer": var imagelayer = new TmxImageLayer(e); layer = imagelayer; ImageLayers.Add(imagelayer); break; case "group": var group = new TmxGroupedLayers(e, width, height); layer = group; Groups.Add(group); break; default: Engine.Log.Warning($"Unknown TMX layer type {e.Name}.", MessageSource.TMX); continue; } Layers.Add(layer); } }
public void LoadMapObjects(TileMap testMap) { TmxList <TmxObject> ObjectList = testMap.FindObjects(); if (ObjectList != null) { foreach (TmxObject thing in ObjectList) { Vector2 newPos = new Vector2((int)thing.X + testMap._Postion.X, (int)thing.Y + testMap._Postion.Y); //_WorldObjectManager.CreateObject(thing, newPos); if (thing.Type == "Dirt") { _WorldObjectManager.CreateObject(thing, newPos); } else { _GatherableManager.CreateGatherable(thing, newPos); } } } }
/** * Load tileset images. **/ void LoadSprites() { tilesets = tmxMap.Tilesets; tileSprites = new Dictionary <int, Sprite[]>(); firstGids = new int[tilesets.Count]; fullSprites = new Dictionary <int, Texture2D>(); int index = 0; foreach (TmxTileset tileset in tilesets) { // Get the path of the source image file string tilesetPath = tileset.Image.Source; tilesetPath = Tools.ReplaceString(tilesetPath, "../", ""); tilesetPath = Tools.ReplaceString(tilesetPath, ".png", ""); // Load the Resource as an array of Sprites(when sliced) int firstGid = tileset.FirstGid; firstGids[index] = firstGid; Object[] spriteObjs = Resources.LoadAll(tilesetPath); try { tileSprites[firstGid] = new Sprite[spriteObjs.Length - 1]; } catch (System.OverflowException) { Debug.Log("ERROR: Spritesheet \"" + tilesetPath + "\" is missing! Check filenames!"); } fullSprites[firstGid] = spriteObjs[0] as Texture2D; // Start from 1 since element 0 will be empty. for (int spriteObjIndex = 1; spriteObjIndex < spriteObjs.Length; spriteObjIndex++) { Sprite sprite = spriteObjs[spriteObjIndex] as Sprite; tileSprites[firstGid][spriteObjIndex - 1] = sprite; } index += 1; } }
internal TmxTilesetTile(XElement tile, TmxList<TmxTerrain> terrains, string tmxDir = "") { this.Id = (int)tile.Attribute("id"); this.TerrainEdges = new List<TmxTerrain>(4); var strTerrain = (string)tile.Attribute("terrain") ?? ",,,"; foreach (var v in strTerrain.Split(',')) { int result; var success = int.TryParse(v, out result); TmxTerrain edge; if (success) edge = terrains[result]; else edge = null; this.TerrainEdges.Add(edge); } this.Probability = (double?)tile.Attribute("probability") ?? 1.0; this.Image = new TmxImage(tile.Element("image"), tmxDir); this.Properties = new PropertyDict(tile.Element("properties")); }
public TileMap(string tmxFile, World world) { this.world = world; tmxMap = new TmxMap(tmxFile); layerCount = tmxMap.Layers.Count; tileArray = new Texture2D[layerCount]; tileWidthArray = new int[layerCount]; tileHeightArray = new int[layerCount]; tilesetTilesWideArray = new int[layerCount]; tilesetTilesHighArray = new int[layerCount]; for (int i = 0; i < layerCount; i++) { string textureName = tmxMap.Tilesets[0].Name.ToString(); tileArray[i] = ResourceLoader.LoadTexture2D(world.GraphicsDevice, textureName, "Content/" + textureName + ".png"); tileWidthArray[i] = tmxMap.Tilesets[0].TileWidth; tileHeightArray[i] = tmxMap.Tilesets[0].TileHeight; tilesetTilesWideArray[i] = tileArray[0].Width / tileWidthArray[i]; tilesetTilesHighArray[i] = tileArray[0].Height / tileHeightArray[i]; } TmxList <TmxObjectGroup> groups = tmxMap.ObjectGroups; foreach (TmxObjectGroup group in groups) { if (group.Name == "collisions") { TmxList <TmxObject> tmxObjects = group.Objects; foreach (TmxObject tmxObject in tmxObjects) { Collider collider = new SampleCollider(new Rectangle((int)tmxObject.X, (int)tmxObject.Y, (int)tmxObject.Width, (int)tmxObject.Height)); colliders.Add(collider); } } } }
public TmxMap(string filename) { XDocument xDoc = ReadXml(filename); var xMap = xDoc.Element("map"); Version = (string) xMap.Attribute("version"); Width = (int) xMap.Attribute("width"); Height = (int) xMap.Attribute("height"); TileWidth = (int) xMap.Attribute("tilewidth"); TileHeight = (int) xMap.Attribute("tileheight"); HexSideLength = (int?) xMap.Attribute("hexsidelength"); // Map orientation type var orientDict = new Dictionary<string, OrientationType> { {"unknown", OrientationType.Unknown}, {"orthogonal", OrientationType.Orthogonal}, {"isometric", OrientationType.Isometric}, {"staggered", OrientationType.Staggered}, {"hexagonal", OrientationType.Hexagonal}, }; var orientValue = (string) xMap.Attribute("orientation"); if (orientValue != null) Orientation = orientDict[orientValue]; // Hexagonal stagger axis var staggerAxisDict = new Dictionary<string, StaggerAxisType> { {"x", StaggerAxisType.X}, {"y", StaggerAxisType.Y}, }; var staggerAxisValue = (string) xMap.Attribute("staggeraxis"); if (staggerAxisValue != null) StaggerAxis = staggerAxisDict[staggerAxisValue]; // Hexagonal stagger index var staggerIndexDict = new Dictionary<string, StaggerIndexType> { {"odd", StaggerIndexType.Odd}, {"even", StaggerIndexType.Even}, }; var staggerIndexValue = (string) xMap.Attribute("staggerindex"); if (staggerIndexValue != null) StaggerIndex = staggerIndexDict[staggerIndexValue]; // Tile render order var renderDict = new Dictionary<string, RenderOrderType> { {"right-down", RenderOrderType.RightDown}, {"right-up", RenderOrderType.RightUp}, {"left-down", RenderOrderType.LeftDown}, {"left-up", RenderOrderType.LeftUp} }; var renderValue = (string) xMap.Attribute("renderorder"); if (renderValue != null) RenderOrder = renderDict[renderValue]; NextObjectID = (int?)xMap.Attribute("nextobjectid"); BackgroundColor = new TmxColor(xMap.Attribute("backgroundcolor")); Properties = new PropertyDict(xMap.Element("properties")); Tilesets = new TmxList<TmxTileset>(); foreach (var e in xMap.Elements("tileset")) Tilesets.Add(new TmxTileset(e, TmxDirectory)); Layers = new TmxList<TmxLayer>(); foreach (var e in xMap.Elements("layer")) Layers.Add(new TmxLayer(e, Width, Height)); ObjectGroups = new TmxList<TmxObjectGroup>(); foreach (var e in xMap.Elements("objectgroup")) ObjectGroups.Add(new TmxObjectGroup(e)); ImageLayers = new TmxList<TmxImageLayer>(); foreach (var e in xMap.Elements("imagelayer")) ImageLayers.Add(new TmxImageLayer(e, TmxDirectory)); }
private TmxLayer GetCollisionTilesLayer(TmxList <TmxLayer> layersSet) => layersSet[0];
// TMX tileset element constructor public TmxTileset(XElement xTileset, string tmxDir = "") { XAttribute xFirstGid = xTileset.Attribute("firstgid"); var source = (string) xTileset.Attribute("source"); if (source != null) { throw new NotSupportedException( "External tilesets are not yet supported. Please move the tileset or implement this."); // This won't work just yet; we'll need to implement a seperate loader probably // Prepend the parent TMX directory if necessary source = Path.Combine(tmxDir, source); // source is always preceded by firstgid FirstGid = (int) xFirstGid; // Everything else is in the TSX file XDocument xDocTileset = ReadXml(new FileStream(source, FileMode.Open)); var ts = new TmxTileset(xDocTileset, TmxDirectory); Name = ts.Name; TileWidth = ts.TileWidth; TileHeight = ts.TileHeight; Spacing = ts.Spacing; Margin = ts.Margin; TileOffset = ts.TileOffset; Image = ts.Image; Terrains = ts.Terrains; Tiles = ts.Tiles; Properties = ts.Properties; } else { // firstgid is always in TMX, but not TSX if (xFirstGid != null) FirstGid = (int) xFirstGid; Name = (string) xTileset.Attribute("name"); TileWidth = (int) xTileset.Attribute("tilewidth"); TileHeight = (int) xTileset.Attribute("tileheight"); Spacing = (int?) xTileset.Attribute("spacing") ?? 0; Margin = (int?) xTileset.Attribute("margin") ?? 0; TileOffset = new TmxTileOffset(xTileset.Element("tileoffset")); Image = new TmxImage(xTileset.Element("image"), tmxDir); Terrains = new TmxList<TmxTerrain>(); XElement xTerrainType = xTileset.Element("terraintypes"); if (xTerrainType != null) { foreach (XElement e in xTerrainType.Elements("terrain")) Terrains.Add(new TmxTerrain(e)); } Tiles = new List<TmxTilesetTile>(); foreach (XElement xTile in xTileset.Elements("tile")) { var tile = new TmxTilesetTile(xTile, Terrains, tmxDir); Tiles.Add(tile); } Properties = new PropertyDict(xTileset.Element("properties")); } }
public TmxTilesetTile(XElement xTile, TmxList<TmxTerrain> Terrains, string tmxDir = "") { Id = (int) xTile.Attribute("id"); TerrainEdges = new List<TmxTerrain>(4); int result; TmxTerrain edge; string strTerrain = (string) xTile.Attribute("terrain") ?? ",,,"; foreach (string v in strTerrain.Split(',')) { bool success = int.TryParse(v, out result); if (success) edge = Terrains[result]; else edge = null; TerrainEdges.Add(edge); } Probability = (double?) xTile.Attribute("probability") ?? 1.0; Properties = new PropertyDict(xTile.Element("properties")); }
public void CreateBoundingRects(TmxList<TmxObject> rectangles, PlayingState playingState) { foreach (TmxObject rectangle in rectangles) { Guid id = playingState.CreateEntity(); playingState.Entities.Where(l => l.ID == id).First().ComponentFlags = ComponentMasks.LevelObjects; Vector2 position = new Vector2((float)rectangle.X, (float)rectangle.Y); Rectangle boundedBox = new Rectangle((int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height); playingState.DirectionComponents[id] = new DirectionComponent() { Direction = 0f }; playingState.DisplayComponents[id] = new DisplayComponent() { Source = new Rectangle(342, 108, TileMap.TileSize, TileMap.TileSize) }; playingState.PositionComponents[id] = new PositionComponent() { Position = position }; playingState.AABBComponents[id] = new AABBComponent() { BoundedBox = boundedBox }; playingState.MapObjectComponents[id] = new MapObjectComponent() { Type = MapObjectType.Wall }; } }
public void CreateEnemySpawns(TmxList<TmxObject> enemySpawns, PlayingState playingState) { LevelCollisionDetection levelCollisionDetection = new LevelCollisionDetection(TileMap, TileMap.TileSize); foreach (TmxObject enemypawn in enemySpawns) { Guid id = playingState.CreateEntity(); playingState.Entities.Where(x => x.ID == id).First().ComponentFlags = ComponentMasks.Enemy; playingState.DirectionComponents[id] = new DirectionComponent() { Direction = (float)(enemypawn.Rotation * (Math.PI / 180)) }; playingState.DisplayComponents[id] = new DisplayComponent() { Source = new Rectangle(343, 470, TileMap.TileSize, TileMap.TileSize) }; playingState.HealthComponents[id] = new HealthComponent() { Health = 15 }; playingState.PositionComponents[id] = new PositionComponent() { Position = new Vector2((float)enemypawn.X + (TileMap.TileSize / 2), (float)enemypawn.Y + (TileMap.TileSize / 2)) }; playingState.VelocityComponents[id] = new VelocityComponent() { xVelocity = 0f, yVelocity = 0f, xTerminalVelocity = 3f, yTerminalVelocity = 3f }; playingState.AccelerationComponents[id] = new AccelerationComponent() { xAcceleration = 3f, yAcceleration = 3f }; playingState.DamageComponents[id] = new DamageComponent() { AttackRange = 1 * TileMap.TileSize, Damage = 5 }; //our origin is going to offset our bounded box playingState.AABBComponents[id] = new AABBComponent() { BoundedBox = new Rectangle((int)enemypawn.X, (int)enemypawn.Y, TileMap.TileSize, TileMap.TileSize) }; //parse patrol paths string patrolPaths = enemypawn.Properties["PatrolPath"]; List<Vector2> PatrolVectors = new List<Vector2>(); if (!string.IsNullOrWhiteSpace(patrolPaths)) { string[] newPaths = patrolPaths.Split('|'); for (int i = 0; i < newPaths.Length; i++) { newPaths[i] = newPaths[i].Replace("(", ""); newPaths[i] = newPaths[i].Replace(")", ""); int x = Int32.Parse(newPaths[i].Split(',')[0]); int y = Int32.Parse(newPaths[i].Split(',')[1]); //center our points too x = (x * TileMap.TileSize) + (TileMap.TileSize / 2); y = (y * TileMap.TileSize) + (TileMap.TileSize / 2); PatrolVectors.Add(new Vector2(x, y)); } } playingState.AIComponents[id] = new AIComponent() { ActiveState = new Stack<AIState>(), LineOfSight = 6 * TileMap.TileSize, PatrolPath = PatrolVectors, ActivePath = new LinkedList<Tile>(), Astar = TileMap.aStar, AITree = AIPawnSystem.CreatePawnTree(id, playingState, levelCollisionDetection, TileMap.TileSize, TileMap.TileSize) }; playingState.AIComponents[id].ActiveState.Push(AIState.STILL); playingState.LabelComponents[id] = new LabelComponent() { Label = "", Position = new Vector2((float)enemypawn.X, (float)(enemypawn.Y - 5)) }; } }
public void CreatePlayerSpawn(TmxList<TmxObject> playerSpawns, PlayingState playingState) { foreach (TmxObject playerspawn in playerSpawns) { Guid id = playingState.CreateEntity(); playingState.Entities.Where(x => x.ID == id).First().ComponentFlags = ComponentMasks.Player; playingState.DirectionComponents[id] = new DirectionComponent() { Direction = 0f }; playingState.DisplayComponents[id] = new DisplayComponent() { Source = new Rectangle(451, 470, TileMap.TileSize, TileMap.TileSize) }; playingState.HealthComponents[id] = new HealthComponent() { Health = 100 }; playingState.PositionComponents[id] = new PositionComponent() { Position = new Vector2((float)playerspawn.X, (float)playerspawn.Y) }; playingState.VelocityComponents[id] = new VelocityComponent() { xVelocity = 0f, yVelocity = 0f, xTerminalVelocity = 6f, yTerminalVelocity = 6f }; playingState.AccelerationComponents[id] = new AccelerationComponent() { xAcceleration = 6f, yAcceleration = 6f }; //our origin is going to offset our bounded box playingState.AABBComponents[id] = new AABBComponent(){ BoundedBox = new Rectangle((int)playerspawn.X - (TileMap.TileSize / 2), (int)playerspawn.Y - (TileMap.TileSize / 2), TileMap.TileSize, TileMap.TileSize)}; } }
/// <summary> /// This mehod loads images in dictionary by path discribed in tilesets list. /// For example, we have map made in Tiled. Tiled save map in XML format. This XML file contains /// information about tilesets in map. We load this information. Than we load images by this /// information. We load all images, that may be used in map building in Tiled. /// </summary> /// <param name="tilesets"></param> /// <returns></returns> private void LoadImagesFromTilesets(TmxList<TmxTileset> tilesets) { this.tmxTilesetsImages.Clear(); try { foreach (TmxTileset tileset in tilesets) { LoadTilesetImages(tileset); } } catch (Exception exp) { MessageBox.Show(exp.Message); } }
/// <summary> /// This method load animation info from tilesets. Then we will use this information /// to create animated objects. /// </summary> /// <param name="tilesets"></param> private void LoadAnimationsFromTilesets(TmxList<TmxTileset> tilesets) { this.tmxAnimations.Clear(); foreach (TmxTileset tileset in tilesets) { foreach (TmxTilesetTile tile in tileset.Tiles) { if (tile.AnimationFrames.Count != 0) { Int32 tileKey = tileset.FirstGid + tile.Id; List<ResourceFrame> animation = new List<ResourceFrame>(); foreach (var frame in tile.AnimationFrames) { Int32 frameKey = tileset.FirstGid + frame.Id; ResourceFrame nFrame = new ResourceFrame(frameKey, frame.Duration); animation.Add(nFrame); } tmxAnimations.Add(tileKey, animation); } } } }
public TileEngine(int mapWidth, int mapHeight, Collection <TmxLayerTile> tiles, TmxTileset tileset, TmxList <TmxObjectGroup> objectGroups) { Map = new byte[mapWidth, mapHeight]; foreach (var tile in tiles) { Map[tile.X, tile.Y] = (byte)tile.Gid; } TilesetName = tileset.Name; TileWidth = tileset.TileWidth; TileHeight = tileset.TileHeight; TileMapTilesWide = mapWidth; TileMapTilesHigh = mapHeight; CollidableGids = new List <int>(); foreach (var tile in tileset.Tiles) { if (tile.Value.ObjectGroups != null) { CollidableGids.Add(tile.Value.Id); } } }
private List <TmxLayer> ConvertDrawableLayers(TmxList <TmxLayer> layersSet) => layersSet.Skip(1).ToList();
public TmxTilesetTile(XElement xTile, TmxList<TmxTerrain> Terrains, string tmxDir = "") { Id = (int)xTile.Attribute("id"); TerrainEdges = new Collection<TmxTerrain>(); int result; TmxTerrain edge; var strTerrain = (string)xTile.Attribute("terrain") ?? ",,,"; foreach (var v in strTerrain.Split(',')) { var success = int.TryParse(v, out result); if (success) edge = Terrains[result]; else edge = null; TerrainEdges.Add(edge); // TODO: Assert that TerrainEdges length is 4 } Probability = (double?)xTile.Attribute("probability") ?? 1.0; Image = new TmxImage(xTile.Element("image"), tmxDir); ObjectGroups = new TmxList<TmxObjectGroup>(); foreach (var e in xTile.Elements("objectgroup")) ObjectGroups.Add(new TmxObjectGroup(e)); AnimationFrames = new Collection<TmxAnimationFrame>(); if (xTile.Element("animation") != null) { foreach (var e in xTile.Element("animation").Elements("frame")) AnimationFrames.Add(new TmxAnimationFrame(e)); } Properties = new PropertyDict(xTile.Element("properties")); }
public TmxTilesetTile(XElement xTile, TmxList<TmxTerrain> Terrains, string tmxDir = "") { Id = (int)xTile.Attribute("id"); var strTerrain = ((string)xTile.Attribute("terrain")).Split(','); int result; for (var i = 0; i < 4; i++) { var success = int.TryParse(strTerrain[i], out result); if (success) TerrainEdges[i] = Terrains[result]; else TerrainEdges[i] = null; } Probability = (double?)xTile.Attribute("probability") ?? 1.0; Image = new TmxImage(xTile.Element("image"), tmxDir); Properties = new PropertyDict(xTile.Element("properties")); }