Added saving and loading

Added PlayerData class to GamerManager.cs
- Stored in Application.PersistantDataPath + "/playerInfo.dat"
- Safer way to store / transfer data?
- Add saving of local time when calling Save() or Load()
- Compare save time to load time and give afk rewards earned based on
rotation speed
This commit is contained in:
Shaun Reed 2017-09-03 18:56:05 -04:00
parent f20d8340bb
commit a339149a71
6 changed files with 154 additions and 37 deletions

Binary file not shown.

View File

@ -7,17 +7,16 @@ public class ExperienceBar : MonoBehaviour {
// Eventually.. public RectTransform gainedExp // Eventually.. public RectTransform gainedExp
//Array used as expRequired[leveldesired] //Array used as expRequired[leveldesired]
[SerializeField]
private int level = 1; //playerdata - needs saved
[SerializeField] public int currentLevel = 1;
private float expObtained = 0; public float currentExp = 0;
[SerializeField] public float currentRequirement = 100;
private float expRequired = 100; public GameObject currentLevelText;
[SerializeField] [SerializeField]
private float fillAmount; private float fillAmount;
[SerializeField] [SerializeField]
private GameObject levelText;
[SerializeField]
private Image expBarSprite; private Image expBarSprite;
[SerializeField] [SerializeField]
private float lerpSpeed; private float lerpSpeed;
@ -32,11 +31,17 @@ public class ExperienceBar : MonoBehaviour {
// Use this for initialization // Use this for initialization
void Start () { void Start () {
fillAmount = 0; fillAmount = currentExp / currentRequirement;
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
fillAmount = currentExp / currentRequirement;
if (currentLevelText.GetComponent<Text>().text != currentLevel.ToString()){
currentLevelText.GetComponent<Text>().text = currentLevel.ToString();
}
if (fillAmount != expBarSprite.fillAmount) if (fillAmount != expBarSprite.fillAmount)
{ {
@ -47,7 +52,7 @@ public class ExperienceBar : MonoBehaviour {
if (expBarSprite.fillAmount >= 1.0f) if (expBarSprite.fillAmount >= 1.0f)
{ {
LevelUp(); LevelUp();
clicksNeeded = (expRequired - expObtained) / Increment; clicksNeeded = (currentRequirement - currentExp) / Increment;
fillAmount = 0; fillAmount = 0;
} }
} }
@ -55,28 +60,28 @@ public class ExperienceBar : MonoBehaviour {
public void ExpMore() public void ExpMore()
{ {
++clicks; ++clicks;
expObtained = expObtained + Increment; currentExp = currentExp + Increment;
fillAmount = (expObtained / expRequired); fillAmount = currentExp / currentRequirement;
Debug.Log("fillAmount = " + fillAmount); Debug.Log("fillAmount = " + fillAmount);
} }
public void LevelUp() public void LevelUp()
{ {
++level; ++currentLevel;
levelText.GetComponent<Text>().text = level.ToString(); currentLevelText.GetComponent<Text>().text = currentLevel.ToString();
previousExpRequired = expRequired; previousExpRequired = currentRequirement;
expObtained = 0; currentExp = 0;
expRequired = Mathf.Pow(expRequired, 1.05f); currentRequirement = Mathf.Pow(currentRequirement, 1.05f);
GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().RaiseRotationSpeed(); GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().RaiseRotationSpeed();
} }
public void ResetExp() public void ResetExp()
{ {
level = 1; currentLevel = 1;
levelText.GetComponent<Text>().text = level.ToString(); currentLevelText.GetComponent<Text>().text = currentLevel.ToString();
fillAmount = 0; fillAmount = 0;
expObtained = 0; currentExp = 0;
expRequired = 100; currentRequirement = 100;
clicksNeeded = 10; clicksNeeded = 10;
clicks = 0; clicks = 0;
} }
@ -84,9 +89,17 @@ public class ExperienceBar : MonoBehaviour {
public void ExpLess() public void ExpLess()
{ {
--clicks; --clicks;
expObtained = expObtained - Increment; currentExp = currentExp - Increment;
fillAmount = (expObtained / expRequired); fillAmount = (currentExp / currentRequirement);
Debug.Log("fillAmount = " + fillAmount); Debug.Log("fillAmount = " + fillAmount);
} }
public float GetExperience()
{
float currentExp;
currentExp = this.currentExp;
return currentExp;
}
} }

View File

@ -0,0 +1,95 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class GameManager : MonoBehaviour {
//Used to save playerdata
//Serializable tells unity it can save to a file
[Serializable]
private class PlayerData
{
public int level;
public float experience;
public float requirement;
public float speed;
public float increment;
}
// Use this for initialization
void Start () {
Load();
}
// Update is called once per frame
void Update () {
}
void OnEnable()
{
Load();
}
void OnApplicationPause()
{
Save();
}
void OnApplicationQuit()
{
Save();
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
int currentLevel = GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentLevel;
float currentExp = GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentExp;
float currentRequirement = GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentRequirement;
float currentSpeed = GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentSpeed;
float currentIncrement = GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentIncrement;
PlayerData data = new PlayerData();
data.level = currentLevel;
data.experience = currentExp;
data.requirement = currentRequirement;
data.speed = currentSpeed;
data.increment = currentIncrement;
bf.Serialize(file, data);
file.Close();
Debug.Log("Saved");
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentSpeed = data.speed;
GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentIncrement = data.increment;
GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentLevel = data.level;
GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentExp = data.experience;
GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentRequirement = data.requirement;
Debug.Log("Loaded");
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 2da23f5b35feb9749839423cfa5b61b2
timeCreated: 1503806997
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -3,8 +3,10 @@ using System.Collections;
public class SpinningCube : MonoBehaviour public class SpinningCube : MonoBehaviour
{ {
public float m_Speed = 20f;
public float increments = 10f; //Playerdata -- Needs saved
public float currentSpeed = 20f;
public float currentIncrement = 10f;
private string RotationDirection = "Up"; private string RotationDirection = "Up";
@ -52,21 +54,21 @@ public class SpinningCube : MonoBehaviour
public void RaiseRotationSpeed() public void RaiseRotationSpeed()
{ {
m_Speed = m_Speed + increments; currentSpeed = currentSpeed + currentIncrement;
Debug.Log("Rotation Speed: " + m_Speed); Debug.Log("Rotation Speed: " + currentSpeed);
} }
public void LowerRotationSpeed() public void LowerRotationSpeed()
{ {
m_Speed = m_Speed - increments; currentSpeed = currentSpeed - currentIncrement;
Debug.Log("Rotation Speed: " + m_Speed); Debug.Log("Rotation Speed: " + currentSpeed);
} }
public void ResetRotationSpeed() public void ResetRotationSpeed()
{ {
m_Speed = 20.0f; currentSpeed = 20.0f;
Debug.Log("Rotation Speed Reset"); Debug.Log("Rotation Speed Reset");
} }
@ -111,16 +113,11 @@ public class SpinningCube : MonoBehaviour
gameObject.GetComponent<Renderer>().material.color = Color.cyan; gameObject.GetComponent<Renderer>().material.color = Color.cyan;
} }
public void Start()
{
Vector3 rotationOrigin = GameObject.FindGameObjectWithTag("Player").transform.rotation.eulerAngles;
}
void FixedUpdate() void FixedUpdate()
{ {
//Set angle1 = eulerAngle of axis being rotated prior to applying rotation //Set angle1 = eulerAngle of axis being rotated prior to applying rotation
angle1 = this.gameObject.transform.rotation.eulerAngles.y; angle1 = this.gameObject.transform.rotation.eulerAngles.y;
transform.Rotate(m_RotationDirection * Time.deltaTime * m_Speed); transform.Rotate(m_RotationDirection * Time.deltaTime * currentSpeed);
//angle2 = eulerAngle of axis after rotation applied //angle2 = eulerAngle of axis after rotation applied
angle2 = this.gameObject.transform.rotation.eulerAngles.y; angle2 = this.gameObject.transform.rotation.eulerAngles.y;
//Difference between angle2 and angle1, how much the object rotated between frames //Difference between angle2 and angle1, how much the object rotated between frames
@ -132,7 +129,7 @@ public class SpinningCube : MonoBehaviour
//If object has rotated 20 degrees (m_speed = 20), when angle1 = 350, && angle2 = 10 //If object has rotated 20 degrees (m_speed = 20), when angle1 = 350, && angle2 = 10
//angle2(10)-angle1(350) = -340 //angle2(10)-angle1(350) = -340
//Object has rotated past 360 //Object has rotated past 360
if ((m_Speed > 0) && (angledif < 0)) if ((currentSpeed > 0) && (angledif < 0))
{ {
++rotations; ++rotations;
GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().ExpMore(); GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().ExpMore();