프로그래밍 공부
작성일
2024. 4. 24. 14:30
작성자
WDmil
728x90

C++로 애니메이션을 적용한 뒤, 무기객체에 대해 참조하고 캐릭터에 적용할 수 있다.

 

무기를 생성하고, 해당 무기 테이블에서 데이터를 참조하여 스켈레톤의 소켓값에 대입할 수 있다.


캐릭터에 무기를 주려면, 무기 데이터를 가지고 있는 Data Asset을 생성해야 한다.

 

전에 만들어 두었던 Weapon Data를 가지는 Data Asset을 생성한다.

 

WeaponDataAsset

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Engine/DataAsset.h"
#include "WeaponDataAsset.generated.h"

DECLARE_LOG_CATEGORY_EXTERN(WeaponDataAssetLog, All, Display)
/**
 * 
 */
class ABaseWeapon;
UCLASS()
class FTPSGAME_API UWeaponDataAsset : public UDataAsset
{
	GENERATED_BODY()
	
	UPROPERTY()
	TObjectPtr<ABaseWeapon> WeaponPtr;

	UPROPERTY(EditDefaultsOnly, meta = (AllowPrivateAccess = "true"))
	TSubclassOf<ABaseWeapon> WeaponClass;

	UPROPERTY(EditDefaultsOnly, Category = "Property", meta = (AllowPrivateAccess ="true"))
	float weight;

	// Socket
	UPROPERTY(EditDefaultsOnly, Category = "Socket", meta = (AllowPrivateAccess = "true"))
	FName MainHolderSocket;

	UPROPERTY(EditDefaultsOnly, Category = "Socket", meta = (AllowPrivateAccess = "true"))
	FName SubHolderSocket;

	UPROPERTY(EditDefaultsOnly, Category = "Socket", meta = (AllowPrivateAccess = "true"))
	FName HandleSocket;

public:
	ABaseWeapon* GetWeapon() { return WeaponPtr; }

	void CreateWeapon(AActor* InOwner);
};

 

가지는 데이터 정보는, 현재 무기의 WeaponClass와 Weight이다.

 

여기에 무기의 데이터가 추가된다면, 공격력, 탄속 등의 데이터가 들어갈 수 있다.

 

또한, CreateWeapon을 통해, 현재 무기를 가지는 객체에 상속되게 무기를 생성해줄 수 있다.

// Fill out your copyright notice in the Description page of Project Settings.


#include "Data/Weapon/WeaponDataAsset.h"
#include "Actors/Weapons/BaseWeapon.h"

void UWeaponDataAsset::CreateWeapon(AActor* InOwner)
{
	// Spawn Weapon
	FActorSpawnParameters Parameters;
	Parameters.Owner = InOwner;
	if (WeaponClass == nullptr)
	{
		WeaponPtr = InOwner->GetWorld()->SpawnActor<ABaseWeapon>(ABaseWeapon::StaticClass(), FVector(), FRotator(), Parameters);
	}
	else
		WeaponPtr = InOwner->GetWorld()->SpawnActor<ABaseWeapon>(WeaponClass, FVector(), FRotator(), Parameters);

	// AttachWeapon ( Object)
	WeaponPtr->AttachToActor(InOwner, FAttachmentTransformRules::KeepRelativeTransform);
}

 

BaseChracter에서 무기를 가질 수 있도록 객체를 수정해준다.

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "BaseCharacter.generated.h"

#define SKELETAL_MESH_COMP(NAME) \
UPROPERTY(VisibleDefaultsOnly) \
TObjectPtr<USkeletalMeshComponent> NAME;

class UWeaponComponent;
class ABaseWeapon;
class UWeaponDataAsset;
UCLASS()
class FTPSGAME_API ABaseCharacter : public ACharacter
{
	GENERATED_BODY()
	
protected:
	SKELETAL_MESH_COMP(Body)
	SKELETAL_MESH_COMP(Head)
	SKELETAL_MESH_COMP(LeftGranade)
	SKELETAL_MESH_COMP(RightGranade)
	SKELETAL_MESH_COMP(Tablet)
	SKELETAL_MESH_COMP(LeftBag)
	SKELETAL_MESH_COMP(RightBag)
	SKELETAL_MESH_COMP(VestBag)
	SKELETAL_MESH_COMP(BackBag)
	SKELETAL_MESH_COMP(ExoLegs)

	UPROPERTY(EditDefaultsOnly, Category = "Weapon")
	TObjectPtr<UWeaponComponent> WeaponComponent;

	UPROPERTY(EditDefaultsOnly, Category = "Weapon")
	TObjectPtr<UWeaponDataAsset> EquipWeapon;

public:
	// Sets default values for this character's properties
	ABaseCharacter();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	void AttachWeapon();
public:	
	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};

UENUM()
enum class EAttachType
{
	Handle,
	MainHolder,
	SubHolder
};

 

EquipWeapon과 WeaponComponent를 생성한다.

 

여기서 EquipWeapon은 현재 가지는 무기를 의미하고, WeaponComponent는 앞으로 얻는 무기, 최대 무기를 두개 인벤토리에 넣을 수 있도록 관리한다.


WeaponComponent

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "WeaponComponent.generated.h"

class ABaseCharacter;
class ABaseWeapon;

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class FTPSGAME_API UWeaponComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UWeaponComponent();
private:
	UPROPERTY()
	TObjectPtr<ABaseCharacter> Owner;

	UPROPERTY()
	TObjectPtr<ABaseWeapon> MainWeapn;

	UPROPERTY()
	TObjectPtr<ABaseWeapon> SubWeapon;



protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;


private:
	void SpawnWeapon();
		
};

 

Weapon을 관리하는 컴포넌트 이다. 무기를 넣는 인벤토리 라고 이해하면 편하게 생각할 수 있다.

 

모든 코드를 컴파일하고 위와같이 데이터를 기입하면, WeaponData에 따라 설정된 객체가 생성된다.


WeaponData

 

WeaponData는 위와같이 구성할 수 있다.

 

생성할 때 만들어놓은 C++코드에 상속받아 생성하게 되면 위와같이 블루프린트와 C++이 제한적으로나마 통신할 수 있다.

 

무기가 Character의 Root 위치에 생성되는 것을 확인할 수 있다.

 


그리고, 무기를 붙일 부분과, 가방부분의 소켓을 생성해놓으면 된다.

728x90