Advertisement
evelynshilosky

TimeManager - Part 40

Apr 26th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7.  
  8. public class TimeManager : MonoBehaviour
  9. {
  10.     public static TimeManager Instance { get; set; }
  11.  
  12.     public UnityEvent OnDayPass = new UnityEvent(); // Day Passed Event
  13.     public enum Month
  14.     {
  15.         March,
  16.         April,
  17.         May,
  18.         June,
  19.         July,
  20.         August,
  21.         September,
  22.         October,
  23.         November,
  24.         December,
  25.         January,
  26.         February
  27.     } // 3 months per season 28 days per month so multiply that by 3 for season length
  28.     public enum Season
  29.     {
  30.         Spring,
  31.         Summer,
  32.         Autumn,
  33.         Winter
  34.     }
  35.  
  36.     public Season currentSeason = Season.Spring;
  37.     public Month currentMonth = Month.March;
  38.  
  39.     public int daysPerMonth = 28;
  40.     public int daysPerSeason = 84;
  41.    
  42.     private int daysInCurrentSeason = 1;
  43.     private int daysInCurrentMonth = 1;
  44.  
  45.     public enum DayOfWeek
  46.     {
  47.         Monday,
  48.         Tuesday,
  49.         Wednesday,
  50.         Thursday,
  51.         Friday,
  52.         Saturday,
  53.         Sunday
  54.     }
  55.  
  56.     public DayOfWeek currentDayOfWeek = DayOfWeek.Monday;
  57.  
  58.     private void Awake()
  59.     {
  60.         if (Instance != null && Instance != this)
  61.         {
  62.             Destroy(gameObject);
  63.         }
  64.         else
  65.         {
  66.             Instance = this;
  67.         }
  68.     }
  69.  
  70.     public int dayInGame = 1;
  71.  
  72.     public int yearInGame = 0;
  73.  
  74.     public TextMeshProUGUI dayUI;
  75.  
  76.     private void Start()
  77.     {
  78.         daysPerSeason = daysPerMonth * 3;
  79.         UpdateUI();
  80.     }
  81.  
  82.     public void TriggerNextDay()
  83.     {
  84.         dayInGame += 1;
  85.         daysInCurrentSeason += 1; daysInCurrentMonth += 1;
  86.  
  87.         currentDayOfWeek = (DayOfWeek)(((int)currentDayOfWeek + 1) % 7);
  88.  
  89.         if (daysInCurrentSeason > daysPerSeason)
  90.         {
  91.             // Switch to next season
  92.             daysInCurrentSeason = 1;
  93.             currentSeason = GetNextSeason();
  94.         }
  95.  
  96.         if (daysInCurrentMonth > daysPerMonth)
  97.         {
  98.             // Switch to next month
  99.             daysInCurrentMonth = 1;
  100.             currentMonth = GetNextMonth();
  101.         }
  102.  
  103.         UpdateUI();
  104.  
  105.         OnDayPass.Invoke();
  106.     }
  107.  
  108.     private Season GetNextSeason()
  109.     {
  110.         int currentSeasonIndex = (int)currentSeason;
  111.         int nextSeasonIndex = (currentSeasonIndex + 1) % 4;
  112.  
  113.         // Increase Year by one
  114.         if (nextSeasonIndex == 0)
  115.         {
  116.             yearInGame += 1;
  117.         }
  118.        
  119.         return (Season)nextSeasonIndex;
  120.     }
  121.     private Month GetNextMonth()
  122.     {
  123.         int currentMonthIndex = (int)currentMonth;
  124.         int nextMonthIndex = (currentMonthIndex + 1) % 12;
  125.  
  126.         return (Month)nextMonthIndex;
  127.     }
  128.     private void UpdateUI()
  129.     {
  130.         dayUI.text = $"{currentSeason}: {currentDayOfWeek}, {currentMonth} {daysInCurrentMonth}"; //Summer: Monday, August 1
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement