// Fill out your copyright notice in the Description page of Project Settings.


#include "PawnMain.h"

// Sets default values
APawnMain::APawnMain()
{
 	// 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;

	/*USkeletalMeshComponent *MainMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("RootMesh"));
	GravityArrow = CreateDefaultSubobject<UArrowComponent>(TEXT("GravityArrow"));*/
}

// Called when the game starts or when spawned
void APawnMain::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void APawnMain::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	switch (this->MovementType) {
		case EMovementType::Walk: {
			FVector ActorLocation = GetActorLocation();
			FVector AdditionalDirection = GetActorForwardVector().Rotation().RotateVector(this->MovementDirection);

			FHitResult HitResult;
			GetWorld()->LineTraceSingleByChannel(HitResult, ActorLocation, ActorLocation + this->GravityVector, ECC_Visibility);
			SetActorLocation(
				FVector(ActorLocation.X + AdditionalDirection.X, ActorLocation.Y + AdditionalDirection.Y, ActorLocation.Z + JumpForce)
				+ ((HitResult.ImpactPoint.IsZero()) ? this->GravityVector 
					: ( FVector(HitResult.ImpactPoint.X, HitResult.ImpactPoint.Y, HitResult.ImpactPoint.Z ) - ActorLocation))
				* MovementSpeed * DeltaTime);
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, (FVector(HitResult.ImpactPoint.X, HitResult.ImpactPoint.Y, HitResult.ImpactPoint.Z) - ActorLocation).ToString());
			break;
		}
		default: {
			SetActorLocation(GetActorLocation()
				+ GetActorForwardVector().Rotation().RotateVector(this->MovementDirection)
				* MovementSpeed * DeltaTime);
			break;
		}
	}
	
}

// Called to bind functionality to input
void APawnMain::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}

void APawnMain::MoveX(float Value) {
	MovementDirection.X = Value;
}
void APawnMain::MoveY(float Value) {
	MovementDirection.Y = Value;
}
void APawnMain::MoveZ(float Value) {
	MovementDirection.Z = Value;
}