Example #1
0
 public void NextDay(int dayPass)
 {
     foreach (Transform currentObject in transform)
     {
         FarmSlot farmSlot = currentObject.gameObject.GetComponent <FarmSlot>();
         if (farmSlot)
         {
             farmSlot.PlantGrowth(dayPass);
         }
     }
 }
    public void LoadFarmSlots()
    {
        if (File.Exists(Application.persistentDataPath + PATH_FARM_SLOTS))
        {
            BinaryFormatter     bf            = new BinaryFormatter();
            FileStream          file          = File.Open(Application.persistentDataPath + PATH_FARM_SLOTS, FileMode.Open);
            List <FarmSlotSave> farmSlotSaves = (List <FarmSlotSave>)bf.Deserialize(file);
            file.Close();

            int iter               = 0;
            int farmSlotIndex      = 0;
            int subFarmIndex       = 0;
            int farmSlotSavesCount = farmSlotSaves.Count;

            /*foreach (FarmSlotSave farmSlot in farmSlotSaves) {
             *  Debug.Log(string.Format("FarmSlot : SubFarm #{0}, FarmSlot #{1}, PlantModelID #{2}," +
             *      " IsRotten? {3}, CurrentState {4}, CurrentDayPass {5}",
             *      farmSlot.subFarmIndex, farmSlot.farmSlotIndex, farmSlot.plantModelID,
             *      farmSlot.isRotten, farmSlot.currentState, farmSlot.currentDayPass));
             * }*/

            foreach (Transform child in farm.transform)
            {
                farmSlotIndex = 0;
                if (child.gameObject.GetComponent <SubFarm>())
                {
                    foreach (Transform grandChild in child.gameObject.transform)
                    {
                        FarmSlot farmSlot = grandChild.gameObject.GetComponent <FarmSlot>();
                        if (farmSlot)
                        {
                            if (iter < farmSlotSavesCount && farmSlotSaves[iter].farmSlotIndex == farmSlotIndex &&
                                farmSlotSaves[iter].subFarmIndex == subFarmIndex)
                            {
                                farmSlot.SetPlantFromSave(farmSlotSaves[iter].plantModelID, farmSlotSaves[iter].isRotten,
                                                          farmSlotSaves[iter].currentState, farmSlotSaves[iter].currentDayPass);
                                iter++;
                            }
                            else
                            {
                                farmSlot.RemovePlant(false);
                            }
                            farmSlotIndex++;
                        }
                    }
                    subFarmIndex++;
                }
            }
        }
        else
        {
            //Debug.LogWarning("SaveLoadController.LoadFarmSlot : " + Application.persistentDataPath + PATH_FARM_SLOTS + " not found.");
        }
    }
    public void SaveFarmSlots()
    {
        List <FarmSlotSave> farmSlotSaves = new List <FarmSlotSave>();
        int subFarmIndex  = 0;
        int farmSlotIndex = 0;

        //print(farm);
        foreach (Transform child in farm.transform)
        {
            farmSlotIndex = 0;
            if (child.gameObject.GetComponent <SubFarm>())
            {
                foreach (Transform grandChild in child.gameObject.transform)
                {
                    FarmSlot farmSlot = grandChild.gameObject.GetComponent <FarmSlot>();
                    if (farmSlot)
                    {
                        if (farmSlot.plantModel)
                        {
                            FarmSlotSave farmSlotSave = new FarmSlotSave();
                            farmSlotSave.subFarmIndex   = subFarmIndex;
                            farmSlotSave.farmSlotIndex  = farmSlotIndex;
                            farmSlotSave.plantModelID   = farmSlot.plantModel.id;
                            farmSlotSave.isRotten       = farmSlot.isRotten;
                            farmSlotSave.currentState   = farmSlot.currentState;
                            farmSlotSave.currentDayPass = farmSlot.currentDayPass;
                            farmSlotSaves.Add(farmSlotSave);
                        }
                        farmSlotIndex++;
                    }
                }
                subFarmIndex++;
            }
        }

        BinaryFormatter bf   = new BinaryFormatter();
        FileStream      file = File.Create(Application.persistentDataPath + PATH_FARM_SLOTS);

        bf.Serialize(file, farmSlotSaves);
        file.Close();
    }