Пример #1
0
        void mapParse(XmlDocument xmlDc)
        {
            Maps = new Dictionary <string, Map>();
            foreach (XmlNode node in xmlDc.SelectNodes("Resource/Map"))
            {
                Map m = new Map();
                m.options = new Dictionary <string, string>();
                int           width         = -1;
                int           height        = -1;
                Sprite        defaultSprite = badSprite;
                List <mapObj> objs          = new List <mapObj>();
                foreach (XmlNode child in node.ChildNodes)
                {
                    child.InnerText = child.InnerText.Trim();
                    switch (child.Name.ToLower())
                    {
                    case "name":
                        m.name = child.InnerText;
                        break;

                    case "width":
                        try { width = Convert.ToInt32(child.InnerText); }
                        catch { Game1.dbg.Log("Map incorrect width"); }
                        break;

                    case "height":
                        try { height = Convert.ToInt32(child.InnerText); }
                        catch { Game1.dbg.Log("Map incorrect height"); }
                        break;

                    case "maintilename":
                        defaultSprite = getSprite(child.InnerText.ToLower());
                        break;

                    default:
                        if (child.Name != "object")
                        {
                            m.options.Add(child.Name, child.InnerText);
                        }
                        break;
                    }
                }
                int counter = 0;

                foreach (XmlNode objNode in xmlDc.SelectNodes("Resource/object"))
                {
                    mapObj mobj = new mapObj();
                    foreach (XmlNode childObj in objNode.ChildNodes)
                    {
                        childObj.InnerText = childObj.InnerText.Trim().ToLower();
                        switch (childObj.Name.ToLower())
                        {
                        case "name":
                            mobj.name = childObj.InnerText;
                            break;

                        case "sprite":
                            mobj.sprite = getSprite(childObj.InnerText);
                            break;

                        case "x":
                            try { mobj.x = Convert.ToInt32(childObj.InnerText); }
                            catch { Game1.dbg.Log("Map incorrect object x"); }
                            break;

                        case "y":
                            try { mobj.y = Convert.ToInt32(childObj.InnerText); }
                            catch { Game1.dbg.Log("Map incorrect object y"); }
                            break;
                        }
                    }
                    if (mobj.name == null)
                    {
                        mobj.name = "Anonym" + counter;
                    }
                    objs.Add(mobj);
                }
                if (width < 0 || height < 0)
                {
                    Game1.dbg.Log("Map width or height is incorrect", GameException.LevelCatastrophic);
                }
                if (defaultSprite.Equals(badSprite))
                {
                    Game1.dbg.Log("Default sprite not defined", GameException.LevelCatastrophic);
                }
                m.data = new Sprite[width, height];
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        m.data[i, j] = defaultSprite.Copy();
                    }
                }
                m.defaultSprite = defaultSprite;
                for (int i = 0; i < objs.Count; i++)
                {
                    if (objs[i].x < 0 || objs[i].y < 0)
                    {
                        Game1.dbg.Log("Object coordinates incorrect", GameException.LevelCatastrophic);
                    }
                    m.data[objs[i].y, objs[i].x] = objs[i].sprite.Copy();
                }
                m.width  = width;
                m.height = height;
                Maps.Add(m.name, m);
            }
        }
Пример #2
0
        void mapParse(XmlDocument xmlDc)
        {
            Maps = new Dictionary<string, Map>();
            foreach (XmlNode node in xmlDc.SelectNodes("Resource/Map"))
            {
                Map m = new Map();
                m.options = new Dictionary<string, string>();
                int width = -1;
                int height = -1;
                Sprite defaultSprite = badSprite;
                List<mapObj> objs = new List<mapObj>();
                foreach (XmlNode child in node.ChildNodes)
                {
                    child.InnerText = child.InnerText.Trim();
                    switch (child.Name.ToLower())
                    {
                        case "name":
                            m.name = child.InnerText;
                            break;
                        case "width":
                            try { width = Convert.ToInt32(child.InnerText); }
                            catch { Game1.dbg.Log("Map incorrect width"); }
                            break;
                        case "height":
                            try { height = Convert.ToInt32(child.InnerText); }
                            catch { Game1.dbg.Log("Map incorrect height"); }
                            break;
                        case "maintilename":
                            defaultSprite = getSprite(child.InnerText.ToLower());
                            break;
                        default:
                            if (child.Name != "object")
                                m.options.Add(child.Name, child.InnerText);
                            break;
                    }
                }
                int counter = 0;

                foreach (XmlNode objNode in xmlDc.SelectNodes("Resource/object"))
                {
                    mapObj mobj = new mapObj();
                    foreach (XmlNode childObj in objNode.ChildNodes)
                    {
                        childObj.InnerText = childObj.InnerText.Trim().ToLower();
                        switch (childObj.Name.ToLower())
                        {
                            case "name":
                                mobj.name = childObj.InnerText;
                                break;
                            case "sprite":
                                mobj.sprite = getSprite(childObj.InnerText);
                                break;
                            case "x":
                                try { mobj.x = Convert.ToInt32(childObj.InnerText); }
                                catch { Game1.dbg.Log("Map incorrect object x"); }
                                break;
                            case "y":
                                try { mobj.y = Convert.ToInt32(childObj.InnerText); }
                                catch { Game1.dbg.Log("Map incorrect object y"); }
                                break;
                        }
                    }
                    if (mobj.name == null) mobj.name = "Anonym" + counter;
                    objs.Add(mobj);
                }
                if (width < 0 || height < 0)
                    Game1.dbg.Log("Map width or height is incorrect", GameException.LevelCatastrophic);
                if (defaultSprite.Equals(badSprite))
                    Game1.dbg.Log("Default sprite not defined", GameException.LevelCatastrophic);
                m.data = new Sprite[width, height];
                for (int i = 0; i < height; i++)
                    for (int j = 0; j < width; j++)
                        m.data[i, j] = defaultSprite.Copy();
                m.defaultSprite = defaultSprite;
                for (int i = 0; i < objs.Count; i++)
                {
                    if (objs[i].x < 0 || objs[i].y < 0)
                        Game1.dbg.Log("Object coordinates incorrect", GameException.LevelCatastrophic);
                    m.data[objs[i].y, objs[i].x] = objs[i].sprite.Copy();
                }
                m.width = width;
                m.height = height;
                Maps.Add(m.name, m);
            }
        }