Advertisement
killit

Broken SaveSystem

Oct 10th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.71 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7.  
  8. public class SaveSystem : MonoBehaviour {
  9.  
  10.     public static SaveSystem saveSystem;
  11.     public int playerLevel = 1;
  12.     public float xp;
  13.     public float xpToNextLevel = 100;
  14.     public int skillPoints = 0;
  15.     public int gold = 0;
  16.     public bool hasGame;
  17.     public int numberOfEnemys = 1;
  18.     public float playerHeath = 100;
  19.  
  20.     [Header("Shop")]
  21.     public List<ShopStuff> shopList = new List<ShopStuff>();
  22.     public int mainWeapon = 1;
  23.     public int sideWeapon = 1;
  24.     public bool hasItem;
  25.  
  26.     [Header("Inventory")]
  27.     public List<Item> items = new List<Item>();
  28.  
  29.     [Header("Perks")]
  30.     public float drawSpeed = 260f;
  31.     public int drawSpeedDamage;
  32.     public int drawSpeedN;
  33.     public int AddHealth;
  34.     public int AddHealthN;
  35.     public float addStamina;
  36.  
  37.     [Header("Level")]
  38.     public bool main;
  39.     public bool arena;
  40.     public bool trainig;
  41.     public bool death;
  42.     public bool Barracks;
  43.  
  44.     public GameObject playerControlls;
  45.  
  46.     [Header("Customize Character")]
  47.     public string playerName;
  48.  
  49.     void Awake () {
  50.         if (saveSystem != null)
  51.         {
  52.             Debug.LogError ("More then one GameControllers in the scene");
  53.         }
  54.         else
  55.         {
  56.             saveSystem = this;
  57.         }
  58.         Load ();
  59.     }
  60.  
  61.     void Update()
  62.     {
  63.         if (xp >= xpToNextLevel)
  64.         {
  65.             playerLevel++;
  66.             skillPoints++;
  67.             xpToNextLevel += xpToNextLevel;
  68.         }
  69.     }
  70.  
  71.     public void Controlls()
  72.     {
  73.         playerControlls.SetActive(!playerControlls.activeSelf);
  74.     }
  75.  
  76.     public void Save()
  77.     {
  78.         BinaryFormatter bf = new BinaryFormatter();
  79.         FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.data");
  80.  
  81.         PlayerData data = new PlayerData();
  82.         data.playerLevel = playerLevel;
  83.         data.xp = xp;
  84.         data.xpToNextLevel = xpToNextLevel;
  85.         data.skillPoints = skillPoints;
  86.         hasGame = true;
  87.         data.hasGame = hasGame;
  88.         data.gold = gold;
  89.         data.drawSpeed = drawSpeed;
  90.         data.drawSpeedNumber = drawSpeedN;
  91.         data.AddHealth = AddHealth;
  92.         data.AddHealthN = AddHealthN;
  93.         data.drawSpeedDamage = drawSpeedDamage;
  94.         data.numberOfEnemys = numberOfEnemys;
  95.         data.mainWeapon = mainWeapon;
  96.         data.sideWeapon = sideWeapon;
  97.         data.hasItem = hasItem;
  98.         data.arena = arena;
  99.         data.death = death;
  100.         data.main = main;
  101.         data.trainig = trainig;
  102.         data.Barracks = Barracks;
  103.         data.playerHeath = playerHeath;
  104.         data.addStamina = addStamina;
  105.         data.playerName = playerName;
  106.         data.items = items;
  107.         data.shopList = shopList;
  108.  
  109.         bf.Serialize(file, data);
  110.  
  111.         file.Close();
  112.  
  113.         Debug.Log("has been saved");
  114.     }
  115.  
  116.     public void Load()
  117.     {
  118.         Debug.Log("has deen Laoding");
  119.         if (File.Exists(Application.persistentDataPath + "/playerInfo.data"))
  120.         {
  121.             BinaryFormatter bf = new BinaryFormatter();
  122.             FileStream file = File.Open (Application.persistentDataPath +   "/playerInfo.data", FileMode.Open);
  123.             PlayerData data =  (PlayerData)bf.Deserialize (file);
  124.  
  125.             file.Close();
  126.             playerLevel = data.playerLevel;
  127.             xpToNextLevel = data.xpToNextLevel;
  128.             skillPoints = data.skillPoints;
  129.             xp = data.xp;
  130.             gold = data.gold;
  131.             drawSpeed = data.drawSpeed;
  132.             drawSpeedN = data.drawSpeedNumber;
  133.             AddHealth = data.AddHealth;
  134.             AddHealthN = data.AddHealthN;
  135.             hasGame = data.hasGame;
  136.             drawSpeedDamage = data.drawSpeedDamage;
  137.             numberOfEnemys = data.numberOfEnemys;
  138.             mainWeapon = data.mainWeapon;
  139.             sideWeapon = data.sideWeapon;
  140.             shopList = data.shopList;
  141.             hasItem = data.hasItem;
  142.             death = data.death;
  143.             main = data.main;
  144.             trainig = data.trainig;
  145.             arena = data.arena;
  146.             Barracks = data.Barracks;
  147.             playerHeath = data.playerHeath;
  148.             addStamina = data.addStamina;
  149.             playerName = data.playerName;
  150.             items = data.items;
  151.  
  152.             Debug.Log ("has deen Laoded");
  153.         }
  154.     }
  155.  
  156.     public void Delete()
  157.     {
  158.         File.Delete(Application.persistentDataPath + "/playerInfo.data");
  159.         Debug.Log("has deen deleted");
  160.         playerLevel = 1;
  161.         skillPoints = 0;
  162.         xp = 0;
  163.         xpToNextLevel = 100;
  164.         gold = 0;
  165.         hasGame = false;
  166.         drawSpeed = 260f;
  167.         drawSpeedN = 0;
  168.         AddHealthN = 0;
  169.         AddHealth = 0;
  170.         drawSpeedDamage = 0;
  171.         numberOfEnemys = 1;
  172.         PlayerPrefs.DeleteAll();
  173.         mainWeapon = 1;
  174.         sideWeapon = 1;
  175.         hasItem = false;
  176.         playerHeath = 100;
  177.         main = true;
  178.         trainig = false;
  179.         arena = false;
  180.         death = false;
  181.         Barracks = false;
  182.         addStamina = 0;
  183.         playerName = "Player";
  184.         items.Clear();
  185.        
  186.  
  187.        Debug.Log ("has staets has been reset");
  188.     }
  189. }
  190.  
  191. [Serializable]
  192. class PlayerData
  193. {
  194.     public int playerLevel;
  195.     public float xp;
  196.     public float xpToNextLevel;
  197.     public int skillPoints;
  198.     public int gold;
  199.     public int AddHealth;
  200.     public int AddHealthN;
  201.     public int drawSpeedNumber;
  202.     public int drawSpeedDamage;
  203.     public float drawSpeed;
  204.     public bool hasGame;
  205.     public int numberOfEnemys;
  206.     public List<ShopStuff> shopList = new List<ShopStuff>();
  207.     public int mainWeapon;
  208.     public int sideWeapon;
  209.     public float addStamina;
  210.     public bool hasItem;
  211.  
  212.     public bool main;
  213.     public bool arena;
  214.     public bool trainig;
  215.     public bool death;
  216.     public bool Barracks;
  217.     public float playerHeath;
  218.  
  219.     public string playerName;
  220.  
  221.     public List<Item> items = new List<Item>();
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement