zworld-em/Source/zworld/FXComponent.cpp

71 lines
2.6 KiB
C++
Raw Normal View History

2025-05-11 22:07:21 +08:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "FXComponent.h"
#include "NiagaraSystem.h"
#include "NiagaraComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraDataChannel.h"
#include "NameRegisterLibrary.h"
#include "NiagaraDataChannelAccessor.h"
// Sets default values for this component's properties
UFXComponent::UFXComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UFXComponent::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void UFXComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
UObject* UFXComponent::PlayFX(UObject* FXAsset, USceneComponent* MeshComp, FName SocketName, FVector LocalOffset, FRotator LocalRotation, int32 DataChannelCount, FFXSpawnInfo SpawnInfo) {
UObject* FXObject = nullptr;
if (UNiagaraSystem* NiagaraAsset = Cast<UNiagaraSystem>(FXAsset))
{
FXObject = SpawnSystemAttached(NiagaraAsset, MeshComp, SocketName, LocalOffset, LocalRotation, SpawnInfo);
}else if (UNiagaraDataChannelAsset* NDCAsset = Cast<UNiagaraDataChannelAsset>(FXAsset))
{
FXObject = UNiagaraDataChannelLibrary::WriteToNiagaraDataChannel(this, NDCAsset, FNiagaraDataChannelSearchParameters(GetOwner()->GetActorLocation()), DataChannelCount,
true, true, true, *GetOwner()->GetName());
if (auto NDCWriter = Cast<UNiagaraDataChannelWriter>(FXObject))
{
NDCWriter->WriteVector(UNameRegisterLibrary::Name_Position, 0, LocalOffset);
NDCWriter->WriteVector(UNameRegisterLibrary::Name_Rotation, 0, FVector(LocalRotation.Pitch, LocalRotation.Yaw, LocalRotation.Roll));
NDCWriter->WriteQuat(UNameRegisterLibrary::Name_Quaternion, 0, LocalRotation.Quaternion());
}
}
return FXObject;
}
UNiagaraComponent* UFXComponent::SpawnSystemAttached(UNiagaraSystem* SystemTemplate,
USceneComponent* AttachToComponent, FName AttachPointName, FVector Location, FRotator Rotation, FFXSpawnInfo SpawnInfo)
{
UNiagaraComponent* FXObject = UNiagaraFunctionLibrary::SpawnSystemAttached(SystemTemplate, AttachToComponent, AttachPointName, Location, Rotation,
SpawnInfo.LocationType, SpawnInfo.bAutoDestroy, SpawnInfo.bAutoActivate, SpawnInfo.PoolingMethod, SpawnInfo.bPreCullCheck);
if (FXObject)
{
FXObject->SetTickBehavior(SpawnInfo.TickBehavior);
}
return FXObject;
}