예제 #1
0
        public void LoadFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                var xml = new XmlDocument();
                xml.Load(fileName);

                Filename = Path.GetFileName(fileName);

                // Tileset->Images
                foreach (XmlNode imageNode in xml.ChildNodes[0].ChildNodes)
                {
                    if (imageNode.Name.ToLower().Equals("image"))
                    {
                        var image = new Image()
                        {
                            Name         = imageNode.Attributes["file"].Value,
                            SpriteWidth  = imageNode.Attributes["width"].ToValue <int>(),
                            SpriteHeight = imageNode.Attributes["height"].ToValue <int>()
                        };

                        var texture = new Bitmap(Path.Combine(ResourceUtils.GetResourcePath(ResourceUtils.AssetsType.Gfx),
                                                              imageNode.Attributes["file"].Value));

                        foreach (XmlNode spriteNode in imageNode.ChildNodes)
                        {
                            if (spriteNode.Name.ToLower().Equals("sprite"))
                            {
                                var sprite = new Sprite()
                                {
                                    ID     = spriteNode.Attributes["id"].ToValue <int>(),
                                    X      = spriteNode.Attributes["x"].ToValue <int>(),
                                    Y      = spriteNode.Attributes["y"].ToValue <int>(),
                                    Frames = spriteNode.Attributes["frames"].ToValue <int>()
                                };

                                sprite.Texture = GraphicsUtils.Cut(image.SpriteWidth * sprite.X, image.SpriteHeight * sprite.Y,
                                                                   image.SpriteWidth, image.SpriteHeight, texture);

                                image.Sprites.Add(sprite);
                            }
                        }

                        Images.Add(image);
                    }
                }
            }
        }
예제 #2
0
        public void LoadFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                Point spriteGroupPosition = Point.Empty;

                var xml = new XmlDocument();
                xml.Load(fileName);

                Filename = Path.GetFileName(fileName);

                foreach (XmlNode entityNode in xml.ChildNodes[0].ChildNodes)
                {
                    if (entityNode.Name.ToLower().Equals("object"))
                    {
                        var entity = new Entity()
                        {
                            ID       = int.Parse(entityNode.Attributes["id"].Value),
                            Filename = entityNode.Attributes["texture"].Value
                        };

                        var texture = new Bitmap(Path.Combine(ResourceUtils.GetResourcePath(ResourceUtils.AssetsType.Gfx),
                                                              entityNode.Attributes["texture"].Value));

                        foreach (XmlNode spriteNode in entityNode.ChildNodes)
                        {
                            if (spriteNode.Name.ToLower().Equals("sprite"))
                            {
                                if (ValidateSpriteGroup(spriteNode.Attributes["name"].Value))
                                {
                                    if (spriteNode.Attributes["name"].Value.Split('_')[1] == "a")
                                    {
                                        spriteGroupPosition = new Point(spriteNode.ChildNodes[0].Attributes["x"].ToValue <int>(), spriteNode.ChildNodes[0].Attributes["y"].ToValue <int>());
                                    }
                                }
                                else
                                {
                                    spriteGroupPosition = new Point(spriteNode.ChildNodes[0].Attributes["x"].ToValue <int>(), spriteNode.ChildNodes[0].Attributes["y"].ToValue <int>());
                                }

                                var sprite = new Sprite()
                                {
                                    Name = spriteNode.Attributes["name"].Value,
                                    X    = spriteGroupPosition.X,
                                    Y    = spriteGroupPosition.Y,
                                };

                                sprite.Texture = GraphicsUtils.Cut(spriteNode.ChildNodes[0].Attributes["x"].ToValue <int>(),
                                                                   spriteNode.ChildNodes[0].Attributes["y"].ToValue <int>(),
                                                                   spriteNode.Attributes["width"].ToValue <int>(),
                                                                   spriteNode.Attributes["height"].ToValue <int>(), texture);

                                entity.Sprites.Add(sprite);
                            }
                        }

                        var drawNode = entityNode.ChildNodes.FindNode("draw");
                        foreach (XmlNode variationNode in drawNode.ChildNodes)
                        {
                            var template = new Template()
                            {
                                Name = variationNode.Attributes["name"].Value,
                            };

                            foreach (XmlNode layerNode in variationNode.ChildNodes)
                            {
                                var layer = new Layer()
                                {
                                    Name   = layerNode.Attributes["name"].Value,
                                    Offset = new Point(layerNode.Attributes["xoffset"].ToValue <int>(), layerNode.Attributes["yoffset"].ToValue <int>()),
                                };

                                if (layerNode.Name.ToLower().Equals("background"))
                                {
                                    layer.Level = Layer.LevelType.Background;
                                }
                                else if (layerNode.Name.ToLower().Equals("foreground"))
                                {
                                    layer.Level = Layer.LevelType.Foreground;
                                }

                                template.Layers.Add(layer);
                            }

                            entity.Templates.Add(template);
                        }

                        Entities.Add(entity);
                    }
                }
            }
        }