# myue521Character.h #
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "myue521Character.generated.h"
UCLASS(config=Game)
class Amyue521Character : public ACharacter
{
public:
void NotifyServer();
virtual void myJump();
public:
FString Str = TEXT("walawala");
FString topicLogKey = TEXT("log");
FString walkLogKey = TEXT("walk");
FString Forward = TEXT("Forward");
FString Right = TEXT("Right");
void TriggeredBySocketMsg(FString jsonString);
void doMyForward(int f);
void doMyRight(int r);
};
# myue521Character.cpp #
#include "myue521Character.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "UObject/ConstructorHelpers.h"
#include "UWebSocketGameInstance.h"
void Amyue521Character::myJump()
{
bPressedJump = true;
JumpKeyHoldTime = 0.0f;
this->NotifyServer();
}
void Amyue521Character::Look(const FInputActionValue& Value)
{
FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
void Amyue521Character::NotifyServer() {
UUWebSocketGameInstance* GameInstance = Cast<UUWebSocketGameInstance>(GetGameInstance());
if (GameInstance) {
if (GameInstance->WebSocket->IsConnected()) {
GameInstance->WebSocket->Send("FROM UnrealEngine 5");
}
}
}
void Amyue521Character::TriggeredBySocketMsg(FString jsonString)
{
TSharedRef< TJsonReader<> > Reader = TJsonReaderFactory<>::Create(jsonString);
TSharedPtr<FJsonObject> Root;
if (FJsonSerializer::Deserialize(Reader, Root))
{
if (Root->HasField(TEXT("Topic"))) {
FString Topic = Root->GetStringField(TEXT("Topic"));
TSharedPtr<FJsonObject, ESPMode::ThreadSafe> Data = Root->GetObjectField(TEXT("Data"));
FString KeyField = Data->GetStringField("Key");
FString ValueField = Data->GetStringField("Value");
if (this->topicLogKey.Equals(KeyField, ESearchCase::IgnoreCase)) {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, ValueField);
}else if (this->walkLogKey.Equals(KeyField, ESearchCase::IgnoreCase)) {
FString DirectionField = Data->GetStringField("Direction");
if (this->Forward.Equals(DirectionField, ESearchCase::IgnoreCase)) {
this->doMyForward(FCString::Atoi(*ValueField));
}else if (this->Right.Equals(Right, ESearchCase::IgnoreCase)) {
this->doMyRight(FCString::Atoi(*ValueField));
}
}
else {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "1-Key: " + KeyField + ",Value:" + ValueField);
}
}
else {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "解析json异常");
}
}
}
void Amyue521Character::doMyForward(int f)
{
FVector2D MovementVector = FInputActionValue::Axis2D(0, f);
if (Controller != nullptr)
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void Amyue521Character::doMyRight(int r)
{
FVector2D MovementVector = FInputActionValue::Axis2D(r, 0);
if (Controller != nullptr)
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}