Unreal/Game 2
6. 무기 생성 및 소켓 장착
kyoun
2019. 6. 19. 14:43
클래스 기능을 분담화 하기 위해서 여러 클래스를 만듬
C++ Weapon : 무기의 추상화 클래스, Class Ammo를 가지고 있다. 필요하면 기능 추가예정
class KGAME_API AKWeapon : public AActor
{
GENERATED_BODY()
public:
AKWeapon();
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Weapon")
USkeletalMeshComponent* Mesh;
UPROPERTY(VisibleAnywhere, Category = "Weapon")
class AKAmmo* Ammo;
protected:
virtual void BeginPlay() override;
int CurrentAmmo;
int MaxAmmo;
int MinAmmo;
float ammoSpeed;
};
C++ Ammo : 탄 메시 및 충돌체, 발사체 틱,
UCLASS()
class KGAME_API AKAmmo : public AActor
{
GENERATED_BODY()
public:
AKAmmo();
protected:
virtual void Tick(float DeltaTime) override;
UPROPERTY(VisibleAnywhere, Category = "Ammo")
UStaticMeshComponent* Mesh;
UPROPERTY(VisibleAnywhere, Category = "Collsion")
UBoxComponent* BoxComponent;
UPROPERTY(VisibleAnywhere, Category = "Effect")
UParticleSystemComponent* Effect;
};
C++ Pistol : 권총 클래스, 탄은 12발
//h
UCLASS()
class KGAME_API AKPistol : public AKWeapon
{
GENERATED_BODY()
public:
AKPistol();
protected:
virtual void BeginPlay() override;
};
//cpp
AKPistol::AKPistol()
{
static ConstructorHelpers::FObjectFinder<USkeletalMesh> gunmesh(TEXT("SkeletalMesh'/Game/Pistol_Starter/SkeletalMesh/SK_1911_01.SK_1911_01'"));
if (gunmesh.Succeeded())
{
Mesh->SetSkeletalMesh(gunmesh.Object);
}
}
void AKPistol::BeginPlay()
{
Super::BeginPlay();
MaxAmmo = 12;
MinAmmo = 0;
CurrentAmmo = MaxAmmo;
}
그리고 캐릭터는 KWeapon 포인터를 하나 들고 있게함
장비가 여러개 될 경우에는 교체예정
별도로 코드로 할 경우에는 재귀적으로 노드를 순위하면서 이름을 찾아야함
void AKCharacter::SetWeapon(class AKWeapon* NewWeapon)
{
FName WeaponSocket(TEXT("hand_rSocket"));
if (nullptr != NewWeapon)
{
NewWeapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, WeaponSocket);
NewWeapon->SetOwner(this);
CurrentWeapon = NewWeapon;
}
}
구현한 소켓이름을 찾아서 붙이는 함수