Advertisement
CassataGames

Untitled

Feb 8th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.95 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.IO;
  6.  
  7. public class CampaignControl : MonoBehaviour {
  8.  
  9.     public static CampaignControl Access;
  10.  
  11.     public string SaveFileName;
  12.     public SaveClass SaveData;
  13.  
  14.     public CampaignSection[] Sections;
  15.  
  16.     public bool TestSave;
  17.  
  18.     public int TestSection;
  19.     public int TestLevel;
  20.  
  21.     void Start ()
  22.     {
  23.         Access = this;
  24.         foreach (CampaignSection Section in Sections)
  25.         {
  26.             Section.SetLevels();
  27.         }
  28.         CheckSaveFile();
  29.     }
  30.  
  31.     void Update()
  32.     {
  33.  
  34.         if (TestSave)
  35.         {
  36.             TestSave = false;
  37.             SaveToFile(true);
  38.         }
  39.     }
  40.  
  41.     public void PlayTest()
  42.     {
  43.         PlayLevel(TestSection, TestLevel);
  44.     }
  45.  
  46.     /// <summary>
  47.     /// Select a campaign level to play.
  48.     /// Remember: Index 1
  49.     /// All values passed in here are subtracted by 1 to compensate for this.
  50.     /// </summary>
  51.     /// <param name="Section">Which section should the level play from?</param>
  52.     /// <param name="Level">Which level from this section should be played?</param>
  53.     public void PlayLevel(int Section, int Level)
  54.     {
  55.         CampaignSection SelectedSection = Sections[Section - 1];
  56.         LevelData SelectedLevel = SelectedSection.Levels[Level - 1];
  57.         GameControl.Access.PlayLevel(SelectedLevel, Section, Level);
  58.     }
  59.  
  60.     void CheckSaveFile()
  61.     {
  62.         if (!Directory.Exists(Application.dataPath + "/UserData/"))
  63.         {
  64.             Directory.CreateDirectory(Application.dataPath + "/UserData/");
  65.             Debug.Log("Created: " + Application.dataPath + "/UserData/");
  66.         }
  67.         Debug.Log("Checking for:" + Application.dataPath + "/UserData/" + SaveFileName + ".dat");
  68.         if (!File.Exists(Application.dataPath + "/UserData/" + SaveFileName + ".dat"))
  69.         {
  70.             Debug.Log("Creating new save file: " + Application.dataPath + "/UserData/" + SaveFileName + ".dat");
  71.             BinaryFormatter formatter = new BinaryFormatter();
  72.             FileStream file = File.Create(Application.dataPath + "/UserData/" + SaveFileName + ".dat");
  73.             formatter.Serialize(file, SaveData);
  74.             file.Close();
  75.         }
  76.         else
  77.         {
  78.             Debug.Log("Loading Save File");
  79.             BinaryFormatter formatter = new BinaryFormatter();
  80.             FileStream file = File.Open(Application.dataPath + "/UserData/" + SaveFileName + ".dat", FileMode.Open);
  81.             SaveData = (SaveClass)formatter.Deserialize(file);
  82.  
  83.             int SectionIndex = 0;
  84.             foreach(SaveClass.Section Section in SaveData.Sections)
  85.             {
  86.                 try
  87.                 {
  88.                     Sections[SectionIndex].LevelBeaten = Section.LevelBeaten;
  89.                     Sections[SectionIndex].LevelCompleted = Section.LevelCompleted;
  90.                 }
  91.                 catch
  92.                 {
  93.                     Debug.Log("The save file probably has more sections than the game currently has.");
  94.                 }
  95.                 SectionIndex++;
  96.             }
  97.         }
  98.     }
  99.  
  100.     /// <summary>
  101.     /// Take level completed bool lists and save them to the SaveData class.
  102.     /// </summary>
  103.     /// <param name="NoFile">Stream SaveData class to SaveFile</param>
  104.     public void SaveToFile(bool ToFile = true)
  105.     {
  106.         SaveData.Sections.Clear();
  107.         foreach (CampaignSection Section in Sections)
  108.         {
  109.             SaveData.Sections.Add(new SaveClass.Section(Section.SectionName, Section.Levels.Count));
  110.         }
  111.  
  112.         int SectionIndex = 0;
  113.         foreach (CampaignSection Section in Sections)
  114.         {
  115.             SaveData.Sections[SectionIndex].LevelBeaten = Section.LevelBeaten;
  116.             SaveData.Sections[SectionIndex].LevelCompleted = Section.LevelCompleted;
  117.             SectionIndex++;
  118.         }
  119.         if (ToFile)
  120.         {
  121.             Debug.Log("Saving to file...");
  122.             BinaryFormatter formatter = new BinaryFormatter();
  123.             FileStream file = File.Create(Application.dataPath + "/UserData/" + SaveFileName + ".dat");
  124.             formatter.Serialize(file, SaveData);
  125.             file.Close();
  126.             Debug.Log("Save Complete.");
  127.         }
  128.     }
  129. }
  130.  
  131. [System.Serializable]
  132. public class CampaignSection
  133. {
  134.     public string SectionName;
  135.     public TextAsset[] RawFiles;
  136.     public List<LevelData> Levels;
  137.     /// <summary>
  138.     /// Levels are broken into two states. Beaten and Completed.
  139.     /// Beating a level means you've simply destroyed all the diamonds.
  140.     /// Completing a level means you've collected all the cubes when you beat the level.
  141.     /// </summary>
  142.     public List<bool> LevelBeaten;
  143.     public List<bool> LevelCompleted;
  144.     public int LevelIndex;
  145.  
  146.     public bool Unlocked;
  147.  
  148.     public void SetLevels()
  149.     {
  150.         foreach (TextAsset RawFile in RawFiles)
  151.         {
  152.             LevelIndex++;
  153.             BinaryFormatter formatter = new BinaryFormatter();
  154.             Stream file = new MemoryStream(RawFile.bytes);
  155.             Levels.Add((LevelData)formatter.Deserialize(file));
  156.             LevelBeaten.Add(false);
  157.             LevelCompleted.Add(false);
  158.         }
  159.     }
  160. }
  161.  
  162. [System.Serializable]
  163. public class SaveClass
  164. {
  165.     public List<Section> Sections;
  166.     [System.Serializable]
  167.     public class Section
  168.     {
  169.         public string SectionName;
  170.         public int LevelCount;
  171.         public List<bool> LevelBeaten;
  172.         public List<bool> LevelCompleted;
  173.  
  174.         public Section(string SectionName, int LevelCount)
  175.         {
  176.             this.SectionName = SectionName;
  177.             this.LevelCount = LevelCount;
  178.             LevelBeaten = new List<bool>();
  179.             LevelCompleted = new List<bool>();
  180.             for (int i = 0; i < LevelCount; i++)
  181.             {
  182.                 LevelBeaten.Add(false);
  183.                 LevelCompleted.Add(false);
  184.             }
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement