AzureAbyss/Source/the_twilight_abyss/Quest/QuestSystem.cpp

79 lines
2.1 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "QuestSystem.h"
// Sets default values for this component's properties
UQuestSystem::UQuestSystem()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
static ConstructorHelpers::FClassFinder<UUserWidget> QuestWidgetClass(TEXT("/Game/Blueprints/Quest_UI/Quest_UI"));
QuestWidget = QuestWidgetClass.Class;
}
// Called when the game starts
void UQuestSystem::BeginPlay()
{
Super::BeginPlay();
PlayerInventory = GetOwner()->FindComponentByClass<UInventoryComponent>();
PlayerInventory->OnInventoryUpdated.AddDynamic(this, &UQuestSystem::CheckActiveQuestConditions);
}
// Called every frame
void UQuestSystem::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}
void UQuestSystem::CheckActiveQuestConditions()
{
for (UQuest* Quest : ActiveQuests)
{
if (Quest->CheckConditions(GetWorldState()))
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Quest Completed!"));
CompletedQuests.Add(Quest);
Quest->ApplyRewards(PlayerInventory);
}
}
}
UWorldState* UQuestSystem::GetWorldState() const
{
UWorldState* WorldState = NewObject<UWorldState>();
WorldState->Items = PlayerInventory->Items;
WorldState->QuestFlags = QuestFlags;
return WorldState;
}
bool UQuestSystem::AddQuest(UQuest* Quest)
{
if (!Quest->CheckPreConditions(GetWorldState())) return false;
ActiveQuests.Add(Quest);
return true;
}
void UQuestSystem::AddQuestFlag(const FString FlagName, const bool FlagValue)
{
QuestFlags.Add(FlagName, FlagValue);
CheckActiveQuestConditions();
}
bool UQuestSystem::CheckPreConditions(UQuest* Quest) const
{
if (Quest->CheckPreConditions(GetWorldState())) return true;
return false;
}
bool UQuestSystem::HasActiveQuest(UQuest* Quest) const
{
if (ActiveQuests.Contains(Quest)) return true;
return false;
}