Add basic weapon selection functionality

This commit is contained in:
baz 2025-07-02 15:49:20 +01:00
parent 4c727e4a15
commit 1854d9c86b
9 changed files with 63 additions and 11 deletions

View File

@ -1,10 +1,10 @@
[/Script/EngineSettings.GameMapsSettings] [/Script/EngineSettings.GameMapsSettings]
GameDefaultMap=/Game/Levels/Level.Level GameDefaultMap=/Game/Levels/MainMenu/MainMenu.MainMenu
GlobalDefaultGameMode=/Game/Gamemode/BP_DefaultGamemode.BP_DefaultGamemode_C GlobalDefaultGameMode=/Game/Gamemode/BP_DefaultGamemode.BP_DefaultGamemode_C
GameInstanceClass=/Script/vampires.VampireGameInstance GameInstanceClass=/Game/Gamemode/BP_VampireGameInstance.BP_VampireGameInstance_C
EditorStartupMap=/Game/Levels/Level.Level EditorStartupMap=/Game/Levels/MainMenu/MainMenu.MainMenu
[/Script/WindowsTargetPlatform.WindowsTargetSettings] [/Script/WindowsTargetPlatform.WindowsTargetSettings]
DefaultGraphicsRHI=DefaultGraphicsRHI_DX12 DefaultGraphicsRHI=DefaultGraphicsRHI_DX12

BIN
Content/Gamemode/BP_VampireGameInstance.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Levels/MainMenu/MainMenu.umap (Stored with Git LFS)

Binary file not shown.

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

Binary file not shown.

View File

@ -6,12 +6,23 @@
#include "Engine/GameInstance.h" #include "Engine/GameInstance.h"
#include "VampireGameInstance.generated.h" #include "VampireGameInstance.generated.h"
class AWeapon;
/** /**
* *
*/ */
UCLASS() UCLASS()
class VAMPIRES_API UVampireGameInstance : public UGameInstance class VAMPIRES_API UVampireGameInstance : public UGameInstance
{ {
GENERATED_BODY() GENERATED_BODY()
public:
UPROPERTY()
TSubclassOf<AWeapon> StarterWeapon;
UPROPERTY(EditDefaultsOnly)
TSoftObjectPtr<UWorld> MainMenuWorld;
UPROPERTY(EditDefaultsOnly)
TSoftObjectPtr<UWorld> GameWorld;
}; };

View File

@ -8,7 +8,10 @@
#include "EXPComponent.h" #include "EXPComponent.h"
#include "GoldComponent.h" #include "GoldComponent.h"
#include "Inputable.h" #include "Inputable.h"
#include "VampireGameInstance.h"
#include "VampireGameMode.h" #include "VampireGameMode.h"
#include "Weapon.h"
#include "WeaponInventoryComponent.h"
#include "Blueprint/UserWidget.h" #include "Blueprint/UserWidget.h"
#include "Blueprint/WidgetBlueprintLibrary.h" #include "Blueprint/WidgetBlueprintLibrary.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
@ -58,6 +61,17 @@ void AVampirePlayerController::OnPossess(APawn* aPawn)
currentPlayerHUD->AddToViewport(); currentPlayerHUD->AddToViewport();
} }
} }
if (UVampireGameInstance* gameInstance = Cast<UVampireGameInstance>(GetGameInstance()))
{
UWeaponInventoryComponent* weaponInventoryComponent = aPawn->GetComponentByClass<
UWeaponInventoryComponent>();
if (weaponInventoryComponent && gameInstance->StarterWeapon)
{
weaponInventoryComponent->initialInventory.Add(gameInstance->StarterWeapon);
}
}
} }
void AVampirePlayerController::OnUnPossess() void AVampirePlayerController::OnUnPossess()

View File

@ -28,10 +28,13 @@ void UWeaponInventoryComponent::InitializeInventory()
inventory.Empty(); inventory.Empty();
for (TSubclassOf<AWeapon> weapon : initialInventory) for (TSubclassOf<AWeapon> weapon : initialInventory)
{
if (IsValid(weapon))
{ {
AddWeaponToInventory(weapon); AddWeaponToInventory(weapon);
} }
} }
}
void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf<AWeapon> Weapon) void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf<AWeapon> Weapon)
{ {
@ -39,7 +42,6 @@ void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf<AWeapon> Weapon
SpawnParameters.Owner = GetOwner(); SpawnParameters.Owner = GetOwner();
AWeapon* weapon = GetWorld()->SpawnActor<AWeapon>(Weapon, GetOwner()->GetTransform(), SpawnParameters); AWeapon* weapon = GetWorld()->SpawnActor<AWeapon>(Weapon, GetOwner()->GetTransform(), SpawnParameters);
if (weapon->FollowPlayer) if (weapon->FollowPlayer)
{ {
weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepRelativeTransform); weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepRelativeTransform);
@ -50,6 +52,7 @@ void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf<AWeapon> Weapon
} }
inventory.Add(weapon); inventory.Add(weapon);
obtainableWeapons.Remove(Weapon);
} }
TArray<AWeapon*> UWeaponInventoryComponent::GetInventory() TArray<AWeapon*> UWeaponInventoryComponent::GetInventory()

View File

@ -2,6 +2,8 @@
#include "GarlicWeapon.h" #include "GarlicWeapon.h"
#include "MovieSceneTracksComponentTypes.h"
#include "Components/SphereComponent.h" #include "Components/SphereComponent.h"
#include "vampires/EnemyCharacter.h" #include "vampires/EnemyCharacter.h"
#include "vampires/HealthComponent.h" #include "vampires/HealthComponent.h"
@ -9,7 +11,7 @@
AGarlicWeapon::AGarlicWeapon() AGarlicWeapon::AGarlicWeapon()
{ {
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component")); SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SphereComponent->SetupAttachment(RootComponent); SetRootComponent(SphereComponent);
SphereComponent->SetSphereRadius(150.0f); SphereComponent->SetSphereRadius(150.0f);
SphereComponent->SetCollisionProfileName(TEXT("Weapon")); SphereComponent->SetCollisionProfileName(TEXT("Weapon"));

View File

@ -4,9 +4,12 @@
#include "StarterWeaponButtonWidget.h" #include "StarterWeaponButtonWidget.h"
#include "StarterWeaponButtonDataObject.h" #include "StarterWeaponButtonDataObject.h"
#include "Blueprint/WidgetBlueprintLibrary.h"
#include "Components/Button.h" #include "Components/Button.h"
#include "Components/Image.h" #include "Components/Image.h"
#include "Components/TextBlock.h" #include "Components/TextBlock.h"
#include "Kismet/GameplayStatics.h"
#include "vampires/VampireGameInstance.h"
void UStarterWeaponButtonWidget::NativeConstruct() void UStarterWeaponButtonWidget::NativeConstruct()
{ {
@ -34,4 +37,20 @@ void UStarterWeaponButtonWidget::NativeOnListItemObjectSet(UObject* ListItemObje
void UStarterWeaponButtonWidget::OnClicked() void UStarterWeaponButtonWidget::OnClicked()
{ {
if (UVampireGameInstance* gameInstance = Cast<UVampireGameInstance>(GetGameInstance()))
{
gameInstance->StarterWeapon = WeaponTemplate;
if (!gameInstance->GameWorld.IsNull())
{
UGameplayStatics::OpenLevelBySoftObjectPtr(GetWorld(), gameInstance->GameWorld);
if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{
PlayerController->bShowMouseCursor = false;
UWidgetBlueprintLibrary::SetInputMode_GameOnly(PlayerController);
}
SetIsFocusable(false);
}
}
} }