unreal/AdventureCameraPawn.cpp

179 lines
5.0 KiB
C++
Raw Normal View History

2022-01-11 11:07:17 +01:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "AdventureCameraPawn.h"
#include "AdventureMap.h"
#include "AdventurePlayerController.h"
#include "AdventureCharacter.h"
#include "GameFramework/PlayerController.h"
#include "Kismet/GameplayStatics.h"
#include "math.h"
// Sets default values
AAdventureCameraPawn::AAdventureCameraPawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
ESASize = .01;
2022-01-11 17:32:25 +01:00
BaseScrollSpeed = 16;
2022-01-13 16:34:06 +01:00
ScrollAccelleration = BaseScrollSpeed * 2.4;
2022-01-11 11:07:17 +01:00
ESAMaxBoundSlope = 1 / (1 - (1-ESASize));
ESAMaxBoundIntercept = 1 - ESAMaxBoundSlope;
ESAMinBoundSlope = -1 / ESASize;
ESAMinBoundIntercept = 0 - (ESAMinBoundSlope * ESASize);
}
// Called when the game starts or when spawned
void AAdventureCameraPawn::BeginPlay()
{
Super::BeginPlay();
ControllerRef = (AAdventurePlayerController*)UGameplayStatics::GetPlayerController(GetWorld(), 0);
2022-01-11 17:32:25 +01:00
// The Viewport properties are inaccurate right after BeginPlay, so we need to wait a short time before saving them here.
FTimerHandle UnusedHandle;
GetWorldTimerManager().SetTimer(
UnusedHandle, this, &AAdventureCameraPawn::GetTheDamnViewport, 1, false);
2022-01-11 17:32:25 +01:00
}
void AAdventureCameraPawn::GetTheDamnViewport()
{
2022-01-11 11:07:17 +01:00
Viewport = GetWorld()->GetGameViewport();
ViewSize = Viewport->Viewport->GetSizeXY();
}
// Called every frame
void AAdventureCameraPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AAdventureCameraPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (PlayerInputComponent)
{
PlayerInputComponent->BindAxis("Mouse X", this, &AAdventureCameraPawn::EdgeScrollSide);
PlayerInputComponent->BindAxis("Mouse Y", this, &AAdventureCameraPawn::EdgeScrollVert);
}
}
void AAdventureCameraPawn::EdgeScrollSide(float AxisValue)
{
float mouseX;
float mouseY;
ControllerRef->GetMousePosition(mouseX, mouseY);
float mousePosX = mouseX / ViewSize.X;
2022-01-11 17:32:25 +01:00
float mousePosY = mouseY / ViewSize.Y;
2022-01-11 11:07:17 +01:00
float VertAccelleration = GetInputAxisValue("Mouse Y") * (ScrollAccelleration * .75);
if (mousePosX <= ESASize) // if in LEFT area
{
2022-01-18 17:01:31 +01:00
////// Scroll LEFT
2022-01-11 11:07:17 +01:00
float SpeedX = (mousePosX * ESAMinBoundSlope + ESAMinBoundIntercept) * BaseScrollSpeed;
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(-SpeedX, 0, 0));
2022-01-11 17:32:25 +01:00
2022-01-18 17:01:31 +01:00
if (mousePosY > .005) // if not touching TOP EDGE
2022-01-11 17:32:25 +01:00
{
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(0, -VertAccelleration, 0));
2022-01-11 17:32:25 +01:00
}
2022-01-11 11:07:17 +01:00
if (AxisValue < .0 && mousePosX <= .005) // if mouse moving LEFT and touching EDGE
{
////// Add LEFT accelleration
SpeedX = AxisValue * ScrollAccelleration;
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(SpeedX, 0, 0));
2022-01-11 11:07:17 +01:00
}
}
if (mousePosX >= 1-ESASize) // if in RIGHT area
{
2022-01-18 17:01:31 +01:00
////// Scroll RIGHT
2022-01-11 11:07:17 +01:00
float SpeedX = (mousePosX * ESAMaxBoundSlope + ESAMaxBoundIntercept) * BaseScrollSpeed;
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(SpeedX, 0, 0));
2022-01-11 17:32:25 +01:00
2022-01-18 17:01:31 +01:00
if (mousePosY < .995) // if not touching BOT EDGE
2022-01-11 17:32:25 +01:00
{
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(0, -VertAccelleration, 0));
2022-01-11 17:32:25 +01:00
}
2022-01-11 11:07:17 +01:00
if (AxisValue > .0 && mousePosX >= .995) // if mouse moving RIGHT and touching EDGE
{
////// Add RIGHT accelleration
SpeedX = AxisValue * ScrollAccelleration;
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(SpeedX, 0, 0));
2022-01-11 11:07:17 +01:00
}
}
}
void AAdventureCameraPawn::EdgeScrollVert(float AxisValue)
{
float mouseX;
float mouseY;
ControllerRef->GetMousePosition(mouseX, mouseY);
float mousePosY = mouseY / ViewSize.Y;
2022-01-11 17:32:25 +01:00
float mousePosX = mouseX / ViewSize.X;
2022-01-11 11:07:17 +01:00
float SideAccelleration = GetInputAxisValue("Mouse X") * (ScrollAccelleration * .75);
if (mousePosY <= ESASize) // if in TOP area
{
2022-01-18 17:01:31 +01:00
////// Scroll TOP
2022-01-11 11:07:17 +01:00
float SpeedY = (mousePosY * ESAMinBoundSlope + ESAMinBoundIntercept) * BaseScrollSpeed;
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(0, -SpeedY, 0));
2022-01-11 11:07:17 +01:00
2022-01-11 17:32:25 +01:00
if (mousePosX > .005) // if not touching EDGE
{
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(SideAccelleration, 0, 0));
2022-01-11 17:32:25 +01:00
}
2022-01-11 11:07:17 +01:00
if (AxisValue > .0 && mousePosY <= .005) // if mouse moving TOP and touching EDGE
{
////// Add TOP accelleration
SpeedY = AxisValue * ScrollAccelleration;
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(0, -SpeedY, 0));
2022-01-11 11:07:17 +01:00
}
}
if (mousePosY >= 1-ESASize) // if in BOTTOM area
{
2022-01-18 17:01:31 +01:00
////// Scroll BOTTOM
2022-01-11 11:07:17 +01:00
float SpeedY = (mousePosY * ESAMaxBoundSlope + ESAMaxBoundIntercept) * BaseScrollSpeed;
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(0, SpeedY, 0));
2022-01-11 11:07:17 +01:00
2022-01-18 17:01:31 +01:00
if (mousePosX < .995) // if not touching BOT EDGE
2022-01-11 17:32:25 +01:00
{
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(SideAccelleration, 0, 0));
2022-01-11 17:32:25 +01:00
}
2022-01-11 11:07:17 +01:00
if (AxisValue < .0 && mousePosY >= .995) // if mouse moving BOTTOM and touching EDGE
{
////// add BOTTOM accelleration
SpeedY = AxisValue * ScrollAccelleration;
2022-01-18 17:01:31 +01:00
AddActorLocalOffset(FVector(0, -SpeedY, 0));
2022-01-11 11:07:17 +01:00
}
}
}
void AAdventureCameraPawn::FollowAdvPawn()
{
UE_LOG(LogTemp, Warning, TEXT("following..."));
AdvPawnLocationCurrent = AdvPawnRef->GetActorLocation();
AdvPawnLocationDelta = AdvPawnLocationCurrent - AdvPawnLocationPrevious;
UE_LOG(LogTemp, Warning, TEXT("%s"), *AdvPawnLocationDelta.ToString());
AddActorLocalOffset(AdvPawnLocationDelta);
AdvPawnLocationPrevious = AdvPawnLocationCurrent;
}