Added revolver and basic reloading

This commit is contained in:
TjgL 2025-01-23 22:11:27 +01:00
parent bc9c73df78
commit 6a82183c66
15 changed files with 155 additions and 18 deletions

Binary file not shown.

Binary file not shown.

BIN
Content/Legumix/Weapon/Revolver/BP_Revolver.uasset (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -42,6 +42,16 @@ void ALMPlayer::PickUpAmmo(ALMAmmo* Ammo)
WeaponManager->AddAmmoType(Ammo->GetAmmoType(), Ammo->GetAmmoAmount());
}
int ALMPlayer::GetAmmoCount(const EAmmoType AmmoType) const
{
return WeaponManager->GetAmmoCount(AmmoType);
}
int ALMPlayer::RemoveAmmo(const EAmmoType AmmoType, const int Count) const
{
return WeaponManager->RemoveAmmo(AmmoType, Count);
}
void ALMPlayer::SetWeaponManager(ULMWeaponManager* Manager)
{
UE_LOG(LogTemp, Warning, TEXT("Set weapon manager"))

View File

@ -0,0 +1,32 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Weapon/LMRevolver.h"
#include "Player/LMBulletInfo.h"
#include "Player/LMPlayer.h"
ALMRevolver::ALMRevolver()
{
PrimaryActorTick.bCanEverTick = true;
}
void ALMRevolver::PrimaryFire()
{
if (!Player)
return;
PlaySound(FireSound);
PlayAnimation(PrimaryFireAnimation);
const FVector Origin = Player->GetWeaponFiringOrigin();
const FVector Direction = Player->GetAimVector();
ClipAmmo--;
FLMBulletInfo ShotInfo = FLMBulletInfo(1, Origin, Direction, WeaponSpread, MaxDistance, DamageFalloff, AmmoType, Damage);
Player->FireBullets(ShotInfo);
}

View File

@ -31,8 +31,3 @@ void ALMShotgun::PrimaryFire()
Player->FireBullets(ShotInfo);
}
void ALMShotgun::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}

View File

@ -42,6 +42,7 @@ void ALMWeaponBase::Setup(USkeletalMeshComponent* Mesh, AActor* CharOwner)
void ALMWeaponBase::Reload()
{
DefaultReload();
}
void ALMWeaponBase::PrimaryFire()
@ -52,7 +53,7 @@ void ALMWeaponBase::PlaySound(USoundWave* Sound, const bool Replacing)
{
if (AudioComponent->IsPlaying() && !Replacing)
return;
AudioComponent->Stop();
AudioComponent->SetSound(Sound);
AudioComponent->Play();
@ -66,4 +67,25 @@ void ALMWeaponBase::PlayAnimation(UAnimMontage* Animation)
{
AnimInstance->Montage_Play(Animation);
}
}
}
void ALMWeaponBase::DefaultReload()
{
if (State != EWeaponState::EWS_Idle)
return;
const int AmmoCount = Player->GetAmmoCount(AmmoType);
if (AmmoCount <= 0)
return;
if (ClipAmmo >= MaxClip)
return;
PlaySound(ReloadSound, true);
PlayAnimation(ReloadAnimation);
State = EWeaponState::EWS_Reloading;
// TODO: Use Animations
OnDefaultReload();
}

View File

@ -42,6 +42,30 @@ void ULMWeaponManager::SetupAmmoData()
}
}
int ULMWeaponManager::RemoveAmmo(const EAmmoType Ammo, const int Count)
{
if (AmmoData.Contains(Ammo))
{
FLMAmmoData &Data = AmmoData[Ammo];
int Difference = Data.AmmoCount - Count;
UE_LOG(LogTemp, Display, TEXT("Difference: %i | Removing: %i | Storage: %i"), Difference, Count, Data.AmmoCount)
if (Difference < 0)
{
UE_LOG(LogTemp, Error, TEXT("%i - %i = %i"), Data.AmmoCount, Count, Data.AmmoCount - Count);
Data.AmmoCount = FMath::Clamp(Data.AmmoCount - Count, 0, Data.MaxAmmo);
UE_LOG(LogTemp, Warning, TEXT("Difference < 0 | Munition to add: %i | Removed: %i | New Munition: %i"), Count + Difference, Count, Data.AmmoCount)
return Count + Difference;
}
Data.AmmoCount -= Count;
UE_LOG(LogTemp, Warning, TEXT("New munition: %i | Removed: %i"), Data.AmmoCount, Count)
return Count;
}
return 0;
}
void ULMWeaponManager::Initialize(USkeletalMeshComponent* Mesh)
{
ArmsMesh = Mesh;
@ -84,6 +108,14 @@ void ULMWeaponManager::AddAmmoType(EAmmoType AmmoType, int AmmoCount)
}
}
int ULMWeaponManager::GetAmmoCount(const EAmmoType AmmoType)
{
if (AmmoData.Contains(AmmoType))
return AmmoData[AmmoType].AmmoCount;
return 0;
}
void ULMWeaponManager::Fire()
{
ALMWeaponBase* Weapon = GetCurrentWeapon();

View File

@ -24,9 +24,26 @@ public:
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UFUNCTION()
UFUNCTION(BlueprintCallable)
void PickUpAmmo(ALMAmmo* Ammo);
/**
* Gets the number of ammo from the given type.
* @param AmmoType The ammo type to get.
* @return The amount of ammo from the given type.
*/
UFUNCTION(BlueprintCallable)
int GetAmmoCount(EAmmoType AmmoType) const;
/**
* Removes a given amount of ammo from the type.
* @param AmmoType The type of ammo to remove.
* @param Count The amount of ammo to remove.
* @return The amount that was removed.
*/
UFUNCTION(BlueprintCallable)
int RemoveAmmo(EAmmoType AmmoType, int Count) const;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

View File

@ -0,0 +1,17 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "LMWeaponBase.h"
#include "LMRevolver.generated.h"
UCLASS()
class LEGUMEMIX_API ALMRevolver : public ALMWeaponBase
{
GENERATED_BODY()
public:
ALMRevolver();
virtual void PrimaryFire() override;
};

View File

@ -13,7 +13,6 @@ class LEGUMEMIX_API ALMShotgun : public ALMWeaponBase
public:
ALMShotgun();
virtual void Tick(float DeltaTime) override;
virtual void PrimaryFire() override;
private:

View File

@ -39,12 +39,21 @@ public:
*/
UFUNCTION(BlueprintCallable)
FName GetAttachmentSocketName() const { return WeaponSocket; }
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
void OnDefaultReload();
private:
void DefaultReload();
protected: /* Weapon Data */
/** The sound to play when firing. */
UPROPERTY(EditAnywhere, BlueprintReadWrite,Category="Legumix|Sounds")
TObjectPtr<USoundWave> FireSound;
UPROPERTY(EditAnywhere, BlueprintReadWrite,Category="Legumix|Sounds")
TObjectPtr<USoundWave> ReloadSound;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Legumix|Animations", meta=(AllowPrivateAccess=true))
TObjectPtr<UAnimMontage> PrimaryFireAnimation;

View File

@ -33,6 +33,8 @@ public:
UFUNCTION(BlueprintCallable, Category=Legumix)
void AddAmmoType(EAmmoType AmmoType, int AmmoCount);
UFUNCTION(BlueprintCallable, Category=Legumix)
int GetAmmoCount(EAmmoType AmmoType);
UFUNCTION(BlueprintCallable, Category=Legumix)
void Fire();
@ -45,6 +47,8 @@ public:
UFUNCTION(BlueprintCallable, Category="Legumix|Ammo")
void SetupAmmoData();
UFUNCTION(BlueprintCallable, Category="Legumix|Ammo")
int RemoveAmmo(EAmmoType Ammo, int Count);
protected:
virtual void BeginPlay() override;