Advertisement
Ryoh

TimeTrackingStateMachineDemo

Jun 11th, 2022
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1.     public enum DayOfWeek
  2.     {
  3.         Sunday,
  4.         Monday,
  5.     }
  6.  
  7.     public enum TimeOfDay
  8.     {
  9.         Morning,
  10.         Evening
  11.         // I'm not sure if I like AfterSchool being a time of day. I might keep track of whether it is a school day separate from Time of day, and keep TimeOfDay to being specific times like Evening and morning. A bool isSchoolDay should suffice for now.
  12.     }
  13.  
  14.     public class TimeTracker
  15.     {
  16.         // currentDay is private, but CurrentDay is a public getter. This means you can check the variable, but not change it.
  17.         // The same is done for currentTime.
  18.         private DayOfWeek currentDay;
  19.         public DayOfWeek CurrentDay => currentDay;
  20.  
  21.         private TimeOfDay currentTime;
  22.         public TimeOfDay CurrentTime => currentTime;
  23.         // I didn't implement a month state, but I think you can figure it out.
  24.  
  25.         // I assume holidays will be added and I think a dayOfMonth counter would be useful for that. I also think the totalDays is something that would be tracked.
  26.         // TODO: implement where they should increment.
  27.         private int dayOfMonth = 0;
  28.         private int totalDays = 0;
  29.  
  30.         // Constructor used to create the class. Give it the starting day, and time.
  31.         public TimeTracker(DayOfWeek startDay, TimeOfDay startTime)
  32.         {
  33.             this.currentDay = startDay;
  34.             this.currentTime = startTime;
  35.         }
  36.  
  37.         public void AdvanceTime()
  38.         {
  39.             switch (currentTime)
  40.             {
  41.                 case TimeOfDay.Morning:
  42.                     AdvanceTimeFromMorning();
  43.                     break;
  44.                 case TimeOfDay.Evening:
  45.                     AdvanceTimeFromEvening();
  46.                     break;
  47.                 default:
  48.                     Debug.LogError("Current Time of day not defined.");
  49.                     break;
  50.             }
  51.         }
  52.  
  53.         private void AdvanceTimeFromMorning()
  54.         {
  55.             // Advance time to evening.
  56.             currentTime = TimeOfDay.Evening;
  57.         }
  58.  
  59.         private void AdvanceTimeFromEvening()
  60.         {
  61.             currentTime = TimeOfDay.Morning;
  62.             AdvanceDay();
  63.         }
  64.  
  65.         private void AdvanceDay()
  66.         {
  67.             switch (currentDay)
  68.             {
  69.                 case DayOfWeek.Sunday:
  70.                     currentDay = DayOfWeek.Monday;
  71.                     break;
  72.                 case DayOfWeek.Monday:
  73.                     //currentDay = Tuesday that I didn't define.
  74.                     break;
  75.                 default:
  76.                     Debug.LogError("Current Time of day not defined.");
  77.                     break;
  78.             }
  79.         }
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement