// Fill out your copyright notice in the Description page of Project Settings. #include "VRCharacterBase.h" #include "Kismet/GameplayStatics.h" #include "Kismet/KismetSystemLibrary.h" #include "Components/CapsuleComponent.h" #include "VRHUDBase.h" #include "VRGroupBase.h" static FName FirstPersonCameraName(TEXT("FirstPersonCamera")); // Sets default values AVRCharacterBase::AVRCharacterBase(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer) { // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; GetCapsuleComponent()->InitCapsuleSize(50, 50); GetCapsuleComponent()->SetRelativeLocation(FVector(-240, 60, 190)); GetMesh()->SetRelativeLocation(FVector(0, 0, -100)); GetMesh()->SetRelativeRotation(FRotator(0, -90, 0)); FirstPersonCamera = CreateDefaultSubobject(FirstPersonCameraName); if (FirstPersonCamera) { FirstPersonCamera->SetRelativeLocation(FVector(60, 0, 15)); FirstPersonCamera->SetupAttachment(GetCapsuleComponent()); } BeginWaitConnected_Timeline.SetTimelineLength(2); CurveFloat = CreateDefaultSubobject(FName("CurveFloat")); if (CurveFloat) { CurveFloat->FloatCurve.AddKey(0, 0); CurveFloat->FloatCurve.AddKey(2, 100); } FOnTimelineFloatStatic onTimelineUpdate; onTimelineUpdate.BindUFunction(this, FName("OnBeginWaitConnected_TimelineUpdate")); BeginWaitConnected_Timeline.AddInterpFloat(CurveFloat, onTimelineUpdate); FOnTimelineEventStatic onTimelineFinished; onTimelineFinished.BindUFunction(this, FName("OnBeginWaitConnected_TimelineFinished")); BeginWaitConnected_Timeline.SetTimelineFinishedFunc(onTimelineFinished); } // Called when the game starts or when spawned void AVRCharacterBase::BeginPlay() { Super::BeginPlay(); } // Called every frame void AVRCharacterBase::Tick(float DeltaTime) { Super::Tick(DeltaTime); BeginWaitConnected_Timeline.TickTimeline(DeltaTime); } // Called to bind functionality to input void AVRCharacterBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); PlayerInputComponent->BindAxis("MoveForward", this, &AVRCharacterBase::MoveForward); PlayerInputComponent->BindAxis("MoveRight", this, &AVRCharacterBase::MoveRight); PlayerInputComponent->BindAxis("Turn", this, &AVRCharacterBase::Turn); PlayerInputComponent->BindAxis("LookUp", this, &AVRCharacterBase::LookUp); PlayerInputComponent->BindAction("BeginWaitConnected", EInputEvent::IE_Pressed, this, &AVRCharacterBase::BeginWaitConnected); PlayerInputComponent->BindAction("TestButton", EInputEvent::IE_Pressed, this, &AVRCharacterBase::Test); } void AVRCharacterBase::MoveForward(float value) { FVector direction = GetActorForwardVector(); AddMovementInput(direction * value); } void AVRCharacterBase::MoveRight(float value) { FVector direction = GetActorRightVector(); AddMovementInput(direction * value); } void AVRCharacterBase::Turn(float value) { AddControllerYawInput(value); } void AVRCharacterBase::LookUp(float value) { FirstPersonCamera->AddRelativeRotation(FRotator(-value, 0, 0)); } void AVRCharacterBase::BeginWaitConnected() { UE_LOG(LogTemp, Warning, TEXT("AVRCharacterBase::BeginWaitConnected")); BeginWaitConnected_Timeline.PlayFromStart(); } void AVRCharacterBase::CreateGroup() { if (IsInGroup()) { UE_LOG(LogTemp, Warning, TEXT("已在阵列中")); return; } GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap); AVRGroupBase* group = GetWorld()->SpawnActor(); group->SetActorTransform(GetActorTransform()); AttachToActor(group, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true), NAME_None); AddActorLocalOffset(FVector(0, 0, 0)); SetActorRotation(FRotator(0, 0, 0)); } bool AVRCharacterBase::IsInGroup() { AActor* parent = GetAttachParentActor(); return parent != nullptr; } void AVRCharacterBase::OnBeginWaitConnected_TimelineUpdate(float value) { UE_LOG(LogTemp, Warning, TEXT("AVRCharacterBase::OnTimelineUpdate: %f"), value); AVRHUDBase* hud = Cast(UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetHUD()); hud->UpdateProgress((int)value); } void AVRCharacterBase::OnBeginWaitConnected_TimelineFinished() { UE_LOG(LogTemp, Warning, TEXT("AVRCharacterBase::OnTimelineFinished")); AVRHUDBase* hud = Cast(UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetHUD()); hud->UpdateProgress(100); CreateGroup(); } void AVRCharacterBase::Test() { UGameInstance *gameInstance = GetGameInstance(); }