Add item drop command
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "AgrarianGamePlayerController.h"
|
||||
#include "AgrarianGameCharacter.h"
|
||||
#include "AgrarianInventoryComponent.h"
|
||||
#include "AgrarianItemPickup.h"
|
||||
#include "AgrarianPersistenceSubsystem.h"
|
||||
#include "AgrarianShelterActor.h"
|
||||
#include "AgrarianSurvivalComponent.h"
|
||||
@@ -127,6 +128,17 @@ void AAgrarianGamePlayerController::AgrarianHeal()
|
||||
ServerAgrarianHeal();
|
||||
}
|
||||
|
||||
void AAgrarianGamePlayerController::AgrarianDropItem(FName ItemId, int32 Quantity)
|
||||
{
|
||||
if (ItemId == NAME_None || Quantity <= 0)
|
||||
{
|
||||
ClientMessage(TEXT("Usage: AgrarianDropItem <ItemId> <Quantity>"));
|
||||
return;
|
||||
}
|
||||
|
||||
ServerAgrarianDropItem(ItemId, Quantity);
|
||||
}
|
||||
|
||||
void AAgrarianGamePlayerController::AgrarianTravel(float X, float Y, float Z)
|
||||
{
|
||||
ServerAgrarianTravel(FVector(X, Y, Z));
|
||||
@@ -214,6 +226,48 @@ void AAgrarianGamePlayerController::ServerAgrarianHeal_Implementation()
|
||||
ClientMessage(TEXT("Agrarian survival restored."));
|
||||
}
|
||||
|
||||
void AAgrarianGamePlayerController::ServerAgrarianDropItem_Implementation(FName ItemId, int32 Quantity)
|
||||
{
|
||||
AAgrarianGameCharacter* AgrarianCharacter = GetPawn<AAgrarianGameCharacter>();
|
||||
UAgrarianInventoryComponent* InventoryComponent = AgrarianCharacter ? AgrarianCharacter->GetInventoryComponent() : nullptr;
|
||||
if (!AgrarianCharacter || !InventoryComponent)
|
||||
{
|
||||
ClientMessage(TEXT("No Agrarian inventory component found."));
|
||||
return;
|
||||
}
|
||||
|
||||
FAgrarianItemStack DroppedStack;
|
||||
if (!InventoryComponent->ExtractItem(ItemId, Quantity, DroppedStack))
|
||||
{
|
||||
ClientMessage(FString::Printf(TEXT("Could not drop %d x %s."), Quantity, *ItemId.ToString()));
|
||||
return;
|
||||
}
|
||||
|
||||
const FVector DropLocation = AgrarianCharacter->GetActorLocation()
|
||||
+ AgrarianCharacter->GetActorForwardVector() * 150.0f
|
||||
+ FVector(0.0f, 0.0f, 40.0f);
|
||||
const FRotator DropRotation(0.0f, AgrarianCharacter->GetActorRotation().Yaw, 0.0f);
|
||||
|
||||
FActorSpawnParameters SpawnParameters;
|
||||
SpawnParameters.Owner = AgrarianCharacter;
|
||||
SpawnParameters.Instigator = AgrarianCharacter;
|
||||
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
|
||||
|
||||
AAgrarianItemPickup* Pickup = GetWorld()
|
||||
? GetWorld()->SpawnActor<AAgrarianItemPickup>(AAgrarianItemPickup::StaticClass(), DropLocation, DropRotation, SpawnParameters)
|
||||
: nullptr;
|
||||
if (!Pickup)
|
||||
{
|
||||
InventoryComponent->AddItem(DroppedStack);
|
||||
ClientMessage(FString::Printf(TEXT("Failed to spawn dropped %s; item restored."), *ItemId.ToString()));
|
||||
return;
|
||||
}
|
||||
|
||||
Pickup->PickupStack = DroppedStack;
|
||||
Pickup->Quantity = DroppedStack.Quantity;
|
||||
ClientMessage(FString::Printf(TEXT("Dropped %d x %s."), DroppedStack.Quantity, *ItemId.ToString()));
|
||||
}
|
||||
|
||||
void AAgrarianGamePlayerController::ServerAgrarianTravel_Implementation(FVector Destination)
|
||||
{
|
||||
APawn* ControlledPawn = GetPawn();
|
||||
|
||||
@@ -66,6 +66,9 @@ public:
|
||||
UFUNCTION(Exec)
|
||||
void AgrarianHeal();
|
||||
|
||||
UFUNCTION(Exec)
|
||||
void AgrarianDropItem(FName ItemId, int32 Quantity);
|
||||
|
||||
UFUNCTION(Exec)
|
||||
void AgrarianTravel(float X, float Y, float Z);
|
||||
|
||||
@@ -85,6 +88,9 @@ protected:
|
||||
UFUNCTION(Server, Reliable)
|
||||
void ServerAgrarianHeal();
|
||||
|
||||
UFUNCTION(Server, Reliable)
|
||||
void ServerAgrarianDropItem(FName ItemId, int32 Quantity);
|
||||
|
||||
UFUNCTION(Server, Reliable)
|
||||
void ServerAgrarianTravel(FVector Destination);
|
||||
};
|
||||
|
||||
@@ -72,6 +72,14 @@ bool UAgrarianInventoryComponent::AddItem(const FAgrarianItemStack& Stack)
|
||||
|
||||
bool UAgrarianInventoryComponent::RemoveItem(FName ItemId, int32 Quantity)
|
||||
{
|
||||
FAgrarianItemStack RemovedStack;
|
||||
return ExtractItem(ItemId, Quantity, RemovedStack);
|
||||
}
|
||||
|
||||
bool UAgrarianInventoryComponent::ExtractItem(FName ItemId, int32 Quantity, FAgrarianItemStack& OutStack)
|
||||
{
|
||||
OutStack = FAgrarianItemStack();
|
||||
|
||||
if (!GetOwner() || !GetOwner()->HasAuthority() || Quantity <= 0 || !HasItem(ItemId, Quantity))
|
||||
{
|
||||
return false;
|
||||
@@ -87,6 +95,13 @@ bool UAgrarianInventoryComponent::RemoveItem(FName ItemId, int32 Quantity)
|
||||
}
|
||||
|
||||
const int32 Removed = FMath::Min(Stack.Quantity, Remaining);
|
||||
if (OutStack.ItemId == NAME_None)
|
||||
{
|
||||
OutStack = Stack;
|
||||
OutStack.Quantity = 0;
|
||||
}
|
||||
|
||||
OutStack.Quantity += Removed;
|
||||
Stack.Quantity -= Removed;
|
||||
Remaining -= Removed;
|
||||
|
||||
|
||||
@@ -43,6 +43,9 @@ public:
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Inventory")
|
||||
bool RemoveItem(FName ItemId, int32 Quantity);
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Inventory")
|
||||
bool ExtractItem(FName ItemId, int32 Quantity, FAgrarianItemStack& OutStack);
|
||||
|
||||
UFUNCTION(Server, Reliable, BlueprintCallable, Category = "Agrarian|Inventory")
|
||||
void ServerAddItem(const FAgrarianItemStack& Stack);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user