100 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			100 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
// Fill out your copyright notice in the Description page of Project Settings.
 | 
						|
 | 
						|
#include "RoverController.h"
 | 
						|
#include "Rover.h"
 | 
						|
#include "RoverGameModeBase.h"
 | 
						|
#include "Kismet/GameplayStatics.h"
 | 
						|
#include "Kismet/KismetMathLibrary.h"
 | 
						|
 | 
						|
ARoverController* ARoverController::PlayerController = nullptr;
 | 
						|
 | 
						|
ARoverController::ARoverController()
 | 
						|
{
 | 
						|
	PlayerController = this;
 | 
						|
}
 | 
						|
void ARoverController::BeginPlay()
 | 
						|
{
 | 
						|
 | 
						|
	Super::BeginPlay();
 | 
						|
	ARoverGameModeBase::Init();
 | 
						|
	if (UGameplayStatics::GetPlayerController(GetWorld(), 0) == this) {
 | 
						|
 | 
						|
		SetShowMouseCursor(true);
 | 
						|
		Rover = Cast<ARover>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));
 | 
						|
 | 
						|
		UE_LOG(LogTemp, Log, TEXT("Init Successful"));
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
void ARoverController::SetMouseShowStatus(bool bStatus)
 | 
						|
{
 | 
						|
	if (bStatus) {
 | 
						|
		GetWorldTimerManager().ClearTimer(ResetMousePositionTimer);
 | 
						|
	}
 | 
						|
	else {
 | 
						|
		GetMousePosition(ClickMousePosition.X, ClickMousePosition.Y);
 | 
						|
		GetWorldTimerManager().ClearTimer(ResetMousePositionTimer);
 | 
						|
		GetWorldTimerManager().SetTimer(ResetMousePositionTimer, this, &ARoverController::ResetMousePositionToClickPoint, 0.05f, true);
 | 
						|
	}
 | 
						|
	SetShowMouseCursor(bStatus);
 | 
						|
}
 | 
						|
 | 
						|
bool ARoverController::GetUnderMouseHitPosition(FHitResult& HitResult)
 | 
						|
{
 | 
						|
	FVector2D MousePosition;
 | 
						|
	GetMousePosition(MousePosition.X, MousePosition.Y);
 | 
						|
	return GetUnderScreenHitPosition(MousePosition, HitResult);
 | 
						|
}
 | 
						|
 | 
						|
bool ARoverController::GetUnderScreenHitPosition(const FVector2D& ScreenPosition, FHitResult& HitResult)
 | 
						|
{
 | 
						|
	FVector WorldLocation;
 | 
						|
	FVector WorldDirection;
 | 
						|
	DeprojectScreenPositionToWorld(ScreenPosition.X, ScreenPosition.Y, WorldLocation, WorldDirection);
 | 
						|
	FVector End = WorldLocation + WorldDirection * 10000000000;
 | 
						|
	if (GetWorld()->LineTraceSingleByChannel(HitResult, WorldLocation, End, ECollisionChannel::ECC_Visibility)) {
 | 
						|
		return true;
 | 
						|
	}
 | 
						|
	return false;
 | 
						|
}
 | 
						|
 | 
						|
bool ARoverController::GetUnderscreenCenterHitPosition(FHitResult& HitResult)
 | 
						|
{
 | 
						|
	FVector WorldLocation;
 | 
						|
	FVector WorldDirection;
 | 
						|
	FVector2D ScreenCenter = GetScreenCenterPosition();
 | 
						|
	DeprojectScreenPositionToWorld(ScreenCenter.X, ScreenCenter.Y, WorldLocation, WorldDirection);
 | 
						|
	FVector End = WorldLocation + WorldDirection * 10000000000;
 | 
						|
	if (GetWorld()->LineTraceSingleByChannel(HitResult, WorldLocation, End, ECollisionChannel::ECC_Visibility)) {
 | 
						|
		return true;
 | 
						|
	}
 | 
						|
	return false;
 | 
						|
}
 | 
						|
 | 
						|
FVector2D ARoverController::GetScreenCenterPosition()
 | 
						|
{
 | 
						|
	FVector2D ScreenCenter;
 | 
						|
	GEngine->GameViewport->GetViewportSize(ScreenCenter);
 | 
						|
	ScreenCenter *= 0.5;
 | 
						|
	return ScreenCenter;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
void ARoverController::ResetMousePositionToClickPoint()
 | 
						|
{
 | 
						|
	SetMouseLocation(ClickMousePosition.X, ClickMousePosition.Y);
 | 
						|
}
 | 
						|
 | 
						|
void ARoverController::RotateCameraToTargetPoint(FRotator Rotation)
 | 
						|
{
 | 
						|
	FRotator SelfRotation = GetControlRotation();
 | 
						|
	if (UKismetMathLibrary::Dot_VectorVector(SelfRotation.Vector(), Rotation.Vector()) >= 0.999999) {
 | 
						|
		SetControlRotation(Rotation);
 | 
						|
		Rover->UpdatascreenCenterPointLocation();
 | 
						|
		GetWorldTimerManager().ClearTimer(RotateCameraTimer);
 | 
						|
		return;
 | 
						|
	}
 | 
						|
	SetControlRotation(UKismetMathLibrary::RInterpTo(SelfRotation, Rotation, GetWorld()->GetDeltaSeconds(), 5.f));
 | 
						|
}
 |