Move weapon and inventory logic to base Character class

This commit is contained in:
Louis Hobbs 2023-02-09 21:56:52 +00:00
parent 84807d6416
commit d56e25ad62
4 changed files with 183 additions and 179 deletions

View File

@ -41,3 +41,150 @@ void ANakatomiCharacter::SetHealthComponent(UHealthComponent* component)
{ {
HealthComponent = component; HealthComponent = component;
} }
void ANakatomiCharacter::SetInventoryToDefault()
{
if (WeaponInventory.Num() > 0)
{
for (size_t i = 0; i < WeaponInventory.Num(); i++)
{
WeaponInventory[i]->Destroy();
}
WeaponInventory.Empty();
}
for (size_t i = 0; i < DefaultWeaponInventory.Num(); i++)
{
if (DefaultWeaponInventory[i])
{
AWeapon* weapon = InitializeWeapon(DefaultWeaponInventory[i]);
WeaponInventory.AddUnique(weapon);
}
}
if (WeaponInventory.Num() > 0)
{
CurrentInventorySlot = 0;
SetCurrentWeapon(WeaponInventory[CurrentInventorySlot]);
}
}
void ANakatomiCharacter::SelectInventorySlot(int slot)
{
if (slot < WeaponInventory.Num())
{
CurrentInventorySlot = slot;
SetCurrentWeapon(WeaponInventory[CurrentInventorySlot]);
}
}
void ANakatomiCharacter::InventoryIncrement()
{
if (WeaponInventory.Num() > 0)
{
SelectInventorySlot((CurrentInventorySlot + 1) % WeaponInventory.Num());
}
}
void ANakatomiCharacter::InventoryDecrement()
{
if (WeaponInventory.Num() > 0)
{
if (CurrentInventorySlot - 1 < 0)
{
SelectInventorySlot(WeaponInventory.Num() - 1);
}
else
{
SelectInventorySlot((CurrentInventorySlot - 1) % WeaponInventory.Num());
}
}
}
void ANakatomiCharacter::RemoveWeaponFromInventory(int i)
{
if (WeaponInventory[i] == CurrentWeapon)
{
CurrentWeapon = nullptr;
}
WeaponInventory[i]->Destroy();
WeaponInventory.RemoveAt(i);
if (WeaponInventory.Num() == 0)
{
CurrentInventorySlot = -1;
}
else if (int index = WeaponInventory.Find(CurrentWeapon) == INDEX_NONE)
{
SetCurrentWeapon(WeaponInventory[CurrentInventorySlot % WeaponInventory.Num()]);
}
else
{
CurrentInventorySlot = index;
}
}
void ANakatomiCharacter::RemoveWeaponFromInventory(AWeapon* weapon)
{
if (int index = WeaponInventory.Find(weapon) != INDEX_NONE)
{
RemoveWeaponFromInventory(index - 1);
}
}
void ANakatomiCharacter::RemoveCurrentWeaponFromInventory()
{
RemoveWeaponFromInventory(CurrentWeapon);
}
void ANakatomiCharacter::AddWeaponToInventory(TSubclassOf<class AWeapon> weapon)
{
if (weapon)
{
AWeapon* newWeapon = InitializeWeapon(weapon);
WeaponInventory.Add(newWeapon);
if (WeaponInventory.Num() == 1)
{
SetCurrentWeapon(WeaponInventory[0]);
}
}
}
AWeapon* ANakatomiCharacter::InitializeWeapon(TSubclassOf<class AWeapon> weapon)
{
FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
AWeapon* Weapon = GetWorld()->SpawnActor<AWeapon>(weapon, SpawnParameters);
Weapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, "WeaponHand");
Weapon->SetActorEnableCollision(false);
Weapon->SetActorHiddenInGame(true);
return Weapon;
}
AWeapon* ANakatomiCharacter::GetCurrentWeapon()
{
return CurrentWeapon;
}
void ANakatomiCharacter::SetCurrentWeapon(AWeapon* weapon)
{
if (CurrentWeapon == weapon)
{
return;
}
if (CurrentWeapon)
{
CurrentWeapon->SetActorHiddenInGame(true);
}
if (weapon)
{
CurrentWeapon = weapon;
CurrentWeapon->SetActorHiddenInGame(false);
}
}

