AzureAbyss/Source/the_twilight_abyss/PlayerTemp/TempCharacter.cpp

341 lines
11 KiB
C++
Raw Normal View History

// Fill out your copyright notice in the Description page of Project Settings.
#include "TempCharacter.h"
#include "Blueprint/UserWidget.h"
#include "the_twilight_abyss/BaseItems/InventoryComponent.h"
#include "the_twilight_abyss/BaseItems/Items/BaseItem.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "the_twilight_abyss/MerchantInteraction/Interaction.h"
#include <Runtime/Engine/Classes/Kismet/GameplayStatics.h>
#include "Components/PawnNoiseEmitterComponent.h"
#include "Blueprint/WidgetBlueprintLibrary.h"
#include "Kismet/KismetMathLibrary.h"
2022-11-10 12:57:31 +00:00
// 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;
this->GetCharacterMovement()->GetNavAgentPropertiesRef().bCanCrouch = true;
if (static ConstructorHelpers::FObjectFinder<UTexture2D> CrossHairTexture(TEXT("Texture2D'/Game/Blueprints/Display_UI/InteracableIcon.InteracableIcon'")); IsValid(CrossHairTexture.Object))
{
InteractableCrossHair = CrossHairTexture.Object;
}
}
// Called when the game starts or when spawned
void ATempCharacter::BeginPlay()
{
Super::BeginPlay();
FirstPlayerController = GetWorld()->GetFirstPlayerController();
CombatSystem = Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState());
WidgetPointer = Cast<UWidgetInteractionComponent>(this->GetComponentByClass(UWidgetInteractionComponent::StaticClass()));
Health = 100;
ThisCamera = Cast<UCameraComponent>(this->FindComponentByClass<UCameraComponent>());
PlayerCapsule = GetCapsuleComponent();
TArray<AActor*> AllActorsInScene;
2023-02-06 23:51:45 +00:00
//MAKE SURE POST PROCESSING IS IN THE SCENE OR GAME WILL CRASH
UGameplayStatics::GetAllActorsOfClass(GetWorld(), APostProcessVolume::StaticClass(), AllActorsInScene);
if (ensureMsgf(AllActorsInScene.Num() > 0, TEXT("No Post Processing Volume in scene")))
{
PostProcessVolume = Cast<APostProcessVolume>(AllActorsInScene[0]);
}
Enemy = TEXT("Enemy");
2023-03-06 15:37:31 +00:00
//UGameplayStatics::GetAllActorsWithTag(GetWorld(), Enemy, AIActors);
Ammo = TEXT("Ammo");
NoiseEmitter = Cast<UPawnNoiseEmitterComponent>(this->FindComponentByClass(UPawnNoiseEmitterComponent::StaticClass()));
//Widget Refs
CrossHairWidget = CreateWidget<UUserWidget>(GetWorld(), CrossHairSub);
CrossHairWidget->SetVisibility(ESlateVisibility::Visible);
CrossHair = Cast<UImage>(CrossHairWidget->GetWidgetFromName(TEXT("Crosshair")));
CrossHairWidget->AddToViewport();
InventoryWidget = CreateWidget<UUserWidget>(GetWorld(), InventoryMenuSub);
InventoryWidget->SetVisibility(ESlateVisibility::Hidden);
InventoryWidget->AddToViewport();
EscapeMenuWidget = CreateWidget<UUserWidget>(GetWorld(), EscapeMenuSub);
EscapeMenuWidget->SetVisibility(ESlateVisibility::Hidden);
EscapeMenuWidget->AddToViewport();
}
2022-11-10 12:57:31 +00:00
//Binds the input we made in the setup player component to the forward vector
void ATempCharacter::ForwardInput(float Axis)
{
AddMovementInput(UKismetMathLibrary::GetForwardVector(FRotator(0, GetControlRotation().Yaw, 0)) * Axis);
if (isInStealth == false) MakeNoise(1.0f, this, GetActorLocation(), 1200.0f);
}
2022-11-10 12:57:31 +00:00
//Binds the input we made in the setup player component to the right vector
void ATempCharacter::RightMoveInput(float Axis)
{
AddMovementInput(UKismetMathLibrary::GetRightVector(FRotator(0, GetControlRotation().Yaw, 0)) * Axis);
if (isInStealth == false) MakeNoise(1.0f, this, GetActorLocation(), 1200.0f);
}
void ATempCharacter::Sneak()
{
UE_LOG(LogTemp, Display, TEXT("Sneak activated"));
if (bIsCrouched)
{
UnCrouch();
isInStealth = false;
if (PostProcessVolume != nullptr) PostProcessVolume->Settings.VignetteIntensity = 0.0f;
}
else
{
Crouch();
isInStealth = true;
if (PostProcessVolume != nullptr) PostProcessVolume->Settings.VignetteIntensity = 0.8f;
}
}
2022-11-10 12:57:31 +00:00
void ATempCharacter::ResetWidgetPointer() const
{
WidgetPointer->SetWorldLocationAndRotation(FVector(0, 0, 0), FRotator(0, 0, 0).Quaternion());
}
// Called every frame
void ATempCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (CombatSystem->bIsInCombat)
{
FVector VectorRotation;
FVector WidgetLocation;
FirstPlayerController->DeprojectMousePositionToWorld(WidgetLocation, VectorRotation);
WidgetPointer->SetWorldLocationAndRotation(WidgetLocation, VectorRotation.Rotation().Quaternion());
}
else
{
const FVector Start = ThisCamera->GetComponentLocation();
const FVector End = Start + TraceDistance * ThisCamera->GetForwardVector();
FCollisionQueryParams TraceParams;
TraceParams.AddIgnoredActor(this);
if (FHitResult OutResult = FHitResult(); GetWorld()->LineTraceSingleByChannel(OutResult, Start, End, ECC_Pawn, TraceParams))
{
if (!IsValid(OutResult.GetActor()))
{
CrossHair->SetBrush(FSlateBrush());
CrossHair->SetDesiredSizeOverride(FVector2D(5, 5));
return;
}
if (OutResult.GetActor()->ActorHasTag("Probertium") || OutResult.GetActor()->ActorHasTag("Iroquid")
|| OutResult.GetActor()->ActorHasTag("Azos") || OutResult.GetActor()->ActorHasTag("Eis")
|| OutResult.GetActor()->ActorHasTag("NPC") || OutResult.GetActor()->FindComponentByClass<UInventoryComponent>())
{
CrossHair->SetBrush(UWidgetBlueprintLibrary::MakeBrushFromTexture(InteractableCrossHair));
CrossHair->SetDesiredSizeOverride(FVector2D(43.6, 51.3));
return;
}
CrossHair->SetBrush(FSlateBrush());
CrossHair->SetDesiredSizeOverride(FVector2D(5, 5));
}
else
{
CrossHair->SetBrush(FSlateBrush());
CrossHair->SetDesiredSizeOverride(FVector2D(5, 5));
}
}
}
// 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(TEXT("Jump"), IE_Pressed, this, &ATempCharacter::Jump);
PlayerInputComponent->BindAction(TEXT("Sneak"), IE_Pressed, this, &ATempCharacter::Sneak);
PlayerInputComponent->BindAction(TEXT("Sneak"), IE_Released, this, &ATempCharacter::Sneak);
2022-11-11 22:21:10 +00:00
PlayerInputComponent->BindAction("Interact", IE_Pressed, this, &ATempCharacter::KeyPressed);
}
2022-11-11 22:21:10 +00:00
// When the player presses the E key
void ATempCharacter::KeyPressed()
{
2022-11-11 22:21:10 +00:00
LineTraceLogic();
}
// Line trace logic
2022-11-11 22:21:10 +00:00
void ATempCharacter::LineTraceLogic()
{
//UE_LOG(LogTemp, Display, TEXT("LineTraceLogic activated"));
ThisCamera = Cast<UCameraComponent>(this->FindComponentByClass<UCameraComponent>());
float GlobalTrace = TraceDistance;
FVector Start = ThisCamera->GetComponentLocation();
FVector End = Start + GlobalTrace * ThisCamera->GetForwardVector();
FCollisionQueryParams TraceParams;
TraceParams.AddIgnoredActor(this);
2023-04-28 00:40:49 +00:00
bHit = GetWorld()->LineTraceSingleByChannel(OutHit, Start, End, ECC_Pawn, TraceParams);
2022-11-11 22:21:10 +00:00
if (bHit)
{
if (OutHit.GetActor() == nullptr)
{
return;
}
if (OutHit.GetActor()->ActorHasTag(TEXT("Probertium")))
{
AddToInventory();
return;
}
if (OutHit.GetActor()->ActorHasTag(TEXT("Iroquid")))
{
AddToInventory();
return;
}
if (OutHit.GetActor()->ActorHasTag(TEXT("Azos")))
{
AddToInventory();
return;
}
if (OutHit.GetActor()->ActorHasTag(TEXT("Eis")))
{
AddToInventory();
return;
}
if (OutHit.GetActor()->FindComponentByClass<UInventoryComponent>())
{
UE_LOG(LogTemp, Display, TEXT("Hit Merchant"));
auto ItemArray = OutHit.GetActor()->FindComponentByClass<UInventoryComponent>()->GetItem(0);
if (GoldBalance >= ItemArray->ItemCostPrice)
{
GoldBalance -= ItemArray->ItemCostPrice;
Inventory->AddItem(ItemArray);
UE_LOG(LogTemp, Display, TEXT("Item Purchased"));
}
if (GoldBalance <= 0)
{
UE_LOG(LogTemp, Display, TEXT("Not Enough Gold"));
}
}
// if the actor hit has the interaction component/script then it will activate the code
if (const AInteraction* MyInteractable = Cast<AInteraction>(OutHit.GetActor()))
{
if (MyInteractable->ShopDialogWidget->IsVisible())
{
UE_LOG(LogTemp, Display, TEXT("ShopKeeper text is visible"));
bShopKeeperText = true;
}
}
}
}
void ATempCharacter::AddToInventory()
{
if (OutHit.GetActor()->FindComponentByClass<UInventoryComponent>())
{
UE_LOG(LogTemp, Display, TEXT("Hit Merchant"));
auto ItemArray = OutHit.GetActor()->FindComponentByClass<UInventoryComponent>()->GetItem(0);
if (GoldBalance >= ItemArray->ItemCostPrice)
{
GoldBalance -= ItemArray->ItemCostPrice;
Inventory->AddItem(ItemArray);
UE_LOG(LogTemp, Display, TEXT("Item Purchased"));
}
if (GoldBalance <= 0)
{
UE_LOG(LogTemp, Display, TEXT("Not Enough Gold"));
}
OutHit.GetActor()->Destroy();
}
}
void ATempCharacter::InputDisabler()
{
//TURNING OFF CROSSHAIR
CrossHairWidget->SetVisibility(ESlateVisibility::Hidden);
//Set to UI Mode Only
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
PlayerController->SetInputMode(FInputModeGameAndUI());
PlayerController->bShowMouseCursor = true;
PlayerController->SetIgnoreMoveInput(true);
PlayerController->SetIgnoreLookInput(true);
disableTab = true;
if (ThisCamera != nullptr)
{
OriginalCameraLocation = ThisCamera->GetComponentLocation();
OriginalCameraRotation = ThisCamera->GetComponentRotation();
OriginalCameraFOV = ThisCamera->FieldOfView;
UE_LOG(LogTemp, Display, TEXT("Original Camera Location: %s"), *OriginalCameraLocation.ToString());
}
}
void ATempCharacter::InputEnabler()
{
//Reset UI Mode
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
PlayerController->SetInputMode(FInputModeGameOnly());
PlayerController->bShowMouseCursor = false;
PlayerController->SetIgnoreMoveInput(false);
PlayerController->SetIgnoreLookInput(false);
CrossHairWidget->SetVisibility(ESlateVisibility::HitTestInvisible);
//Enable Character Movement
if (ACharacter* PlayerCharacter = Cast<ACharacter>(GetWorld()->GetFirstPlayerController()->GetPawn()))
{
PlayerCharacter->EnableInput(GetWorld()->GetFirstPlayerController());
}
UE_LOG(LogTemp, Display, TEXT("Enabling Inputs"));
disableTab = true;
TraceDistance = 300;
if (ThisCamera != nullptr)
{
ThisCamera->SetWorldLocation(OriginalCameraLocation);
ThisCamera->SetWorldRotation(OriginalCameraRotation);
ThisCamera->FieldOfView = OriginalCameraFOV;
UE_LOG(LogTemp, Display, TEXT("Original Camera Location: %s"), *OriginalCameraLocation.ToString());
}
}
void ATempCharacter::UseItem(class UBaseItem* Item)
{
if (Item)
{
Item->Use(this);
Item->OnUse(this); //OnUse is a Blueprint Version
}
}
void ATempCharacter::BuyItem(AActor* Item)
{
2023-05-09 11:50:34 +00:00
if (Item == nullptr)
{
2023-05-09 11:50:34 +00:00
UE_LOG(LogTemp, Display, TEXT("Item is null"));
return;
}
2023-05-09 11:50:34 +00:00
else
{
2023-05-09 11:50:34 +00:00
UBaseItem* ItemArray = Item->FindComponentByClass<UInventoryComponent>()->GetItem(0);
if (GoldBalance <= 0)
{
UE_LOG(LogTemp, Display, TEXT("Not Enough Gold"));
}
else if (GoldBalance >= ItemArray->ItemCostPrice)
{
GoldBalance -= ItemArray->ItemCostPrice;
Inventory->AddItem(ItemArray);
UE_LOG(LogTemp, Display, TEXT("Item Purchased"));
}
UE_LOG(LogTemp, Display, TEXT("BUY ITEM FIRING"));
TraceDistance = 1000;
LineTraceLogic();
}
}