예제 #1
0
        public Building Load(BuildingPrefab a_prefab)
        {
            // Properties
            Building.Properties properties = new Building.Properties();
            properties.health = a_prefab.health;
            properties.power = a_prefab.powerUsage;
            properties.buildTime = a_prefab.buildTime;
            properties.cost = a_prefab.cost;
            properties.miniSize = new Vector2(6f, 6f);
            properties.ID = a_prefab.ID;

            // Type
            Building building = null;
            switch (a_prefab.type)
            {
                case Building.Type.HEADQUARTERS:
                    building = new Headquarters(properties, a_prefab.model, a_prefab.texture);
                    break;

                case Building.Type.POWERFACTORY:
                    building = new PowerFactory(properties, a_prefab.model, a_prefab.texture);
                    break;

                case Building.Type.UNITFACTORY:
                    building = new UnitFactory(properties, a_prefab.model, a_prefab.texture);
                    break;

                default:
                    building = new Building(properties, a_prefab.model, a_prefab.texture);
                    break;
            }

            return building;
        }
예제 #2
0
파일: Main.cs 프로젝트: JJJohan/RTS
        // Place a building.
        public static Building CreateBuilding(BuildingPrefab a_prefab, Vector3 a_pos, Vector3 a_rot)
        {
            // Initialise the building.;
            BuildingTemplate template = new BuildingTemplate();
            Building building = template.Load(a_prefab);
            building.Construct(a_pos, a_rot);
            m_buildingList.Add(building);

            return building;
        }
예제 #3
0
        // Create ghost building.
        public void Create(BuildingPrefab a_prefab)
        {
            MeshFilter filter = gameObject.AddComponent<MeshFilter>();
            MeshRenderer renderer = gameObject.AddComponent<MeshRenderer>();
            Rigidbody rigid = gameObject.AddComponent<Rigidbody>();
            BoxCollider collider = gameObject.AddComponent<BoxCollider>();
            m_prefab = a_prefab;
            gameObject.name = "Cursor Building";
            m_air = false;
            m_inRange = true;
            m_placeable = false;
            m_visible = true;
            m_collisions = 0;

            // Get list of headquarters buildings
            m_headquarters = new List<Headquarters>();
            foreach (Building building in Main.m_buildingList)
            {
                if (building.BuildingType() == Building.Type.HEADQUARTERS)
                    m_headquarters.Add((Headquarters)building);
            }

            // Load resources.
            filter.mesh = m_prefab.model;
            renderer.material.mainTexture = m_prefab.texture;

            // Configure entity.
            renderer.material.color = new Color(1f, 1f, 1f, .5f);
            renderer.material.shader = Shader.Find("Transparent/Diffuse");
            collider.size = filter.mesh.bounds.size;
            collider.size = new Vector3(collider.size.x, collider.size.y * 0.9f, collider.size.z);
            collider.center = new Vector3(0f, filter.mesh.bounds.size.y * 0.5f, 0f);

            // Set up boundary check.
            rigid.isKinematic = true;
            rigid.useGravity = false;
            collider.isTrigger = true;
            m_dims = new Vector2(collider.bounds.size.x / 2, collider.bounds.size.z / 2);
            m_largest = m_dims.x;
            if (m_dims.y > m_largest) m_largest = m_dims.y;
            m_placeable = true;

            // Initialise bounding box.
            m_points = new Vector3[9];
            m_points[0] = new Vector3(m_dims.x, 0.5f, m_dims.y);
            m_points[1] = new Vector3(-m_dims.x, 0.5f, m_dims.y);
            m_points[2] = new Vector3(-m_dims.x, 0.5f, -m_dims.y);
            m_points[3] = new Vector3(m_dims.x, 0.5f, -m_dims.y);
            m_points[4] = new Vector3(m_dims.x, 0.5f, 0f);
            m_points[5] = new Vector3(-m_dims.x, 0.5f, 0f);
            m_points[6] = new Vector3(0f, 0.5f, m_dims.y);
            m_points[7] = new Vector3(0f, 0.5f, -m_dims.y);
            m_points[8] = new Vector3(0f, 0.5f, 0f);
            m_copy = true;
        }
예제 #4
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);
        }