Пример #1
0
    public void Save(string filename, GameObject[] objects)
    {
        Debug.Log("Game Saved ");
        FileStream   outFile = new FileStream(filename, FileMode.Create, FileAccess.Write);
        StreamWriter writer  = new StreamWriter(outFile);

        foreach (GameObject obj in objects)
        {
            if (obj.tag == "TeamA" || obj.tag == "TeamB")
            {
                UnitAController unitInfo = obj.GetComponent <UnitAController>();
                writer.WriteLine(unitInfo.Save());
            }
            else if (obj.tag == "Wizard")
            {
                WizardController unitInfo = obj.GetComponent <WizardController>();
                writer.WriteLine(unitInfo.Save());
            }
            else if (obj.tag == "rbA" || obj.tag == "rbB")
            {
                ResourceManagement unitInfo     = obj.GetComponent <ResourceManagement>();
                BuildingInfo       buildingInfo = obj.GetComponent <BuildingInfo>();
                writer.WriteLine(unitInfo.Save() + buildingInfo.Save());
            }
            else if (obj.tag == "FactoryBuildingA" || obj.tag == "FactoryBuildingB")
            {
                BuildingInfo buildingInfo = obj.GetComponent <BuildingInfo>();
                writer.WriteLine(obj.tag + " " + obj.transform.position.x + " " + obj.transform.position.y + " " + obj.transform.position.z + " " + buildingInfo.Save());
            }
            else if (obj.tag == "Building")
            {
                SpawnWizard buildingSpawnInfo = obj.GetComponent <SpawnWizard>();
                writer.WriteLine(obj.tag + " " + buildingSpawnInfo.UnitAmount + " " + obj.transform.position.x + " " + obj.transform.position.y + " " + obj.transform.position.z);
            }
        }
        writer.Close();
        outFile.Close();
    }
Пример #2
0
    public void Load(string filename)
    {
        Debug.Log("Loading unit");
        FileStream   inFile = new FileStream(filename, FileMode.Open, FileAccess.Read);
        StreamReader reader = new StreamReader(inFile);
        string       recordIn;

        recordIn = reader.ReadLine();
        while (recordIn != null)
        {
            string[] info   = recordIn.Split(' ');
            int      length = recordIn.IndexOf(" ");

            string firstField = recordIn.Substring(0, length);

            switch (firstField)
            {
            case "FactoryBuildingA":
                GameObject newBuildingA = Instantiate(FactoryBuildingA) as GameObject;
                newBuildingA.transform.position = new Vector3(float.Parse(info[1]), float.Parse(info[2]), float.Parse(info[3]));
                BuildingInfo buildingInfoA = newBuildingA.GetComponent <BuildingInfo>();
                buildingInfoA.Health = int.Parse(info[4]);
                break;

            case "FactoryBuildingB":
                GameObject newBuildingB = Instantiate(FactoryBuildingB) as GameObject;
                newBuildingB.transform.position = new Vector3(float.Parse(info[1]), float.Parse(info[2]), float.Parse(info[3]));
                BuildingInfo buildingInfoB = newBuildingB.GetComponent <BuildingInfo>();
                buildingInfoB.Health = int.Parse(info[4]);
                break;

            case "Building":
                GameObject newBuildingW = Instantiate(WizardBuilding) as GameObject;
                newBuildingW.transform.position = new Vector3(float.Parse(info[2]), float.Parse(info[3]), float.Parse(info[4]));
                SpawnWizard buildingInfoW = newBuildingW.GetComponent <SpawnWizard>();
                buildingInfoW.UnitAmount = 10;
                break;

            case "rbA":
                GameObject newBuildingrbA = Instantiate(ResourceBuildingA) as GameObject;
                ResourceBuildingA.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5]));
                BuildingInfo buildingInfoRbA = newBuildingrbA.GetComponent <BuildingInfo>();
                buildingInfoRbA.Health = int.Parse(info[6]);
                ResourceManagement resourceInfoA = ResourceBuildingA.GetComponent <ResourceManagement>();
                resourceInfoA.ResourceTotal      = int.Parse(info[1]);
                resourceInfoA.ResourcesGenerated = int.Parse(info[2]);
                break;

            case "rbB":
                GameObject newBuildingrbB = Instantiate(ResourceBuildingB) as GameObject;
                ResourceBuildingB.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5]));
                BuildingInfo buildingInfoRbB = newBuildingrbB.GetComponent <BuildingInfo>();
                buildingInfoRbB.Health = int.Parse(info[6]);
                ResourceManagement resourceInfoB = ResourceBuildingB.GetComponent <ResourceManagement>();
                resourceInfoB.ResourceTotal      = int.Parse(info[1]);
                resourceInfoB.ResourcesGenerated = int.Parse(info[2]);
                break;

            case "TeamA":
                if (info[2] == "400")
                {
                    GameObject teamAMeleeUnit = Instantiate(TeamAMeleeUnit) as GameObject;
                    teamAMeleeUnit.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5]));
                    UnitAController teamAMeleeUnitInfo = teamAMeleeUnit.GetComponent <UnitAController>();
                    teamAMeleeUnitInfo.Health   = int.Parse(info[1]);
                    teamAMeleeUnitInfo.UnitType = "Melee";
                }
                else
                {
                    GameObject teamARangedUnit = Instantiate(TeamARangedUnit) as GameObject;
                    teamARangedUnit.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5]));
                    UnitAController teamARangedUnitInfo = teamARangedUnit.GetComponent <UnitAController>();
                    teamARangedUnitInfo.Health   = int.Parse(info[1]);
                    teamARangedUnitInfo.UnitType = "Ranged";
                }
                break;

            case "TeamB":
                if (info[2] == "400")
                {
                    GameObject teamBMeleeUnit = Instantiate(TeamBMeleeUnit) as GameObject;
                    teamBMeleeUnit.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5]));
                    UnitAController teamBMeleeUnitInfo = teamBMeleeUnit.GetComponent <UnitAController>();
                    teamBMeleeUnitInfo.Health   = int.Parse(info[1]);
                    teamBMeleeUnitInfo.UnitType = "Melee";
                }
                else
                {
                    GameObject teamBRangedUnit = Instantiate(TeamBRangedUnit) as GameObject;
                    teamBRangedUnit.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5]));
                    UnitAController teamBRangedUnitInfo = teamBRangedUnit.GetComponent <UnitAController>();
                    teamBRangedUnitInfo.Health   = int.Parse(info[1]);
                    teamBRangedUnitInfo.UnitType = "Ranged";
                }
                break;

            case "Wizard":
                Debug.Log("Wizard Spawn");
                GameObject wizard = Instantiate(WizardUnit) as GameObject;
                wizard.transform.position = new Vector3(float.Parse(info[2]), float.Parse(info[3]), float.Parse(info[4]));
                WizardController wizardInfo = wizard.GetComponent <WizardController>();
                wizardInfo.Health = int.Parse(info[1]);
                break;
            }
            recordIn = reader.ReadLine();
        }
        reader.Close();
        inFile.Close();
    }