Advertisement
emonegarand

GameController.cs

Apr 21st, 2018
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.13 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.IO;
  7. using UnityEngine;
  8.  
  9. public class GameController : MonoBehaviour {
  10.  
  11.     static GameController gameController = null;
  12.     private UnlockablesScript unlockables;
  13.  
  14.     public int score;
  15.     public int money;
  16.     public int totalBalance;
  17.     public int selectedPaddle;
  18.     public int selectedBall;
  19.     public float difficultySetting;
  20.     public int tries;
  21.     public int startingTries;
  22.  
  23.     void Awake()
  24.     {
  25.         if (gameController == null)
  26.         {
  27.             DontDestroyOnLoad(gameObject);
  28.             gameController = this;
  29.         }
  30.         else if (gameController != null && gameController != this)
  31.         {
  32.             Destroy(gameObject);
  33.         }
  34.         Load();
  35.     }
  36.  
  37.     // Use this for initialization
  38.     void Start () {
  39.         difficultySetting = PlayerPrefsManager.GetDifficulty();
  40.         unlockables = FindObjectOfType<UnlockablesScript>();
  41.         startingTries = 3;
  42.     }
  43.    
  44.     // Update is called once per frame
  45.     void Update () {
  46.  
  47.         //debugging keys, remove once completed
  48.         if (Input.GetKeyDown(KeyCode.Alpha0))
  49.         {
  50.             selectedPaddle++;
  51.  
  52.             if (selectedPaddle >= 8)
  53.             {
  54.                 selectedPaddle = 0;
  55.             }
  56.         }
  57.         if (Input.GetKeyDown(KeyCode.Alpha9))
  58.         {
  59.             selectedBall++;
  60.  
  61.             if (selectedBall >= 8)
  62.             {
  63.                 selectedBall = 0;
  64.             }
  65.         }
  66.         if (Input.GetKeyDown(KeyCode.Alpha8))
  67.         {
  68.  
  69.             if (difficultySetting < 4)
  70.             {
  71.                 difficultySetting += 1f;
  72.                 Debug.Log("Difficulty value = " + difficultySetting);
  73.             }
  74.            
  75.  
  76.             else if (difficultySetting == 4f)
  77.             {
  78.                 difficultySetting = 1f;
  79.                 Debug.Log("Difficulty value = " + difficultySetting);
  80.             }
  81.         }
  82.  
  83.         //
  84.         CheckDifficultySetting();
  85.     }
  86.  
  87.     private void CheckDifficultySetting()
  88.     {
  89.         if (difficultySetting == 1) //Easy
  90.         {
  91.             startingTries = 5;
  92.         }
  93.         else if (difficultySetting == 2) // Normal
  94.         {
  95.             startingTries = 3;
  96.         }
  97.         else if (difficultySetting == 3) // Hard
  98.         {
  99.             startingTries = 1;
  100.         }
  101.         else if (difficultySetting == 4) // Masochist
  102.         {
  103.             startingTries = 0;
  104.         }
  105.  
  106.     }
  107.     public void AddToBalance(int deposit)
  108.     {
  109.         totalBalance += deposit;
  110.     }
  111.     public void Save()
  112.     {
  113.         BinaryFormatter bf = new BinaryFormatter();
  114.         FileStream file = File.Create(Application.persistentDataPath + "/player.dat");
  115.  
  116.         PlayerData data = new PlayerData();
  117.         data.totalMoney = totalBalance;
  118.         data.p01 = unlockables.paddles[0];
  119.         data.p02 = unlockables.paddles[1];
  120.         data.p03 = unlockables.paddles[2];
  121.         data.p04 = unlockables.paddles[3];
  122.         data.p05 = unlockables.paddles[4];
  123.         data.p06 = unlockables.paddles[5];
  124.         data.p07 = unlockables.paddles[6];
  125.  
  126.         data.b01 = unlockables.balls[0];
  127.         data.b02 = unlockables.balls[1];
  128.         data.b03 = unlockables.balls[2];
  129.         data.b04 = unlockables.balls[3];
  130.         data.b05 = unlockables.balls[4];
  131.         data.b06 = unlockables.balls[5];
  132.         data.b07 = unlockables.balls[6];
  133.  
  134.         data.colorChange = unlockables.colorChangeMode;
  135.  
  136.         bf.Serialize(file, data);
  137.     }
  138.  
  139.     public void Load()
  140.     {
  141.         if (File.Exists(Application.persistentDataPath + "/player.dat"))
  142.         {
  143.             BinaryFormatter bf = new BinaryFormatter();
  144.             FileStream file = File.Open(Application.persistentDataPath + "/player.dat", FileMode.Open);
  145.             PlayerData data = (PlayerData)bf.Deserialize(file);
  146.             file.Close();
  147.  
  148.             //change later so money only adds to total for each game
  149.             totalBalance = data.totalMoney;
  150.  
  151.             unlockables.paddles[0] = data.p01;
  152.             unlockables.paddles[1] = data.p02;
  153.             unlockables.paddles[2] = data.p03;
  154.             unlockables.paddles[3] = data.p04;
  155.             unlockables.paddles[4] = data.p05;
  156.             unlockables.paddles[5] = data.p06;
  157.             unlockables.paddles[6] = data.p07;
  158.  
  159.             unlockables.balls[0] = data.b01;
  160.             unlockables.balls[1] = data.b02;
  161.             unlockables.balls[2] = data.b03;
  162.             unlockables.balls[3] = data.b04;
  163.             unlockables.balls[4] = data.b05;
  164.             unlockables.balls[5] = data.b06;
  165.             unlockables.balls[6] = data.b07;
  166.  
  167.             unlockables.colorChangeMode = data.colorChange;
  168.         }
  169.     }
  170.     public void Delete() //Deletes player data binary if player wants to clear all unlocks and accumulated money.
  171.     {
  172.         if(File.Exists(Application.persistentDataPath + "/player.dat"))
  173.         {
  174.             BinaryFormatter bf = new BinaryFormatter();
  175.             File.Delete(Application.persistentDataPath + "/player.dat");
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement