unreal/AdventureCameraPawn.cpp

191 lines
5.6 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;
BaseScrollSpeed = 20;
ScrollAccelleration = BaseScrollSpeed * 3;
ESASize = .01;
ESAMaxBoundSlope = 1 / (1 - (1-ESASize));
ESAMaxBoundIntercept = 1 - ESAMaxBoundSlope;
ESAMinBoundSlope = -1 / ESASize;
ESAMinBoundIntercept = 0 - (ESAMinBoundSlope * ESASize);
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void AAdventureCameraPawn::BeginPlay()
{
Super::BeginPlay();
OwningPC = (AAdventurePlayerController*)UGameplayStatics::GetPlayerController(GetWorld(), 0); // for now
// Viewport properties not accurate on BeginPlay (must wait before accessing)
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);
if (IsValid(FollowPawn)) {
if (FollowPawn->bIsMoving) {
FollowAdvPawn(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);
PlayerInputComponent->BindAxis("Move AD", this, &AAdventureCameraPawn::ScrollSide);
PlayerInputComponent->BindAxis("Move WS", this, &AAdventureCameraPawn::ScrollVert);
}
}
void AAdventureCameraPawn::ScrollSide(float AxisValue)
{
float DeltaLoc = AxisValue * BaseScrollSpeed * 1.7;
AddActorLocalOffset(FVector(DeltaLoc, 0, 0));
}
void AAdventureCameraPawn::ScrollVert(float AxisValue)
{
float DeltaLoc = AxisValue * BaseScrollSpeed * -1.7;
AddActorLocalOffset(FVector(0, DeltaLoc, 0));
}
void AAdventureCameraPawn::EdgeScrollSide(float AxisValue)
{
float mouseX;
float mouseY;
OwningPC->GetMousePosition(mouseX, mouseY);
float mousePosX = mouseX / ViewSize.X;
float mousePosY = mouseY / ViewSize.Y;
float VertAccelleration = GetInputAxisValue("Mouse Y") * (ScrollAccelleration * .75);
if (mousePosX <= ESASize) // in LEFT area
{
////// Scroll LEFT
float SpeedX = (mousePosX * ESAMinBoundSlope + ESAMinBoundIntercept) * BaseScrollSpeed;
AddActorLocalOffset(FVector(-SpeedX, 0, 0));
if (mousePosY > .005) // not touching TOP EDGE
{
AddActorLocalOffset(FVector(0, -VertAccelleration, 0));
}
if (AxisValue < .0 && mousePosX <= .005) // mouse moving LEFT && touching EDGE
{
////// Add LEFT accelleration
SpeedX = AxisValue * ScrollAccelleration;
AddActorLocalOffset(FVector(SpeedX, 0, 0));
}
}
if (mousePosX >= 1-ESASize) // in RIGHT area
{
////// Scroll RIGHT
float SpeedX = (mousePosX * ESAMaxBoundSlope + ESAMaxBoundIntercept) * BaseScrollSpeed;
AddActorLocalOffset(FVector(SpeedX, 0, 0));
if (mousePosY < .995) // not touching BOT EDGE
{
AddActorLocalOffset(FVector(0, -VertAccelleration, 0));
}
if (AxisValue > .0 && mousePosX >= .995) // mouse moving RIGHT && touching EDGE
{
////// Add RIGHT accelleration
SpeedX = AxisValue * ScrollAccelleration;
AddActorLocalOffset(FVector(SpeedX, 0, 0));
}
}
}
void AAdventureCameraPawn::EdgeScrollVert(float AxisValue)
{
float mouseX;
float mouseY;
OwningPC->GetMousePosition(mouseX, mouseY);
float mousePosY = mouseY / ViewSize.Y;
float mousePosX = mouseX / ViewSize.X;
float SideAccelleration = GetInputAxisValue("Mouse X") * (ScrollAccelleration * .75);
if (mousePosY <= ESASize) // in TOP area
{
////// Scroll TOP
float SpeedY = (mousePosY * ESAMinBoundSlope + ESAMinBoundIntercept) * BaseScrollSpeed;
AddActorLocalOffset(FVector(0, -SpeedY, 0));
if (mousePosX > .005) // not touching EDGE
{
AddActorLocalOffset(FVector(SideAccelleration, 0, 0));
}
if (AxisValue > .0 && mousePosY <= .005) // mouse moving TOP && touching EDGE
{
////// Add TOP accelleration
SpeedY = AxisValue * ScrollAccelleration;
AddActorLocalOffset(FVector(0, -SpeedY, 0));
}
}
if (mousePosY >= 1-ESASize) // in BOTTOM area
{
////// Scroll BOTTOM
float SpeedY = (mousePosY * ESAMaxBoundSlope + ESAMaxBoundIntercept) * BaseScrollSpeed;
AddActorLocalOffset(FVector(0, SpeedY, 0));
if (mousePosX < .995) // not touching BOT EDGE
{
AddActorLocalOffset(FVector(SideAccelleration, 0, 0));
}
if (AxisValue < .0 && mousePosY >= .995) // mouse moving BOTTOM && touching EDGE
{
////// add BOTTOM accelleration
SpeedY = AxisValue * ScrollAccelleration;
AddActorLocalOffset(FVector(0, -SpeedY, 0));
}
}
}
void AAdventureCameraPawn::FollowAdvPawn(float DeltaSeconds)
{
bool MapPawnInViewX = (FMath::Abs(FollowPawn->GetActorTransform().GetLocation().X - this->GetActorLocation().X)) < ViewSize.X;
bool MapPawnInViewY = (FMath::Abs(FollowPawn->GetActorTransform().GetLocation().Y - this->GetActorLocation().Y)) < ViewSize.Y;
if (MapPawnInViewX && MapPawnInViewY) { AddActorLocalOffset(FollowPawn->GetVelocity() * DeltaSeconds); }
}