KLingo Project Documentation 1.0.0
Unreal Engine 5.6 C++ Project Documentation
로딩중...
검색중...
일치하는것 없음
Trolley.cpp
이 파일의 문서화 페이지로 가기
1// Copyright (c) 2025 Doppleddiggong. All rights reserved. Unauthorized copying, modification, or distribution of this file, via any medium is strictly prohibited. Proprietary and confidential.
2
3
4#include "Trolley.h"
5
7#include "GameFramework/Character.h"
8
9
10// Sets default values
12{
13 // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
14 PrimaryActorTick.bCanEverTick = true;
15
16 // Create root mesh component
17 MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
18 RootComponent = MeshComp;
19 MeshComp->SetSimulatePhysics(false); // Custom velocity control, no physics simulation
20 MeshComp->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
21 MeshComp->SetCollisionProfileName(TEXT("BlockAllDynamic")); // Block physics objects like Food
22 MeshComp->SetMobility(EComponentMobility::Movable); // Must be movable to change location at runtime
23 MeshComp->SetNotifyRigidBodyCollision(true); // Enable hit events for OnComponentHit
24
25 // Create interactable component
26 InteractableComp = CreateDefaultSubobject<UInteractableComponent>(TEXT("InteractableComp"));
27 InteractableComp->InteractionType = EInteractionType::Button; // Use Button type for pushing
28 InteractableComp->InteractionPrompt = TEXT("Press E to Push");
29
30 // Initialize velocity
31 CurrentVelocity = FVector::ZeroVector;
32}
33
34// Called when the game starts or when spawned
36{
37 Super::BeginPlay();
38
39 // Bind interaction delegate
41 {
42 InteractableComp->OnInteractionTriggered.AddDynamic(this, &ATrolley::OnPushed);
43 InteractableComp->OnOutlineStateChanged.AddDynamic(this, &ATrolley::OnOutlineStateChanged);
44 }
45
46 // Bind collision delegate
47 if (MeshComp)
48 {
49 MeshComp->OnComponentHit.AddDynamic(this, &ATrolley::OnTrolleyHit);
50 }
51}
52
53// Called every frame
54void ATrolley::Tick(float DeltaTime)
55{
56 Super::Tick(DeltaTime);
57
58 // Check if moving
60 {
61 // Keep velocity parallel to ground (ignore Z axis)
62 FVector HorizontalVelocity = CurrentVelocity;
63 HorizontalVelocity.Z = 0.0f;
64
65 // Calculate movement delta
66 FVector DeltaMovement = HorizontalVelocity * DeltaTime;
67
68 // Move the trolley
69 FVector OldLocation = GetActorLocation();
70 FVector NewLocation = OldLocation + DeltaMovement;
71 SetActorLocation(NewLocation, true); // true = sweep for collision
72
73 // Apply deceleration
74 FVector DecelerationVector = HorizontalVelocity.GetSafeNormal() * Deceleration * DeltaTime;
75 CurrentVelocity -= DecelerationVector;
76
77 // Stop if velocity is too small
79 {
80 CurrentVelocity = FVector::ZeroVector;
81 }
82 }
83}
84
85void ATrolley::OnPushed(AActor* Interactor)
86{
87 if (!Interactor) return;
88
89 // Get push direction from interactor's forward vector
90 FVector PushDirection = Interactor->GetActorForwardVector();
91 PushDirection.Z = 0.0f; // Keep it parallel to ground
92 PushDirection.Normalize();
93
94 // Add velocity in push direction
95 CurrentVelocity += PushDirection * PushForce;
96}
97
98void ATrolley::OnTrolleyHit(UPrimitiveComponent* HitComponent, AActor* OtherActor,
99 UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
100{
101 if (!OtherActor)
102 return;
103
104 // Check if the other actor is a player character
105 ACharacter* Character = Cast<ACharacter>(OtherActor);
106 if (!Character || !Character->IsPlayerControlled())
107 return;
108
109 // Calculate push direction (from player to trolley)
110 FVector PushDirection = GetActorLocation() - OtherActor->GetActorLocation();
111 PushDirection.Z = 0.0f; // Keep it parallel to ground
112 PushDirection.Normalize();
113
114 // Add velocity in push direction with reduced force
116}
117
118void ATrolley::OnOutlineStateChanged(bool bShouldShowOutline)
119{
120 if (MeshComp)
121 {
122 MeshComp->SetRenderCustomDepth(bShouldShowOutline);
123 }
124}
virtual void Tick(float DeltaTime) override
Definition Trolley.cpp:54
float Deceleration
감속도 (cm/s²)
Definition Trolley.h:47
float MinVelocityThreshold
정지 판정 속도 임계값 (cm/s)
Definition Trolley.h:51
TObjectPtr< class UInteractableComponent > InteractableComp
상호작용 컴포넌트
Definition Trolley.h:35
void OnOutlineStateChanged(bool bShouldShowOutline)
InteractableComponent의 아웃라인 상태 변경 이벤트 핸들러
Definition Trolley.cpp:118
void OnTrolleyHit(UPrimitiveComponent *HitComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, FVector NormalImpulse, const FHitResult &Hit)
수레 메시에 충돌이 발생했을 때 호출
Definition Trolley.cpp:98
void OnPushed(AActor *Interactor)
플레이어가 수레를 밀었을 때 호출
Definition Trolley.cpp:85
float PushForce
밀었을 때 추가되는 힘 (cm/s)
Definition Trolley.h:43
virtual void BeginPlay() override
Definition Trolley.cpp:35
float CollisionPushMultiplier
충돌 시 추가되는 힘의 배율 (PushForce 대비)
Definition Trolley.h:55
TObjectPtr< class UStaticMeshComponent > MeshComp
수레 메시
Definition Trolley.h:31
FVector CurrentVelocity
현재 속도 벡터
Definition Trolley.h:59