Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- public class CampaignControl : MonoBehaviour {
- public string SaveFileName;
- public SaveClass SaveData;
- public CampaignSection[] Sections;
- public bool TestSave;
- public int TestSection;
- public int TestLevel;
- void Start ()
- {
- foreach (CampaignSection Section in Sections)
- {
- Section.SetLevels();
- }
- CheckSaveFile();
- }
- void Update()
- {
- if (TestSave)
- {
- TestSave = false;
- SaveToFile(true);
- }
- }
- public void PlayTest()
- {
- PlayLevel(TestSection, TestLevel);
- }
- /// <summary>
- /// Select a campaign level to play.
- /// Remember: Index 1
- /// All values passed in here are subtracted by 1 to compensate for this.
- /// </summary>
- /// <param name="Section">Which section should the level play from?</param>
- /// <param name="Level">Which level from this section should be played?</param>
- public void PlayLevel(int Section, int Level)
- {
- CampaignSection SelectedSection = Sections[Section - 1];
- LevelData SelectedLevel = SelectedSection.Levels[Level - 1];
- GameControl.Access.PlayLevel(SelectedLevel, Section, Level);
- }
- void CheckSaveFile()
- {
- if (!Directory.Exists(Application.dataPath + "/UserData/"))
- {
- Directory.CreateDirectory(Application.dataPath + "/UserData/");
- Debug.Log("Created: " + Application.dataPath + "/UserData/");
- }
- Debug.Log("Checking for:" + Application.dataPath + "/UserData/" + SaveFileName + ".dat");
- if (!File.Exists(Application.dataPath + "/UserData/" + SaveFileName + ".dat"))
- {
- Debug.Log("Creating new save file: " + Application.dataPath + "/UserData/" + SaveFileName + ".dat");
- BinaryFormatter formatter = new BinaryFormatter();
- FileStream file = File.Create(Application.dataPath + "/UserData/" + SaveFileName + ".dat");
- formatter.Serialize(file, SaveData);
- file.Close();
- }
- else
- {
- Debug.Log("Loading Save File");
- BinaryFormatter formatter = new BinaryFormatter();
- FileStream file = File.Open(Application.dataPath + "/UserData/" + SaveFileName + ".dat", FileMode.Open);
- SaveData = (SaveClass)formatter.Deserialize(file);
- int SectionIndex = 0;
- foreach(SaveClass.Section Section in SaveData.Sections)
- {
- try
- {
- Sections[SectionIndex].LevelBeaten = Section.LevelBeaten;
- Sections[SectionIndex].LevelCompleted = Section.LevelCompleted;
- }
- catch
- {
- Debug.Log("The save file probably has more sections than the game currently has.");
- }
- SectionIndex++;
- }
- }
- }
- /// <summary>
- /// Take level completed bool lists and save them to the SaveData class.
- /// </summary>
- /// <param name="NoFile">Stream SaveData class to SaveFile</param>
- public void SaveToFile(bool ToFile = true)
- {
- SaveData.Sections.Clear();
- foreach (CampaignSection Section in Sections)
- {
- SaveData.Sections.Add(new SaveClass.Section(Section.SectionName, Section.Levels.Count));
- }
- int SectionIndex = 0;
- foreach (CampaignSection Section in Sections)
- {
- SaveData.Sections[SectionIndex].LevelBeaten = Section.LevelBeaten;
- SaveData.Sections[SectionIndex].LevelCompleted = Section.LevelCompleted;
- SectionIndex++;
- }
- if (ToFile)
- {
- Debug.Log("Saving to file...");
- BinaryFormatter formatter = new BinaryFormatter();
- FileStream file = File.Create(Application.dataPath + "/UserData/" + SaveFileName + ".dat");
- formatter.Serialize(file, SaveData);
- file.Close();
- Debug.Log("Save Complete.");
- }
- }
- }
- [System.Serializable]
- public class CampaignSection
- {
- public string SectionName;
- public TextAsset[] RawFiles;
- public List<LevelData> Levels;
- /// <summary>
- /// Levels are broken into two states. Beaten and Completed.
- /// Beating a level means you've simply destroyed all the diamonds.
- /// Completing a level means you've collected all the cubes when you beat the level.
- /// </summary>
- public List<bool> LevelBeaten;
- public List<bool> LevelCompleted;
- public int LevelIndex;
- public bool Unlocked;
- public void SetLevels()
- {
- foreach (TextAsset RawFile in RawFiles)
- {
- LevelIndex++;
- BinaryFormatter formatter = new BinaryFormatter();
- Stream file = new MemoryStream(RawFile.bytes);
- Levels.Add((LevelData)formatter.Deserialize(file));
- LevelBeaten.Add(false);
- LevelCompleted.Add(false);
- }
- }
- }
- [System.Serializable]
- public class SaveClass
- {
- public List<Section> Sections;
- [System.Serializable]
- public class Section
- {
- public string SectionName;
- public int LevelCount;
- public List<bool> LevelBeaten;
- public List<bool> LevelCompleted;
- public Section(string SectionName, int LevelCount)
- {
- this.SectionName = SectionName;
- this.LevelCount = LevelCount;
- LevelBeaten = new List<bool>();
- LevelCompleted = new List<bool>();
- for (int i = 0; i < LevelCount; i++)
- {
- LevelBeaten.Add(false);
- LevelCompleted.Add(false);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement