This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
mochapine64backup/MoCha/Assets/Scripts/GetGoals.cs

250 lines
6.9 KiB
C#

using System;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text;
//Pretty much a copy of GetData...
namespace PedometerU.Tests
{
public class GetGoals : MonoBehaviour
{
// Goals Progress Bar
public Image pbbg;
public Image pbf;
// Monster Progress Bar
public Image mpbbg;
public Image mpbf;
private string[] items = { "circle_eye", "crazy_hair", "circle_head", "gap_mouth", "pig_nose", "crown" };
// Display Text
public Text goalText;
public Text stepText;
public Text pointsText;
public Text remainingText;
// Goal information.
public string currentDayStr = System.DateTime.Now.ToString("MM/dd/yyyy");
public string goalDayStr = System.DateTime.Now.ToString("MM/dd/yyyy");
public string prefKey;
// Used for time calculation.
private int timeUsed;
private int timeRemaining;
private const int secondsperday = 86400;
//Progress Bar...
public float barDisplay; //current progress
//private Vector2 barPos;
// This gets overwritten later by the background's size values.
private Vector2 barSize = new Vector2(5000, 10);
//Pedometer
private Pedometer pedometer;
int userSteps;
int userPoints;
int stepsGoal = 100;
private int savedSteps;
private int savedPoints;
void OnStep (int steps, double distance) {
//userDistance = (distance * 3.28084);
userSteps = steps + savedSteps;
PlayerPrefs.SetInt("currentSteps",userSteps);
stepText.text = userSteps.ToString () + "/" + stepsGoal.ToString();
userPoints = steps*5 + savedPoints;
PlayerPrefs.SetInt("totalPoints",userPoints);
pointsText.text = "Points: " + userPoints.ToString ();
}
private void OnDisable () {
// Release the pedometer
pedometer.Dispose();
pedometer = null;
}
//saved value from last session loads in on START
//write usersteps + saved val to pref in ONSTEP
void Start ()
{
savedSteps = PlayerPrefs.GetInt("currentSteps");
savedPoints = PlayerPrefs.GetInt("totalPoints");
// Create a new pedometer
pedometer = new Pedometer(OnStep);
// Reset UI
stepText.text = savedSteps.ToString () + "/" + stepsGoal.ToString();
if(PlayerPrefs.HasKey("totalPoints"))
{
pointsText.text = "Points: " + PlayerPrefs.GetInt("totalPoints").ToString ();
}
else
{
PlayerPrefs.SetInt("totalPoints", 0);
pointsText.text = "Points: " + PlayerPrefs.GetInt("totalPoints").ToString ();
}
userSteps = savedSteps;
userPoints = savedPoints;
//OnStep(savedSteps, 0);
Debug.Log(savedSteps);
//goalText.text = prefKey;
goalText.color = new Color(0f, 0f, 0f);
Debug.Log(pbbg.rectTransform.anchoredPosition.x.ToString() + " " + pbbg.rectTransform.anchoredPosition.y.ToString());
//barPos = new Vector2(pbbg.transform.position.x, 1895 - pbbg.transform.position.y);
currentDayStr = System.DateTime.Now.ToString("MM/dd/yyyy");
goalDayStr = System.DateTime.Now.ToString("MM/dd/yyyy");
Debug.Log(goalDayStr);
if(prefKey == "daily")
savePersistantGoalDate("daily");
}
//on a button press? begin a goal?
void startGoal()
{
}
void savePersistantGoalDate(string key)
{
PlayerPrefs.SetString(key, goalDayStr);
}
bool checkNewDay(string key)
{
string checkDay = PlayerPrefs.GetString(key);
if(checkDay == currentDayStr)
return false;
return true;
}
// Was used for testing.
private float tempcounter = 0.1f;
// Call this to update the goals progress bar.
public void updateProgressBar()
{
//Debug.Log (pbbg.rectTransform.rect.width + " " + pbbg.rectTransform.rect.height);
barSize.x = pbbg.rectTransform.rect.width;
//tempcounter += 0.0005f;
// resize the fill.
RectTransform temp = pbf.rectTransform;
//temp.sizeDelta = new Vector2 (-barSize.x + barSize.x * tempcounter, 0);
temp.sizeDelta = new Vector2 (-barSize.x + (barSize.x * barDisplay), 0);
temp.anchoredPosition = new Vector2 (temp.rect.width * 0.5f - (barSize.x * 0.5f),0);
}
// Call this to update the monster's progress bar.
public void updateMonsterProgressBar()
{
float fractionused = (float)(timeUsed) / (float)(secondsperday);
//Debug.Log (dayleft);
barSize.x = mpbbg.rectTransform.rect.width;
//tempcounter += 0.0005f;
// resize the fill.
RectTransform temp = mpbf.rectTransform;
//temp.sizeDelta = new Vector2 (-barSize.x + barSize.x * tempcounter, 0);
temp.sizeDelta = new Vector2 (-barSize.x + (barSize.x * fractionused), 0);
temp.anchoredPosition = new Vector2 (temp.rect.width * 0.5f - (barSize.x * 0.5f),0);
}
// Calculates the time remaining for the day.
// Will likely make a separate function for smaller timed goals if we have time.
public void updateTimeRemaining()
{
timeUsed = ((System.DateTime.Now.Hour * 60 * 60) + (System.DateTime.Now.Minute * 60) + System.DateTime.Now.Second);
timeRemaining = secondsperday - timeUsed;
}
// Format the time remaining variable to a proper string.
public string formatTimeRemaining()
{
string output = "Time remaining: ";
output += "" + (timeRemaining / 3600);
output += ":" + (timeRemaining % 3600 / 60);
output += ":" + (timeRemaining % 60);
return output;
}
void Update()
{
updateTimeRemaining ();
updateProgressBar ();
updateMonsterProgressBar ();
//needs current points counting toward this goal (daily?)
//needs the total required points for this goal
currentDayStr = System.DateTime.Now.ToString("MM/dd/yyyy");
remainingText.text = formatTimeRemaining ();
//barDisplay = Time.time*0.05f; //put actual progress here (current/total)
barDisplay = (float)userSteps/(float)stepsGoal;
//failed to complete goal
if(currentDayStr != goalDayStr)
{
Debug.Log(currentDayStr);
Debug.Log(goalDayStr);
Debug.Log("DAY HAS PASSED!!");
//make it so user dismisses the goal instead!
//Destroy(this);
//destroy the goal and set a new one?
}
//goal complete
if(barDisplay >= 1.0f)
{
Debug.Log("Goal complete! +100pts!");
userSteps = 0;
PlayerPrefs.SetInt("currentSteps", userSteps);
savedSteps = 0;
//OnStep(0,0);
stepText.text = userSteps.ToString () + "/" + stepsGoal.ToString();
//destroy the goal and set a new one?
savedPoints += stepsGoal*10;
PlayerPrefs.SetInt("totalPoints", savedPoints);
pointsText.text = "Points: " + PlayerPrefs.GetInt("totalPoints").ToString ();
//Random item
StringBuilder invList = new StringBuilder ();
invList.Append(PlayerPrefs.GetString ("inventory"));
invList.Append(items[(int)Math.Round(UnityEngine.Random.value*6.0f)]).Append(" ");
PlayerPrefs.SetString ("inventory", invList.ToString());
}
//only applies if "daily" goal
if(prefKey == "daily")
{
if(checkNewDay(prefKey))
{
Debug.Log("You failed your goal"); //not true if progress is actually complete!
goalDayStr = currentDayStr;
savePersistantGoalDate("daily");
}
}
}
}
}