This repository has been archived on 2025-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
2025-01-24 16:55:36 +01:00

117 lines
2.4 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Weapon/LMWeaponBase.h"
#include "Components/AudioComponent.h"
#include "Player/LMPlayer.h"
ALMWeaponBase::ALMWeaponBase()
{
PrimaryActorTick.bCanEverTick = true;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
AudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Audio"));
AudioComponent->SetupAttachment(RootComponent);
WeaponMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
WeaponMesh->SetupAttachment(RootComponent);
WeaponSocket = FName();
}
void ALMWeaponBase::BeginPlay()
{
Super::BeginPlay();
}
void ALMWeaponBase::Setup(USkeletalMeshComponent* Mesh, AActor* CharOwner)
{
SetActorHiddenInGame(true);
FAttachmentTransformRules Rules = FAttachmentTransformRules(EAttachmentRule::SnapToTarget,
EAttachmentRule::KeepRelative,
EAttachmentRule::KeepRelative,
false);
AttachToComponent(Mesh, Rules);
Player = Cast<ALMPlayer>(CharOwner);
}
void ALMWeaponBase::OnEquip_Implementation()
{
PlaySound(EquipSound);
}
bool ALMWeaponBase::Reload()
{
return DefaultReload();
}
void ALMWeaponBase::PrimaryFire()
{
}
void ALMWeaponBase::PlaySound(USoundWave* Sound, const bool Replacing)
{
if (AudioComponent->IsPlaying() && !Replacing)
return;
AudioComponent->Stop();
AudioComponent->SetSound(Sound);
AudioComponent->Play();
}
void ALMWeaponBase::PlayAnimation(UAnimMontage* Animation)
{
UAnimInstance* AnimInstance = WeaponMesh->GetAnimInstance();
if (Animation && AnimInstance)
{
AnimInstance->Montage_Play(Animation);
}
}
bool ALMWeaponBase::DefaultReload()
{
if (State != EWeaponState::EWS_Idle)
return false;
const int AmmoCount = Player->GetAmmoCount(AmmoType);
if (AmmoCount <= 0)
return false;
if (ClipAmmo >= MaxClip)
return false;
PlaySound(ReloadSound, true);
PlayAnimation(ReloadAnimation);
State = EWeaponState::EWS_Reloading;
// TODO: Use Animations
OnDefaultReload();
GetWorldTimerManager().SetTimer(ReloadTimer, this, &ALMWeaponBase::AllowRefire, ReloadDelay);
Player->OnReload();
return true;
}
void ALMWeaponBase::AllowRefire()
{
// TODO : improve this
if (FireTimer.IsValid())
GetWorldTimerManager().ClearTimer(FireTimer);
else if (ReloadTimer.IsValid())
GetWorldTimerManager().ClearTimer(ReloadTimer);
State = EWeaponState::EWS_Idle;
if (ClipAmmo <= 0)
{
Reload();
}
}