#include "SingularityRolePlayerController.h" #include "Components/InputComponent.h" #include "Engine/World.h" #include "SingularityGameMode.h" #include "SingularitySpacetimeBridge.h" ASingularityRolePlayerController::ASingularityRolePlayerController() { PrimaryActorTick.bCanEverTick = true; } void ASingularityRolePlayerController::BeginPlay() { Super::BeginPlay(); if (const ASingularityGameMode* Mode = GetWorld()->GetAuthGameMode()) { SpacetimeBridge = Mode->SpacetimeBridge; } if (!SpacetimeBridge) { // Clients do not own an authoritative GameMode, so they get a local // bridge instance that their transport adapter can bind to. SpacetimeBridge = NewObject(this); } } void ASingularityRolePlayerController::SetupInputComponent() { Super::SetupInputComponent(); check(InputComponent); InputComponent->BindAxis(TEXT("Singularity_MoveX"), this, &ASingularityRolePlayerController::HandleMoveX); InputComponent->BindAxis(TEXT("Singularity_MoveZ"), this, &ASingularityRolePlayerController::HandleMoveZ); InputComponent->BindAction(TEXT("Singularity_Action"), IE_Pressed, this, &ASingularityRolePlayerController::HandleActionPressed); InputComponent->BindAction(TEXT("Singularity_Action"), IE_Released, this, &ASingularityRolePlayerController::HandleActionReleased); } void ASingularityRolePlayerController::Tick(float DeltaSeconds) { Super::Tick(DeltaSeconds); if (!IsLocalController()) return; SendAccumulator += DeltaSeconds; if (SpacetimeBridge && SendAccumulator >= (1.0f / 30.0f)) { SendAccumulator = 0.0f; ++ClientTick; SpacetimeBridge->SubmitRoleInput(ControlledRole, MoveX, MoveZ, bAction, ClientTick); } } void ASingularityRolePlayerController::HandleMoveX(float Value) { MoveX = FMath::Clamp(Value, -1.0f, 1.0f); } void ASingularityRolePlayerController::HandleMoveZ(float Value) { MoveZ = FMath::Clamp(Value, -1.0f, 1.0f); } void ASingularityRolePlayerController::HandleActionPressed() { bAction = true; } void ASingularityRolePlayerController::HandleActionReleased() { bAction = false; }