File size: 2,212 Bytes
511a267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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<ASingularityGameMode>())
    {
        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<USingularitySpacetimeBridge>(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;
}