tengri/Source/TengriPlatformer/World/Interactive/TengriPickupActor.cpp

83 lines
2.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// Request Games © All rights reserved
// Source/TengriPlatformer/World/TengriPickupActor.cpp
#include "TengriPickupActor.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
ATengriPickupActor::ATengriPickupActor()
{
PrimaryActorTick.bCanEverTick = false; // Физике тик не нужен
// 1. Mesh Setup
MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
RootComponent = MeshComp;
// Enable physics by default
MeshComp->SetSimulatePhysics(true);
MeshComp->SetCollisionProfileName(TEXT("PhysicsActor")); // Стандартный профиль UE для предметов
MeshComp->SetMassOverrideInKg(NAME_None, 10.0f); // Вес по дефолту
// 2. Trigger Setup
TriggerComp = CreateDefaultSubobject<USphereComponent>(TEXT("TriggerComp"));
TriggerComp->SetupAttachment(MeshComp);
TriggerComp->SetSphereRadius(80.0f);
// Trigger collision settings
TriggerComp->SetCollisionProfileName(TEXT("Trigger"));
}
void ATengriPickupActor::BeginPlay()
{
Super::BeginPlay();
}
void ATengriPickupActor::OnPickedUp(USceneComponent* AttachTo, const FName SocketName)
{
if (!MeshComp || !AttachTo) return;
bIsHeld = true;
// 1. Disable Physics
MeshComp->SetSimulatePhysics(false);
// 2. Disable Collision
MeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
// 3. Attach to Hand
FAttachmentTransformRules AttachmentRules(
EAttachmentRule::SnapToTarget, // Location Rule
EAttachmentRule::SnapToTarget, // Rotation Rule
EAttachmentRule::KeepWorld, // Scale Rule
false // bWeldSimulatedBodies (добавили этот аргумент)
);
AttachToComponent(AttachTo, AttachmentRules, SocketName);
}
void ATengriPickupActor::OnDropped(const FVector Impulse, const bool bVelChange)
{
if (!MeshComp) return;
bIsHeld = false;
// 1. Detach
DetachFromActor(FDetachmentTransformRules::KeepWorldTransform);
// 2. Re-enable Collision
MeshComp->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
if (!MeshComp) return;
// 3. Re-enable Physics
MeshComp->SetSimulatePhysics(true);
// 4. Apply Throw Force
if (!Impulse.IsNearlyZero())
{
MeshComp->AddImpulse(Impulse, NAME_None, bVelChange);
}
}