Add Weapon Switching Input Action and Callback

This commit is contained in:
Louis Hobbs 2023-01-05 01:53:32 +00:00
parent f002cfa870
commit 86b34b7226
5 changed files with 47 additions and 16 deletions

BIN
Content/Input/Actions/IA_WeaponSwitch.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Input/InputMappingContext.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Player/PlayerCharacter.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -107,6 +107,11 @@ void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCom
Input->BindAction(SprintAction, ETriggerEvent::Started, this, &APlayerCharacter::SetSprintingCallback);
Input->BindAction(SprintAction, ETriggerEvent::Completed, this, &APlayerCharacter::SetWalkingCallback);
}
if (WeaponSwitchingAction)
{
Input->BindAction(WeaponSwitchingAction, ETriggerEvent::Triggered, this, &APlayerCharacter::WeaponSwitchingCallback);
}
}
}
@ -229,7 +234,7 @@ void APlayerCharacter::SetInventoryToDefault()
}
void APlayerCharacter::SelectInventorySlot(int slot)
{
{
if (slot < WeaponInventory.Num())
{
CurrentInventorySlot = slot;
@ -237,20 +242,40 @@ void APlayerCharacter::SelectInventorySlot(int slot)
}
}
void APlayerCharacter::InventoryIncrementCallback(const FInputActionInstance& Instance)
void APlayerCharacter::WeaponSwitchingCallback(const FInputActionInstance& Instance)
{
SelectInventorySlot((CurrentInventorySlot + 1) % WeaponInventory.Num());
}
float value = Instance.GetValue().Get<float>();
void APlayerCharacter::InventoryDecrementCallback(const FInputActionInstance& Instance)
{
if (CurrentInventorySlot - 1 < 0)
if (value > 0)
{
SelectInventorySlot(WeaponInventory.Num() - 1);
InventoryIncrement();
}
else
{
SelectInventorySlot((CurrentInventorySlot - 1) % WeaponInventory.Num());
InventoryDecrement();
}
}
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());
}
}
}

View File

@ -55,7 +55,8 @@ public:
UPROPERTY()
TArray<AWeapon*> WeaponInventory;
// TODO: Add weapon switching actions
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UInputAction* WeaponSwitchingAction;
protected:
@ -112,9 +113,11 @@ public:
void SelectInventorySlot(int slot);
void InventoryIncrementCallback(const FInputActionInstance& Instance);
void WeaponSwitchingCallback(const FInputActionInstance& Instance);
void InventoryDecrementCallback(const FInputActionInstance& Instance);
void InventoryIncrement();
void InventoryDecrement();
AWeapon* InitializeWeapon(TSubclassOf<class AWeapon> weapon);