// Project Sword & Gun Copyright © 2021 Kevin Poretti


#include "Weapons/SNGWeaponBase.h"

/** Sets default values for this actor's properties. */
ASNGWeaponBase::ASNGWeaponBase()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	
	WeaponMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("WeaponMesh"));
	WeaponMesh->SetupAttachment(RootComponent);
}

/**
 *	Calculates the final damage value to be applied to the receiver when this weapon successfully hits something.
 *
 *	By default, this just returns 0. This is meant to be overriden on a per class basis for custom damage calculation.
 *
 *	@returns The final damage to be applied to the receiver
 */
float ASNGWeaponBase::CalculateDamage()
{
	return 0.0f;
}

/**
 *	Equips this weapon.
 */
void ASNGWeaponBase::SetEquipped()
{
	SetVisibility(true);
	OnEquipped.Broadcast();
}

/**
 *	Unequips this weapon.
 */
void ASNGWeaponBase::SetUnequipped()
{
	SetVisibility(false);
	OnUnequipped.Broadcast();
}

/**
 *	Plays impacts when this weapon successfully hits something.
 *
 *	@param SurfaceType the type of the surface that was hit by this weapon
 *	@param ImpactTransform the location and rotation of the impact (rotation is usually the impacted normal direction)
 */
void ASNGWeaponBase::PlayImpactEffects(EPhysicalSurface SurfaceType, FTransform ImpactTransform)
{
	ASNGImpactEffect* ImpactEffect = GetWorld()->SpawnActorDeferred<ASNGImpactEffect>(ImpactTemplate, ImpactTransform);
	if(ImpactEffect)
	{
		ImpactEffect->ImpactSurface = SurfaceType;
		ImpactEffect->FinishSpawning(ImpactTransform);
	}
}

/**
 *	Sets visibility of the weapon mesh.
 */
void ASNGWeaponBase::SetVisibility(bool IsVisible)
{
	WeaponMesh->SetVisibility(IsVisible);
}

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

/** Called every frame. */
void ASNGWeaponBase::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

