检测重量开关门

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 //OpenDoor.h
 3 #pragma once
 4 #include"CoreMinimal.h"
 5 #include "Components/ActorComponent.h"
 6 #include"GameFramework/Actor.h"
 7 #include"Engine/TriggerVolume.h"
 8 #include"Engine/World.h"
 9 #include"Components/PrimitiveComponent.h"
10 #include"OutputDeviceDebug.h"
11 #include "OpenDoor.generated.h"
12 
13 DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOpenDoorRequest);  //开门代理
14 DECLARE_DYNAMIC_MULTICAST_DELEGATE(FCloseDoorRequest); //关门代理
15 
16 UCLASS(Blueprintable,ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
17 class MYCPP_API UOpenDoor : public UActorComponent
18 {
19     GENERATED_BODY()
20 
21 public:    
22     // Sets default values for this component's properties
23     UOpenDoor();
24 
25     UPROPERTY(EditAnywhere)
26     float Angle = 0.0f;//旋转角度
27 
28     UPROPERTY(EditAnywhere)
29     ATriggerVolume*TriggerArea = nullptr;//可以开门的区域
30 
31 private:
32     void OpenDoor();  //开门函数
33     void CloseDoor(); //关门函数
34     float GetTotalMassOfActors(); //获取区域重量
35 protected:
36     virtual void BeginPlay() override;
37 
38 public:    
39     UFUNCTION(BlueprintCallable,Category="MyOpenDoor")
40     void TextFunFromC();  //从c++到蓝图函数
41 
42     UPROPERTY(BlueprintAssignable)
43         FOpenDoorRequest OnOpenDoorRequest;  //创建开门代理
44     UPROPERTY(BlueprintAssignable)
45         FCloseDoorRequest OnCloseDoorRequest;  //创建关门代理
46 
47     virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
48 public:
49     UPROPERTY(EditAnywhere)
50     float MassToOpen = 25.0f;  //所需重量
51         
52     
53 };
 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 //OpenDoor.cpp
 3 #include "MyCpp.h"
 4 #include "OpenDoor.h"
 5 
 6 
 7 // Sets default values for this component's properties
 8 UOpenDoor::UOpenDoor()
 9 {
10     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
11     // off to improve performance if you don't need them.
12     PrimaryComponentTick.bCanEverTick = true;
13 
14     // ...
15 }
16 
17 
18 // Called when the game starts
19 void UOpenDoor::BeginPlay()
20 {
21     Super::BeginPlay();
22     /*
23     FString cmd = FString::Printf(TEXT("TextFunFromBP ABCD ZLKJ"));
24     FOutputDeviceDebug device;
25     CallFunctionByNameWithArguments(*cmd,device,NULL,true);
26     OnTestReques.Broadcast();    */
27 
28     
29 }
30 
31 
32 // Called every frame
33 void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
34 {
35     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);  //判断重量是否达标
36     if (GetTotalMassOfActors() >= MassToOpen) {
37         OpenDoor();
38     }
39     else
40     {
41         CloseDoor();
42     }
43 }
44 
45  //开门函数实现
46 void UOpenDoor::OpenDoor() { 
47 
48     OnOpenDoorRequest.Broadcast(); // 绑定开门代理
49 }
50 
51 //关门函数实现
52 void UOpenDoor::CloseDoor() {
53 
54     OnCloseDoorRequest.Broadcast(); //绑定关门代理
55 }
56 
57 float UOpenDoor::GetTotalMassOfActors() {
58     if (TriggerArea == nullptr) {
59         UE_LOG(LogTemp, Error, TEXT("TriggerArea Is NULL!")); //判断检测区域是否为空
60         return -1.0f;
61     }
62     //与判断区域重合的物体数组
63     TArray<AActor*>OverLappingActors;
64     TriggerArea->GetOverlappingActors(OverLappingActors);
65     float TotalMass = 0.f;
66     for (AActor*Actor: OverLappingActors) {
67         TotalMass= TotalMass+Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();//得到重量
68     }
69     return TotalMass;
70 }
71 
72 void UOpenDoor::TextFunFromC() {
73     UE_LOG(LogTemp, Warning, TEXT("This is a CPP Function"));//从c++到蓝图
74 }
 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "Components/ActorComponent.h"
 6 #include"Engine/World.h"
 7 #include"GameFramework/Actor.h"
 8 #include"DrawDebugHelpers.h"
 9 #include"PhysicsEngine/PhysicsHandleComponent.h"
10 #include "Grabber.generated.h"
11 
12 UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
13 class MYCPP_API UGrabber : public UActorComponent
14 {
15     GENERATED_BODY()
16 
17 public:    
18     // Sets default values for this component's properties
19     UGrabber();
20 
21 protected:
22     // Called when the game starts
23     virtual void BeginPlay() override;
24 
25 public:    
26     // Called every frame
27     virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
28 
29 private:
30     UPROPERTY(EditAnywhere)
31     float Reach = 100.f;    //射线长度变量
32     UInputComponent*InputComponent = nullptr;//输入组件
33     UPhysicsHandleComponent*PhysicsHandle = nullptr;//物理握把组件
34     FVector GetLineStartPos(); //获取射线起点
35     FVector GetLineEndPos();  //获取射线终点
36     FHitResult GetFirstPhysicsBodyInReach(); ////获取射线碰撞的第一个信息
37     void SetPhysicsHandleComponent(); //获取物理握把组件
38     void SetInputComponent(); //获取输入组件
39     void Grab();  //举起物体
40     void Release();  //放下物体
41 };
  1 // Fill out your copyright notice in the Description page of Project Settings.
  2 
  3 #include "MyCpp.h"
  4 #include "Grabber.h"
  5 
  6 
  7 // Sets default values for this component's properties
  8 UGrabber::UGrabber()
  9 {
 10     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
 11     // off to improve performance if you don't need them.
 12     PrimaryComponentTick.bCanEverTick = true;
 13 
 14     // ...
 15 }
 16 
 17 
 18 // Called when the game starts
 19 void UGrabber::BeginPlay()
 20 {
 21     Super::BeginPlay();
 22 
 23     SetPhysicsHandleComponent();
 24     SetInputComponent();
 25 }
 26 
 27 
 28 // Called every frame
 29 void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
 30 {
 31     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
 32 
 33     if (PhysicsHandle->GetGrabbedComponent() != nullptr)   //每帧刷新抓到物体的位置
 34         PhysicsHandle->SetTargetLocation(GetLineEndPos());
 35 }
 36 
 37 //获取物理握把组件
 38 void UGrabber::SetPhysicsHandleComponent() {
 39     PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
 40     if (PhysicsHandle == nullptr) {
 41         UE_LOG(LogTemp, Error, TEXT("PhysicsHandle Not Fund"));
 42     }
 43 }
 44 
 45 //获取输入组件
 46 void UGrabber::SetInputComponent() {
 47     InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
 48     if (InputComponent == nullptr) {
 49         UE_LOG(LogTemp, Error, TEXT("InputComponent Not Fund"));
 50     }
 51     else {
 52         InputComponent->BindAction("Grab", EInputEvent::IE_Pressed, this, &UGrabber:: Grab);
 53         InputComponent->BindAction("Grab", EInputEvent::IE_Released, this, &UGrabber::Release);
 54     }
 55 }
 56 
 57 //获取射线起点
 58 FVector UGrabber::GetLineStartPos() {
 59 
 60 FVector PlayerViewPointLocation;   //玩家位置变量
 61 FRotator PlayerViewPointRotation;  //玩家旋转变量
 62 GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(PlayerViewPointLocation, PlayerViewPointRotation); //设置玩家位置变量
 63 return PlayerViewPointLocation;
 64 }
 65 
 66 //获取射线终点
 67 FVector UGrabber::GetLineEndPos() {
 68     FVector PlayerViewPointLocation;   //玩家位置变量
 69     FRotator PlayerViewPointRotation;  //玩家旋转变量
 70     GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(PlayerViewPointLocation, PlayerViewPointRotation); //设置玩家位置变量
 71     FVector LineEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector()*Reach;  //射线结束变量
 72     return LineEnd;
 73 }
 74 
 75 //获取射线碰撞的第一个信息
 76 FHitResult UGrabber::GetFirstPhysicsBodyInReach()
 77 {
 78 
 79     /*DrawDebugLine(    //画出射线  测试
 80         GetWorld(),
 81         GetLineStartPos(),
 82         GetLineEndPos(),
 83         FColor(4, 10, 0),
 84         false,
 85         0.0f,
 86         0.0f,
 87         10.0f
 88     ); */
 89     FCollisionQueryParams QueryParameter = FCollisionQueryParams("", false, GetOwner());  //设置射线检测模式
 90 
 91     FHitResult HitResult;  //射线检测到的目标
 92     GetWorld()->LineTraceSingleByObjectType(  //应用射线检测
 93         HitResult,
 94         GetLineStartPos(),
 95         GetLineEndPos(),
 96         FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
 97         QueryParameter
 98     );
 99     return HitResult;
100 }
101 
102 //举起物体
103 void UGrabber::Grab() {
104     FHitResult HitResult = GetFirstPhysicsBodyInReach();
105     UPrimitiveComponent*ComponentToGrab = HitResult.GetComponent();
106     if (HitResult.GetActor() != nullptr) {
107     PhysicsHandle->GrabComponentAtLocationWithRotation(
108         ComponentToGrab,
109         NAME_None,
110         HitResult.GetActor()->GetActorLocation(),
111         HitResult.GetActor()->GetActorRotation()
112     );
113 }
114 }
115 
116 //放下物体
117 void UGrabber::Release() {
118     if (PhysicsHandle->GetGrabbedComponent() != nullptr)
119     {
120         PhysicsHandle->ReleaseComponent();
121     }
122 }
 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "Components/ActorComponent.h"
 7 #include "GameFramework/Actor.h"
 8 #include "MyActorComponentDesk.generated.h"
 9 
10 UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
11 class MYCPP_API UMyActorComponentDesk : public UActorComponent
12 {
13     GENERATED_BODY()
14 
15 public:    
16     // Sets default values for this component's properties
17     UMyActorComponentDesk();
18 
19 protected:
20     // Called when the game starts
21     virtual void BeginPlay() override;
22 
23 public:    
24     // Called every frame
25     virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
26 
27         
28     
29 };
 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #include "MyCpp.h"
 4 #include "MyActorComponentDesk.h"
 5 
 6 
 7 // Sets default values for this component's properties
 8 UMyActorComponentDesk::UMyActorComponentDesk()
 9 {
10     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
11     // off to improve performance if you don't need them.
12     PrimaryComponentTick.bCanEverTick = true;
13 
14     // ...
15 }
16 // Called when the game starts
17 void UMyActorComponentDesk::BeginPlay()
18 {
19     Super::BeginPlay();
20 
21 
22     AActor*Owner = GetOwner();//获得Owner
23     FString Name = Owner->GetName();//获取名字
24     FVector Location = Owner->GetActorLocation();//获得位置坐标
25     UE_LOG(LogTemp, Warning, TEXT("Name:%s Location:%s"), *Name,*Location.ToString());//打印坐标
26     FRotator NewRotation = FRotator(0.0f, 90.0f, 0.0f);//设置旋转变量
27     Owner->SetActorRotation(NewRotation);//设置旋转量
28 
29 
30 }
31 // Called every frame
32 void UMyActorComponentDesk::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
33 {
34     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
35 
36     // ...
37 }

 

posted on 2017-12-03 10:53  Doctor_uee  阅读(643)  评论(0编辑  收藏  举报

导航