Guest User

Untitled

a guest
Feb 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.IO;
  6.  
  7. [Serializable]
  8. public enum PlayerClass {Knight, Guardian, Occultist, Cutthroat, none};
  9. [Serializable]
  10. public enum Difficulty { Chill, Story, Challenge, none };
  11.  
  12. public class GameController : MonoBehaviour {
  13.  
  14. public static GameController controller;
  15. public GameObject playerObject;
  16. public int playerNumber = 0;
  17. public string playerName = "";
  18. public float[] playerSkinColor;
  19. public bool[] unlockedAbilities;
  20. public Ability playerAbility1;
  21. public Ability playerAbility2;
  22. public Ability playerAbility3;
  23. public Ability playerAbility4;
  24. public string strikeModifier = "none";
  25. public LimitBreakName limitBreakModifier = LimitBreakName.none;
  26. public int limitBreakTracker;
  27. public string[] playerInventory;
  28. public int[] playerInventoryQuantity;
  29. public bool [,] playerEquipmentList;
  30. public int[] playerEquippedIDs;
  31. public Difficulty difficultyScale;
  32. public int playerLevel;
  33. public int playerEXP;
  34. public int playerAttack;
  35. public int playerDefense;
  36. public int playerProwess;
  37. public int playerSpeed;
  38. public int playerBaseAtk;
  39. public int playerBaseDef;
  40. public int playerBasePrw;
  41. public int playerBaseSpd;
  42. public float[] playerColorPreference;
  43. public int levelsCompleted;
  44. public int stagesCompleted;
  45. public EnemyEncounter currentEncounter;
  46. public int decisionsMade;
  47. public bool[] playerDecisions;
  48. public int playerGoodPoints;
  49. public int playerEvilPoints;
  50. public int[] skillTree;
  51.  
  52. public int TOTAL_ABILITIES = 20;
  53. public int totalAbilities;
  54. public bool[] arenaCompleted;
  55.  
  56. public string[] charNames;
  57. public int numChars;
  58. public PlayerClass[] charClasses = new PlayerClass[6];
  59.  
  60. public bool volumeMuted = false;
  61. public float musicScale = 1.0f;
  62.  
  63. public int curArenaStage = 0;
  64.  
  65. // Use this for initialization
  66. void Awake () {
  67.  
  68. if(controller == null)
  69. {
  70. DontDestroyOnLoad(gameObject);
  71. controller = this;
  72. difficultyScale = Difficulty.Story;
  73. unlockedAbilities = new bool[TOTAL_ABILITIES];
  74. playerSkinColor = new float[4];
  75. playerColorPreference = new float[4];
  76. playerEquippedIDs = new int[16];
  77. playerEquipmentList = new bool[30, 4];
  78. skillTree = new int[10];
  79. decisionsMade = 0;
  80. arenaCompleted = new bool[6];
  81. charNames = new string[6];
  82. charClasses = new PlayerClass[6];
  83. for(int i = 0; i < 6; ++i)
  84. charClasses[i] = PlayerClass.none;
  85.  
  86. if (File.Exists(Application.persistentDataPath + "/accountInfo.dat"))
  87. {
  88. LoadCharacters();
  89. }
  90. else
  91. {
  92. print("no file existed");
  93. var sr = File.CreateText(Application.persistentDataPath + "/accountInfo.dat");
  94. charNames[0] = "Skip";
  95. numChars = 0;
  96. charClasses[0] = PlayerClass.none;
  97. SaveCharacters();
  98. }
  99. }
  100. else if(controller != this)
  101. {
  102. Destroy(gameObject);
  103. }
  104. }
  105.  
  106. /*ONLY FOR SAVING ACCOUNT INFORMATION*/
  107. public void SaveCharacters()
  108. {
  109. BinaryFormatter bf = new BinaryFormatter();
  110. FileStream accountInfoFile = File.Create(Application.persistentDataPath + "/accountInfo.dat");
  111.  
  112. AccountData data = new AccountData();
  113.  
  114. data.characterNames = charNames;
  115. data.numberOfCharacters = numChars;
  116. data.characterClasses = charClasses;
  117.  
  118. data.VolumeMuted = volumeMuted;
  119. data.MusicScale = musicScale;
  120.  
  121. bf.Serialize(accountInfoFile, data);
  122. accountInfoFile.Close();
  123. }
  124.  
  125. public bool LoadCharacters()
  126. {
  127. if (File.Exists(Application.persistentDataPath + "/accountInfo.dat"))
  128. {
  129. BinaryFormatter bf = new BinaryFormatter();
  130. FileStream file = File.Open(Application.persistentDataPath + "/accountInfo.dat", FileMode.Open);
  131. AccountData data = (AccountData)bf.Deserialize(file);
  132. charNames = data.characterNames;
  133. numChars = data.numberOfCharacters;
  134. charClasses = data.characterClasses;
  135.  
  136. volumeMuted = data.VolumeMuted;
  137. musicScale = data.MusicScale;
  138.  
  139. file.Close();
  140. return true;
  141. }
  142.  
  143. return false;
  144. }
  145.  
  146. /*FOR LOADING PLAYER SPECIFIC DATA*/
  147.  
  148. public void Save(string saveName)
  149. {
  150. BinaryFormatter bf = new BinaryFormatter();
  151. FileStream playerInfoFile = File.Create(Application.persistentDataPath + "/playerInfo_" + saveName + ".dat");
  152.  
  153. PlayerData data = new PlayerData();
  154.  
  155. data.PlayerNumber = playerNumber;
  156. data.PlayerName = playerName;
  157. data.PlayerSkinColor = playerSkinColor;
  158. data.Level = playerLevel;
  159. data.PlayerExperience = playerEXP;
  160. data.UnlockedAbilities = unlockedAbilities;
  161. data.ability1 = playerAbility1;
  162. data.ability2 = playerAbility2;
  163. data.ability3 = playerAbility3;
  164. data.ability4 = playerAbility4;
  165. data.StrikeMod = strikeModifier;
  166. data.limitBreakMod = limitBreakModifier;
  167. data.limitBreakTrack = limitBreakTracker;
  168. data.attack = playerAttack;
  169. data.defense = playerDefense;
  170. data.prowess = playerProwess;
  171. data.speed = playerSpeed;
  172. data.PlayerColor = playerColorPreference;
  173. data.DecisionsMade = decisionsMade;
  174. data.PlayerDecisions = playerDecisions;
  175. data.GoodPoints = playerGoodPoints;
  176. data.EvilPoints = playerEvilPoints;
  177. data.SkillTree = skillTree;
  178. data.TotalAbilities = totalAbilities;
  179.  
  180. data.ArenaCompleted = arenaCompleted;
  181. data.EquipmentList = playerEquipmentList;
  182. data.EquippedIDs = playerEquippedIDs;
  183. data.InventoryList = playerInventory;
  184. data.LevelsCompleted = levelsCompleted;
  185. data.StagesCompleted = stagesCompleted;
  186.  
  187. data.difficulty = difficultyScale;
  188.  
  189. bf.Serialize(playerInfoFile, data);
  190. playerInfoFile.Close();
  191. }
  192.  
  193. public void Load(string saveName)
  194. {
  195. if(File.Exists(Application.persistentDataPath + "/playerInfo_" + saveName + ".dat"))
  196. {
  197. BinaryFormatter bf = new BinaryFormatter();
  198. FileStream file = File.Open(Application.persistentDataPath + "/playerInfo_" + saveName + ".dat", FileMode.Open);
  199. PlayerData data = (PlayerData)bf.Deserialize(file);
  200. file.Close();
  201.  
  202. // set variables here
  203. playerNumber = data.PlayerNumber;
  204. playerName = data.PlayerName;
  205. playerLevel = data.Level;
  206. playerEXP = data.PlayerExperience;
  207. difficultyScale = data.difficulty;
  208. unlockedAbilities = data.UnlockedAbilities;
  209. playerAbility1 = data.ability1;
  210. playerAbility2 = data.ability2;
  211. playerAbility3 = data.ability3;
  212. playerAbility4 = data.ability4;
  213. strikeModifier = data.StrikeMod;
  214. limitBreakModifier = data.limitBreakMod;
  215. limitBreakTracker = data.limitBreakTrack;
  216. playerSkinColor = data.PlayerSkinColor;
  217. playerColorPreference = data.PlayerColor;
  218. playerAttack = data.attack;
  219. playerDefense = data.defense;
  220. playerProwess = data.prowess;
  221. playerSpeed = data.speed;
  222. decisionsMade = data.DecisionsMade;
  223. playerDecisions = data.PlayerDecisions;
  224. playerEvilPoints = data.EvilPoints;
  225. playerGoodPoints = data.GoodPoints;
  226. totalAbilities = data.TotalAbilities;
  227. skillTree = data.SkillTree;
  228.  
  229. arenaCompleted = data.ArenaCompleted;
  230. playerInventory = data.InventoryList;
  231. playerEquipmentList = data.EquipmentList;
  232. playerEquippedIDs = data.EquippedIDs;
  233. levelsCompleted = data.LevelsCompleted;
  234. stagesCompleted = data.StagesCompleted;
  235. }
  236. }
  237.  
  238. public void Delete(string saveName)
  239. {
  240. if(File.Exists(Application.persistentDataPath + "/playerInfo_" + saveName + ".dat"))
  241. {
  242. File.Delete(Application.persistentDataPath + "/playerInfo_" + saveName + ".dat");
  243. }
  244. }
  245.  
  246. public void AddItemToInventory(string itemName, int quantity = 1)
  247. {
  248. int index = InventoryContainsItem(itemName);
  249.  
  250. if (index != -1)
  251. {
  252. playerInventoryQuantity[index] += quantity;
  253. }
  254. else
  255. {
  256. // increase inventory size
  257. string[] temp = new string[playerInventory.Length + 1];
  258. playerInventory.CopyTo(temp, 0);
  259. playerInventory = temp;
  260. playerInventoryQuantity[index] += quantity;
  261.  
  262. // increase inventory quantity size
  263. int[] temp2 = new int[playerInventoryQuantity.Length + 1];
  264. playerInventoryQuantity.CopyTo(temp2, 0);
  265. playerInventoryQuantity = temp2;
  266. playerInventoryQuantity[playerInventoryQuantity.Length - 1] = quantity;
  267. }
  268. }
  269.  
  270. public void RemoveItemFromInventory(string itemName, int quantity = 1, bool removeAll = false)
  271. {
  272. int index = InventoryContainsItem(itemName);
  273.  
  274. if (index != -1)
  275. {
  276. if(removeAll)
  277. {
  278. playerInventoryQuantity[index] = 0;
  279. }
  280. else
  281. {
  282. playerInventoryQuantity[index] -= quantity;
  283.  
  284. if(playerInventoryQuantity[index] <= 0)
  285. {
  286. // decrease inventory size
  287. string[] tempArr1 = new string[playerInventory.Length - 1];
  288. // copy initial portion of array
  289. for(int j = 0; j < index; ++j)
  290. tempArr1[j] = playerInventory[j];
  291. // copy over remaining portion of the array -1
  292. for (int i = index; i < tempArr1.Length; ++i)
  293. tempArr1[i] = playerInventory[i + 1];
  294.  
  295. playerInventory = tempArr1;
  296.  
  297. // decrease inventory quantity size
  298. int[] tempArr2 = new int[playerInventoryQuantity.Length - 1];
  299. // copy first portion of the inventory
  300. for (int j = 0; j < index; ++j)
  301. tempArr2[j] = playerInventoryQuantity[j];
  302. // copy over the remaining portion
  303. for (int i = index; i < tempArr2.Length; ++i)
  304. tempArr2[i] = playerInventoryQuantity[i + 1];
  305.  
  306. playerInventoryQuantity = tempArr2;
  307. }
  308. }
  309. }
  310. else
  311. return;
  312. }
  313.  
  314. // Returns the index of the item in the invetory where it was located
  315. // returns -1 is it failed to find the item
  316. public int InventoryContainsItem(string itemName)
  317. {
  318. int j = 0;
  319.  
  320. foreach(string i in playerInventory)
  321. {
  322. if(i == itemName)
  323. return j;
  324. ++j;
  325. }
  326.  
  327. return -1;
  328. }
  329.  
  330. public void setPlayerSkinColor(Color newColor)
  331. {
  332. playerSkinColor[0] = newColor.r;
  333. playerSkinColor[1] = newColor.g;
  334. playerSkinColor[2] = newColor.b;
  335. playerSkinColor[3] = newColor.a;
  336. }
  337.  
  338. public Color getPlayerSkinColor()
  339. {
  340. Color player_C = new Color(playerSkinColor[0], playerSkinColor[1], playerSkinColor[2], playerSkinColor[3]);
  341. return player_C;
  342. }
  343.  
  344. public void setPlayerColorPreference(Color newColor)
  345. {
  346. playerColorPreference[0] = newColor.r;
  347. playerColorPreference[1] = newColor.g;
  348. playerColorPreference[2] = newColor.b;
  349. playerColorPreference[3] = newColor.a;
  350. }
  351.  
  352. public Color getPlayerColorPreference()
  353. {
  354. Color player_C = new Color(playerColorPreference[0], playerColorPreference[1], playerColorPreference[2], playerColorPreference[3]);
  355. return player_C;
  356. }
  357. }
  358.  
  359. [Serializable]
  360. class PlayerData
  361. {
  362. public int PlayerNumber;
  363. public string PlayerName;
  364. public float[] PlayerSkinColor;
  365. public bool[] UnlockedAbilities;
  366. public Ability ability1, ability2, ability3, ability4;
  367. public string StrikeMod;
  368. public int Level;
  369. public int PlayerExperience;
  370. public Difficulty difficulty;
  371. public LimitBreakName limitBreakMod;
  372. public int limitBreakTrack;
  373. public string [] InventoryList;
  374. public int [] InventoryQuantities;
  375. public bool [,] EquipmentList;
  376. public int attack, defense, prowess, speed;
  377. public int [] EquippedIDs; // [0,1] = head, [2,3] = torso, [4,5] = legs, [6,7] = back, [8,9] = gloves, [10,11] = shoes, [12,13] = weapon, [14,15] = aura
  378. public float[] PlayerColor;
  379. public int LevelsCompleted;
  380. public int StagesCompleted;
  381. public int DecisionsMade;
  382. public bool[] PlayerDecisions;
  383. public int GoodPoints;
  384. public int EvilPoints;
  385. public int[] SkillTree;
  386.  
  387. public int TotalAbilities;
  388. public bool[] ArenaCompleted;
  389. }
  390.  
  391. [Serializable]
  392. class AccountData
  393. {
  394. public string[] characterNames;
  395. public PlayerClass[] characterClasses;
  396. public int numberOfCharacters = 0;
  397.  
  398. public bool VolumeMuted = false;
  399. public float MusicScale = 1.0f;
  400. }
Add Comment
Please, Sign In to add comment