Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6.  
  7. public class DataManager : MonoBehaviour
  8. {
  9.     public static DataManager Instance;
  10.  
  11.     public string testID = "0";
  12.     string filename;
  13.  
  14.     int keyboardCount = 0;
  15.     int guitarCount = 0;
  16.     int controllerCount = 0;
  17.  
  18.     public bool playedKeyboard = false;
  19.     public bool playedController = false;
  20.     public bool playedGuitar = false;
  21.  
  22.     [HideInInspector] public string keyTime1 , keyTime2, conTime1, conTime2, ghTime1, ghTime2;
  23.     [HideInInspector] public int keyCrash1, keyCrash2, conCrash1, conCrash2, ghCrash1, ghCrash2;
  24.     [HideInInspector] public int keyOrb1, keyOrb2, conOrb1, conOrb2, ghOrb1, ghOrb2;
  25.     [HideInInspector] public int totalOrbs;
  26.     [HideInInspector] public int keyOrbAc1, keyOrbAc2, conOrbAc1, conOrbAc2, ghOrbAc1, ghOrbAc2;
  27.     [HideInInspector] public int keyTotAc1, keyTotAc2, conTotAc1, conTotAc2, ghTotAc1, ghTotAc2;
  28.     [HideInInspector] public int keyScore1, keyScore2, conScore1, conScore2, ghScore1, ghScore2;
  29.  
  30.     void Awake()
  31.     {
  32.         //singelton
  33.         if (Instance == null)
  34.         {
  35.             Instance = this as DataManager;
  36.             DontDestroyOnLoad(gameObject);
  37.         }
  38.         else
  39.             Destroy(gameObject);
  40.     }
  41.  
  42.     public bool PlayKeyboard()
  43.     {
  44.         if (keyboardCount < 2)
  45.         {
  46.             keyboardCount++;
  47.             playedKeyboard = true;
  48.             return true;
  49.         }
  50.         else
  51.             return false;
  52.     }
  53.  
  54.     public bool PlayController()
  55.     {
  56.         if (controllerCount < 2)
  57.         {
  58.             controllerCount++;
  59.             playedController = true;
  60.             return true;
  61.         }
  62.         else
  63.             return false;
  64.     }
  65.  
  66.     public bool PlayGuitar()
  67.     {
  68.         if (guitarCount < 2)
  69.         {
  70.             guitarCount++;
  71.             playedGuitar = true;
  72.             return true;
  73.         }
  74.         else
  75.             return false;
  76.     }
  77.  
  78.     public void SaveData(string endTime, int crashes, int orbsTaken, int totalOrbs, int orbAccuracy, int totalAccuracy, int score)
  79.     {
  80.         if (playedKeyboard)
  81.         {
  82.             if (keyboardCount == 1)
  83.             {
  84.                 keyTime1  = endTime;
  85.                 keyCrash1  = crashes;
  86.                 keyOrb1 = orbsTaken;
  87.                 this.totalOrbs = totalOrbs;
  88.                 keyOrbAc1 = orbAccuracy;
  89.                 keyTotAc1 = totalAccuracy;
  90.                 keyScore1 = score;
  91.             }
  92.             if (keyboardCount == 2)
  93.             {
  94.                 keyTime2 = endTime;
  95.                 keyCrash2 = crashes;
  96.                 keyOrb2 = orbsTaken;
  97.                 this.totalOrbs = totalOrbs;
  98.                 keyOrbAc2 = orbAccuracy;
  99.                 keyTotAc2 = totalAccuracy;
  100.                 keyScore2 = score;
  101.             }
  102.  
  103.             playedKeyboard = false;
  104.         }
  105.         else if (playedController)
  106.         {
  107.             if (controllerCount == 1)
  108.             {
  109.                 conTime1  = endTime;
  110.                 conCrash1  = crashes;
  111.                 conOrb1 = orbsTaken;
  112.                 this.totalOrbs = totalOrbs;
  113.                 conOrbAc1 = orbAccuracy;
  114.                 conTotAc1 = totalAccuracy;
  115.                 conScore1 = score;
  116.             }
  117.             if (controllerCount == 2)
  118.             {
  119.                 conTime2 = endTime;
  120.                 conCrash2 = crashes;
  121.                 conOrb2 = orbsTaken;
  122.                 this.totalOrbs = totalOrbs;
  123.                 conOrbAc2 = orbAccuracy;
  124.                 conTotAc2 = totalAccuracy;
  125.                 conScore2 = score;
  126.             }
  127.  
  128.             playedController = false;
  129.         }
  130.         else if (playedGuitar)
  131.         {
  132.             if (guitarCount == 1)
  133.             {
  134.                 ghTime1  = endTime;
  135.                 ghCrash1  = crashes;
  136.                 ghOrb1 = orbsTaken;
  137.                 this.totalOrbs = totalOrbs;
  138.                 ghOrbAc1 = orbAccuracy;
  139.                 ghTotAc1 = totalAccuracy;
  140.                 ghScore1 = score;
  141.             }
  142.             if (guitarCount == 2)
  143.             {
  144.                 ghTime2 = endTime;
  145.                 ghCrash2 = crashes;
  146.                 ghOrb2 = orbsTaken;
  147.                 this.totalOrbs = totalOrbs;
  148.                 ghOrbAc2 = orbAccuracy;
  149.                 ghTotAc2 = totalAccuracy;
  150.                 ghScore2 = score;
  151.             }
  152.  
  153.             playedGuitar = false;
  154.         }
  155.     }
  156.  
  157.     public void SaveToTxt()
  158.     {
  159.         filename = "Data " + testID + ".txt";
  160.         using (StreamWriter sw = new StreamWriter(filename))
  161.         {
  162.             sw.WriteLine("-------------------");
  163.             sw.WriteLine("");
  164.             sw.WriteLine("KEYBOARD - PLAYTHROUGH 1");
  165.             sw.WriteLine("");
  166.             sw.WriteLine("TIME: " + keyTime1);
  167.             sw.WriteLine("CRASHES: " + keyCrash1);
  168.             sw.WriteLine("ORBS: " + keyOrb1 + " / " + totalOrbs + " (" + keyOrbAc1 + "%)");
  169.             sw.WriteLine("ACCURACY: " + keyTotAc1 + "%");
  170.             sw.WriteLine("SCORE: " + keyScore1);
  171.             sw.WriteLine("");
  172.             sw.WriteLine("-------------------");
  173.             sw.WriteLine("");
  174.             sw.WriteLine("KEYBOARD - PLAYTHROUGH 2");
  175.             sw.WriteLine("");
  176.             sw.WriteLine("TIME: " + keyTime2);
  177.             sw.WriteLine("CRASHES: " + keyCrash2);
  178.             sw.WriteLine("ORBS: " + keyOrb2 + " / " + totalOrbs + " (" + keyOrbAc2 + "%)");
  179.             sw.WriteLine("ACCURACY: " + keyTotAc2 + "%");
  180.             sw.WriteLine("SCORE: " + keyScore2);
  181.             sw.WriteLine("");
  182.             sw.WriteLine("-------------------");
  183.             sw.WriteLine("");
  184.             sw.WriteLine("CONTROLLER - PLAYTHROUGH 1");
  185.             sw.WriteLine("");
  186.             sw.WriteLine("TIME: " + conTime1);
  187.             sw.WriteLine("CRASHES: " + conCrash1);
  188.             sw.WriteLine("ORBS: " + conOrb1 + " / " + totalOrbs + " (" + conOrbAc1 + "%)");
  189.             sw.WriteLine("ACCURACY: " + conTotAc1 + "%");
  190.             sw.WriteLine("SCORE: " + conScore1);
  191.             sw.WriteLine("");
  192.             sw.WriteLine("-------------------");
  193.             sw.WriteLine("");
  194.             sw.WriteLine("CONTROLLER - PLAYTHROUGH 2");
  195.             sw.WriteLine("");
  196.             sw.WriteLine("TIME: " + conTime2);
  197.             sw.WriteLine("CRASHES: " + conCrash2);
  198.             sw.WriteLine("ORBS: " + conOrb2 + " / " + totalOrbs + " (" + conOrbAc2 + "%)");
  199.             sw.WriteLine("ACCURACY: " + conTotAc2 + "%");
  200.             sw.WriteLine("SCORE: " + conScore2);
  201.             sw.WriteLine("");
  202.             sw.WriteLine("-------------------");
  203.             sw.WriteLine("");
  204.             sw.WriteLine("GUITAR - PLAYTHROUGH 1");
  205.             sw.WriteLine("");
  206.             sw.WriteLine("TIME: " + ghTime1);
  207.             sw.WriteLine("CRASHES: " + ghCrash1);
  208.             sw.WriteLine("ORBS: " + ghOrb1 + " / " + totalOrbs + " (" + ghOrbAc1 + "%)");
  209.             sw.WriteLine("ACCURACY: " + ghTotAc1 + "%");
  210.             sw.WriteLine("SCORE: " + ghScore1);
  211.             sw.WriteLine("");
  212.             sw.WriteLine("-------------------");
  213.             sw.WriteLine("");
  214.             sw.WriteLine("GUITAR - PLAYTHROUGH 2");
  215.             sw.WriteLine("");
  216.             sw.WriteLine("TIME: " + ghTime2);
  217.             sw.WriteLine("CRASHES: " + ghCrash2);
  218.             sw.WriteLine("ORBS: " + ghOrb2 + " / " + totalOrbs + " (" + ghOrbAc2 + "%)");
  219.             sw.WriteLine("ACCURACY: " + ghTotAc2 + "%");
  220.             sw.WriteLine("SCORE: " + ghScore2);
  221.             sw.WriteLine("");
  222.             sw.WriteLine("-------------------");
  223.         }
  224.     }
  225.  
  226.     public void Save()
  227.     {
  228.         BinaryFormatter bf = new BinaryFormatter();
  229.  
  230.         FileStream file = File.Create("ZombieCrash_Data/TestData.dat");
  231.  
  232.         TestData data = new TestData();
  233.  
  234.         data.testID = testID;
  235.  
  236.         data.keyboardCount = keyboardCount;
  237.         data.controllerCount = controllerCount;
  238.         data.guitarCount = guitarCount;
  239.  
  240.         data.keyTime1 = keyTime1;
  241.         data.keyTime2 = keyTime2;
  242.         data.conTime1 = conTime1;
  243.         data.conTime2 = conTime2;
  244.         data.ghTime1 = ghTime1;
  245.         data.ghTime2 = ghTime2;
  246.         data.keyCrash1 = keyCrash1;
  247.         data.keyCrash2 = keyCrash2;
  248.         data.conCrash1 = conCrash1;
  249.         data.conCrash2 = conCrash2;
  250.         data.ghCrash1 = ghCrash1;
  251.         data.ghCrash2 = ghCrash2;
  252.         data.keyOrb1 = keyOrb1;
  253.         data.keyOrb2 = keyOrb2;
  254.         data.conOrb1 = conOrb1;
  255.         data.conOrb2 = conOrb2;
  256.         data.ghOrb1 = ghOrb1;
  257.         data.ghOrb2 = ghOrb2;
  258.         data.totalOrbs = totalOrbs;
  259.         data.keyOrbAc1 = keyOrbAc1;
  260.         data.keyOrbAc2 = keyOrbAc2;
  261.         data.conOrbAc1 = conOrbAc1;
  262.         data.conOrbAc2 = conOrbAc2;
  263.         data.ghOrbAc1 = ghOrbAc1;
  264.         data.ghOrbAc2 = ghOrbAc2;
  265.         data.keyTotAc1 = keyTotAc1;
  266.         data.keyTotAc2 = keyTotAc2;
  267.         data.conTotAc1 = conTotAc1;
  268.         data.conTotAc2 = conTotAc2;
  269.         data.ghTotAc1 = ghTotAc1;
  270.         data.ghTotAc2 = ghTotAc2;
  271.         data.keyScore1 = keyScore1;
  272.         data.keyScore2 = keyScore2;
  273.         data.conScore1 = conScore1;
  274.         data.conScore2 = conScore2;
  275.         data.ghScore1 = ghScore1;
  276.         data.ghScore2 = ghScore2;
  277.  
  278.  
  279.         bf.Serialize(file, data);
  280.         file.Close();
  281.     }
  282.  
  283.     public void Load()
  284.     {
  285.         if (File.Exists("ZombieCrash_Data/TestData.dat"))
  286.         {
  287.             BinaryFormatter bf = new BinaryFormatter();
  288.             FileStream file = File.Open("ZombieCrash_Data/TestData.dat", FileMode.Open);
  289.             TestData data = (TestData)bf.Deserialize(file);
  290.             file.Close();
  291.  
  292.             testID = data.testID;
  293.  
  294.             keyboardCount = data.keyboardCount;
  295.             controllerCount = data.controllerCount;
  296.             guitarCount = data.guitarCount;
  297.  
  298.             keyTime1 = data.keyTime1;
  299.             keyTime2 = data.keyTime2;
  300.             conTime1 = data.conTime1;
  301.             conTime2 = data.conTime2;
  302.             ghTime1 = data.ghTime1;
  303.             ghTime2 = data.ghTime2;
  304.             keyCrash1 = data.keyCrash1;
  305.             keyCrash2 = data.keyCrash2;
  306.             conCrash1 = data.conCrash1;
  307.             conCrash2 = data.conCrash2;
  308.             ghCrash1 = data.ghCrash1;
  309.             ghCrash2 = data.ghCrash2;
  310.             keyOrb1 = data.keyOrb1;
  311.             keyOrb2 = data.keyOrb2;
  312.             conOrb1 = data.conOrb1;
  313.             conOrb2 = data.conOrb2;
  314.             ghOrb1 = data.ghOrb1;
  315.             ghOrb2 = data.ghOrb2;
  316.             totalOrbs = data.totalOrbs;
  317.             keyOrbAc1 = data.keyOrbAc1;
  318.             keyOrbAc2 = data.keyOrbAc2;
  319.             conOrbAc1 = data.keyOrbAc1;
  320.             conOrbAc2 = data.keyOrbAc2;
  321.             ghOrbAc1 = data.ghOrbAc1;
  322.             ghOrbAc2 = data.ghOrbAc2;
  323.             keyTotAc1 = data.keyTotAc1;
  324.             keyTotAc2 = data.keyTotAc2;
  325.             conTotAc1 = data.conTotAc1;
  326.             conTotAc2 = data.conTotAc2;
  327.             ghTotAc1 = data.ghTotAc1;
  328.             ghTotAc2 = data.ghTotAc2;
  329.             keyScore1 = data.keyScore1;
  330.             keyScore2 = data.keyScore2;
  331.             conScore1 = data.conScore1;
  332.             conScore2 = data.conScore2;
  333.             ghScore1 = data.ghScore1;
  334.             ghScore2 = data.ghScore2;
  335.         }
  336.     }
  337.  
  338. }
  339.  
  340. [Serializable]
  341. class TestData
  342. {
  343.     public string testID;
  344.     public int keyboardCount, guitarCount, controllerCount;
  345.  
  346.     public string keyTime1 , keyTime2, conTime1, conTime2, ghTime1, ghTime2;
  347.     public int keyCrash1, keyCrash2, conCrash1, conCrash2, ghCrash1, ghCrash2;
  348.     public int keyOrb1, keyOrb2, conOrb1, conOrb2, ghOrb1, ghOrb2;
  349.     public int totalOrbs;
  350.     public int keyOrbAc1, keyOrbAc2, conOrbAc1, conOrbAc2, ghOrbAc1, ghOrbAc2;
  351.     public int keyTotAc1, keyTotAc2, conTotAc1, conTotAc2, ghTotAc1, ghTotAc2;
  352.     public int keyScore1, keyScore2, conScore1, conScore2, ghScore1, ghScore2;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement