2022-11-10 11:52:10 +00:00
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
#include "TempCharacter.h"
|
2022-11-18 13:07:44 +00:00
|
|
|
|
|
|
|
#include "IDetailTreeNode.h"
|
2022-11-12 18:47:13 +00:00
|
|
|
#include "Blueprint/UserWidget.h"
|
2022-11-14 17:42:26 +00:00
|
|
|
#include "the_twilight_abyss/BaseItems/InventoryComponent.h"
|
2022-11-14 16:52:57 +00:00
|
|
|
#include "the_twilight_abyss/BaseItems/Items/BaseItem.h"
|
2022-11-13 16:43:01 +00:00
|
|
|
#include "the_twilight_abyss/MerchantInteraction/Interaction.h"
|
2022-11-10 16:38:38 +00:00
|
|
|
|
2022-11-10 11:52:10 +00:00
|
|
|
|
2022-11-10 12:57:31 +00:00
|
|
|
// CONSTRUCTOR
|
2022-11-10 11:52:10 +00:00
|
|
|
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;
|
2022-11-14 17:42:26 +00:00
|
|
|
Inventory = CreateDefaultSubobject<UInventoryComponent>("Inventory");
|
|
|
|
Inventory->MaxItemSlots = 10;
|
2022-11-10 11:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the game starts or when spawned
|
|
|
|
void ATempCharacter::BeginPlay()
|
|
|
|
{
|
|
|
|
Super::BeginPlay();
|
2022-11-14 15:53:50 +00:00
|
|
|
Health = 100;
|
2022-11-10 11:52:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 12:57:31 +00:00
|
|
|
//Binds the input we made in the setup player component to the forward vector
|
2022-11-10 11:52:10 +00:00
|
|
|
void ATempCharacter::ForwardInput(float Axis)
|
|
|
|
{
|
2022-11-11 22:21:10 +00:00
|
|
|
AddMovementInput(GetActorForwardVector() * Axis);
|
2022-11-10 11:52:10 +00:00
|
|
|
}
|
2022-11-10 12:57:31 +00:00
|
|
|
//Binds the input we made in the setup player component to the right vector
|
2022-11-10 11:52:10 +00:00
|
|
|
void ATempCharacter::RightMoveInput(float Axis)
|
|
|
|
{
|
2022-11-11 22:21:10 +00:00
|
|
|
AddMovementInput(GetActorRightVector() * Axis);
|
2022-11-10 11:52:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 12:57:31 +00:00
|
|
|
|
2022-11-10 11:52:10 +00:00
|
|
|
// Called every frame
|
|
|
|
void ATempCharacter::Tick(float DeltaTime)
|
|
|
|
{
|
|
|
|
Super::Tick(DeltaTime);
|
|
|
|
}
|
|
|
|
|
2022-11-14 16:52:57 +00:00
|
|
|
// Gives the character the functionality
|
2022-11-10 11:52:10 +00:00
|
|
|
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);
|
2022-11-11 22:21:10 +00:00
|
|
|
PlayerInputComponent->BindAction("Interact", IE_Pressed, this, &ATempCharacter::KeyPressed);
|
2022-11-10 11:52:10 +00:00
|
|
|
}
|
2022-11-11 22:21:10 +00:00
|
|
|
|
2022-11-14 16:52:57 +00:00
|
|
|
// When the player presses the E key
|
2022-11-11 20:21:44 +00:00
|
|
|
void ATempCharacter::KeyPressed()
|
2022-11-10 11:52:10 +00:00
|
|
|
{
|
2022-11-11 22:21:10 +00:00
|
|
|
LineTraceLogic();
|
|
|
|
}
|
|
|
|
|
2022-11-14 16:52:57 +00:00
|
|
|
// Line trace logic
|
2022-11-11 22:21:10 +00:00
|
|
|
void ATempCharacter::LineTraceLogic()
|
|
|
|
{
|
|
|
|
float GlobalTrace = TraceDistance;
|
|
|
|
FHitResult OutHit;
|
2022-11-11 20:21:44 +00:00
|
|
|
FVector Start = GetActorLocation();
|
2022-11-11 22:21:10 +00:00
|
|
|
FVector End = Start + GlobalTrace * GetActorForwardVector();
|
|
|
|
|
2022-11-10 16:38:38 +00:00
|
|
|
FCollisionQueryParams TraceParams;
|
2022-11-11 22:21:10 +00:00
|
|
|
|
2022-11-11 20:21:44 +00:00
|
|
|
TraceParams.AddIgnoredActor(this);
|
|
|
|
|
2022-11-13 03:00:21 +00:00
|
|
|
bool bHit = GetWorld()->LineTraceSingleByChannel(OutHit, Start, End, ECC_Visibility, TraceParams);
|
2022-11-11 22:21:10 +00:00
|
|
|
if (bHit)
|
2022-11-10 16:38:38 +00:00
|
|
|
{
|
2022-11-13 16:43:01 +00:00
|
|
|
if(OutHit.GetActor() == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-11-15 03:27:44 +00:00
|
|
|
if(OutHit.GetActor()->ActorHasTag("HealingJelly"))
|
|
|
|
{
|
|
|
|
if(GoldBalance >= 100)
|
|
|
|
{
|
|
|
|
GoldBalance -= 100;
|
2022-11-18 13:07:44 +00:00
|
|
|
Inventory->AddItem(OutHit.GetActor()->FindComponentByClass<UInventoryComponent>()->GetItem(0));
|
|
|
|
//Inventory->AddItem(Cast<UBaseItem>(OutHit.GetActor()));
|
|
|
|
//Inventory->AddItem(OutHit.GetActor()->Get("HealingJelly"));
|
2022-11-15 03:27:44 +00:00
|
|
|
UE_LOG(LogTemp, Display, TEXT("Item Purchased"));
|
2022-11-18 13:07:44 +00:00
|
|
|
|
2022-11-15 03:27:44 +00:00
|
|
|
}
|
|
|
|
if(GoldBalance <= 0)
|
|
|
|
{
|
|
|
|
UE_LOG(LogTemp, Display, TEXT("Not Enough Gold"));
|
|
|
|
}
|
|
|
|
}
|
2022-11-13 16:43:01 +00:00
|
|
|
if (AInteraction* MyInteractable = Cast<AInteraction>(OutHit.GetActor()))
|
2022-11-11 20:35:41 +00:00
|
|
|
{
|
2022-11-11 22:21:10 +00:00
|
|
|
DrawDebugLine(GetWorld(), Start, End, FColor::Green, false, 1.0f);
|
2022-11-13 16:43:01 +00:00
|
|
|
MyInteractable->OnInteract();
|
2022-11-11 22:21:10 +00:00
|
|
|
UE_LOG(LogTemp, Display, TEXT("HIT: %s"), *OutHit.GetActor()->GetName());
|
2022-11-11 20:35:41 +00:00
|
|
|
}
|
2022-11-10 16:38:38 +00:00
|
|
|
}
|
2022-11-10 11:52:10 +00:00
|
|
|
}
|
2022-11-14 16:52:57 +00:00
|
|
|
|
|
|
|
void ATempCharacter::UseItem(class UBaseItem* Item)
|
|
|
|
{
|
|
|
|
if(Item)
|
|
|
|
{
|
|
|
|
Item->Use(this);
|
|
|
|
Item->OnUse(this); //Blueprint Version
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 03:27:44 +00:00
|
|
|
|
|
|
|
void ATempCharacter::BuyItem(UBaseItem* BuyItem)
|
|
|
|
{
|
|
|
|
if(BuyItem)
|
|
|
|
{
|
|
|
|
BuyItem->Buy(this);
|
|
|
|
BuyItem->OnBuy(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|