VRCharacterBase.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. static FName FirstPersonCameraName(TEXT("FirstPersonCamera"));
  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. FirstPersonCamera = CreateDefaultSubobject<UCameraComponent>(FirstPersonCameraName);
  18. if (FirstPersonCamera) {
  19. FirstPersonCamera->SetRelativeLocation(FVector(60, 0, 15));
  20. FirstPersonCamera->SetupAttachment(GetCapsuleComponent());
  21. }
  22. MyTimeline.SetTimelineLength(2);
  23. MyCurveFloat = CreateDefaultSubobject<UCurveFloat>(FName("MyCurveFloat"));
  24. if (MyCurveFloat) {
  25. MyCurveFloat->FloatCurve.AddKey(0, 0);
  26. MyCurveFloat->FloatCurve.AddKey(2, 100);
  27. }
  28. FOnTimelineFloatStatic onTimelineUpdate;
  29. onTimelineUpdate.BindUFunction(this, FName("OnTimelineUpdate"));
  30. MyTimeline.AddInterpFloat(MyCurveFloat, onTimelineUpdate);
  31. FOnTimelineEventStatic onTimelineFinished;
  32. onTimelineFinished.BindUFunction(this, FName("OnTimelineFinished"));
  33. MyTimeline.SetTimelineFinishedFunc(onTimelineFinished);
  34. }
  35. // Called when the game starts or when spawned
  36. void AVRCharacterBase::BeginPlay()
  37. {
  38. Super::BeginPlay();
  39. }
  40. // Called every frame
  41. void AVRCharacterBase::Tick(float DeltaTime)
  42. {
  43. Super::Tick(DeltaTime);
  44. MyTimeline.TickTimeline(DeltaTime);
  45. }
  46. // Called to bind functionality to input
  47. void AVRCharacterBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  48. {
  49. Super::SetupPlayerInputComponent(PlayerInputComponent);
  50. PlayerInputComponent->BindAxis("MoveForward", this, &AVRCharacterBase::MoveForward);
  51. PlayerInputComponent->BindAxis("MoveRight", this, &AVRCharacterBase::MoveRight);
  52. PlayerInputComponent->BindAxis("Turn", this, &AVRCharacterBase::Turn);
  53. PlayerInputComponent->BindAxis("LookUp", this, &AVRCharacterBase::LookUp);
  54. PlayerInputComponent->BindAction("BeginWaitConnected", EInputEvent::IE_Pressed, this, &AVRCharacterBase::BeginWaitConnected);
  55. PlayerInputComponent->BindAction("TestButton", EInputEvent::IE_Pressed, this, &AVRCharacterBase::Test);
  56. }
  57. void AVRCharacterBase::MoveForward(float value) {
  58. FVector direction = GetActorForwardVector();
  59. AddMovementInput(direction * value);
  60. }
  61. void AVRCharacterBase::MoveRight(float value) {
  62. FVector direction = GetActorRightVector();
  63. AddMovementInput(direction * value);
  64. }
  65. void AVRCharacterBase::Turn(float value) {
  66. AddControllerYawInput(value);
  67. }
  68. void AVRCharacterBase::LookUp(float value) {
  69. FirstPersonCamera->AddRelativeRotation(FRotator(-value, 0, 0));
  70. }
  71. void AVRCharacterBase::BeginWaitConnected() {
  72. UE_LOG(LogTemp, Warning, TEXT("AVRCharacterBase::BeginWaitConnected"));
  73. MyTimeline.PlayFromStart();
  74. }
  75. void AVRCharacterBase::OnTimelineUpdate(float value) {
  76. UE_LOG(LogTemp, Warning, TEXT("AVRCharacterBase::OnTimelineUpdate: %f"), value);
  77. }
  78. void AVRCharacterBase::OnTimelineFinished() {
  79. UE_LOG(LogTemp, Warning, TEXT("AVRCharacterBase::OnTimelineFinished"));
  80. }
  81. void AVRCharacterBase::Test() {
  82. UGameInstance *gameInstance = GetGameInstance();
  83. AVRHUDBase* hud = Cast<AVRHUDBase>(UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetHUD());
  84. hud->Test();
  85. }