unreal/AdventureCameraPawn.cpp

188 lines
5.6 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;
2022-01-28 21:58:04 +01:00
BaseScrollSpeed = 20;
ScrollAccelleration = BaseScrollSpeed * 3;
2022-01-11 11:07:17 +01:00
ESASize = .01;
2022-01-11 11:07:17 +01:00
ESAMaxBoundSlope = 1 / (1 - (1-ESASize));
ESAMaxBoundIntercept = 1 - ESAMaxBoundSlope;
ESAMinBoundSlope = -1 / ESASize;
ESAMinBoundIntercept = 0 - (ESAMinBoundSlope * ESASize);
AutoPossessPlayer = EAutoReceiveInput::Player0;
2022-01-11 11:07:17 +01:00
}
// Called when the game starts or when spawned
void AAdventureCameraPawn::BeginPlay()
{
Super::BeginPlay();
OwningPC = (AAdventurePlayerController*)UGameplayStatics::GetPlayerController(GetWorld(), 0); // for now
2022-01-11 11:07:17 +01:00
// Viewport properties not accurate on BeginPlay (must wait before accessing)
2022-01-11 17:32:25 +01:00
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);
2022-01-26 16:48:58 +01:00
if (IsValid(FollowPawn)) { if (FollowPawn->bIsMoving) { FollowAdvPawn(DeltaTime); } }
2022-01-11 11:07:17 +01:00
}
// 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);
2022-01-11 11:07:17 +01:00
}
}
2022-01-26 16:48:58 +01:00
void AAdventureCameraPawn::ScrollSide(float AxisValue)
{
2022-01-28 21:58:04 +01:00
float DeltaLoc = AxisValue * BaseScrollSpeed * 1.7;
AddActorLocalOffset(FVector(DeltaLoc, 0, 0));
}
2022-01-26 16:48:58 +01:00
void AAdventureCameraPawn::ScrollVert(float AxisValue)
{
2022-01-28 21:58:04 +01:00
float DeltaLoc = AxisValue * BaseScrollSpeed * -1.7;
AddActorLocalOffset(FVector(0, DeltaLoc, 0));
}
2022-01-11 11:07:17 +01:00
void AAdventureCameraPawn::EdgeScrollSide(float AxisValue)
{
float mouseX;
float mouseY;
OwningPC->GetMousePosition(mouseX, mouseY);
2022-01-11 11:07:17 +01:00
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;
OwningPC->GetMousePosition(mouseX, mouseY);
2022-01-11 11:07:17 +01:00
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
}
}
}
2022-01-26 16:48:58 +01:00
void AAdventureCameraPawn::FollowAdvPawn(float DeltaSeconds)
2022-01-11 11:07:17 +01:00
{
2022-01-26 16:48:58 +01:00
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); }
2022-01-11 11:07:17 +01:00
}