View File

@ -5,6 +5,7 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Character.h" #include "GameFramework/Character.h"
#include "HealthComponent.h" #include "HealthComponent.h"
#include "Weapon.h"
#include "NakatomiCharacter.generated.h" #include "NakatomiCharacter.generated.h"
/** /**
@ -15,11 +16,24 @@ class NAKATOMI_API ANakatomiCharacter : public ACharacter
{ {
GENERATED_BODY() GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TArray<TSubclassOf<class AWeapon>> DefaultWeaponInventory;
UPROPERTY()
TArray<AWeapon*> WeaponInventory;
UPROPERTY()
AWeapon* CurrentWeapon = nullptr;
private: private:
UPROPERTY(VisibleDefaultsOnly) UPROPERTY(VisibleDefaultsOnly)
UHealthComponent* HealthComponent = nullptr; UHealthComponent* HealthComponent = nullptr;
int CurrentInventorySlot = 0;
public: public:
// Sets default values for this character's properties // Sets default values for this character's properties
ANakatomiCharacter(); ANakatomiCharacter();
@ -38,4 +52,26 @@ public:
UHealthComponent* GetHealthComponent(); UHealthComponent* GetHealthComponent();
void SetHealthComponent(UHealthComponent* healthComponent); void SetHealthComponent(UHealthComponent* healthComponent);
void SetInventoryToDefault();
void SelectInventorySlot(int slot);
void InventoryIncrement();
void InventoryDecrement();
void AddWeaponToInventory(TSubclassOf<class AWeapon> weapon);
void RemoveWeaponFromInventory(int i);
void RemoveWeaponFromInventory(AWeapon* weapon);
void RemoveCurrentWeaponFromInventory();
AWeapon* InitializeWeapon(TSubclassOf<class AWeapon> weapon);
AWeapon* GetCurrentWeapon();
void SetCurrentWeapon(AWeapon* weapon);
}; };

View File

@ -11,7 +11,6 @@
#include "Destructable.h" #include "Destructable.h"
#include "EnemyCharacter.h" #include "EnemyCharacter.h"
#define COLLISION_WEAPON ECC_GameTraceChannel1 #define COLLISION_WEAPON ECC_GameTraceChannel1
// Sets default values // Sets default values
@ -273,43 +272,8 @@ void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits)
} }
} }
} }
}
void APlayerCharacter::SetInventoryToDefault() void APlayerCharacter::SetInventoryToDefault()
{
if (WeaponInventory.Num() > 0)
{
for (size_t i = 0; i < WeaponInventory.Num(); i++)
{
WeaponInventory[i]->Destroy();
}
WeaponInventory.Empty();
}
for (size_t i = 0; i < DefaultWeaponInventory.Num(); i++)
{
if (DefaultWeaponInventory[i])
{
AWeapon* weapon = InitializeWeapon(DefaultWeaponInventory[i]);
WeaponInventory.AddUnique(weapon);
}
}
if (WeaponInventory.Num() > 0)
{
CurrentInventorySlot = 0;
SetCurrentWeapon(WeaponInventory[CurrentInventorySlot]);
}
}
void APlayerCharacter::SelectInventorySlot(int slot)
{
if (slot < WeaponInventory.Num())
{
CurrentInventorySlot = slot;
SetCurrentWeapon(WeaponInventory[CurrentInventorySlot]);
}
} }
void APlayerCharacter::WeaponSwitchingCallback(const FInputActionInstance& Instance) void APlayerCharacter::WeaponSwitchingCallback(const FInputActionInstance& Instance)
@ -326,116 +290,6 @@ void APlayerCharacter::WeaponSwitchingCallback(const FInputActionInstance& Insta
} }
} }
void APlayerCharacter::InventoryIncrement()
{
if (WeaponInventory.Num() > 0)
{
SelectInventorySlot((CurrentInventorySlot + 1) % WeaponInventory.Num());
}
}
void APlayerCharacter::InventoryDecrement()
{
if (WeaponInventory.Num() > 0)
{
if (CurrentInventorySlot - 1 < 0)
{
SelectInventorySlot(WeaponInventory.Num() - 1);
}
else
{
SelectInventorySlot((CurrentInventorySlot - 1) % WeaponInventory.Num());
}
}
}
AWeapon* APlayerCharacter::InitializeWeapon(TSubclassOf<class AWeapon> weapon)
{
FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
AWeapon* Weapon = GetWorld()->SpawnActor<AWeapon>(weapon, SpawnParameters);
Weapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, "WeaponHand");
Weapon->SetActorEnableCollision(false);
Weapon->SetActorHiddenInGame(true);
return Weapon;
}
AWeapon* APlayerCharacter::GetCurrentWeapon()
{
return CurrentWeapon;
}
void APlayerCharacter::SetCurrentWeapon(AWeapon* weapon)
{
if (CurrentWeapon == weapon)
{
return;
}
if (CurrentWeapon)
{
CurrentWeapon->SetActorHiddenInGame(true);
}
if (weapon)
{
CurrentWeapon = weapon;
CurrentWeapon->SetActorHiddenInGame(false);
}
}
void APlayerCharacter::AddWeaponToInventory(TSubclassOf<class AWeapon> weapon)
{
if (weapon)
{
AWeapon* newWeapon = InitializeWeapon(weapon);
WeaponInventory.Add(newWeapon);
if (WeaponInventory.Num() == 1)
{
SetCurrentWeapon(WeaponInventory[0]);
}
}
}
void APlayerCharacter::RemoveWeaponFromInventory(int i)
{
if (WeaponInventory[i] == CurrentWeapon)
{
CurrentWeapon = nullptr;
}
WeaponInventory[i]->Destroy();
WeaponInventory.RemoveAt(i);
if (WeaponInventory.Num() == 0)
{
CurrentInventorySlot = -1;
}
else if (int index = WeaponInventory.Find(CurrentWeapon) == INDEX_NONE)
{
SetCurrentWeapon(WeaponInventory[CurrentInventorySlot % WeaponInventory.Num()]);
}
else
{
CurrentInventorySlot = index;
}
}
void APlayerCharacter::RemoveWeaponFromInventory(AWeapon* weapon)
{
if (int index = WeaponInventory.Find(weapon) != INDEX_NONE)
{
RemoveWeaponFromInventory(index - 1);
}
}
void APlayerCharacter::RemoveCurrentWeaponFromInventory()
{
RemoveWeaponFromInventory(CurrentWeapon);
}
void APlayerCharacter::OnFire() void APlayerCharacter::OnFire()
{ {
if (!IsFiring) if (!IsFiring)

View File

@ -51,12 +51,6 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
int MappingPriority = 0; int MappingPriority = 0;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TArray<TSubclassOf<class AWeapon>> DefaultWeaponInventory;
UPROPERTY()
TArray<AWeapon*> WeaponInventory;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UInputAction* WeaponSwitchingAction; UInputAction* WeaponSwitchingAction;
@ -78,11 +72,6 @@ private:
float DefaultMovementSpeed; float DefaultMovementSpeed;
int CurrentInventorySlot = 0;
UPROPERTY()
AWeapon* CurrentWeapon = nullptr;
FTimerHandle FireTimerHandle; FTimerHandle FireTimerHandle;
FTimerHandle CooldownTimerHandle; FTimerHandle CooldownTimerHandle;
@ -124,30 +113,8 @@ public:
void CalculateHits(TArray<FHitResult>* hits); void CalculateHits(TArray<FHitResult>* hits);
void SetInventoryToDefault();
void SelectInventorySlot(int slot);
void WeaponSwitchingCallback(const FInputActionInstance& Instance); void WeaponSwitchingCallback(const FInputActionInstance& Instance);
void InventoryIncrement();
void InventoryDecrement();
AWeapon* InitializeWeapon(TSubclassOf<class AWeapon> weapon);
AWeapon* GetCurrentWeapon();
void SetCurrentWeapon(AWeapon* weapon);
void AddWeaponToInventory(TSubclassOf<class AWeapon> weapon);
void RemoveWeaponFromInventory(int i);
void RemoveWeaponFromInventory(AWeapon* weapon);
void RemoveCurrentWeaponFromInventory();
void OnFire(); void OnFire();
void WeaponCooldownHandler(); void WeaponCooldownHandler();