4#include "Net/UnrealNetwork.h"
5#include "AudioDevice.h"
6#include "ActiveSound.h"
7#include "GripMotionControllerComponent.h"
9#include "Components/StaticMeshComponent.h"
10#include "Components/BoxComponent.h"
11#include "Components/AudioComponent.h"
12#include "Kismet/GameplayStatics.h"
13#include "Sound/SoundAttenuation.h"
18 , bDestroyOnExit(true)
19 , bToggleCollisionOnUsage(true)
22 PrimaryActorTick.bCanEverTick =
true;
30 ToolMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(FName(
"ToolMesh"));
37 SetReplicateMovement(
true);
58 Super::Tick(DeltaSeconds);
70void APixoVRToolBase::OnOverlapBegin(UPrimitiveComponent* OverlappedComp,
AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult& SweepResult)
72 if (!
bIsGripped && OtherActor !=
this && OtherActor != GetOwner())
74 FRotator NewRotation = GetActorRotation();
75 NewRotation.Roll = 0.0f;
76 NewRotation.Pitch = 0.0f;
79 SetActorRotation(NewRotation);
83bool APixoVRToolBase::IsDestroyable_Implementation()
88void APixoVRToolBase::HandleDestroy_Implementation()
100 Super::GetLifetimeReplicatedProps(OutLifetimeProps);
110 if (GrippingController)
164 bUseCustomTransform =
true;
181 SetActorEnableCollision(
false);
188 SetActorEnableCollision(
true);
192void APixoVRToolBase::MulticastToggleCollision_Implementation(ECollisionEnabled::Type Type)
200void APixoVRToolBase::ServerStartPlayingSound_Implementation(USoundBase* Sound,
const FVector& SoundLocation,
float VolumeMultiplier )
205bool APixoVRToolBase::ServerStartPlayingSound_Validate(USoundBase* Sound,
const FVector& SoundLocation,
float VolumeMultiplier )
210void APixoVRToolBase::MulticastStartPlayingSound_Implementation(USoundBase* Sound,
const FVector& SoundLocation,
float VolumeMultiplier)
212 UWorld* ThisWorld = GetWorld();
213 if (!Sound || !ThisWorld || !ThisWorld->bAllowAudioPlayback || ThisWorld->IsNetMode(NM_DedicatedServer))
218 if (FAudioDevice* AudioDevice = ThisWorld->GetAudioDevice().GetAudioDevice())
220 USoundAttenuation* AttenuationSettings = Sound->AttenuationSettings;
221 if(!AttenuationSettings)
225 UE_LOG(LogTemp, Warning, TEXT(
"Attenuation settings for sound = %s - nullptr, used default settings"), *Sound->GetName());
228 AudioDevice->PlaySoundAtLocation(Sound, ThisWorld, VolumeMultiplier, 1.f, 0.f, SoundLocation, FRotator::ZeroRotator, AttenuationSettings,
nullptr,
nullptr,
nullptr);
232void APixoVRToolBase::ServerStopPlayingSound_Implementation(USoundBase* sound)
237bool APixoVRToolBase::ServerStopPlayingSound_Validate(USoundBase* sound)
242void APixoVRToolBase::MulticastStopPlayingSound_Implementation(USoundBase* Sound)
244 UWorld* ThisWorld = GetWorld();
245 if (!Sound || !ThisWorld || !ThisWorld->bAllowAudioPlayback || ThisWorld->IsNetMode(NM_DedicatedServer))
250 if (FAudioDevice* AudioDevice = ThisWorld->GetAudioDevice().GetAudioDevice())
252 auto StopSound = [AudioDevice, Sound]()
254 if(AudioDevice && Sound)
256 const TArray<FActiveSound*>& ActiveSounds = AudioDevice->GetActiveSounds();
257 for (FActiveSound* ActiveSound : ActiveSounds)
259 if (ActiveSound->GetSound() == Sound)
261 AudioDevice->StopActiveSound(ActiveSound);
267 if(IsInAudioThread())
273 if(IsAudioThreadRunning())
275 FAudioThread::RunCommandOnAudioThread(StopSound);
281void APixoVRToolBase::ServerOnDropped_Implementation()
286bool APixoVRToolBase::ServerOnDropped_Validate()
291void APixoVRToolBase::ServerStartPlayingSoundViaComponent_Implementation(USoundBase* Sound,
float VolumeMultiplier )
296bool APixoVRToolBase::ServerStartPlayingSoundViaComponent_Validate(USoundBase* Sound,
float VolumeMultiplier )
301void APixoVRToolBase::MulticastStartPlayingSoundViaComponent_Implementation(USoundBase* Sound,
float VolumeMultiplier)
303 UWorld* ThisWorld = GetWorld();
304 if (!Sound || (
AudioComponent &&
AudioComponent->IsPlaying()) || !ThisWorld || !ThisWorld->bAllowAudioPlayback || ThisWorld->IsNetMode(NM_DedicatedServer))
309 USoundAttenuation* AttenuationSettings = Sound->AttenuationSettings;
310 if(!AttenuationSettings)
314 UE_LOG(LogTemp, Warning, TEXT(
"Attenuation settings for sound = %s - nullptr, used default settings"), *Sound->GetName());
317 AudioComponent = UGameplayStatics::SpawnSoundAttached(Sound, RootComponent,
"RootComponent", FVector(ForceInit), FRotator::ZeroRotator, EAttachLocation::KeepRelativeOffset,
true, VolumeMultiplier, 1.f, 0.f, AttenuationSettings,
nullptr,
true);
322void APixoVRToolBase::ServerStopPlayingSoundViaComponent_Implementation()
327bool APixoVRToolBase::ServerStopPlayingSoundViaComponent_Validate()
332void APixoVRToolBase::MulticastStopPlayingSoundViaComponent_Implementation()
334 UWorld* ThisWorld = GetWorld();
335 if (!ThisWorld || !ThisWorld->bAllowAudioPlayback || ThisWorld->IsNetMode(NM_DedicatedServer))
346void APixoVRToolBase::ServerAdjustPlayingSoundVolumeViaComponent_Implementation(
float InVolumeMultiplier )
351bool APixoVRToolBase::ServerAdjustPlayingSoundVolumeViaComponent_Validate(
float InVolumeMultiplier)
356void APixoVRToolBase::MulticastAdjustPlayingSoundVolumeViaComponent_Implementation(
float InVolumeMultiplier)
360 if (InVolumeMultiplier == 0.f || FMath::Abs(
AudioComponent->VolumeMultiplier - InVolumeMultiplier) >= 0.05f)
367void APixoVRToolBase::ServerUseTool_Implementation(UGripMotionControllerComponent* GrippingController,
AActor* GrippedActor)
373 GrabHand =
IsRightHand(GrippingController) ? EControllerHand::Right : EControllerHand::Left;
378 SetActorRotation(Cast<APixoVRHand>(GrippingController->GetOwner())->GrabSphere->GetComponentRotation());
396bool APixoVRToolBase::ServerUseTool_Validate(UGripMotionControllerComponent* GrippingController,
AActor* GrippedActor)
401void APixoVRToolBase::ServerDestroyTool_Implementation()
413 GrabHand = EControllerHand::AnyHand;
419bool APixoVRToolBase::ServerDestroyTool_Validate()
426 const auto HandType = Cast<APixoVRHand>(GrippingController->GetOwner())->HandMotionSource;
427 return HandType == EControllerHand::Right ? true :
false;
EPixoVRGripModeEnum
UENUM(BlueprintType)
EPixoVRGripTriggerModeEnum
UENUM(BlueprintType)
@ GRIP_TRIGGER_TOGGLE_DROP