Add some controls such as selective active cookspot, lights and selecting fire intensity

This commit is contained in:
Stefan Stefanov 2023-11-15 02:53:56 +02:00
parent b01e9b5891
commit f8cd10d74a
14 changed files with 125 additions and 12 deletions

View file

@ -0,0 +1,2 @@
[/Script/AdvancedPreviewScene.SharedProfiles]

Binary file not shown.

View file

@ -14,19 +14,30 @@ AOvenCookspot::AOvenCookspot()
FireMesh = CreateDefaultSubobject<UStaticMeshComponent>("FireSM");
FireElementMesh = CreateDefaultSubobject<UStaticMeshComponent>("FireElementSM");
CookpotMesh = CreateDefaultSubobject<UStaticMeshComponent>("CookpotSM");
FireLight = CreateDefaultSubobject<UPointLightComponent>("FireLight");
FireElementMesh->SetupAttachment(RootComponent);
FireMesh->SetupAttachment(RootComponent);
CookpotMesh->SetupAttachment(RootComponent);
FireLight->SetupAttachment(RootComponent);
}
void AOvenCookspot::UpdateFire(const float DeltaTime) const
{
if (FireScaleCurveXY && FireScaleCurveZ && FireMesh)
{
const float ScaleXY = FireScaleCurveXY->GetFloatValue(CurrentHeat);
const float ScaleZ = FireScaleCurveZ->GetFloatValue(CurrentHeat);
FireMesh->SetWorldScale3D(FVector(ScaleXY, ScaleXY, ScaleZ));
if (CurrentHeat < 0.10)
{
FireMesh->SetWorldScale3D(FVector(0.0f));
FireLight->SetVisibility(false);
}
else
{
const float ScaleXY = FireScaleCurveXY->GetFloatValue(CurrentHeat);
const float ScaleZ = FireScaleCurveZ->GetFloatValue(CurrentHeat);
FireMesh->SetWorldScale3D(FVector(ScaleXY, ScaleXY, ScaleZ));
FireLight->SetVisibility(true);
}
}
else
{

View file

@ -3,6 +3,7 @@
#pragma once
#include "CoreMinimal.h"
#include "Components/PointLightComponent.h"
#include "GameFramework/Actor.h"
#include "OvenCookspot.generated.h"
@ -26,6 +27,8 @@ public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void SetCurrentHeat(const double InCurrentHeat) { CurrentHeat = InCurrentHeat; }
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
@ -37,6 +40,9 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UStaticMeshComponent> CookpotMesh;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UPointLightComponent> FireLight;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Default")
TObjectPtr<UCurveFloat> FireScaleCurveXY;
@ -44,5 +50,5 @@ public:
TObjectPtr<UCurveFloat> FireScaleCurveZ;
private:
// [0.0, 1.0]
double CurrentHeat;
double CurrentHeat{0.0};
};

View file

@ -50,7 +50,9 @@ auto FCookstove::SetCookingTime(const FTimespan CookingTimespan) -> void
void FCookstove::Update(const float DeltaTime, int32 &PlayerScore)
{
// If the id is not set, we have no work
OvenCookstoveActor->SetCurrentHeat(CurrentHeat);
// If the id is not set, we have no work meal-wise
if (CurrentMealID == -1 || CurrentlyCooking == false)
{
return;
@ -112,18 +114,27 @@ void AOvenPawn::BeginPlay()
{
Super::BeginPlay();
// Setup the arrays
Cookstoves.AddDefaulted(CookspotLocations.Num());
CookspotDials.AddDefaulted(CookspotLocations.Num());
verifyf(Cookstoves.Num() == CookspotDials.Num() &&
Cookstoves.Num() == CookspotLocations.Num() &&
CookspotNeedles.Num() == CookspotLocations.Num(),
TEXT("Data is not synced up! One or more arrays with different sizes!"))
// Tie up the data together
size_t CookspotIdx{0};
for (const AActor *CookspotLoc : CookspotLocations)
{
FActorSpawnParameters SpawnParameters;
auto *OvenCookspotActor =
GetWorld()->SpawnActor<AOvenCookspot>(CookstovePrefab,
CookspotLoc->GetTransform(),
SpawnParameters);
auto *OvenCookspotActor = GetWorld()->SpawnActor<AOvenCookspot>(CookstovePrefab,
CookspotLoc->GetTransform(),
SpawnParameters);
OvenCookspotActor->SetActorLocation(CookspotLoc->GetActorLocation());
Cookstoves[CookspotIdx].OvenCookstoveActor = OvenCookspotActor;
CookspotDials[CookspotIdx].NeedleSM = CookspotNeedles[CookspotIdx];
CookspotIdx++;
}
}
@ -132,9 +143,13 @@ void AOvenPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// TODO: Copying dial ratio to active cookspot back and forth is not ideal for organization, refactor.
// Get the dial ratio and convert it to the CurrentHeat
Cookstoves[ActiveDialIndex].CurrentHeat = CookspotDials[ActiveDialIndex].DialRatio;
int32 ScoreThisFrame{0};
// Check the cooking stoves and update 'em
for (FCookstove Cookstove : Cookstoves)
for (FCookstove &Cookstove : Cookstoves)
{
Cookstove.Update(DeltaTime, ScoreThisFrame);
}
@ -152,7 +167,7 @@ void AOvenPawn::SetupPlayerInputComponent(UInputComponent *PlayerInputComponent)
bool AOvenPawn::TrySpawningNewRandomMeal()
{
bool bSuccess{false};
for (FCookstove Cookstove : Cookstoves)
for (FCookstove &Cookstove : Cookstoves)
{
if (!Cookstove.CurrentlyCooking)
{
@ -166,4 +181,59 @@ bool AOvenPawn::TrySpawningNewRandomMeal()
}
}
return bSuccess;
}
auto AOvenPawn::RotateNeedleMouse(double MouseDeltaX) -> void
{
verifyf(ActiveDialIndex < CookspotDials.Num(),
TEXT(
"The active dials index cannot be out of the bounds of the array 'CookspotDials' with size: %d, current idx: %d"
), CookspotDials.Num(), ActiveDialIndex)
FDial &Dial = CookspotDials[ActiveDialIndex];
verifyf(Dial.NeedleSM, TEXT("The NeedleSM pointer needs to be valid!"))
Dial.MouseDeltaX = FMath::Clamp((MouseDeltaX / 50.0f) + Dial.MouseDeltaX, -1.0f, 1.0f);
const float NewNeedleRotation =
FMath::GetMappedRangeValueClamped(FVector2D(-1.0f, 1.0f),
FVector2D(0.0f, -140.0f),
Dial.MouseDeltaX);
Dial.NeedleSM->SetRelativeRotation(FRotator(NewNeedleRotation, 0.0f, 0.0f));
Dial.DialRatio = (Dial.MouseDeltaX + 1.0f) / 2.0;
}
auto AOvenPawn::RotateNeedleController(const FVector2D ThumbstickLocation) -> void
{
verifyf(ActiveDialIndex < CookspotDials.Num(),
TEXT(
"The active dials index cannot be out of the bounds of the array 'CookspotDials' with size: %d, current idx: %d"
), CookspotDials.Num(), ActiveDialIndex)
FDial &Dial = CookspotDials[ActiveDialIndex];
verifyf(Dial.NeedleSM, TEXT("The NeedleSM pointer needs to be valid!"))
// Clamp the thumbstick location to a range of [-1.0f, 1.0f]
Dial.DialRatioController += FMath::Clamp(ThumbstickLocation.X / 10.0f, -1.0f, 1.0f);
const float NewNeedleRotation = FMath::GetMappedRangeValueClamped(FVector2D(-1.0f, 1.0f), FVector2D(0.0f, -140.0f),
Dial.DialRatioController);
Dial.NeedleSM->SetRelativeRotation(FRotator(NewNeedleRotation, 0.0f, 0.0f));
Dial.DialRatio = (Dial.DialRatioController + 1.0) / 2.0;
}
auto AOvenPawn::CycleActiveOvenDial(const bool CycleRight) -> void
{
if (CycleRight)
{
ActiveDialIndex = (ActiveDialIndex + 1) % CookspotLocations.Num();
}
else
{
ActiveDialIndex = ActiveDialIndex == 0 ? CookspotLocations.Num() - 1 : ActiveDialIndex - 1;
}
UE_LOG(LogTemp, Warning, TEXT("Cycled ActiveDialIndex: %d"), ActiveDialIndex);
}

View file

@ -128,6 +128,15 @@ public:
auto TrySpawningNewRandomMeal() -> bool;
UFUNCTION(BlueprintCallable, Category="Oven Dials|Controlls")
void RotateNeedleMouse(double MouseDeltaX);
UFUNCTION(BlueprintCallable, Category="Oven Dials|Controlls")
void RotateNeedleController(FVector2D ThumbstickLocation);
UFUNCTION(BlueprintCallable, Category="Oven Dials|Controlls")
void CycleActiveOvenDial(bool CycleRight);
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Oven Settings")
double ScoreMultiplier{1.0};
@ -148,10 +157,25 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Oven Settings")
TObjectPtr<UDataTable> RecipesDataTable;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Oven Settings")
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Oven Static Meshes")
TArray<TObjectPtr<AActor>> CookspotLocations;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Oven Static Meshes")
TArray<UStaticMeshComponent *> CookspotNeedles;
private:
// An array of all the cookstoves, from left to right on the screen.
TArray<FCookstove> Cookstoves;
struct FDial
{
double DialRatio{0.0};
double DialRatioController{0.0};
double MouseDeltaX{0.0};
TObjectPtr<UStaticMeshComponent> NeedleSM;
};
TArray<FDial> CookspotDials;
int32 ActiveDialIndex{0};
};