Files
MaybeThisTime/Source/MaybeThisTime/Private/Units/BaseUnit.cpp
T
2026-08-15 14:53:19 +02:00

162 lines
4.4 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Units/BaseUnit.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/TopDownGameState.h"
#include "Net/UnrealNetwork.h"
#include "Commands/CommandComponent.h"
// Sets default values
ABaseUnit::ABaseUnit()
{
PrimaryActorTick.bCanEverTick = false;
AbilitySystemComponent = CreateDefaultSubobject<UAbilitySystemComponent>(TEXT("AbilitySystemComponent"));
AbilitySystemComponent->SetIsReplicated(true);
AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed);
InventoryComponent = CreateDefaultSubobject<UInventoryComponent>(TEXT("InventoryComponent"));
InventoryComponent->SetIsReplicated(true);
AttributeSet = CreateDefaultSubobject<UBaseAttributeSet>( TEXT("AttributeSet"));
CommandComponent = CreateDefaultSubobject<UCommandComponent>(TEXT("CommandComponent"));
GetCapsuleComponent()->SetCollisionResponseToChannel(ECC_GameTraceChannel2, ECR_Block);
}
// Called when the game starts or when spawned
void ABaseUnit::BeginPlay()
{
Super::BeginPlay();
if (!HasAuthority()) return;
UAbilitySystemComponent* ASC = GetAbilitySystemComponent();
if (!ASC || !AttributeSet) return;
AttributeSet->SetSatiety(100.f);
AttributeSet->SetMaxSatiety(100.f);
// Apply default resource consumption
for (TSubclassOf<UGameplayEffect> EffectClass : PassiveUpkeepEffects)
{
if (!EffectClass) continue;
FGameplayEffectContextHandle Context = ASC->MakeEffectContext();
ASC->ApplyGameplayEffectToSelf(EffectClass->GetDefaultObject<UGameplayEffect>(), 1.f, Context);
}
}
UAbilitySystemComponent* ABaseUnit::GetAbilitySystemComponent() const
{
return AbilitySystemComponent;
}
void ABaseUnit::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ABaseUnit, DisplayName);
DOREPLIFETIME(ABaseUnit, PrimaryFaction);
}
UInventoryComponent* ABaseUnit::GetInventoryComponent_Implementation() const
{
return InventoryComponent;
}
EOutlinerStatus ABaseUnit::GetOutlinerStatus() const
{
return EOutlinerStatus::OK;
}
void ABaseUnit::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
// Unregister from GameState when destroyed
if (ATopDownGameState* GS = GetWorld()->GetGameState<ATopDownGameState>())
{
GS->UnregisterUnit(this);
}
}
/*
void ABaseUnit::InteractionLineTrace()
{
FVector Start;
FRotator Direction;
if (!Controller) return;
Controller->GetPlayerViewPoint(Start, Direction);
FVector End = Start + (Direction.Vector() * InteractionDistance);
FHitResult Hit;
FCollisionQueryParams Params;
Params.AddIgnoredActor(this);
bool bHit = GetWorld()->LineTraceSingleByChannel(
Hit,
Start,
End,
ECC_Visibility,
Params
);
AActor* NewLookedAtActor = bHit ? Hit.GetActor() : nullptr;
if (NewLookedAtActor == LookedAtActor) return;
if (LookedAtActor && LookedAtActor->Implements<UInteractionInterface>())
{
IInteractionInterface::Execute_LookingAt(LookedAtActor, this, false);
}
LookedAtActor = nullptr;
if (NewLookedAtActor && NewLookedAtActor->Implements<UInteractionInterface>())
{
LookedAtActor = NewLookedAtActor;
IInteractionInterface::Execute_LookingAt(LookedAtActor, this, true);
}
}
void ABaseUnit::InteractWithInteractable_Implementation(AActor* TargetActor)
{
if (!LookedAtActor || !LookedAtActor->Implements<UInteractionInterface>()) return;
IInteractionInterface::Execute_Interact(LookedAtActor, this);
}
void ABaseUnit::TryPickup_Implementation(AItemActor* ItemActor)
{
if (!HasAuthority()) return;
if (!ItemActor || !InventoryComponent) return;
FItemData ItemToAdd = ItemActor->GetItemData();
FItemData OutRemainingItem = FItemData();
EAddItemResult Result = InventoryComponent->TryAddItem(ItemToAdd, OutRemainingItem);
switch (Result) {
case EAddItemResult::FullyAdded:
ItemActor->Destroy();
return;
case EAddItemResult::PartiallyAdded:
ItemActor->SetItemObject(OutRemainingItem);
return;
default:
if (GEngine) { GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Couldnt Pick up Item")); }
return;
}
}
*/