// Project Sword & Gun Copyright © 2021 Kevin Poretti


#include "Components/SNGCharacterMovementComponent.h"

USNGCharacterMovementComponent::USNGCharacterMovementComponent()
{
	// Set defaults dealign with grounded movement
	DefaultGravityScale = 1.0f;
	DefaultMaxWalkSpeed = 475.0f;
	FiringMaxWalkSpeed = 300.0f;
	ReloadingMaxWalkSpeed = 300.0f;
	SprintingMaxWalkSpeed = 600.0f;

	// Set defaults dealing with jumping and being airborne
	JumpForce = 1000.0f;
	DefaultAirFriction = 150.0f;
	DefaultAirControl = 0.35;
	
	MinJumpHeight = 200.0f;
	MaxJumpTime = 0.2f;

	LateralVelocityDampening = 0.8f;
	
	MinAllowedDashHeight = 175.0f;
	MinAllowedAttackHeight = 175.0f;
}

/**
 *	Instantly sets all movement parameters to their defaults values (i.e. ground friction to DefaultGroundFriction,
 *	MaxWalkSpeed to DefaultMaxWalkSpeed, etc.)
 */
void USNGCharacterMovementComponent::RestoreMovementDefaults()
{
	GravityScale = DefaultGravityScale;
	MaxWalkSpeed = DefaultMaxWalkSpeed;
	BrakingDecelerationFalling = DefaultAirFriction;
	AirControl = DefaultAirControl;
}

void USNGCharacterMovementComponent::StartJump()
{
	JumpTimer = MaxJumpTime;

	Velocity = FVector(Velocity.X * LateralVelocityDampening, Velocity.Y * LateralVelocityDampening, 0.0f);
}

void USNGCharacterMovementComponent::AddJumpForce()
{
	if(JumpTimer > 0.0f)
	{
		Velocity = FVector(Velocity.X, Velocity.Y, Velocity.Z + (JumpForce * GetWorld()->DeltaTimeSeconds));

		JumpTimer = FMath::Max(JumpTimer - GetWorld()->DeltaTimeSeconds, 0.0f);
	}
}

void USNGCharacterMovementComponent::BeginPlay()
{
	Super::BeginPlay();

	// set character movement parameters to defaults
	RestoreMovementDefaults();

	// Reset timers
	JumpTimer = 0.0f;
}

void USNGCharacterMovementComponent::TickComponent(float DeltaTime, ELevelTick Tick,
	FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, Tick, ThisTickFunction);

	
}
