NebulaGames

C++ to Blueprints Advanced World Date and Time

Mar 25th, 2019
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.69 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #pragma once
  4.  
  5. #include "CoreMinimal.h"
  6. #include "Runtime/Engine/Classes/GameFramework/Actor.h"
  7. #include "Runtime/CoreUObject/Public/UObject/Class.h"
  8. #include "Components/ActorComponent.h"
  9. #include "Runtime/Engine/Public/TimerManager.h"
  10. #include "Kismet/KismetStringLibrary.h"
  11. #include "EarthTime.generated.h"
  12.  
  13. // Enum of Months
  14. UENUM(BlueprintType)                                   
  15. enum class ECalendarMonths : uint8
  16. {
  17.     January, February, March, April, May, June, July,
  18.     August, September, October, November, December
  19. };
  20.  
  21. // Input struct containing necessary values to determine "Sun position" during day/night cycle
  22. USTRUCT(BlueprintType)
  23. struct FDay_Night_Cycle
  24. {
  25.     GENERATED_USTRUCT_BODY()
  26.  
  27. public:
  28.  
  29.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sun Position", meta = (ClampMin = "0", UIMin = "0"))
  30.         float Min_Degrees;
  31.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sun Position", meta = (ClampMax = "359", UIMax = "359"))
  32.         float Max_Degrees;
  33.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sun Position", meta = (ClampMin = "0", UIMin = "0"))
  34.         int32 Sunrise;
  35.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sun Position", meta = (ClampMin = "0", UIMin = "0"))
  36.         int32 Hours_Of_Daylight;
  37.  
  38.         float Sun_Start_Degrees;
  39. };
  40.  
  41. // Input struct containing all necessary values to determine where to begin counting time
  42. USTRUCT(BlueprintType)                                 
  43. struct FStandard_Calendar
  44. {
  45.     GENERATED_USTRUCT_BODY()           
  46.  
  47. public:                                                                                                            
  48.  
  49.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Standard", meta = (ClampMin = "0", ClampMax = "59", UIMin = "0", UIMax = "59"))
  50.         int32 Seconds;
  51.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Standard", meta = (ClampMin = "0", ClampMax = "59", UIMin = "0", UIMax = "59"))
  52.         int32 Minutes;
  53.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Standard", meta = (ClampMin = "0", ClampMax = "23", UIMin = "0", UIMax = "23"))
  54.         int32 Hours;
  55.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Standard", meta = (ClampMin = "1", ClampMax = "31", UIMin = "1", UIMax = "31"))
  56.         int32 Day;
  57.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Standard", meta = (ClampMin = "1", ClampMax = "12", UIMin = "1", UIMax = "12"))
  58.         ECalendarMonths Month;
  59.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Standard", meta = (ClampMin = "0", UIMin = "0"))
  60.         int32 Year;
  61.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Standard")
  62.         FDay_Night_Cycle Daylight_Settings;
  63. };
  64.  
  65. // Struct to define custom calendar types with non-standard day/month/year & second/minute/hour cycles
  66. USTRUCT(BlueprintType)
  67. struct FChaos_Calendar_Defaults
  68. {
  69.     GENERATED_USTRUCT_BODY()
  70.  
  71. public:
  72.  
  73.     // Customized calendar cycle variables
  74.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Seconds Per Minute", ClampMin = "0", UIMin = "0"))
  75.         int32 Seconds;
  76.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Minutes Per Hour", ClampMin = "0", UIMin = "0"))
  77.         int32 Minutes;
  78.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Hours Per Day", ClampMin = "0", UIMin = "0"))
  79.         int32 Hours;
  80.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Days Per Month", ClampMin = "1", UIMin = "1"))
  81.         int32 Day;
  82.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Month Names"))
  83.         TArray<FString> Chaos_Months;
  84.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos")
  85.         FDay_Night_Cycle Daylight_Settings;
  86.  
  87.     // Where to start counting time on a custom calendar cycle
  88.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Start Second", ClampMin = "0", UIMin = "0"))
  89.         int32 Start_Seconds;
  90.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Start Minute", ClampMin = "0", UIMin = "0"))
  91.         int32 Start_Minutes;
  92.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Start Hour", ClampMin = "0", UIMin = "0"))
  93.         int32 Start_Hours;
  94.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Start Day", ClampMin = "1", UIMin = "1"))
  95.         int32 Start_Day;
  96.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Start Month Index", ClampMin = "0", UIMin = "0"))
  97.         int32 Start_Month;
  98.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar|Chaos", meta = (DisplayName = "Start Year", ClampMin = "0", UIMin = "0"))
  99.         int32 Start_Year;
  100. };
  101.  
  102.  
  103. UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
  104. class CODE_TEST_API UEarthTime : public UActorComponent
  105. {
  106.     GENERATED_BODY()
  107.  
  108. public:
  109.  
  110.     // Sets default values for this component's properties
  111.     UEarthTime();
  112.  
  113.     // Called every frame
  114.     virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
  115.  
  116.     // "Read Only" blueprint variables to give access to function data for UI purposes
  117.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  118.         FString World_Clock;
  119.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  120.         FString World_Date;
  121.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  122.         int32 Current_Seconds;
  123.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  124.         int32 Current_Minutes;
  125.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  126.         int32 Current_Hours;
  127.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  128.         int32 Current_Day;
  129.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  130.         int32 Current_Month;
  131.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  132.         int32 Current_Year;
  133.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")
  134.         float Sun_Degrees;
  135.  
  136.     // Blueprint exposed functions to manipulate World Clock settings
  137.     UFUNCTION(BlueprintCallable)                                                                   
  138.         void Earth_Genesis(UPARAM()FStandard_Calendar Standard_Calendar, float Rate, UPARAM(DisplayName = "Military Time") bool TwentyFour, UPARAM()FChaos_Calendar_Defaults Chaos_Calendar, bool Custom_Calendar);
  139.     UFUNCTION(BlueprintCallable)                                                                   
  140.         void AlterTime(float New_Rate, UPARAM(DisplayName = "Military Time") bool NewClock);
  141.     UFUNCTION(BlueprintCallable)                                                                   
  142.         void Pause();
  143.     UFUNCTION(BlueprintCallable)                                                                   
  144.         void Unpause();
  145.     UFUNCTION(BlueprintCallable)                                                                   
  146.         void ClearTimer();
  147.     UFUNCTION(BlueprintCallable)
  148.         void ResetClock();
  149.     UFUNCTION(BlueprintCallable)
  150.         void SaveStandardDate(FStandard_Calendar &End_Date, bool Pause_Timer, bool Clear_Timer);
  151.     UFUNCTION(BlueprintCallable)
  152.         void SaveChaosDate(FChaos_Calendar_Defaults &End_Date, bool Pause_Timer, bool Clear_Timer);
  153.  
  154.     // Internal Functions
  155.     UFUNCTION()
  156.         void AgeOfEarth();
  157.     UFUNCTION()
  158.         void GenerateWorld_Clock();
  159.     UFUNCTION()
  160.         void GenerateWorld_Date();
  161.     UFUNCTION()
  162.         void GenerateDaysReset(int32 ActiveMonth);
  163.     UFUNCTION()
  164.         FString MonthAsString(int32 ConvertMonth);
  165.     UFUNCTION()
  166.         void Sun_Dial();
  167.  
  168.     // Global variables necessary for Earth Time functionality
  169.     bool Time_Altered;                                                         
  170.     bool Military_Time;
  171.     bool Chaos;
  172.  
  173.     float Delta_Degrees;
  174.     float Range;
  175.    
  176.     int32 SecondsReset;
  177.     int32 SecondsResetValue;
  178.     int32 MinutesReset;
  179.     int32 MinutesResetValue;
  180.     int32 HoursReset;
  181.     int32 HoursResetValue;
  182.     int32 DaysReset;
  183.     int32 MonthsReset;
  184.  
  185.     ECalendarMonths Calendar_Months;
  186.     FChaos_Calendar_Defaults Initial_Chaos_Calendar;
  187.     FDay_Night_Cycle Daylight;
  188.     TArray<FString> Chaos_Months;
  189.     FTimerHandle Universal_Age;
  190.     AActor* Parent_Actor;
  191.  
  192. protected:
  193.  
  194.     // Called when the game starts
  195.     virtual void BeginPlay() override;
  196. };
  197.  
  198. ----------------------------------------------------------------------------------------
  199.  
  200. #include "EarthTime.h"
  201.  
  202.  
  203. // Sets default values for this component's properties
  204. UEarthTime::UEarthTime()
  205. {
  206.     // Disable tick for this component
  207.     PrimaryComponentTick.bCanEverTick = false;
  208.  
  209.     // Retrieve actor component's Parent Actor
  210.     Parent_Actor = GetOwner(); 
  211.  
  212.     // Assume no alterations to the rate and AM/PM output desired initially
  213.     Time_Altered = false;
  214.     Military_Time = false;                     
  215.    
  216.     // Set second/minute/hour/day/month cycle to standard values
  217.     SecondsReset = 60;
  218.     SecondsResetValue = 0;
  219.     MinutesReset = 60;
  220.     MinutesResetValue = 0;
  221.     HoursReset = 24;
  222.     HoursResetValue = 0;
  223.     DaysReset = 32;
  224.     MonthsReset = 12;
  225.  
  226.     // Initialize Daylight variables
  227.     Sun_Degrees = 0;
  228.     Delta_Degrees = 0.1;
  229.     Range = 180;
  230. }
  231.  
  232. // Called when the game starts
  233. void UEarthTime::BeginPlay()
  234. {
  235.  
  236.     Super::BeginPlay();
  237.  
  238. }
  239.  
  240. // Called every frame
  241. void UEarthTime::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  242. {
  243.  
  244.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  245.  
  246. }
  247.  
  248. // Set date and time values for timer function
  249. void UEarthTime::Earth_Genesis(UPARAM()FStandard_Calendar Standard_Calendar, float Rate, UPARAM(DisplayName = "Military Time") bool TwentyFour, UPARAM()FChaos_Calendar_Defaults Chaos_Calendar, bool Custom_Calendar)
  250. {          
  251.     // Should we start counting from initial input values or do we already have values
  252.     if (Time_Altered == false)                             
  253.     {
  254.         // Determine if we are using a standard or non-standard calendar, if non-standard initialize user defined settings
  255.         if (Custom_Calendar == true)
  256.         {
  257.             // We won't use AM/PM for non-standard calendars
  258.             Military_Time = true;
  259.  
  260.             // Let the other functions know we are using a custom calendar
  261.             Chaos = true;
  262.  
  263.             // Set the custom calendar cycle
  264.             Initial_Chaos_Calendar = Chaos_Calendar;
  265.             SecondsReset = Chaos_Calendar.Seconds;
  266.             MinutesReset = Chaos_Calendar.Minutes;
  267.             HoursReset = Chaos_Calendar.Hours;
  268.             DaysReset = (Chaos_Calendar.Day + 1);
  269.             MonthsReset = Chaos_Calendar.Chaos_Months.Num();
  270.             Daylight = Chaos_Calendar.Daylight_Settings;
  271.  
  272.             // If no months named, populate array with error message & set start month to "0" index (prevents crash & identifies improper input)
  273.             if (Chaos_Calendar.Chaos_Months.Num() < 1)
  274.             {
  275.                 Chaos_Months = { "No Months Named In Custom Calendar" };
  276.                 Current_Month = 0;
  277.             }
  278.             else
  279.             {
  280.                 Chaos_Months = Chaos_Calendar.Chaos_Months;
  281.                 Current_Month = Chaos_Calendar.Start_Month;
  282.             }
  283.            
  284.             // Set initial start date and time for custom calendar
  285.             Current_Seconds = Chaos_Calendar.Start_Seconds;
  286.             Current_Minutes = Chaos_Calendar.Start_Minutes;
  287.             Current_Hours = Chaos_Calendar.Start_Hours;
  288.             Current_Day = Chaos_Calendar.Start_Day;
  289.             Current_Year = Chaos_Calendar.Start_Year;
  290.  
  291.             // Ensure we don't start a month on Day "0"
  292.             if (Current_Day == 0)
  293.             {
  294.                 Current_Day = 1;
  295.             }
  296.         }
  297.         else
  298.         {
  299.             // Determine if we are using a 12 or 24 hour clock
  300.             Military_Time = TwentyFour;
  301.  
  302.             // Set start time and date to user defined input on a standard calendar
  303.             Current_Seconds = Standard_Calendar.Seconds;
  304.             Current_Minutes = Standard_Calendar.Minutes;
  305.             Current_Hours = Standard_Calendar.Hours;
  306.             Current_Day = Standard_Calendar.Day;
  307.             Current_Month = static_cast<int>(Standard_Calendar.Month);
  308.             Current_Year = Standard_Calendar.Year;
  309.             Daylight = Standard_Calendar.Daylight_Settings;
  310.  
  311.             // Ensure we don't start a month on Day "0"
  312.             if (Current_Day == 0)
  313.             {
  314.                 Current_Day = 1;
  315.             }
  316.         }
  317.  
  318.         // Call function to ensure we don't start a month on a non-valid day
  319.         GenerateDaysReset(Current_Month);
  320.  
  321.         // Set Sun starting position
  322.         Sun_Degrees = Daylight.Sun_Start_Degrees;
  323.     }
  324.    
  325.     // If we have altered time ensure the Sun is appropriately positioned to the point it was just before alteration
  326.     if (Time_Altered == true && Chaos == true)
  327.     {
  328.         Sun_Degrees = Chaos_Calendar.Daylight_Settings.Sun_Start_Degrees;
  329.         Military_Time = true;
  330.     }
  331.     else if (Time_Altered == true && Chaos == false)
  332.     {
  333.         Sun_Degrees = Standard_Calendar.Daylight_Settings.Sun_Start_Degrees;
  334.         Military_Time = TwentyFour;
  335.     }
  336.  
  337.     // Prevent division by "0" from improper user input
  338.     if (Rate == 0)
  339.     {
  340.         Rate = 1;
  341.     }
  342.  
  343.     // Calculate the number of degrees to move the Sun's position based on total daylight hours, total degrees and rate of time passage
  344.     Range = (Daylight.Max_Degrees - Daylight.Min_Degrees);
  345.     Delta_Degrees = (Range / Daylight.Hours_Of_Daylight / MinutesReset / SecondsReset);
  346.  
  347.    
  348.  
  349.     // Create a timer
  350.     Parent_Actor->GetWorldTimerManager().SetTimer(Universal_Age, this, &UEarthTime::AgeOfEarth, 1 / Rate, true);
  351. }
  352.  
  353. // Function that actually tracks date and time
  354. void UEarthTime::AgeOfEarth()                                
  355. {
  356.     Current_Seconds++;                         
  357.  
  358.     if (Current_Seconds == SecondsReset)                   
  359.     {
  360.         Current_Seconds = SecondsResetValue;
  361.         Current_Minutes ++;
  362.  
  363.         if (Current_Minutes == MinutesReset)               
  364.         {
  365.             Current_Minutes = MinutesResetValue;
  366.             Current_Hours ++;
  367.         }
  368.  
  369.         if (Current_Hours == HoursReset)
  370.         {
  371.             Current_Hours = HoursResetValue;
  372.             Current_Day ++;
  373.         }
  374.         if (Current_Day == DaysReset)              
  375.         {
  376.             Current_Day = 1;
  377.             Current_Month ++;
  378.             GenerateDaysReset(Current_Month);
  379.         }
  380.         if (Current_Month == MonthsReset)
  381.         {
  382.             Current_Month = 0;
  383.             Current_Year ++;
  384.         }
  385.  
  386.     }
  387.  
  388.     // Function calls to create World Date and Clock string values and output the Sun position in degrees
  389.     GenerateWorld_Clock();                                     
  390.     GenerateWorld_Date();
  391.     Sun_Dial();
  392. }
  393.  
  394. // Function to convert time values into "00:00:00" format (AM/PM if necessary)
  395. void UEarthTime::GenerateWorld_Clock()                         
  396. {
  397.     FString HoursOutput;
  398.     FString MinutesOutput;
  399.     FString SecondsOutput;
  400.     FString TimeOfDay = "AM";
  401.     int32 StandardHours = Current_Hours;
  402.     bool PM = false;
  403.    
  404.     if (Military_Time == false && StandardHours == 0)
  405.     {
  406.         StandardHours = 12;
  407.         TimeOfDay = "AM";
  408.     }
  409.     else if (Military_Time == false && StandardHours > 12)
  410.     {
  411.         StandardHours -= 12;
  412.         PM = true;
  413.         TimeOfDay = "PM";
  414.     }
  415.     else if (Military_Time == false && StandardHours == 12)
  416.     {
  417.         PM = true;
  418.         TimeOfDay = "PM";
  419.     }
  420.  
  421.     HoursOutput = FString::FromInt(StandardHours);             
  422.     if (HoursOutput.Len() < 2)                                 
  423.     {
  424.         HoursOutput = "0" + HoursOutput;
  425.     }
  426.     MinutesOutput = FString::FromInt(Current_Minutes);             
  427.     if (MinutesOutput.Len() < 2)                                   
  428.     {
  429.         MinutesOutput = "0" + MinutesOutput;
  430.     }
  431.     SecondsOutput = FString::FromInt(Current_Seconds);
  432.     if (SecondsOutput.Len() < 2)
  433.     {
  434.         SecondsOutput = "0" + SecondsOutput;
  435.     }
  436.    
  437.     // If not on military time output string will include "AM/PM" designation
  438.     if (Military_Time == false)
  439.     {
  440.         World_Clock = (HoursOutput + " : " + MinutesOutput + " : " + SecondsOutput + " " + TimeOfDay);     
  441.     }
  442.     else
  443.     {
  444.         World_Clock = (HoursOutput + " : " + MinutesOutput + " : " + SecondsOutput);       
  445.     }
  446. }
  447.  
  448. // Function to convert month, day, year values into "MM/DD/YYYY" format
  449. void UEarthTime::GenerateWorld_Date()                                      
  450. {
  451.     FString MonthOutput;
  452.     FString DayOutput;
  453.     FString YearOutput;
  454.  
  455.     MonthOutput = MonthAsString(Current_Month);
  456.  
  457.     DayOutput = FString::FromInt(Current_Day);
  458.     if (DayOutput.Len() < 2)
  459.     {
  460.         DayOutput = "0" + DayOutput;
  461.     }
  462.  
  463.     YearOutput = FString::FromInt(Current_Year);
  464.    
  465.  
  466.     World_Date = (MonthOutput + " " + DayOutput + ", " + YearOutput);      
  467.  
  468. }
  469.  
  470. // Function to "Pause" World Time
  471. void UEarthTime::Pause()                                                   
  472. {
  473.     Parent_Actor->GetWorldTimerManager().PauseTimer(Universal_Age);
  474. }
  475.  
  476. // Function to "Unpause" World Time
  477. void UEarthTime::Unpause()                                                 
  478. {
  479.     Parent_Actor->GetWorldTimerManager().UnPauseTimer(Universal_Age);
  480. }
  481.  
  482. // Function to "Clear" World Time
  483. void UEarthTime::ClearTimer()                                              
  484. {
  485.     Parent_Actor->GetWorldTimerManager().ClearTimer(Universal_Age);
  486. }
  487.  
  488. // Function to "Reset" World Time to 00:00:00
  489. void UEarthTime::ResetClock()
  490. {
  491.     Current_Seconds = 0;
  492.     Current_Minutes = 0;
  493.     Current_Hours = 0;
  494. }
  495.  
  496. // Function to "Save" current World Time and Date
  497. void UEarthTime::SaveStandardDate(FStandard_Calendar &End_Date, bool Pause_Timer, bool Clear_Timer)
  498. {
  499.     // Should we pause our timer during a save cycle or continue counting
  500.     if (Pause_Timer == true)
  501.     {
  502.         Pause();
  503.     }
  504.  
  505.     End_Date.Month = static_cast<ECalendarMonths>(Current_Month);
  506.     End_Date.Day = Current_Day;
  507.     End_Date.Year = Current_Year;
  508.     End_Date.Hours = Current_Hours;
  509.     End_Date.Minutes = Current_Minutes;
  510.     End_Date.Seconds = Current_Seconds;
  511.     End_Date.Daylight_Settings = Daylight;
  512.  
  513.     // Overwrite the default Sun position during a save cycle
  514.     End_Date.Daylight_Settings.Sun_Start_Degrees = Sun_Degrees;
  515.  
  516.     // Should we stop our timer after saving or continue counting
  517.     if (Clear_Timer == true)
  518.     {
  519.         ClearTimer();
  520.     }
  521. }
  522.  
  523. // Function to "Save" current World Time and Date for custom calendar types
  524. void UEarthTime::SaveChaosDate(FChaos_Calendar_Defaults &End_Date, bool Pause_Timer, bool Clear_Timer)
  525. {
  526.     // Should we pause our timer during a save cycle or continue counting
  527.     if (Pause_Timer == true)
  528.     {
  529.         Pause();
  530.     }
  531.    
  532.     // When saving a custom calendar cycle, save the input defaults back out
  533.     End_Date.Seconds = Initial_Chaos_Calendar.Seconds;
  534.     End_Date.Minutes = Initial_Chaos_Calendar.Minutes;
  535.     End_Date.Hours = Initial_Chaos_Calendar.Hours;
  536.     End_Date.Day = Initial_Chaos_Calendar.Day;
  537.     End_Date.Chaos_Months = Initial_Chaos_Calendar.Chaos_Months;
  538.     End_Date.Daylight_Settings = Daylight;
  539.  
  540.     /* The new start time and date will be set to the last registered values, this way the struct can be directly fed back into the "Earth Genesis"
  541.        function with no additional modifications and the cycle will continue at the exact spot of the last save with the same default cycle values*/
  542.     End_Date.Start_Seconds = Current_Seconds;
  543.     End_Date.Start_Minutes = Current_Minutes;
  544.     End_Date.Start_Hours = Current_Hours;
  545.     End_Date.Start_Day = Current_Day;
  546.     End_Date.Start_Month = Current_Month;
  547.     End_Date.Start_Year = Current_Year;
  548.  
  549.     // Overwrite the default Sun position during a save cycle
  550.     End_Date.Daylight_Settings.Sun_Start_Degrees = Sun_Degrees;
  551.  
  552.     // Should we stop our timer after saving or continue counting
  553.     if (Clear_Timer == true)
  554.     {
  555.         ClearTimer();
  556.     }
  557. }
  558.  
  559. // Function to change rate of World Time passage
  560. void UEarthTime::AlterTime(float New_Rate, UPARAM(DisplayName = "Military Time") bool NewClock)
  561. {
  562.     // Declare both possible input types for a new Earth Genesis call **only one will be valid and used**
  563.     FStandard_Calendar New_Start_Date;
  564.     FChaos_Calendar_Defaults New_Chaos_Date;
  565.  
  566.     if (Chaos == true)
  567.     {
  568.         New_Chaos_Date.Seconds = Current_Seconds;
  569.         New_Chaos_Date.Minutes = Current_Minutes;
  570.         New_Chaos_Date.Hours = Current_Hours;
  571.         New_Chaos_Date.Day = Current_Day;
  572.         New_Chaos_Date.Start_Month = Current_Month;
  573.         New_Chaos_Date.Start_Year = Current_Year;
  574.         New_Chaos_Date.Daylight_Settings = Daylight;
  575.         New_Chaos_Date.Daylight_Settings.Sun_Start_Degrees = Sun_Degrees;
  576.     }
  577.     else
  578.     {
  579.         New_Start_Date.Seconds = Current_Seconds;
  580.         New_Start_Date.Minutes = Current_Minutes;
  581.         New_Start_Date.Hours = Current_Hours;
  582.         New_Start_Date.Day = Current_Day;
  583.         New_Start_Date.Month = static_cast<ECalendarMonths>(Current_Month);
  584.         New_Start_Date.Year = Current_Year;
  585.         New_Start_Date.Daylight_Settings = Daylight;
  586.         New_Start_Date.Daylight_Settings.Sun_Start_Degrees = Sun_Degrees;
  587.     }
  588.  
  589.     // Let other functions know we have altered time
  590.     Time_Altered = true;
  591.  
  592.     // Clear our current timer
  593.     Parent_Actor->GetWorldTimerManager().ClearTimer(Universal_Age);
  594.  
  595.     // Reset our Earth Timer to the new rate and/or clock output type
  596.     Earth_Genesis(New_Start_Date, New_Rate, NewClock, New_Chaos_Date, Chaos);
  597. }
  598.  
  599. // Function to convert Month Enum to string for output to blueprints
  600. FString UEarthTime::MonthAsString(int32 ConvertMonth)
  601. {
  602.     FString EnumMonth;
  603.  
  604.     if (Chaos == true)
  605.     {
  606.         EnumMonth = Chaos_Months[ConvertMonth];
  607.     }
  608.     else
  609.     {
  610.         switch (ConvertMonth)
  611.         {
  612.         case 0:
  613.             EnumMonth = "January";
  614.             break;
  615.         case 1:
  616.             EnumMonth = "February";
  617.             break;
  618.         case 2:
  619.             EnumMonth = "March";
  620.             break;
  621.         case 3:
  622.             EnumMonth = "April";
  623.             break;
  624.         case 4:
  625.             EnumMonth = "May";
  626.             break;
  627.         case 5:
  628.             EnumMonth = "June";
  629.             break;
  630.         case 6:
  631.             EnumMonth = "July";
  632.             break;
  633.         case 7:
  634.             EnumMonth = "August";
  635.             break;
  636.         case 8:
  637.             EnumMonth = "September";
  638.             break;
  639.         case 9:
  640.             EnumMonth = "October";
  641.             break;
  642.         case 10:
  643.             EnumMonth = "November";
  644.             break;
  645.         case 11:
  646.             EnumMonth = "December";
  647.             break;
  648.         default:
  649.             EnumMonth = "Bad Month";
  650.             break;
  651.         }
  652.     }
  653.     return EnumMonth;
  654. }
  655.  
  656. // Function to determine when to "reset" day count (adjusts for Leap year)
  657. void UEarthTime::GenerateDaysReset(int32 ActiveMonth)
  658. {
  659.     // If using a non-standard calendar skip determining how many days/month, we will assume all months have the same # of days
  660.     if (Chaos == true)
  661.         ;
  662.     else
  663.     {
  664.         switch (ActiveMonth)
  665.         {
  666.         case 0: case 2: case 4: case 6: case 7: case 9: case 11:
  667.             DaysReset = 32;
  668.             break;
  669.         case 1:
  670.             if (Current_Year % 4 == 0)
  671.             {
  672.                 DaysReset = 30;
  673.             }
  674.             else
  675.             {
  676.                 DaysReset = 29;
  677.             }
  678.             break;
  679.         case 3: case 5: case 8: case 10:
  680.             DaysReset = 31;
  681.             break;
  682.         default:
  683.             DaysReset = 31;
  684.             break;
  685.         }
  686.     }
  687.  
  688.     // Now that we know how many days are in the current month ensure we are on a valid day
  689.     if (Current_Day >= DaysReset)
  690.     {
  691.         Current_Day = (DaysReset - 1);
  692.     }
  693. }
  694.  
  695. // Function to determine the Sun's position in degrees
  696. void UEarthTime::Sun_Dial()
  697. {
  698.     /* Ensure we are only moving the Sun through the specified "Min" and "Max" degrees and make sure we only
  699.        change the Sun's position during the specified "Daylight" hours, otherwise Sun position will be at "0" degrees*/
  700.     if (Current_Hours < Daylight.Sunrise || Sun_Degrees >= Daylight.Max_Degrees || (Current_Hours - Daylight.Sunrise) >= Daylight.Hours_Of_Daylight)
  701.     {
  702.         Sun_Degrees = 0;
  703.     }
  704.     else
  705.     {
  706.         Sun_Degrees += Delta_Degrees;
  707.     }
  708. }
Advertisement
Add Comment
Please, Sign In to add comment