VRCharacterBase.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "VRCharacterBase.h"
  3. #include "Kismet/GameplayStatics.h"
  4. #include "Kismet/KismetSystemLibrary.h"
  5. #include "Components/CapsuleComponent.h"
  6. #include "VRHUDBase.h"
  7. #include "VRGroupBase.h"
  8. // Sets default values
  9. AVRCharacterBase::AVRCharacterBase(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer)
  10. {
  11. // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  12. PrimaryActorTick.bCanEverTick = true;
  13. GetCapsuleComponent()->InitCapsuleSize(50, 50);
  14. GetCapsuleComponent()->SetRelativeLocation(FVector(-240, 60, 190));
  15. GetMesh()->SetRelativeLocation(FVector(0, 0, -100));
  16. GetMesh()->SetRelativeRotation(FRotator(0, -90, 0));
  17. static ConstructorHelpers::FObjectFinder<USkeletalMesh> MeshComponentAsset(TEXT("SkeletalMesh'/Game/72/zoulu.zoulu'"));
  18. if (MeshComponentAsset.Succeeded()) {
  19. GetMesh()->SetSkeletalMeshAsset(MeshComponentAsset.Object);
  20. }
  21. FirstPersonCamera = CreateDefaultSubobject<UCameraComponent>(FName(TEXT("FirstPersonCamera")));
  22. if (FirstPersonCamera) {
  23. FirstPersonCamera->SetRelativeLocation(FVector(60, 0, 15));
  24. FirstPersonCamera->SetupAttachment(GetCapsuleComponent());
  25. }
  26. BeginWaitConnected_Timeline.SetTimelineLength(2);
  27. CurveFloat = CreateDefaultSubobject<UCurveFloat>(FName(TEXT("CurveFloat")));
  28. if (CurveFloat) {
  29. CurveFloat->FloatCurve.AddKey(0, 0);
  30. CurveFloat->FloatCurve.AddKey(2, 100);
  31. }
  32. FOnTimelineFloatStatic onTimelineUpdate;
  33. onTimelineUpdate.BindUFunction(this, FName(TEXT("OnBeginWaitConnected_TimelineUpdate")));
  34. BeginWaitConnected_Timeline.AddInterpFloat(CurveFloat, onTimelineUpdate);
  35. FOnTimelineEventStatic onTimelineFinished;
  36. onTimelineFinished.BindUFunction(this, FName(TEXT("OnBeginWaitConnected_TimelineFinished")));
  37. BeginWaitConnected_Timeline.SetTimelineFinishedFunc(onTimelineFinished);
  38. }
  39. // Called when the game starts or when spawned
  40. void AVRCharacterBase::BeginPlay()
  41. {
  42. Super::BeginPlay();
  43. }
  44. // Called every frame
  45. void AVRCharacterBase::Tick(float DeltaTime)
  46. {
  47. Super::Tick(DeltaTime);
  48. BeginWaitConnected_Timeline.TickTimeline(DeltaTime);
  49. }
  50. // Called to bind functionality to input
  51. void AVRCharacterBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  52. {
  53. Super::SetupPlayerInputComponent(PlayerInputComponent);
  54. PlayerInputComponent->BindAxis("MoveForward", this, &AVRCharacterBase::MoveForward);
  55. PlayerInputComponent->BindAxis("MoveRight", this, &AVRCharacterBase::MoveRight);
  56. PlayerInputComponent->BindAxis("Turn", this, &AVRCharacterBase::Turn);
  57. PlayerInputComponent->BindAxis("LookUp", this, &AVRCharacterBase::LookUp);
  58. PlayerInputComponent->BindAction("BeginWaitConnected", EInputEvent::IE_Pressed, this, &AVRCharacterBase::BeginWaitConnected);
  59. PlayerInputComponent->BindAction("TestButton", EInputEvent::IE_Pressed, this, &AVRCharacterBase::Test);
  60. }
  61. void AVRCharacterBase::MoveForward(float value) {
  62. FVector direction = GetActorForwardVector();
  63. AddMovementInput(direction * value);
  64. }
  65. void AVRCharacterBase::MoveRight(float value) {
  66. FVector direction = GetActorRightVector();
  67. AddMovementInput(direction * value);
  68. }
  69. void AVRCharacterBase::Turn(float value) {
  70. AddControllerYawInput(value);
  71. }
  72. void AVRCharacterBase::LookUp(float value) {
  73. FirstPersonCamera->AddRelativeRotation(FRotator(-value, 0, 0));
  74. }
  75. void AVRCharacterBase::BeginWaitConnected() {
  76. UE_LOG(LogTemp, Warning, TEXT("AVRCharacterBase::BeginWaitConnected"));
  77. BeginWaitConnected_Timeline.PlayFromStart();
  78. }
  79. void AVRCharacterBase::CreateGroup() {
  80. if (IsInGroup()) {
  81. UE_LOG(LogTemp, Warning, TEXT("已在阵列中"));
  82. return;
  83. }
  84. GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
  85. if (GroupClass == nullptr) GroupClass = AVRGroupBase::StaticClass();
  86. AVRGroupBase* group = static_cast<AVRGroupBase*>(GetWorld()->SpawnActor(GroupClass));
  87. group->SetActorTransform(GetActorTransform());
  88. AttachToActor(group, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true), NAME_None);
  89. AddActorLocalOffset(FVector(0, 0, 0));
  90. SetActorRotation(FRotator(0, 0, 0));
  91. }
  92. bool AVRCharacterBase::IsInGroup() {
  93. AActor* parent = GetAttachParentActor();
  94. return parent != nullptr;
  95. }
  96. void AVRCharacterBase::OnBeginWaitConnected_TimelineUpdate(float value) {
  97. UE_LOG(LogTemp, Warning, TEXT("AVRCharacterBase::OnTimelineUpdate: %f"), value);
  98. AVRHUDBase* hud = Cast<AVRHUDBase>(UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetHUD());
  99. hud->UpdateProgress((int)value);
  100. }
  101. void AVRCharacterBase::OnBeginWaitConnected_TimelineFinished() {
  102. UE_LOG(LogTemp, Warning, TEXT("AVRCharacterBase::OnTimelineFinished"));
  103. AVRHUDBase* hud = Cast<AVRHUDBase>(UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetHUD());
  104. hud->UpdateProgress(100);
  105. CreateGroup();
  106. }
  107. void AVRCharacterBase::Test() {
  108. UGameInstance *gameInstance = GetGameInstance();
  109. }