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]
GameDefaultMap=/Game/Levels/Level.Level
GameDefaultMap=/Game/Levels/MainMenu/MainMenu.MainMenu
GlobalDefaultGameMode=/Game/Gamemode/BP_DefaultGamemode.BP_DefaultGamemode_C
GameInstanceClass=/Script/vampires.VampireGameInstance
EditorStartupMap=/Game/Levels/Level.Level
GameInstanceClass=/Game/Gamemode/BP_VampireGameInstance.BP_VampireGameInstance_C
EditorStartupMap=/Game/Levels/MainMenu/MainMenu.MainMenu
[/Script/WindowsTargetPlatform.WindowsTargetSettings]
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

@ -1,4 +1,4 @@
// Louis Hobbs | 2024-2025
// Louis Hobbs | 2024-2025
#pragma once
@ -6,12 +6,23 @@
#include "Engine/GameInstance.h"
#include "VampireGameInstance.generated.h"
class AWeapon;
/**
*
*/
UCLASS()
class VAMPIRES_API UVampireGameInstance : public UGameInstance
{
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 "GoldComponent.h"
#include "Inputable.h"
#include "VampireGameInstance.h"
#include "VampireGameMode.h"
#include "Weapon.h"
#include "WeaponInventoryComponent.h"
#include "Blueprint/UserWidget.h"
#include "Blueprint/WidgetBlueprintLibrary.h"
#include "Kismet/GameplayStatics.h"
@ -58,6 +61,17 @@ void AVampirePlayerController::OnPossess(APawn* aPawn)
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()

View File

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

View File

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

View File

@ -4,9 +4,12 @@
#include "StarterWeaponButtonWidget.h"
#include "StarterWeaponButtonDataObject.h"
#include "Blueprint/WidgetBlueprintLibrary.h"
#include "Components/Button.h"
#include "Components/Image.h"
#include "Components/TextBlock.h"
#include "Kismet/GameplayStatics.h"
#include "vampires/VampireGameInstance.h"
void UStarterWeaponButtonWidget::NativeConstruct()
{
@ -34,4 +37,20 @@ void UStarterWeaponButtonWidget::NativeOnListItemObjectSet(UObject* ListItemObje
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);
}
}
}