unreal/AdventureCameraPawn.cpp

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