811856b219
Made it so the buy system is universal meaning any devs will not have to keep adding more if statements just to set the items price. It can all be done in editor now and it all handles itself in the code.
124 lines
3.6 KiB
C++
124 lines
3.6 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
#include "TempCharacter.h"
|
|
|
|
#include "IDetailTreeNode.h"
|
|
#include "Blueprint/UserWidget.h"
|
|
#include "the_twilight_abyss/BaseItems/InventoryComponent.h"
|
|
#include "the_twilight_abyss/BaseItems/Items/BaseItem.h"
|
|
#include "the_twilight_abyss/MerchantInteraction/Interaction.h"
|
|
|
|
|
|
// CONSTRUCTOR
|
|
ATempCharacter::ATempCharacter()
|
|
{
|
|
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
|
|
PrimaryActorTick.bCanEverTick = true;
|
|
Inventory = CreateDefaultSubobject<UInventoryComponent>("Inventory");
|
|
Inventory->MaxItemSlots = 10;
|
|
}
|
|
|
|
// Called when the game starts or when spawned
|
|
void ATempCharacter::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
Health = 100;
|
|
}
|
|
|
|
//Binds the input we made in the setup player component to the forward vector
|
|
void ATempCharacter::ForwardInput(float Axis)
|
|
{
|
|
AddMovementInput(GetActorForwardVector() * Axis);
|
|
}
|
|
//Binds the input we made in the setup player component to the right vector
|
|
void ATempCharacter::RightMoveInput(float Axis)
|
|
{
|
|
AddMovementInput(GetActorRightVector() * Axis);
|
|
}
|
|
|
|
|
|
// Called every frame
|
|
void ATempCharacter::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
UE_LOG(LogTemp, Warning, TEXT("Gold: %d"),GoldBalance);
|
|
}
|
|
|
|
// Gives the character the functionality
|
|
void ATempCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
|
{
|
|
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
|
PlayerInputComponent->BindAxis(TEXT("Move Forward / Backward"), this, &ATempCharacter::ForwardInput);
|
|
PlayerInputComponent->BindAxis(TEXT("Move Right / Left"), this, &ATempCharacter::RightMoveInput);
|
|
PlayerInputComponent->BindAxis(TEXT("Turn Right / Left Mouse"), this, &ATempCharacter::AddControllerYawInput);
|
|
PlayerInputComponent->BindAxis(TEXT("Look Up / Down Mouse"), this, &ATempCharacter::AddControllerPitchInput);
|
|
PlayerInputComponent->BindAction("Interact", IE_Pressed, this, &ATempCharacter::KeyPressed);
|
|
}
|
|
|
|
// When the player presses the E key
|
|
void ATempCharacter::KeyPressed()
|
|
{
|
|
LineTraceLogic();
|
|
}
|
|
|
|
// Line trace logic
|
|
void ATempCharacter::LineTraceLogic()
|
|
{
|
|
float GlobalTrace = TraceDistance;
|
|
FHitResult OutHit;
|
|
FVector Start = GetActorLocation();
|
|
FVector End = Start + GlobalTrace * GetActorForwardVector();
|
|
|
|
FCollisionQueryParams TraceParams;
|
|
|
|
TraceParams.AddIgnoredActor(this);
|
|
|
|
bool bHit = GetWorld()->LineTraceSingleByChannel(OutHit, Start, End, ECC_Visibility, TraceParams);
|
|
if (bHit)
|
|
{
|
|
if(OutHit.GetActor() == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
if(OutHit.GetActor()->FindComponentByClass<UInventoryComponent>())
|
|
{
|
|
if(GoldBalance >= OutHit.GetActor()->FindComponentByClass<UInventoryComponent>()->GetItem(0)->ItemCostPrice)
|
|
{
|
|
GoldBalance -= OutHit.GetActor()->FindComponentByClass<UInventoryComponent>()->GetItem(0)->ItemCostPrice;
|
|
Inventory->AddItem(OutHit.GetActor()->FindComponentByClass<UInventoryComponent>()->GetItem(0));
|
|
UE_LOG(LogTemp, Display, TEXT("Item Purchased"));
|
|
}
|
|
if(GoldBalance <= 0)
|
|
{
|
|
UE_LOG(LogTemp, Display, TEXT("Not Enough Gold"));
|
|
}
|
|
}
|
|
if (AInteraction* MyInteractable = Cast<AInteraction>(OutHit.GetActor()))
|
|
{
|
|
DrawDebugLine(GetWorld(), Start, End, FColor::Green, false, 1.0f);
|
|
MyInteractable->OnInteract();
|
|
UE_LOG(LogTemp, Display, TEXT("HIT: %s"), *OutHit.GetActor()->GetName());
|
|
}
|
|
}
|
|
}
|
|
|
|
void ATempCharacter::UseItem(class UBaseItem* Item)
|
|
{
|
|
if(Item)
|
|
{
|
|
Item->Use(this);
|
|
Item->OnUse(this); //Blueprint Version
|
|
}
|
|
}
|
|
|
|
void ATempCharacter::BuyItem(UBaseItem* BuyItem)
|
|
{
|
|
if(BuyItem)
|
|
{
|
|
BuyItem->Buy(this);
|
|
BuyItem->OnBuy(this);
|
|
}
|
|
}
|
|
|