Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- using UnityEngine;
- public class GameController : MonoBehaviour {
- static GameController gameController = null;
- private UnlockablesScript unlockables;
- public int score;
- public int money;
- public int totalBalance;
- public int selectedPaddle;
- public int selectedBall;
- public float difficultySetting;
- public int tries;
- public int startingTries;
- void Awake()
- {
- if (gameController == null)
- {
- DontDestroyOnLoad(gameObject);
- gameController = this;
- }
- else if (gameController != null && gameController != this)
- {
- Destroy(gameObject);
- }
- Load();
- }
- // Use this for initialization
- void Start () {
- difficultySetting = PlayerPrefsManager.GetDifficulty();
- unlockables = FindObjectOfType<UnlockablesScript>();
- startingTries = 3;
- }
- // Update is called once per frame
- void Update () {
- //debugging keys, remove once completed
- if (Input.GetKeyDown(KeyCode.Alpha0))
- {
- selectedPaddle++;
- if (selectedPaddle >= 8)
- {
- selectedPaddle = 0;
- }
- }
- if (Input.GetKeyDown(KeyCode.Alpha9))
- {
- selectedBall++;
- if (selectedBall >= 8)
- {
- selectedBall = 0;
- }
- }
- if (Input.GetKeyDown(KeyCode.Alpha8))
- {
- if (difficultySetting < 4)
- {
- difficultySetting += 1f;
- Debug.Log("Difficulty value = " + difficultySetting);
- }
- else if (difficultySetting == 4f)
- {
- difficultySetting = 1f;
- Debug.Log("Difficulty value = " + difficultySetting);
- }
- }
- //
- CheckDifficultySetting();
- }
- private void CheckDifficultySetting()
- {
- if (difficultySetting == 1) //Easy
- {
- startingTries = 5;
- }
- else if (difficultySetting == 2) // Normal
- {
- startingTries = 3;
- }
- else if (difficultySetting == 3) // Hard
- {
- startingTries = 1;
- }
- else if (difficultySetting == 4) // Masochist
- {
- startingTries = 0;
- }
- }
- public void AddToBalance(int deposit)
- {
- totalBalance += deposit;
- }
- public void Save()
- {
- BinaryFormatter bf = new BinaryFormatter();
- FileStream file = File.Create(Application.persistentDataPath + "/player.dat");
- PlayerData data = new PlayerData();
- data.totalMoney = totalBalance;
- data.p01 = unlockables.paddles[0];
- data.p02 = unlockables.paddles[1];
- data.p03 = unlockables.paddles[2];
- data.p04 = unlockables.paddles[3];
- data.p05 = unlockables.paddles[4];
- data.p06 = unlockables.paddles[5];
- data.p07 = unlockables.paddles[6];
- data.b01 = unlockables.balls[0];
- data.b02 = unlockables.balls[1];
- data.b03 = unlockables.balls[2];
- data.b04 = unlockables.balls[3];
- data.b05 = unlockables.balls[4];
- data.b06 = unlockables.balls[5];
- data.b07 = unlockables.balls[6];
- data.colorChange = unlockables.colorChangeMode;
- bf.Serialize(file, data);
- }
- public void Load()
- {
- if (File.Exists(Application.persistentDataPath + "/player.dat"))
- {
- BinaryFormatter bf = new BinaryFormatter();
- FileStream file = File.Open(Application.persistentDataPath + "/player.dat", FileMode.Open);
- PlayerData data = (PlayerData)bf.Deserialize(file);
- file.Close();
- //change later so money only adds to total for each game
- totalBalance = data.totalMoney;
- unlockables.paddles[0] = data.p01;
- unlockables.paddles[1] = data.p02;
- unlockables.paddles[2] = data.p03;
- unlockables.paddles[3] = data.p04;
- unlockables.paddles[4] = data.p05;
- unlockables.paddles[5] = data.p06;
- unlockables.paddles[6] = data.p07;
- unlockables.balls[0] = data.b01;
- unlockables.balls[1] = data.b02;
- unlockables.balls[2] = data.b03;
- unlockables.balls[3] = data.b04;
- unlockables.balls[4] = data.b05;
- unlockables.balls[5] = data.b06;
- unlockables.balls[6] = data.b07;
- unlockables.colorChangeMode = data.colorChange;
- }
- }
- public void Delete() //Deletes player data binary if player wants to clear all unlocks and accumulated money.
- {
- if(File.Exists(Application.persistentDataPath + "/player.dat"))
- {
- BinaryFormatter bf = new BinaryFormatter();
- File.Delete(Application.persistentDataPath + "/player.dat");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement