示例#1
0
文件: Map.cs 项目: JQE/TiledSharp
        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"));
        }
示例#2
0
文件: Tile.cs 项目: mispy/dungeon
 public void InitProps(PropertyDict tmxProps)
 {
     foreach (var pair in tmxProps)
     {
         if (pair.Key == "obstacle")
         {
             Flags.Obstacle = true;
         }
         else if (pair.Key == "creature")
         {
             Flags.Creature = true;
         }
         else if (pair.Key == "player")
         {
             Flags.Player = true;
         }
         else if (pair.Key == "item")
         {
             Flags.Item = true;
         }
         else if (pair.Key == "name")
         {
             Name         = pair.Value;
             ByName[Name] = this;
         }
         else
         {
             Props[pair.Key] = pair.Value;
         }
     }
 }
示例#3
0
文件: Map.cs 项目: Erikai/OpenORPG
        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"));
        }
示例#4
0
        public void NormalBehavior()
        {
            var videos = new List <(string VideoTitle, List <(DateTime ValidityStart, DateTime ValidityEnd)>)>()
            {
                ("a-yt-video",
                 new List <(DateTime ValidityStart, DateTime ValidityEnd)>()
                {
                    (new DateTime(2018, 1, 1), new DateTime(2018, 1, 2)),
                    (new DateTime(2018, 1, 2), new DateTime(2018, 1, 3)),
                    (new DateTime(2018, 1, 3), DateTime.MaxValue),
                })
            };

            YDS.ThereAreVideosWithManyVersions(videos);

            var videoReport    = GetReportOnVideoTable();
            var expectedReport = new PropertyDict()
            {
                { ContinuityProperty.NotEmpty, true },
                { ContinuityProperty.NoOverlapingInterval, true },
                { ContinuityProperty.UniqueCurrentValue, true },
            };

            CheckReport(expectedReport, videoReport);
        }
示例#5
0
		public TmxMap() {
			Tilesets = new TmxList<TmxTileset>();
			Layers = new TmxList<TmxLayer>();
			ObjectGroups = new TmxList<TmxObjectGroup>();
			ImageLayers = new TmxList<TmxImageLayer>();
			Properties = new PropertyDict();
		}
示例#6
0
        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"));
        }
示例#7
0
 public ExPropertyInfo GetExProperty(string name)
 {
     if (PropertyDict.ContainsKey(name))
     {
         return(new ExPropertyInfo(PropertyDict[name].GetProperty(), true));
     }
     return(null);
 }
示例#8
0
文件: Tools.cs 项目: bradur/FGJ-2019
 public static string GetProperty(PropertyDict properties, string property)
 {
     if (properties != null && properties.ContainsKey(property))
     {
         return(properties[property]);
     }
     return(null);
 }
示例#9
0
        public EntityPropertyDef?GetPropertyDef(string propertyName)
        {
            if (PropertyDict.ContainsKey(propertyName))
            {
                return(PropertyDict[propertyName]);
            }

            return(null);
        }
示例#10
0
    /// <summary>
    /// Gets the gid values of the tiles in an array. The gid are not the acctual gid values of the map, but the gid
    /// values of the tileset. Assume tileset a has 10 tiles and tileset b has 10 tiles. If at position 1 there is
    /// the tileset tile 1 of tileset a and at position 2 the tileset tile 1 of tileset b then the gid values of the
    /// return array are the same. To put it simply, just use one tileset and one layer for this method.
    /// </summary>
    /// <param name="mapName">The name of the map to get the gid values from.</param>
    /// <param name="prefabs">A list of all prefabs and their local position.</param>
    /// <returns>The gid values of the map.</returns>
    public Fast2DArray <int> GetReplacableMap(string mapName, out PropertyDict properties, out List <PrefabLocations> prefabs)
    {
        prefabs = new List <PrefabLocations>();

        TmxMap map = LoadMap(mapName);

        properties = map.Properties;

        Fast2DArray <int> retArr = new Fast2DArray <int>(map.Width, map.Height);

        if (map.TileLayers.Count == 0)
        {
            throw new Exception("Replacable map " + mapName + " has no tile layers!");
        }
        else if (map.TileLayers.Count > 1)
        {
            Debug.LogWarning("Replacable map " + mapName + " has more than 1 tile layer. I am only reading the first layer!");
        }
        if (map.Tilesets.Count != 1)
        {
            Debug.LogWarning("Replacable map " + mapName + " has not only 1 tileset. This might cause issues!");
        }

        for (int t = 0; t < map.TileLayers[0].Tiles.Count; t++)
        {
            TmxLayerTile layerTile = map.TileLayers[0].Tiles[t];
            int          gid       = GetGid(map, layerTile, out _);
            retArr.Set(gid, layerTile.X, map.Height - layerTile.Y - 1);
        }

        //LoadAllObjects(map, atX, atY, loadedGameobjects);

        // Go through each objectlayer and spawn their objects
        for (int i = 0; i < map.ObjectGroups.Count; i++)
        {
            for (int j = 0; j < map.ObjectGroups[i].Objects.Count; j++)
            {
                // Try to get the prefab
                if (TiledDict.Instance.TryGetObject(map.ObjectGroups[i].Objects[j].Type, out GameObject obj) == false)
                {
                    continue;
                }

                // Instantiate and position the object
                Vector3 pos;
                pos.x = ((float)map.ObjectGroups[i].Objects[j].X / map.TileWidth);
                pos.y = map.Height - ((float)map.ObjectGroups[i].Objects[j].Y / map.TileHeight);
                pos.z = 0f;

                prefabs.Add(new PrefabLocations {
                    Prefab = obj, Position = pos
                });
            }
        }

        return(retArr);
    }
示例#11
0
        public void EmptyTable()
        {
            var expectedReport = new PropertyDict()
            {
                { ContinuityProperty.NotEmpty, false },
                { ContinuityProperty.NoOverlapingInterval, true },
                { ContinuityProperty.UniqueCurrentValue, true },
            };
            var videoReport = GetReportOnVideoTable();

            CheckReport(expectedReport, videoReport);
        }
示例#12
0
        public TmxImageLayer(XElement xImageLayer, string tmxDir = "")
        {
            Name = (string) xImageLayer.Attribute("name");
            Width = (int) xImageLayer.Attribute("width");
            Height = (int) xImageLayer.Attribute("height");
            Visible = (bool?) xImageLayer.Attribute("visible") ?? true;
            Opacity = (double?) xImageLayer.Attribute("opacity") ?? 1.0;

            Image = new TmxImage(xImageLayer.Element("image"), tmxDir);

            Properties = new PropertyDict(xImageLayer.Element("properties"));
        }
示例#13
0
        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"));
        }
示例#14
0
        public TmxLayer(XElement xLayer, int width, int height)
        {
            Name = (string)xLayer.Attribute("name");
            Opacity = (double?)xLayer.Attribute("opacity") ?? 1.0;
            Visible = (bool?)xLayer.Attribute("visible") ?? true;
            XOffset = (float?)xLayer.Attribute("offsetx");
            YOffset = (float?)xLayer.Attribute("offsety");

            var xData = xLayer.Element("data");
            var encoding = (string)xData.Attribute("encoding");

            Tiles = new Collection<TmxLayerTile>();
            if (encoding == "base64")
            {
                var decodedStream = new TmxBase64Data(xData);
                var stream = decodedStream.Data;

                using (var br = new BinaryReader(stream))
                    for (int j = 0; j < height; j++)
                        for (int i = 0; i < width; i++)
                            Tiles.Add(new TmxLayerTile(br.ReadUInt32(), i, j));
            }
            else if (encoding == "csv")
            {
                var csvData = (string)xData.Value;
                int k = 0;
                foreach (var s in csvData.Split(','))
                {
                    var gid = uint.Parse(s.Trim());
                    var x = k % width;
                    var y = k / width;
                    Tiles.Add(new TmxLayerTile(gid, x, y));
                    k++;
                }
            }
            else if (encoding == null)
            {
                int k = 0;
                foreach (var e in xData.Elements("tile"))
                {
                    var gid = (uint)e.Attribute("gid");
                    var x = k % width;
                    var y = k / width;
                    Tiles.Add(new TmxLayerTile(gid, x, y));
                    k++;
                }
            }
            else throw new Exception("TmxLayer: Unknown encoding.");

            Properties = new PropertyDict(xLayer.Element("properties"));
        }
示例#15
0
文件: Tools.cs 项目: bradur/FGJ-2019
    public static List <string> getTexts(PropertyDict properties)
    {
        var result = new List <string>();
        var i      = 0;
        var key    = "text" + i;
        var text   = GetProperty(properties, key);

        while (text != null && text.Length > 0)
        {
            result.Add(text);
            key  = "text" + ++i;
            text = GetProperty(properties, key);
        }
        return(result);
    }
示例#16
0
		public TmxImageLayer(TmxMap map, XElement xImageLayer, string tmxDir = "")
			: base(map) {
			Name = (string)xImageLayer.Attribute("name");
			Width = (int)xImageLayer.Attribute("width");
			Height = (int)xImageLayer.Attribute("height");

			var xVisible = xImageLayer.Attribute("visible");
			Visible = xVisible == null || (bool)xVisible;

			var xOpacity = xImageLayer.Attribute("opacity");
			Opacity = xOpacity == null ? 1.0 : (double)xOpacity;

			Image = new TmxImage(xImageLayer.Element("image"), tmxDir);

			Properties = new PropertyDict(xImageLayer.Element("properties"));
		}
示例#17
0
    public void Initialize(GridObjectConfig gridObjectConfig, Vector2 position, PropertyDict objectProperties)
    {
        Properties         = objectProperties;
        config             = gridObjectConfig;
        transform.position = position;
        Player player = GetComponent <Player>();

        if (player != null)
        {
            GameManager.main.SetupPlayer(player);
        }
        spriteRenderer = GetComponent <SpriteRenderer>();
        if (spriteRenderer != null)
        {
            spriteRenderer.sortingOrder = -((int)Math.Floor(transform.position.y) * 2);
        }
    }
示例#18
0
        public void NormalBehaviorOnYoutubeVideoDbWrite()
        {
            var channelId = "fee";

            var titleAtypo = "The True Mening of Patriotism";
            var titleA     = "The True Meaning of Patriotism";
            var titleB     = "The Population Boom";

            var someLakeVideos = new[] {
                new Video()
                {
                    VideoId = "xyz", Title = titleAtypo, Duration = "PT5M11S"
                },
                new Video()
                {
                    VideoId = "zxy", Title = titleB, Duration = "PT2M6S"
                },
            };

            YDS.SomeVideosWereFound(someLakeVideos, channelId);

            Thread.Sleep(1000);

            someLakeVideos = new[] {
                new Video()
                {
                    VideoId = "xyz", Title = titleA, Duration = "PT5M11S"
                },
                new Video()
                {
                    VideoId = "zxy", Title = titleB, Duration = "PT2M6S"
                },
            };
            YDS.SomeVideosWereFound(someLakeVideos, channelId);

            var expectedReport = new PropertyDict()
            {
                { ContinuityProperty.NotEmpty, true },
                { ContinuityProperty.NoOverlapingInterval, true },
                { ContinuityProperty.UniqueCurrentValue, true },
            };
            var videoReport = GetReportOnVideoTable();

            CheckReport(expectedReport, videoReport);
        }
示例#19
0
        public TmxObject(XElement xObject)
        {
            Id = (int?)xObject.Attribute("id") ?? 0;
            Name = (string)xObject.Attribute("name") ?? String.Empty;
            X = (double)xObject.Attribute("x");
            Y = (double)xObject.Attribute("y");
            Width = (double?)xObject.Attribute("width") ?? 0.0;
            Height = (double?)xObject.Attribute("height") ?? 0.0;
            Type = (string)xObject.Attribute("type") ?? String.Empty;
            Visible = (bool?)xObject.Attribute("visible") ?? true;
            Rotation = (double?)xObject.Attribute("rotation") ?? 0.0;

            // Assess object type and assign appropriate content
            var xGid = xObject.Attribute("gid");
            var xEllipse = xObject.Element("ellipse");
            var xPolygon = xObject.Element("polygon");
            var xPolyline = xObject.Element("polyline");

            if (xGid != null)
            {
                Tile = new TmxLayerTile((uint)xGid,
                    Convert.ToInt32(Math.Round(X)),
                    Convert.ToInt32(Math.Round(X)));
                ObjectType = TmxObjectType.Tile;
            }
            else if (xEllipse != null)
            {
                ObjectType = TmxObjectType.Ellipse;
            }
            else if (xPolygon != null)
            {
                Points = ParsePoints(xPolygon);
                ObjectType = TmxObjectType.Polygon;
            }
            else if (xPolyline != null)
            {
                Points = ParsePoints(xPolyline);
                ObjectType = TmxObjectType.Polyline;
            }
            else ObjectType = TmxObjectType.Basic;

            Properties = new PropertyDict(xObject.Element("properties"));
        }
示例#20
0
 public void Init(int xPos, float yPos, int zPos, ObjectType objectType, PropertyDict properties)
 {
     originalRotation = transform.rotation;
     this.objectType  = objectType;
     this.xPos        = xPos;
     this.zPos        = zPos;
     if (this.objectType == ObjectType.ProjectileShooter)
     {
         ProjectileShooter projectileShooter = GetComponent <ProjectileShooter>();
         projectileShooter.Init((ProjectileHeading)GameManager.IntParseFast(properties["ObjectRotation"]));
     }
     else if (properties.ContainsKey("ObjectRotation"))
     {
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, GameManager.Rotations[GameManager.IntParseFast(properties["ObjectRotation"])], transform.eulerAngles.z);
     }
     transform.position = new Vector3(xPos, yPos, zPos);
     currentTile        = TileManager.main.GetTile(xPos, zPos);
     currentTile.AddObject(this);
 }
示例#21
0
 public object GetValue(object obj, string name)
 {
     if (obj.GetType() == typeof(TableInfo))
     {
         TableInfo ti = obj as TableInfo;
         if (name.ToUpper() == ti.PrimaryKeyName)
         {
             return(ti.ID);
         }
         return(ti.GetFieldValue(name));
     }
     else
     {
         if (!PropertyDict.ContainsKey(name))
         {
             throw new DataException(ErrorCodes.UnkownProperty);
         }
         Property p = PropertyDict[name];
         return(p.Info.GetValue(obj, null));
     }
 }
示例#22
0
		public TmxObject(XElement xObject) {
			var xName = xObject.Attribute("name");
			Name = xName == null ? "" : (string)xName;

			Type = (string)xObject.Attribute("type");
			X = (int)xObject.Attribute("x");
			Y = (int)xObject.Attribute("y");

			var xVisible = xObject.Attribute("visible");
			Visible = xVisible == null || (bool)xVisible;

			Width = (int?)xObject.Attribute("width");
			Height = (int?)xObject.Attribute("height");

			// Assess object type and assign appropriate content
			var xGid = xObject.Attribute("gid");
			var xEllipse = xObject.Element("ellipse");
			var xPolygon = xObject.Element("polygon");
			var xPolyline = xObject.Element("polyline");

			if (xGid != null) {
				Gid = (int?)xGid;
				ObjectType = ETmxObjectType.Tile;
			} else if (xEllipse != null) {
				ObjectType = ETmxObjectType.Ellipse;
			} else if (xPolygon != null) {
				Points = ParsePoints(xPolygon);
				ObjectType = ETmxObjectType.Polygon;
			} else if (xPolyline != null) {
				Points = ParsePoints(xPolyline);
				ObjectType = ETmxObjectType.Polyline;
			} else {
				ObjectType = ETmxObjectType.Basic;
			}

			Properties = new PropertyDict(xObject.Element("properties"));
		}
示例#23
0
 public TmxTerrain(XElement xTerrain)
 {
     Name = (string)xTerrain.Attribute("name");
     Tile = (int)xTerrain.Attribute("tile");
     Properties = new PropertyDict(xTerrain.Element("properties"));
 }
示例#24
0
        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"));
        }
示例#25
0
        private void LoadXml(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;
                Columns = ts.Columns;
                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;
                Columns = (int?)xTileset.Attribute("columns");
                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"));
            }
        }
示例#26
0
        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));
        }
示例#27
0
文件: Tileset.cs 项目: JQE/TiledSharp
        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"));
        }
示例#28
0
		public TmxLayer(TmxMap map, XElement xLayer, int width, int height)
			: this(map) {
			Name = (string)xLayer.Attribute("name");
			Width = (int)xLayer.Attribute("width");
			Height = (int)xLayer.Attribute("height");

			var xOpacity = xLayer.Attribute("opacity");
			Opacity = xOpacity == null ? 1.0 : (double)xOpacity;

			var xVisible = xLayer.Attribute("visible");
			Visible = xVisible == null || (bool)xVisible;

			var xData = xLayer.Element("data");
			var encoding = (string)xData.Attribute("encoding");

			Tiles = new List<TmxLayerTile>();
			if (encoding == "base64") {
				var base64data = Convert.FromBase64String((string)xData.Value);
				Stream stream = new MemoryStream(base64data, false);

				var compression = (string)xData.Attribute("compression");
				/*
				if (compression == "gzip")
					stream = new GZipStream(stream, CompressionMode.Decompress,
											false);
				else if (compression == "zlib")
					stream = new Ionic.Zlib.ZlibStream(stream,
								Ionic.Zlib.CompressionMode.Decompress, false);
				else 
				*/
				if (compression != null)
					throw new Exception("Tiled: Unknown compression.");

				using (stream)
				using (var br = new BinaryReader(stream))
					for (int j = 0; j < height; j++)
						for (int i = 0; i < width; i++)
							Tiles.Add(new TmxLayerTile(this, br.ReadUInt32(), i, j));
			} else if (encoding == "csv") {
				var csvData = (string)xData.Value;
				int k = 0;
				foreach (var s in csvData.Split(',')) {
					var gid = uint.Parse(s.Trim());
					var x = k % width;
					var y = k / width;
					Tiles.Add(new TmxLayerTile(this, gid, x, y));
					k++;
				}
			} else if (encoding == null) {
				int k = 0;
				foreach (var e in xData.Elements("tile")) {
					var gid = (uint)e.Attribute("gid");
					var x = k % width;
					var y = k / width;
					Tiles.Add(new TmxLayerTile(this, gid, x, y));
					k++;
				}
			} else throw new Exception("Tiled: Unknown encoding.");

			Properties = new PropertyDict(xLayer.Element("properties"));
		}
示例#29
0
 private static void CheckReport(PropertyDict report, PropertyDict expected)
 {
     Assert.Empty(report.Where(entry => expected[entry.Key] != entry.Value));
     Assert.Empty(expected.Where(entry => report[entry.Key] != entry.Value));
 }
示例#30
0
文件: MapData.cs 项目: bradur/LD33
    public void GenerateObjects(TmxObjectGroupList objectGroupList)
    {
        int objectGroupCount = objectGroupList.Count;

        for (int i = 0; i < objectGroupCount; i += 1)
        {
            TmxObjectGroup objectGroup = objectGroupList[i];
            int            objectCount = objectGroup.Objects.Count;
            PropertyDict   properties  = objectGroup.Properties;

            Transform layerContainer = new GameObject(objectGroup.Name).transform;
            layerContainer.rotation = Quaternion.identity;
            layerContainer.transform.SetParent(objectContainer);
            for (int j = 0; j < objectCount; j += 1)
            {
                if (!properties.ContainsKey("Type"))
                {
                    Debug.Log("ERROR: Object Layer \"" + objectGroup.Name + "\" doesn't have a Type property!");
                    continue;
                }
                string objectType = properties["Type"];
                TmxObjectGroup.TmxObject tmxObject = objectGroup.Objects[j];

                int xpos = (int)tmxObject.X / tile_width;
                int ypos = (int)tmxObject.Y / tile_height - 1;

                Vector3    worldPos      = new Vector3(-xpos + 0.5f, 0.5f, ypos - 0.5f);
                GameObject spawnedObject = (GameObject)GameObject.Instantiate(Resources.Load("Objects/" + objectType), worldPos, Quaternion.identity);
                spawnedObject.name = tmxObject.ToString();

                spawnedObject.transform.position = worldPos;
                spawnedObject.transform.parent   = layerContainer;

                if (objectType == "Hero")
                {
                    hero = spawnedObject.GetComponent <Hero>();
                    hero.Init(tileMap, xpos, ypos);
                }
                if (objectType == "End")
                {
                    endX = xpos;
                    endY = ypos;
                }

                /*TmxObjectGroup.TmxObject startEnd = ObjectGroups[i].Objects[j];
                 *
                 * int startEndX = (int)startEnd.X / tile_width;
                 * int startEndY = (int)startEnd.Y / tile_height;
                 * Vector3 worldPos = new Vector3(-startEndX + 0.5f, 1f, startEndY - 1.5f);
                 *
                 * if (startEnd.Name == "Start")
                 * {
                 *  //player.Spawn(worldPos, tile_width, tile_height);
                 *  GameObject startObject = (GameObject)GameObject.Instantiate(Resources.Load("SpawnPoint"), worldPos, Quaternion.identity);
                 * }
                 * else if (startEnd.Name == "End")
                 * {
                 *  GameObject endObjectPrefab = (GameObject)GameObject.Instantiate(Resources.Load("LevelEndTrigger"));
                 *  endObjectPrefab.transform.position = worldPos;
                 * }*/
            }
        }
    }
示例#31
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"));
            }
        }
示例#32
0
        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"));
        }
示例#33
0
文件: MapData.cs 项目: bradur/LD33
/*        int wallCount = map.ObjectGroups[2].Objects.Count;
 *      CreateWall(tileSheet);
 *
 *      for (int i = 0; i < wallCount; i++)
 *      {
 *          TmxObjectGroup.TmxObject wall = map.ObjectGroups[2].Objects[i];
 *          //Debug.Log("object at [" + enemy.X + ", " + enemy.Y + "]");
 *          int startEndX = (int)wall.X / tile_width;
 *          int startEndY = (int)wall.Y / tile_height;
 *          Vector3 worldPos = new Vector3(-startEndX, 0.5f, startEndY);
 *
 *          GameObject wallObject = (GameObject)GameObject.Instantiate(wallPref, worldPos, Quaternion.identity);
 *          //wallObject.transform.parent = wallContainer.transform;
 *          wallObject.transform.localPosition = worldPos;
 *      }
 *      if (Application.isPlaying)
 *      {
 *          GameObject.Destroy(wallPref);
 *      }*/

    /*
     * int objectCount = map.ObjectGroups[3].Objects.Count;
     *
     * for (int i = 0; i < objectCount; i++)
     * {
     *  TmxObjectGroup.TmxObject genericObject = map.ObjectGroups[3].Objects[i];
     *  //Debug.Log("object at [" + enemy.X + ", " + enemy.Y + "]");
     *  int startEndX = (int)genericObject.X / tile_width;
     *  int startEndY = (int)genericObject.Y / tile_height;
     *  Vector3 worldPos = new Vector3(-startEndX + 0.5f, 0.5f, startEndY -1.5f);
     *
     * }*/


    public void GenerateTiles(TmxLayerList layerList)
    {
        int layerCount = layerList.Count;

        for (int i = 0; i < layerCount; i += 1)
        {
            TmxLayer     layer      = layerList[i];
            int          tileCount  = layer.Tiles.Count;
            PropertyDict properties = layer.Properties;

            string tileType = "Ground";
            if (properties.ContainsKey("Type"))
            {
                tileType = properties["Type"];
            }
            if (tileType == "Ground")
            {
                tiles = new MapSquare[layer.Tiles.Count];
            }
            Transform layerContainer = new GameObject(layer.Name).transform;
            layerContainer.rotation = Quaternion.identity;
            layerContainer.transform.SetParent(tileContainer);

            //Debug.Log(tileCount);

            for (int j = 0; j < tileCount; j += 1)
            {
                TmxLayerTile tmxTile   = layer.Tiles[j];
                int          tileSetId = FindTileSetId(tmxTile.Gid);

                if (tileSetId == -1)
                {
                    continue;
                }
                if (tileType == "Wall")
                {
                    if (tmxTile.Gid == 0)
                    {
                        continue;
                    }
                    tileMap.AddNode(tmxTile.X, tmxTile.Y, false);

                    int     xpos     = (int)tmxTile.X;
                    int     ypos     = (int)tmxTile.Y;
                    Vector3 worldPos = new Vector3(-xpos + 0.5f, 0.5f, ypos - 0.5f);
                    //Debug.Log("[" + tmxTile.X + ", " + tmxTile.Y + "]");
                    GameObject spawnedTile;
                    if (wallPref == null)
                    {
                        CreateWall();
                    }
                    spawnedTile      = (GameObject)Instantiate(wallPref, worldPos, Quaternion.identity);
                    spawnedTile.name = "Wall_" + tmxTile.Gid;
                    spawnedTile.transform.position = worldPos;
                    spawnedTile.transform.parent   = layerContainer;
                }
                if (tileType == "Ground")
                {
                    if (tmxTile.Gid != 0)
                    {
                        tileMap.AddNode(tmxTile.X, tmxTile.Y);
                    }
                    tiles[j] = new MapSquare(tmxTile);
                }
            }
        }
    }