コード例 #1
0
ファイル: UnitTemplate.cs プロジェクト: JJJohan/RTS
        public Unit Load(UnitPrefab a_prefab, Vector3 a_pos, Vector3 a_rot)
        {
            // Properties
            Unit.Properties properties = new Unit.Properties();
            properties.pos = a_pos;
            properties.rot = a_rot;
            properties.health = a_prefab.health;
            properties.buildTime = a_prefab.buildTime;
            properties.cost = a_prefab.cost;
            properties.miniSize = a_prefab.miniMapSize;
            properties.ID = a_prefab.ID;
            properties.turnSpeed = a_prefab.turnSpeed;
            properties.speed = a_prefab.speed;
            properties.accel = a_prefab.accel;

            // Type
            Unit unit = null;
            switch (a_prefab.type)
            {
                case Unit.Type.DOZER:
                    unit = new Dozer(properties, a_prefab.model, a_prefab.texture);
                    break;

                //case Unit.Type.INFANTRY:
                    //unit = new Infantry(properties, a_prefab.model, a_prefab.texture);
                    //break;

                default:
                    unit = new Unit(properties, a_prefab.model, a_prefab.texture);
                    break;
            }

            return unit;
        }
コード例 #2
0
ファイル: FileParser.cs プロジェクト: JJJohan/RTS
        public static void ParseXML(string a_name, bool a_zip)
        {
            XmlDocument xml = new XmlDocument();
            List<ZipEntry> entries = null;
            if (a_zip)
            {
                entries = m_dataFile.SelectEntries(a_name).ToList();
                xml.Load(entries[0].OpenReader());
            }
            else
            {
                xml.Load(a_name);
            }

            XmlNode node = xml.FirstChild;
            while (node != null && node.Name != "Building" && node.Name != "Unit")
            {
                node = node.NextSibling;
            }

            if (node == null)
                goto XMLError;

            PreHash prehash = new PreHash();
            prehash.techReqs = new List<string>();

            if (node.Name == "Building")
            {
                // Name
                XmlAttributeCollection attribs = node.Attributes;
                if (attribs.Count != 1)
                    goto XMLError;
                XmlNode attrib = attribs.GetNamedItem("ID");
                if (attrib == null)
                    goto XMLError;
                prehash.shortName = attrib.Value;

                // Duplicate check
                foreach(PreHash pre in m_prehash.Values)
                {
                    if (pre.shortName == prehash.shortName)
                    {
                        Logger.LogWarning("Prefab overwrite attempt detected - possible mod: " + prehash.shortName);
                        return;
                    }
                }

                // Create prefab
                BuildingPrefab prefab = new BuildingPrefab();
                prefab.dataItem = a_zip;
                prefab.type = Building.Type.DEFAULT;

                // Properties
                XmlNodeList nodes = node.SelectNodes("Properties");
                if (nodes.Count != 1)
                    goto XMLError;
                attribs = nodes[0].Attributes;
                if (attribs.Count < 7 || attribs.Count > 8)
                    goto XMLError;
                attrib = attribs.GetNamedItem("Name");
                if (attrib == null)
                    goto XMLError;
                prefab.name = attrib.Value;
                attrib = attribs.GetNamedItem("MenuID");
                if (attrib == null)
                    goto XMLError;
                prefab.menuID = Int32.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("Cost");
                if (attrib == null)
                    goto XMLError;
                prefab.cost = Int32.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("PowerUsage");
                if (attrib == null)
                    goto XMLError;
                prefab.powerUsage = Int32.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("BuildTime");
                if (attrib == null)
                    goto XMLError;
                prefab.buildTime = Int32.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("Health");
                if (attrib == null)
                    goto XMLError;
                prefab.health = Int32.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("MiniMapIconSize");
                if (attrib == null)
                    goto XMLError;
                if (attrib.Value.Split(',').Length != 2)
                    goto XMLError;
                prefab.miniMapSize = new Vector2(Int32.Parse(attrib.Value.Split(',')[0]), Int32.Parse(attrib.Value.Split(',')[1]));
                attrib = attribs.GetNamedItem("BuildingType");
                if (attrib != null)
                {
                    if (attrib.Value == "HEADQUARTERS")
                      prefab.type = Building.Type.HEADQUARTERS;
                    else if (attrib.Value == "POWERFACTORY")
                      prefab.type = Building.Type.POWERFACTORY;
                    else if (attrib.Value == "UNITFACTORY")
                      prefab.type = Building.Type.UNITFACTORY;
                }

                // Tech
                prefab.techReqs = new List<int>();
                prefab.techUpgrades = new List<BuildingPrefab.Upgrade>();

                nodes = node.SelectNodes("Tech");
                if (nodes.Count != 1)
                    goto XMLError;
                attribs = nodes[0].Attributes;
                attrib = attribs.GetNamedItem("ReplaceModel");
                if (attrib == null)
                {
                    if (attrib.Value == "1")
                        prefab.replace = 1;
                }
                else
                {
                    prefab.replace = 0;
                }

                nodes = node.SelectNodes("Tech/TechReq");
                foreach (XmlNode n in nodes)
                {
                    attribs = n.Attributes;
                    if (attribs.Count != 1)
                        goto XMLError;
                    attrib = attribs.GetNamedItem("Value");
                    if (attrib == null)
                        goto XMLError;
                    prehash.techReqs.Add(attrib.Value);
                }

                nodes = node.SelectNodes("Tech/Upgrade");
                foreach (XmlNode n in nodes)
                {
                    attribs = n.Attributes;
                    if (attribs.Count < 4)
                        goto XMLError;
                    if (prefab.techUpgrades == null)
                        prefab.techUpgrades = new List<BuildingPrefab.Upgrade>();
                    BuildingPrefab.Upgrade upgrade = new BuildingPrefab.Upgrade();
                    upgrade.health = 0;
                    upgrade.power = 0;
                    upgrade.productionMulti = 1f;
                    attrib = attribs.GetNamedItem("Name");
                    if (attrib == null)
                        goto XMLError;
                    upgrade.name = attrib.Value;
                    attrib = attribs.GetNamedItem("Cost");
                    if (attrib == null)
                        goto XMLError;
                    upgrade.cost = Int32.Parse(attrib.Value);
                    attrib = attribs.GetNamedItem("UpgradeTime");
                    if (attrib == null)
                        goto XMLError;
                    upgrade.buildTime = Int32.Parse(attrib.Value);
                    attrib = attribs.GetNamedItem("AdditionalHealth");
                    if (attrib != null)
                        upgrade.health = Int32.Parse(attrib.Value);
                    attrib = attribs.GetNamedItem("AdditionalPowerUsage");
                    if (attrib != null)
                        upgrade.power = Int32.Parse(attrib.Value);
                    attrib = attribs.GetNamedItem("ModelFile");
                    if (attrib != null && attrib.Value.Count() > 0)
                    {
                        upgrade.model = LoadObj(attrib.Value, a_zip);
                        if (upgrade.model == null)
                            goto XMLError;
                    }
                    attrib = attribs.GetNamedItem("TextureFile");
                    if (attrib != null && attrib.Value.Count() > 0)
                    {
                        upgrade.texture = LoadImage(attrib.Value, a_zip);
                        if (upgrade.texture == null)
                            goto XMLError;
                    }
                    prefab.techUpgrades.Add(upgrade);
                }

                // Model
                nodes = node.SelectNodes("Model");
                if (nodes.Count != 1)
                    goto XMLError;
                attribs = nodes[0].Attributes;
                if (attribs.Count != 2)
                    goto XMLError;
                attrib = attribs.GetNamedItem("ModelFileName");
                if (attrib == null || attrib.Value.Count() == 0)
                    goto XMLError;
                prefab.model = LoadObj(attrib.Value, a_zip);
                if (prefab.model == null)
                    goto XMLError;
                attrib = attribs.GetNamedItem("TextureFileName");
                if (attrib != null && attrib.Value.Count() > 0)
                {
                    prefab.texture = LoadImage(attrib.Value, a_zip);
                    if (prefab.texture == null)
                        goto XMLError;
                }

                // Cameo
                nodes = node.SelectNodes("Cameo");
                if (nodes.Count != 1)
                    goto XMLError;
                attribs = nodes[0].Attributes;
                if (attribs.Count != 1)
                    goto XMLError;
                attrib = attribs.GetNamedItem("CameoFileName");
                if (attrib == null || attrib.Value.Count() == 0)
                    goto XMLError;
                prefab.cameo = LoadImage(attrib.Value, a_zip);
                if (prefab.cameo == null)
                    goto XMLError;

                if (prehash.shortName == "CONSTRUCTION_YARD")
                    prefab.ID = 0;
                else
                    prefab.ID = ++m_hash;
                m_prehash.Add(prefab.ID, prehash);
                Main.m_res.prefabs.buildingPrefabs.Add(prefab.ID, prefab);
            }
            else
            {
                // Name
                XmlAttributeCollection attribs = node.Attributes;
                if (attribs.Count != 1)
                    goto XMLError;
                XmlNode attrib = attribs.GetNamedItem("ID");
                if (attrib == null)
                    goto XMLError;
                prehash.shortName = attrib.Value;

                // Duplicate check
                foreach(PreHash pre in m_prehash.Values)
                {
                    if (pre.shortName == prehash.shortName)
                    {
                        Logger.LogWarning("Prefab overwrite attempt detected - possible mod: " + prehash.shortName);
                        return;
                    }
                }

                // Create prefab
                UnitPrefab prefab = new UnitPrefab();
                prefab.dataItem = a_zip;
                prefab.type = Unit.Type.TANK;

                // Properties
                XmlNodeList nodes = node.SelectNodes("Properties");
                if (nodes.Count != 1)
                    goto XMLError;
                attribs = nodes[0].Attributes;
                if (attribs.Count != 11)
                    goto XMLError;
                attrib = attribs.GetNamedItem("Name");
                if (attrib == null)
                    goto XMLError;
                prefab.name = attrib.Value;
                attrib = attribs.GetNamedItem("Factory");
                if (attrib == null)
                    goto XMLError;
                prehash.factoryName = attrib.Value;
                attrib = attribs.GetNamedItem("MenuID");
                if (attrib == null)
                    goto XMLError;
                prefab.menuID = Int32.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("Cost");
                if (attrib == null)
                    goto XMLError;
                prefab.cost = Int32.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("BuildTime");
                if (attrib == null)
                    goto XMLError;
                prefab.buildTime = Int32.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("Health");
                if (attrib == null)
                    goto XMLError;
                prefab.health = Int32.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("Speed");
                if (attrib == null)
                    goto XMLError;
                prefab.speed = float.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("TurnSpeed");
                if (attrib == null)
                    goto XMLError;
                prefab.turnSpeed = float.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("Acceleration");
                if (attrib == null)
                    goto XMLError;
                prefab.accel = float.Parse(attrib.Value);
                attrib = attribs.GetNamedItem("MiniMapIconSize");
                if (attrib == null)
                    goto XMLError;
                if (attrib.Value.Split(',').Length != 2)
                    goto XMLError;
                prefab.miniMapSize = new Vector2(Int32.Parse(attrib.Value.Split(',')[0]), Int32.Parse(attrib.Value.Split(',')[1]));
                attrib = attribs.GetNamedItem("UnitType");
                if (attrib == null)
                    goto XMLError;
                if (attrib.Value == "DOZER")
                  prefab.type = Unit.Type.DOZER;
                else if (attrib.Value == "INFANTRY")
                  prefab.type = Unit.Type.INFANTRY;
                else if (attrib.Value == "TANK")
                  prefab.type = Unit.Type.TANK;

                // Tech
                prefab.techReqs = new List<int>();
                prefab.techUpgrades = new List<UnitPrefab.Upgrade>();

                nodes = node.SelectNodes("Tech/TechReq");
                foreach (XmlNode n in nodes)
                {
                    attribs = n.Attributes;
                    if (attribs.Count != 1)
                        goto XMLError;
                    attrib = attribs.GetNamedItem("Value");
                    if (attrib == null)
                        goto XMLError;
                    prehash.techReqs.Add(attrib.Value);
                }

                nodes = node.SelectNodes("Tech/Upgrade");
                foreach (XmlNode n in nodes)
                {
                    attribs = n.Attributes;
                    if (attribs.Count < 3)
                        goto XMLError;
                    if (prefab.techUpgrades == null)
                        prefab.techUpgrades = new List<UnitPrefab.Upgrade>();
                    UnitPrefab.Upgrade upgrade = new UnitPrefab.Upgrade();
                    upgrade.health = 0;
                    attrib = attribs.GetNamedItem("Name");
                    if (attrib == null)
                        goto XMLError;
                    upgrade.name = attrib.Value;
                    attrib = attribs.GetNamedItem("Cost");
                    if (attrib == null)
                        goto XMLError;
                    upgrade.cost = Int32.Parse(attrib.Value);
                    attrib = attribs.GetNamedItem("UpgradeTime");
                    if (attrib == null)
                        goto XMLError;
                    upgrade.buildTime = Int32.Parse(attrib.Value);
                    attrib = attribs.GetNamedItem("AdditionalHealth");
                    if (attrib != null)
                        upgrade.health = Int32.Parse(attrib.Value);
                    attrib = attribs.GetNamedItem("ModelFile");
                    if (attrib != null && attrib.Value.Count() > 0)
                    {
                        upgrade.model = LoadObj(attrib.Value, a_zip);
                        if (upgrade.model == null)
                            goto XMLError;
                    }
                    attrib = attribs.GetNamedItem("TextureFile");
                    if (attrib != null && attrib.Value.Count() > 0)
                    {
                        upgrade.texture = LoadImage(attrib.Value, a_zip);
                        if (upgrade.texture == null)
                            goto XMLError;
                    }
                    prefab.techUpgrades.Add(upgrade);
                }

                // Model
                nodes = node.SelectNodes("Model");
                if (nodes.Count != 1)
                    goto XMLError;
                attribs = nodes[0].Attributes;
                if (attribs.Count != 2)
                    goto XMLError;
                attrib = attribs.GetNamedItem("ModelFileName");
                if (attrib == null || attrib.Value.Count() == 0)
                    goto XMLError;
                prefab.model = LoadObj(attrib.Value, a_zip);
                if (prefab.model == null)
                    goto XMLError;
                attrib = attribs.GetNamedItem("TextureFileName");
                if (attrib != null && attrib.Value.Count() > 0)
                {
                    prefab.texture = LoadImage(attrib.Value, a_zip);
                    if (prefab.texture == null)
                        goto XMLError;
                }

                // Cameo
                nodes = node.SelectNodes("Cameo");
                if (nodes.Count != 1)
                    goto XMLError;
                attribs = nodes[0].Attributes;
                if (attribs.Count != 1)
                    goto XMLError;
                attrib = attribs.GetNamedItem("CameoFileName");
                if (attrib == null || attrib.Value.Count() == 0)
                    goto XMLError;
                prefab.cameo = LoadImage(attrib.Value, a_zip);
                if (prefab.cameo == null)
                    goto XMLError;

                if (prehash.shortName == "BULLDOZER")
                    prefab.ID = 1;
                else
                    prefab.ID = ++m_hash;
                m_prehash.Add(prefab.ID, prehash);
                Main.m_res.prefabs.unitPrefabs.Add(prefab.ID, prefab);
            }

            return;

            XMLError:
            Logger.LogError("Invalid XML file: " + a_name);
        }