// 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(TEXT("AbilitySystemComponent")); AbilitySystemComponent->SetIsReplicated(true); AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed); InventoryComponent = CreateDefaultSubobject(TEXT("InventoryComponent")); InventoryComponent->SetIsReplicated(true); AttributeSet = CreateDefaultSubobject( TEXT("AttributeSet")); CommandComponent = CreateDefaultSubobject(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 EffectClass : PassiveUpkeepEffects) { if (!EffectClass) continue; FGameplayEffectContextHandle Context = ASC->MakeEffectContext(); ASC->ApplyGameplayEffectToSelf(EffectClass->GetDefaultObject(), 1.f, Context); } } UAbilitySystemComponent* ABaseUnit::GetAbilitySystemComponent() const { return AbilitySystemComponent; } void ABaseUnit::GetLifetimeReplicatedProps(TArray& 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()) { 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()) { IInteractionInterface::Execute_LookingAt(LookedAtActor, this, false); } LookedAtActor = nullptr; if (NewLookedAtActor && NewLookedAtActor->Implements()) { LookedAtActor = NewLookedAtActor; IInteractionInterface::Execute_LookingAt(LookedAtActor, this, true); } } void ABaseUnit::InteractWithInteractable_Implementation(AActor* TargetActor) { if (!LookedAtActor || !LookedAtActor->Implements()) 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; } } */