Added weapon manager

This commit is contained in:
TjgL 2025-01-20 16:27:36 +01:00
parent 59df20c639
commit a107f650c8
8 changed files with 124 additions and 16 deletions

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

Binary file not shown.

BIN
Content/Legumix/Weapon/BP_WeaponManagerComponent.uasset (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -3,32 +3,32 @@
#include "Player/LMPlayer.h" #include "Player/LMPlayer.h"
// Sets default values #include "Weapon/LMWeaponManager.h"
ALMPlayer::ALMPlayer() ALMPlayer::ALMPlayer()
{ {
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
WeaponManager = CreateDefaultSubobject<ULMWeaponManager>(TEXT("Weapon Manager"));
WeaponSkeletalMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Weapon Mesh"));
WeaponSkeletalMeshComponent->SetupAttachment(GetMesh());
WeaponManager->SetWeaponMeshComponent(WeaponSkeletalMeshComponent);
} }
// Called when the game starts or when spawned
void ALMPlayer::BeginPlay() void ALMPlayer::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
} }
// Called every frame
void ALMPlayer::Tick(float DeltaTime) void ALMPlayer::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
} }
// Called to bind functionality to input
void ALMPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) void ALMPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{ {
Super::SetupPlayerInputComponent(PlayerInputComponent); Super::SetupPlayerInputComponent(PlayerInputComponent);
} }

View File

@ -5,13 +5,14 @@
#include "EnhancedInputSubsystems.h" #include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h" #include "EnhancedInputComponent.h"
#include "GameFramework/Character.h" #include "GameFramework/Character.h"
#include "Player/LMPlayer.h"
void ALMPlayerController::BeginPlay() void ALMPlayerController::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
EnhancedInputSubsystem = GetLocalPlayer()->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(); EnhancedInputSubsystem = GetLocalPlayer()->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>();
EnhancedInputSubsystem->AddMappingContext(DefaultMappingContext, 0); EnhancedInputSubsystem->AddMappingContext(DefaultMappingContext, 0);
LegumixPlayer = Cast<ACharacter>(GetPawn()); LegumixPlayer = Cast<ALMPlayer>(GetPawn());
} }
void ALMPlayerController::SetupInputComponent() void ALMPlayerController::SetupInputComponent()

View File

@ -0,0 +1,50 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Weapon/LMWeaponManager.h"
#include "Weapon/LMWeapon.h"
ULMWeaponManager::ULMWeaponManager()
{
PrimaryComponentTick.bCanEverTick = false;
}
void ULMWeaponManager::BeginPlay()
{
Super::BeginPlay();
for (auto Weapon : StartingWeapons)
{
if (Weapon)
{
ULMWeapon* Instance = NewObject<ULMWeapon>(this, Weapon);
Weapons.Add(Instance);
}
}
if (!Weapons.IsEmpty())
{
SetWeapon(CurrentWeaponIndex);
}
}
void ULMWeaponManager::SetWeapon(int Index)
{
if (Index < 0 || Index >= Weapons.Num())
{
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 5, FColor::Red, "Invalid Weapon Index");
return;
}
CurrentWeaponIndex = Index;
if (WeaponMeshComponent)
{
// TODO
// WeaponMeshComponent->SetSkeletalMesh()
}
}

View File

@ -6,24 +6,28 @@
#include "GameFramework/Character.h" #include "GameFramework/Character.h"
#include "LMPlayer.generated.h" #include "LMPlayer.generated.h"
class ULMWeaponManager;
UCLASS() UCLASS()
class LEGUMEMIX_API ALMPlayer : public ACharacter class LEGUMEMIX_API ALMPlayer : public ACharacter
{ {
GENERATED_BODY() GENERATED_BODY()
public: public:
// Sets default values for this character's properties
ALMPlayer(); ALMPlayer();
protected: protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;
public: public:
// Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Legumix", meta = (AllowPrivateAccess = true))
TObjectPtr<ULMWeaponManager> WeaponManager;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Legumix", meta = (AllowPrivateAccess = true))
TObjectPtr<USkeletalMeshComponent> WeaponSkeletalMeshComponent;
}; };

View File

@ -7,6 +7,8 @@
#include "EnhancedInputSubsystems.h" #include "EnhancedInputSubsystems.h"
#include "LMPlayerController.generated.h" #include "LMPlayerController.generated.h"
class ALMPlayer;
/** /**
* *
*/ */
@ -35,7 +37,7 @@ private:
TObjectPtr<UInputAction> JumpAction; TObjectPtr<UInputAction> JumpAction;
UPROPERTY(BlueprintReadOnly, meta = (AllowPrivateAccess = true)) UPROPERTY(BlueprintReadOnly, meta = (AllowPrivateAccess = true))
TObjectPtr<ACharacter> LegumixPlayer; TObjectPtr<ALMPlayer> LegumixPlayer;
virtual void SetupInputComponent() override; virtual void SetupInputComponent() override;
void Move(const FInputActionValue& InputValue); void Move(const FInputActionValue& InputValue);

View File

@ -0,0 +1,48 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "LMWeaponManager.generated.h"
class ULMWeapon;
UCLASS(Blueprintable, BlueprintType, ClassGroup="Legumix", meta=(BlueprintSpawnableComponent))
class LEGUMEMIX_API ULMWeaponManager : public UActorComponent
{
GENERATED_BODY()
public:
ULMWeaponManager();
void SetWeaponMeshComponent(USkeletalMeshComponent* Mesh) { WeaponMeshComponent = Mesh; }
public:
/**
* Get the Weapon currently equipped.
* @return The Current Weapon.
*/
UFUNCTION(BlueprintCallable, Category="Legumix")
ULMWeapon* GetCurrentWeapon() { return Weapons[CurrentWeaponIndex]; }
UFUNCTION(BlueprintCallable, Category="Legumix")
void SetWeapon(int Index);
protected:
virtual void BeginPlay() override;
private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Legumix", meta = (AllowPrivateAccess = "true"))
TObjectPtr<USkeletalMeshComponent> WeaponMeshComponent;
/** The weapons the player starts with. */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Legumix", meta=(AllowPrivateAccess=true))
TArray<TSubclassOf<ULMWeapon>> StartingWeapons;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Legumix", meta=(AllowPrivateAccess=true))
TArray<TObjectPtr<ULMWeapon>> Weapons;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Legumix", meta=(AllowPrivateAccess=true))
int CurrentWeaponIndex = 0;
};