Lost_Edge/Source/Lost_Edge/Private/PlayerBase.cpp

168 lines
4.2 KiB
C++

// Oleg Petruny proprietary.
#include "PlayerBase.h"
#include "Camera/CameraComponent.h"
#include "EnhancedInputSubsystems.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "InputMappingContext.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetMathLibrary.h"
#include "CustomGameInstanceBase.h"
#include "Interactable/Interactable.h"
#include "Interactable/InteractableActivator.h"
#include "Interactable/InteractableCaller.h"
#include "Interactable/InteractableComponent.h"
#include "Interactable/UsableInteractable.h"
#include "CustomGameUserSettings.h"
#include "MainGameModeBase.h"
APlayerBase::APlayerBase()
{
PrimaryActorTick.bCanEverTick = true;
}
void APlayerBase::Tick(float DeltaTime)
{
ACharacter::Tick(DeltaTime);
// stabilize move speed by fps
AddActorLocalOffset(ConsumeMovementInputVector() * DeltaTime);
}
void APlayerBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
ACharacter::SetupPlayerInputComponent(PlayerInputComponent);
}
void APlayerBase::BeginPlay()
{
ACharacter::BeginPlay();
auto world = GetWorld();
GetCharacterMovement()->MaxWalkSpeed = moveSpeed;
cameraManager = UGameplayStatics::GetPlayerCameraManager(world, 0);
if(cameraManager)
{
cameraManager->ViewPitchMin = minPitch;
cameraManager->ViewPitchMax = maxPitch;
}
auto gameSettings = UCustomGameUserSettings::GetCustomGameUserSettings();
camera = FindComponentByClass<UCameraComponent>();
if(camera)
{
camera->PostProcessSettings.MotionBlurAmount = gameSettings->bUseMotionBlur ? 1.0f : 0.0f;
}
if(inputMapping)
{
if(auto PC = Cast<APlayerController>(GetController()))
{
if(auto inputSubsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
{
inputSubsystem->ClearAllMappings();
inputSubsystem->AddMappingContext(inputMapping.LoadSynchronous(), 0);
}
}
}
LoadInteractable();
}
void APlayerBase::SwitchToCameraPawn()
{
if(auto gamemode_base = UGameplayStatics::GetGameMode(GetWorld()))
{
if(auto gamemode = Cast<AMainGameModeBase>(gamemode_base))
{
gamemode->SwitchCameraMode();
}
}
}
void APlayerBase::MoveCamera(FVector2D value)
{
if(cameraLocked)
return;
AddControllerYawInput(value.X);
AddControllerPitchInput(value.Y);
}
void APlayerBase::MoveCharacter(FVector2D value)
{
if(moveLocked)
return;
AddMovementInput(GetActorRightVector(), value.X);
AddMovementInput(GetActorForwardVector(), value.Y);
}
void APlayerBase::Jump()
{
if(jumpLocked)
return;
ACharacter::Jump();
}
void APlayerBase::SwitchRun(bool run)
{
if(runLocked)
return;
if(run)
GetCharacterMovement()->MaxWalkSpeed = moveSpeed * runSpeedMultiplier;
else
GetCharacterMovement()->MaxWalkSpeed = moveSpeed;
}
void APlayerBase::UpdatePitch(float min, float max)
{
minPitch = min;
maxPitch = max;
if(cameraManager)
{
cameraManager->ViewPitchMin = minPitch;
cameraManager->ViewPitchMax = maxPitch;
}
}
void APlayerBase::LoadInteractable()
{
auto GI = Cast<UCustomGameInstanceBase>(GetWorld()->GetGameInstance());
if(!GI)
{
return;
}
TSet<TSubclassOf<UInteractableActivator>> activators;
for(auto& interaction : GI->interactionsActivators)
{
auto actArray = interaction->GetDefaultObject<UInteractableComponent>()->GetActivators_Implementation();
for(auto& act : actArray)
{
if(!activators.Contains(act))
{
activators.Add(act);
auto component = NewObject<UInteractableActivator>(this, act);
component->interactableActivatedDelegate.BindUObject(this, &APlayerBase::InteractableActivated);
component->SetupAttachment(camera);
component->RegisterComponent();
}
}
}
}
void APlayerBase::InteractableActivated(AInteractable* interactable)
{
GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Cyan, TEXT("Player activate: ") + interactable->GetName());
}