Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #pragma once
- #include "CoreMinimal.h"
- #include "Runtime/Engine/Classes/GameFramework/Actor.h"
- #include "Runtime/CoreUObject/Public/UObject/Class.h"
- #include "Components/ActorComponent.h"
- #include "Runtime/Engine/Public/TimerManager.h"
- #include "Kismet/KismetStringLibrary.h"
- #include "EarthTime.generated.h"
- // Enum of Months
- UENUM(BlueprintType)
- enum class ECalendarMonths : uint8
- {
- January, February, March, April, May, June, July,
- August, September, October, November, December
- };
- // Input struct containing all necessary values to determine where to begin counting time
- USTRUCT(BlueprintType)
- struct FStart_Date
- {
- GENERATED_USTRUCT_BODY()
- public:
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "0", ClampMax = "59", UIMin = "0", UIMax = "59"))
- int32 Seconds;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "0", ClampMax = "59", UIMin = "0", UIMax = "59"))
- int32 Minutes;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "0", ClampMax = "23", UIMin = "0", UIMax = "23"))
- int32 Hours;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "1", ClampMax = "31", UIMin = "1", UIMax = "31"))
- int32 Day;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "1", ClampMax = "12", UIMin = "1", UIMax = "12"))
- ECalendarMonths Month;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "0", UIMin = "0"))
- int32 Year;
- };
- UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
- class CODE_TEST_API UEarthTime : public UActorComponent
- {
- GENERATED_BODY()
- public:
- // Sets default values for this component's properties
- UEarthTime();
- // Called every frame
- virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
- // "Read Only" blueprint variables to give access to function data for UI purposes
- UPROPERTY(BlueprintReadOnly, Category = "Earth Time")
- FString World_Clock;
- UPROPERTY(BlueprintReadOnly, Category = "Earth Time")
- FString World_Date;
- UPROPERTY(BlueprintReadOnly, Category = "Earth Time")
- int32 Current_Seconds;
- UPROPERTY(BlueprintReadOnly, Category = "Earth Time")
- int32 Current_Minutes;
- UPROPERTY(BlueprintReadOnly, Category = "Earth Time")
- int32 Current_Hours;
- UPROPERTY(BlueprintReadOnly, Category = "Earth Time")
- int32 Current_Day;
- UPROPERTY(BlueprintReadOnly, Category = "Earth Time")
- int32 Current_Month;
- UPROPERTY(BlueprintReadOnly, Category = "Earth Time")
- int32 Current_Year;
- // Blueprint exposed functions to manipulate World Clock settings
- UFUNCTION(BlueprintCallable)
- void Earth_Genesis(UPARAM(ref)FStart_Date &Start_Date, float Rate, UPARAM(DisplayName = "Military Time") bool TwentyFour);
- UFUNCTION(BlueprintCallable)
- void AlterTime(float New_Rate, UPARAM(DisplayName = "Military Time") bool NewClock);
- UFUNCTION(BlueprintCallable)
- void Pause();
- UFUNCTION(BlueprintCallable)
- void Unpause();
- UFUNCTION(BlueprintCallable)
- void ClearTimer();
- UFUNCTION(BlueprintCallable)
- void SaveDate(FStart_Date &End_Date);
- // Internal Functions
- UFUNCTION()
- void AgeOfEarth();
- UFUNCTION()
- void GenerateWorld_Clock();
- UFUNCTION()
- void GenerateWorld_Date();
- UFUNCTION()
- void GenerateDaysReset(int32 ActiveMonth);
- UFUNCTION()
- FString MonthAsString(int32 ConvertMonth);
- // Global variables necessary for Earth Time functionality
- bool Time_Altered;
- bool Military_Time;
- ECalendarMonths Calendar_Months;
- int32 DaysReset;
- FTimerHandle Universal_Age;
- AActor* Parent_Actor;
- protected:
- // Called when the game starts
- virtual void BeginPlay() override;
- };
- --------------------------------------------------------------------------------------------------------------------------------------------
- #include "EarthTime.h"
- UEarthTime::UEarthTime()
- {
- // Sets default values for this component's properties
- PrimaryComponentTick.bCanEverTick = false; // Disable tick for this component
- Parent_Actor = GetOwner(); // Retrieve actor component's Parent Actor
- Time_Altered = false;
- Military_Time = false;
- int32 DaysReset = 32;
- }
- // Called when the game starts
- void UEarthTime::BeginPlay()
- {
- Super::BeginPlay();
- }
- // Called every frame
- void UEarthTime::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
- {
- Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
- }
- // Set date and time values for timer function
- void UEarthTime::Earth_Genesis(UPARAM(ref)FStart_Date &Start_Date, float Rate, UPARAM()bool TwentyFour)
- {
- Military_Time = TwentyFour; // Set if we are using a 12 or 24 hr clock
- if (Time_Altered == false) // Should we start counting from initial input values or do we already have values
- {
- Current_Seconds = Start_Date.Seconds;
- Current_Minutes = Start_Date.Minutes;
- Current_Hours = Start_Date.Hours;
- Current_Day = Start_Date.Day;
- Current_Month = static_cast<int>(Start_Date.Month);
- Current_Year = Start_Date.Year;
- GenerateDaysReset(Current_Month);
- }
- if (Rate == 0) // Prevent division by "0" from improper user input
- {
- Rate = 1;
- }
- Parent_Actor->GetWorldTimerManager().SetTimer(Universal_Age, this, &UEarthTime::AgeOfEarth, 1/Rate, true); // Create a timer
- }
- // Function that actually tracks date and time
- void UEarthTime::AgeOfEarth()
- {
- int32 SecondsReset = 60;
- int32 SecondsResetValue = 0;
- int32 MinutesReset = 60;
- int32 MinutesResetValue = 0;
- int32 HoursReset = 24;
- int32 HoursResetValue = 0;
- int32 MonthsReset = 12;
- Current_Seconds++;
- if (Current_Seconds == SecondsReset)
- {
- Current_Seconds = SecondsResetValue;
- Current_Minutes ++;
- if (Current_Minutes == MinutesReset)
- {
- Current_Minutes = MinutesResetValue;
- Current_Hours ++;
- }
- if (Current_Hours == HoursReset)
- {
- Current_Hours = HoursResetValue;
- Current_Day ++;
- }
- if (Current_Day == DaysReset)
- {
- Current_Day = 1;
- Current_Month ++;
- GenerateDaysReset(Current_Month);
- }
- if (Current_Month == MonthsReset)
- {
- Current_Month = 0;
- Current_Year ++;
- }
- }
- // Function calls to create World Date and Clock string values
- GenerateWorld_Clock();
- GenerateWorld_Date();
- }
- // Function to convert time values into "00:00:00" format (AM/PM if necessary)
- void UEarthTime::GenerateWorld_Clock()
- {
- FString HoursOutput;
- FString MinutesOutput;
- FString SecondsOutput;
- FString TimeOfDay = "AM";
- int32 StandardHours = Current_Hours;
- bool PM = false;
- if (Military_Time == false && StandardHours == 0)
- {
- StandardHours = 12;
- TimeOfDay = "AM";
- }
- else if (Military_Time == false && StandardHours > 12)
- {
- StandardHours -= 12;
- PM = true;
- TimeOfDay = "PM";
- }
- else if (Military_Time == false && StandardHours == 12)
- {
- PM = true;
- TimeOfDay = "PM";
- }
- HoursOutput = FString::FromInt(StandardHours);
- if (HoursOutput.Len() < 2)
- {
- HoursOutput = "0" + HoursOutput;
- }
- MinutesOutput = FString::FromInt(Current_Minutes);
- if (MinutesOutput.Len() < 2)
- {
- MinutesOutput = "0" + MinutesOutput;
- }
- SecondsOutput = FString::FromInt(Current_Seconds);
- if (SecondsOutput.Len() < 2)
- {
- SecondsOutput = "0" + SecondsOutput;
- }
- // If not on military time output string will include "AM/PM" designation
- if (Military_Time == false)
- {
- World_Clock = (HoursOutput + " : " + MinutesOutput + " : " + SecondsOutput + " " + TimeOfDay);
- }
- else
- {
- World_Clock = (HoursOutput + " : " + MinutesOutput + " : " + SecondsOutput);
- }
- }
- // Function to convert month, day, year values into "MM/DD/YYYY" format
- void UEarthTime::GenerateWorld_Date()
- {
- FString MonthOutput;
- FString DayOutput;
- FString YearOutput;
- MonthOutput = MonthAsString(Current_Month);
- DayOutput = FString::FromInt(Current_Day);
- if (DayOutput.Len() < 2)
- {
- DayOutput = "0" + DayOutput;
- }
- YearOutput = FString::FromInt(Current_Year);
- World_Date = (MonthOutput + " " + DayOutput + ", " + YearOutput);
- }
- // Function to "Pause" World Time
- void UEarthTime::Pause()
- {
- Parent_Actor->GetWorldTimerManager().PauseTimer(Universal_Age);
- }
- // Function to "Unpause" World Time
- void UEarthTime::Unpause()
- {
- Parent_Actor->GetWorldTimerManager().UnPauseTimer(Universal_Age);
- }
- // Function to "Clear" World Time
- void UEarthTime::ClearTimer()
- {
- Parent_Actor->GetWorldTimerManager().ClearTimer(Universal_Age);
- }
- // Function to "Save" current World Time and Date
- void UEarthTime::SaveDate(FStart_Date &End_Date)
- {
- Pause();
- End_Date.Month = static_cast<ECalendarMonths>(Current_Month);
- End_Date.Day = Current_Day;
- End_Date.Year = Current_Year;
- End_Date.Hours = Current_Hours;
- End_Date.Minutes = Current_Minutes;
- End_Date.Seconds = Current_Seconds;
- ClearTimer();
- }
- // Function to change rate of World Time passage
- void UEarthTime::AlterTime(float New_Rate, bool NewClock)
- {
- FStart_Date New_Start_Date;
- Time_Altered = true;
- Parent_Actor->GetWorldTimerManager().ClearTimer(Universal_Age);
- New_Start_Date.Seconds = Current_Seconds;
- New_Start_Date.Minutes = Current_Minutes;
- New_Start_Date.Hours = Current_Hours;
- New_Start_Date.Day = Current_Day;
- New_Start_Date.Month = static_cast<ECalendarMonths>(Current_Month);
- New_Start_Date.Year = Current_Year;
- Earth_Genesis(New_Start_Date, New_Rate, NewClock);
- }
- // Function to convert Month Enum to string for output to blueprints
- FString UEarthTime::MonthAsString(int32 ConvertMonth)
- {
- FString EnumMonth;
- switch (ConvertMonth)
- {
- case 0:
- EnumMonth = "January";
- break;
- case 1:
- EnumMonth = "February";
- break;
- case 2:
- EnumMonth = "March";
- break;
- case 3:
- EnumMonth = "April";
- break;
- case 4:
- EnumMonth = "May";
- break;
- case 5:
- EnumMonth = "June";
- break;
- case 6:
- EnumMonth = "July";
- break;
- case 7:
- EnumMonth = "August";
- break;
- case 8:
- EnumMonth = "September";
- break;
- case 9:
- EnumMonth = "October";
- break;
- case 10:
- EnumMonth = "November";
- break;
- case 11:
- EnumMonth = "December";
- break;
- default:
- EnumMonth = "Bad Month";
- break;
- }
- return EnumMonth;
- }
- //Function to determine when to "reset" day count (adjusts for Leap year)
- void UEarthTime::GenerateDaysReset(int32 ActiveMonth)
- {
- switch (ActiveMonth)
- {
- case 0: case 2: case 4: case 6: case 7: case 9: case 11:
- DaysReset = 32;
- break;
- case 1:
- if (Current_Year % 4 == 0)
- {
- DaysReset = 30;
- }
- else
- {
- DaysReset = 29;
- }
- break;
- case 3: case 5: case 8: case 10:
- DaysReset = 31;
- break;
- default:
- DaysReset = 31;
- break;
- }
- if (Current_Day >= DaysReset)
- {
- Current_Day = (DaysReset- 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment