Advertisement
CassataGames

Campaign Mode

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