AppingCS

SyncTimeSystem

Apr 17th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. //Script by Andriy Skyba
  5. namespace FeelkepStudio
  6. {
  7. public class SyncTimeSystem : MonoBehaviour
  8. {
  9. // Text to show count of coins
  10. [SerializeField] Text earnedcoinsText;
  11. // Timer
  12. float timer = 10;
  13. // Count of coins
  14. int earnedcoins;
  15. void Start()
  16. {
  17. // If key not found
  18. if (!PlayerPrefs.HasKey("Coins"))
  19. // Then set value of coins 0 (default)
  20. PlayerPrefs.SetInt("Coins", 0);
  21. // Get coins of PlayerPrefs key
  22. earnedcoins = PlayerPrefs.GetInt("Coins");
  23. int сurrentTimeToExit = PlayerPrefs.GetInt("LastSession");
  24. // Get now time in seconds
  25. int nowAllSeconds = GetNowTimeInSeconds();
  26. // Get absence time
  27. int absenceTime = nowAllSeconds - сurrentTimeToExit;
  28. // How long was the player absent
  29. Debug.Log("You were absent: " + absenceTime + " seconds");
  30. // Parsing leave time to coins (10 seconds = 1$ (1 coin))
  31. earnedcoins += absenceTime / 10;
  32. Debug.Log("You earned: " + absenceTime / 10 + " coins!");
  33. // Parsing to date time
  34. int hours = 0, minutes = 0, seconds = 0;
  35. // Parsing leave seconds to hour
  36. if (absenceTime / 3600 > 0)
  37. {
  38. // Divide seconds by one hour in seconds, namely (3600)
  39. hours = absenceTime / 3600;
  40. // Get seconds from the resulting hour
  41. absenceTime -= hours * 3600;
  42. }
  43. // Parsing leave seconds to minutes
  44. if (absenceTime / 60 > 0)
  45. {
  46. // Divide seconds by one minute in seconds, namely (60)
  47. minutes = absenceTime / 60;
  48. // Get seconds from the resulting minutes
  49. absenceTime -= minutes * 60;
  50. }
  51. // Just equals last time to seconds
  52. seconds = absenceTime;
  53. // Debug
  54. Debug.Log("You were absent:\nHours: " + hours + " | Minutes: " + minutes + " | Seconds: " + seconds);
  55. // Equate count of coins to text
  56. earnedcoinsText.text = earnedcoins.ToString() + "$";
  57. }
  58. void Update()
  59. {
  60. // Simple cooldown timer
  61. if (timer > 0)
  62. timer -= Time.deltaTime;
  63. // If timer < 0
  64. else if (timer <= 0)
  65. {
  66. // Set timer to 10
  67. timer = 10;
  68. // Add 1 coin
  69. earnedcoins += 1;
  70. // Equate count of coins to text
  71. earnedcoinsText.text = earnedcoins.ToString() + "$";
  72. }
  73. }
  74. // Reset value of coins
  75. public void ResetCoinsValue()
  76. {
  77. // Set value of coins 0
  78. earnedcoins = 0;
  79. // Save value of coins
  80. PlayerPrefs.SetInt("Coins", earnedcoins);
  81. }
  82. // Getting now time in seconds
  83. int GetNowTimeInSeconds()
  84. {
  85. // New variable
  86. int allSeconds = 0;
  87. // Parsing hour to allSeconds
  88. allSeconds += DateTime.Now.Hour * 3600;
  89. // Parsing minute to allSeconds
  90. allSeconds += DateTime.Now.Minute * 60;
  91. // Add now seconds to allSeconds
  92. allSeconds += DateTime.Now.Second;
  93. // Returning result
  94. return allSeconds;
  95. }
  96. // If player is quit
  97. private void OnApplicationQuit()
  98. {
  99. // Debug
  100. Debug.Log("Saving last session...");
  101. // Getting now time in seconds
  102. int allseconds = GetNowTimeInSeconds();
  103. // Save current value of coins
  104. PlayerPrefs.SetInt("Coins", earnedcoins);
  105. // Save now time
  106. PlayerPrefs.SetInt("LastSession", allseconds);
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment