Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Events;
- public class TimeManager : MonoBehaviour
- {
- public static TimeManager Instance { get; set; }
- public UnityEvent OnDayPass = new UnityEvent(); // Day Passed Event
- public enum Month
- {
- March,
- April,
- May,
- June,
- July,
- August,
- September,
- October,
- November,
- December,
- January,
- February
- } // 3 months per season 28 days per month so multiply that by 3 for season length
- public enum Season
- {
- Spring,
- Summer,
- Autumn,
- Winter
- }
- public Season currentSeason = Season.Spring;
- public Month currentMonth = Month.March;
- public int daysPerMonth = 28;
- public int daysPerSeason = 84;
- private int daysInCurrentSeason = 1;
- private int daysInCurrentMonth = 1;
- public enum DayOfWeek
- {
- Monday,
- Tuesday,
- Wednesday,
- Thursday,
- Friday,
- Saturday,
- Sunday
- }
- public DayOfWeek currentDayOfWeek = DayOfWeek.Monday;
- private void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- }
- else
- {
- Instance = this;
- }
- }
- public int dayInGame = 1;
- public int yearInGame = 0;
- public TextMeshProUGUI dayUI;
- private void Start()
- {
- daysPerSeason = daysPerMonth * 3;
- UpdateUI();
- }
- public void TriggerNextDay()
- {
- dayInGame += 1;
- daysInCurrentSeason += 1; daysInCurrentMonth += 1;
- currentDayOfWeek = (DayOfWeek)(((int)currentDayOfWeek + 1) % 7);
- if (daysInCurrentSeason > daysPerSeason)
- {
- // Switch to next season
- daysInCurrentSeason = 1;
- currentSeason = GetNextSeason();
- }
- if (daysInCurrentMonth > daysPerMonth)
- {
- // Switch to next month
- daysInCurrentMonth = 1;
- currentMonth = GetNextMonth();
- }
- UpdateUI();
- OnDayPass.Invoke();
- }
- private Season GetNextSeason()
- {
- int currentSeasonIndex = (int)currentSeason;
- int nextSeasonIndex = (currentSeasonIndex + 1) % 4;
- // Increase Year by one
- if (nextSeasonIndex == 0)
- {
- yearInGame += 1;
- }
- return (Season)nextSeasonIndex;
- }
- private Month GetNextMonth()
- {
- int currentMonthIndex = (int)currentMonth;
- int nextMonthIndex = (currentMonthIndex + 1) % 12;
- return (Month)nextMonthIndex;
- }
- private void UpdateUI()
- {
- dayUI.text = $"{currentSeason}: {currentDayOfWeek}, {currentMonth} {daysInCurrentMonth}"; //Summer: Monday, August 1
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement