unreal/AdventureCameraPawn.cpp

179 lines
5.0 KiB
C++

// 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;
BaseScrollSpeed = 16;
ScrollAccelleration = BaseScrollSpeed * 2.4;
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);
// 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);
}
void AAdventureCameraPawn::GetTheDamnViewport()
{
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;
float mousePosY = mouseY / ViewSize.Y;
float VertAccelleration = GetInputAxisValue("Mouse Y") * (ScrollAccelleration * .75);
if (mousePosX <= ESASize) // if in LEFT area
{
////// Scroll LEFT
float SpeedX = (mousePosX * ESAMinBoundSlope + ESAMinBoundIntercept) * BaseScrollSpeed;
AddActorLocalOffset(FVector(-SpeedX, 0, 0));
if (mousePosY > .005) // if not touching TOP EDGE
{
AddActorLocalOffset(FVector(0, -VertAccelleration, 0));
}
if (AxisValue < .0 && mousePosX <= .005) // if mouse moving LEFT and touching EDGE
{
////// Add LEFT accelleration
SpeedX = AxisValue * ScrollAccelleration;
AddActorLocalOffset(FVector(SpeedX, 0, 0));
}
}
if (mousePosX >= 1-ESASize) // if in RIGHT area
{
////// Scroll RIGHT
float SpeedX = (mousePosX * ESAMaxBoundSlope + ESAMaxBoundIntercept) * BaseScrollSpeed;
AddActorLocalOffset(FVector(SpeedX, 0, 0));
if (mousePosY < .995) // if not touching BOT EDGE
{
AddActorLocalOffset(FVector(0, -VertAccelleration, 0));
}
if (AxisValue > .0 && mousePosX >= .995) // if mouse moving RIGHT and touching EDGE
{
////// Add RIGHT accelleration
SpeedX = AxisValue * ScrollAccelleration;
AddActorLocalOffset(FVector(SpeedX, 0, 0));
}
}
}
void AAdventureCameraPawn::EdgeScrollVert(float AxisValue)
{
float mouseX;
float mouseY;
ControllerRef->GetMousePosition(mouseX, mouseY);
float mousePosY = mouseY / ViewSize.Y;
float mousePosX = mouseX / ViewSize.X;
float SideAccelleration = GetInputAxisValue("Mouse X") * (ScrollAccelleration * .75);
if (mousePosY <= ESASize) // if in TOP area
{
////// Scroll TOP
float SpeedY = (mousePosY * ESAMinBoundSlope + ESAMinBoundIntercept) * BaseScrollSpeed;
AddActorLocalOffset(FVector(0, -SpeedY, 0));
if (mousePosX > .005) // if not touching EDGE
{
AddActorLocalOffset(FVector(SideAccelleration, 0, 0));
}
if (AxisValue > .0 && mousePosY <= .005) // if mouse moving TOP and touching EDGE
{
////// Add TOP accelleration
SpeedY = AxisValue * ScrollAccelleration;
AddActorLocalOffset(FVector(0, -SpeedY, 0));
}
}
if (mousePosY >= 1-ESASize) // if in BOTTOM area
{
////// Scroll BOTTOM
float SpeedY = (mousePosY * ESAMaxBoundSlope + ESAMaxBoundIntercept) * BaseScrollSpeed;
AddActorLocalOffset(FVector(0, SpeedY, 0));
if (mousePosX < .995) // if not touching BOT EDGE
{
AddActorLocalOffset(FVector(SideAccelleration, 0, 0));
}
if (AxisValue < .0 && mousePosY >= .995) // if mouse moving BOTTOM and touching EDGE
{
////// add BOTTOM accelleration
SpeedY = AxisValue * ScrollAccelleration;
AddActorLocalOffset(FVector(0, -SpeedY, 0));
}
}
}
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;
}