2022/03/29 360도 캐릭터 백업

2022. 3. 29. 20:51·Unreal Engine 4/Unreal Engine 4
반응형

.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include <Components/WidgetComponent.h>
#include <Engine/Public/TimerManager.h>
#include <Components/SkeletalMeshComponent.h>
#include "../../Server/Net_ServerCore.h"
#include "CC_KangnamMain.generated.h"

class ANet_ServerCore;

USTRUCT()
struct FStoC
{
   GENERATED_BODY()

   FVector Location;
   FVector Velocity;
   float Direction;
};

/**
 * 
 */
UCLASS()
class KANGNAMSQUARE_API ACC_KangnamMain : public ACharacter
{
   GENERATED_BODY()

public:
   ACC_KangnamMain();

   virtual void Destroyed() override;
   
   void Server_VisibleAreaMove();
   void Client_VisibleAreaMove(FVector,FVector,float);
   
public:
   //서버에 보내기 위함
   void SetMoveableTimer();
   //패킷을 받기위함(서버로부터)
   FStoC StoC;
   
   UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
   float BaseTurnRate;
   UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
   float BaseLookUpRate;
      
protected:
   // Called when the game starts or when spawned
   virtual void BeginPlay() override;
       // Called every frame
   virtual void Tick(float DeltaTime) override;
   // Called to bind functionality to input
   virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
          
   void MoveForward(float Value);
   bool IsMoveForward;
   void MoveRight(float Value);
   bool IsMoveRight;
   
      enum class EViewMode
       {
          TopView,
          TPerson_View,
          FPerson_View
       };
    
       void ChangeViewMode();void SetViewMode(EViewMode NewViewMode);
   EViewMode CurrentViewMode = EViewMode::TopView;

   struct TouchData
   {
      TouchData() { bIsPressed = false; Location = FVector::ZeroVector; bMoved=false; }
      bool bIsPressed;
      ETouchIndex::Type FingerIndex;
      FVector Location;
      bool bMoved;
   };
   TouchData  TouchItem;
   void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location);
   void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location);
   void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location);
   void TryEnableTouchscreenMovement(UInputComponent* InputComponent);
   
private:
   UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera, meta=(AllowPrivateAccess))
   class USpringArmComponent* CameraArm;
          
   UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera, meta=(AllowPrivateAccess))
   class UCameraComponent* MainCamera;

   UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=widget, meta=(AllowPrivateAccess="true"))
   class UWidgetComponent* _chatWidget;

public:
   UPROPERTY()
    ANet_ServerCore* _lobbyServer;

public:
    void ShowChat(FString chat);
    
    UPROPERTY()
    FTimerHandle waitHandle;

   UPROPERTY()
   FTimerHandle CUpdateHandle;
   bool UpdateLoop;

   UPROPERTY()
   FTimerHandle FMoveTimer;

   UPROPERTY()
   FTimerHandle RMoveTimer;
};

.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "CC_KangnamMain.h"
#include "Components/CapsuleComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "../../Widget/W_ShowChat.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include <Kismet/GameplayStatics.h>
#include <UObject/ConstructorHelpers.h>
#include "Engine/Engine.h"

ACC_KangnamMain::ACC_KangnamMain()
{
   // 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(42.f,96.f);
   GetCapsuleComponent()->SetCollisionProfileName(TEXT("CC_Character"));

   _chatWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("ChatShowWidget"));
   _chatWidget->SetupAttachment(RootComponent);
   
   CameraArm=CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraArm"));
   CameraArm->SetupAttachment(RootComponent);
   
   MainCamera=CreateDefaultSubobject<UCameraComponent>(TEXT("MainCamera"));
   MainCamera->SetupAttachment(CameraArm,USpringArmComponent::SocketName);
   BaseTurnRate = 45.f;
   BaseLookUpRate = 45.f;
   SetViewMode(EViewMode::TPerson_View);
   IsMoveForward=false;
   IsMoveRight=false;
   UpdateLoop=true;
   
   static ConstructorHelpers::FObjectFinder<USkeletalMesh> Player_Mesh(TEXT("/Game/MainFolder/BP/Character/CharacterMesh/20220315_woman_rigged_1"));
   if(Player_Mesh.Succeeded())
   {
      GetMesh()->SetSkeletalMesh(Player_Mesh.Object);
   }
   GetMesh()->SetRelativeLocationAndRotation(FVector(0.f,0.f,-88.f),FRotator(0.f,270.f,0.f));

   /*
   GetMesh()->SetAnimationMode(EAnimationMode::AnimationBlueprint);
   static ConstructorHelpers::FClassFinder<UAnimInstance> Player_Anim(TEXT(""));
   if(Player_Anim.Succeeded())
   {
      GetMesh()->SetAnimInstanceClass(Player_Anim.Class);
   }
   */
   
   static ConstructorHelpers::FClassFinder<UUserWidget> ChatWidget(TEXT("/Game/MainFolder/Widget/BPW_ShowChat"));
   if (ChatWidget.Succeeded())
   {
      _chatWidget->SetRelativeLocation(FVector(0.f, 0.f, 100.f));
      _chatWidget->SetDrawSize(FVector2D(400.f, 50.f));
      _chatWidget->SetWidgetSpace(EWidgetSpace::Screen);
      _chatWidget->SetWidgetClass(ChatWidget.Class);
      _chatWidget->SetVisibility(false);
   }
}

void ACC_KangnamMain::Destroyed()
{
   Super::Destroyed();
   UpdateLoop=false;
   GetWorld()->GetTimerManager().PauseTimer(CUpdateHandle);
   GetWorld()->GetTimerManager().PauseTimer(waitHandle);
   GetWorld()->GetTimerManager().PauseTimer(FMoveTimer);
   GetWorld()->GetTimerManager().PauseTimer(RMoveTimer);
   UE_LOG(LogTemp,Warning,TEXT("J Destroy"));
}

void ACC_KangnamMain::SetMoveableTimer()
{
   float WaitTime = 0.2f;
   if(this->GetController() == GetWorld()->GetFirstPlayerController())
   {
      UE_LOG(LogTemp,Warning,TEXT("J Local Player %s %s"),*GetName(),*this->GetController()->GetName());
      if (!GetWorldTimerManager().IsTimerActive(CUpdateHandle))
      {
         GetWorld()->GetTimerManager().SetTimer(CUpdateHandle, FTimerDelegate::CreateLambda([&]()
         {
            Server_VisibleAreaMove();
         }), WaitTime, UpdateLoop);
      }
   }  
}



// Called when the game starts or when spawned
void ACC_KangnamMain::BeginPlay()
{
   Super::BeginPlay();

   _lobbyServer = Cast<ANet_ServerCore>(UGameplayStatics::GetActorOfClass(GetWorld(), ANet_ServerCore::StaticClass()));
}

// Called every frame
void ACC_KangnamMain::Tick(float DeltaTime)
{
   Super::Tick(DeltaTime);
}

// Called to bind functionality to input
void ACC_KangnamMain::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
   Super::SetupPlayerInputComponent(PlayerInputComponent);

   PlayerInputComponent->BindAction("Change_View",EInputEvent::IE_Pressed,this,&ACC_KangnamMain::ChangeViewMode);

   TryEnableTouchscreenMovement(PlayerInputComponent);
   
   PlayerInputComponent->BindAxis("MoveForward",this,&ACC_KangnamMain::MoveForward);
   PlayerInputComponent->BindAxis("MoveRight",this,&ACC_KangnamMain::MoveRight);
   PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
   PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
}

void ACC_KangnamMain::MoveForward(float Value)
{
   if(Value!=0.0f)
   {
      IsMoveForward=true;
   }
   else
   {
      IsMoveForward=false;
   }
}

void ACC_KangnamMain::MoveRight(float Value)
{
   if(Value!=0.0f)
   {
      IsMoveRight=true;
   }
   {
      IsMoveRight=false;
   }
}

void ACC_KangnamMain::Server_VisibleAreaMove()
{
    Proud::Vector3 Loc = Proud::Vector3(GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z);
    Proud::Vector3 Vel = Proud::Vector3(GetVelocity().X, GetVelocity().Y, GetVelocity().Z);
   float Dir=GetActorRotation().Yaw;

   Proud::RmiContext context;
   context.m_reliability = Proud::MessageReliability::MessageReliability_Unreliable;
   context.m_enableLoopback = false;
   _lobbyServer->_netPIDL->_lobbyC2SProxy.VisibleAreaMove(Proud::HostID_Server, context, Loc, Vel, Dir);
}

void ACC_KangnamMain::Client_VisibleAreaMove(FVector Loc, FVector Vel, float Dir)
{
   StoC.Location=Loc;
   StoC.Velocity=Vel;
   StoC.Direction=Dir;

   SetActorRotation(FRotator(0.f,Dir,0.f));
   MoveTimer(IsMoveForward,IsMoveRight);
}

//detail camera setting
void ACC_KangnamMain::SetViewMode(EViewMode NewViewMode)
{
   CurrentViewMode=NewViewMode;
   
   switch (CurrentViewMode)
   {
   case EViewMode::TopView:
      CameraArm->TargetArmLength=800.f;
      CameraArm->SetRelativeRotation(FRotator(-90.f,0.f,0.f));
      CameraArm->bUsePawnControlRotation=false;
      CameraArm->bInheritPitch=false;
      CameraArm->bInheritRoll=false;
      CameraArm->bInheritYaw=false;
      CameraArm->bDoCollisionTest=false;
      bUseControllerRotationYaw=false;
      GetCharacterMovement()->bOrientRotationToMovement=false;
      GetCharacterMovement()->bUseControllerDesiredRotation=true;
      GetCharacterMovement()->RotationRate=FRotator(0.f,360.f,0.f);;
      break;
   case EViewMode::TPerson_View:
      CameraArm->TargetArmLength=600.f;
      CameraArm->SetRelativeRotation(FRotator(0.f,0.f,-45.f));
      CameraArm->bUsePawnControlRotation=true;
      CameraArm->bInheritPitch=true;
      CameraArm->bInheritRoll=true;
      CameraArm->bInheritYaw=true;
      CameraArm->bDoCollisionTest=true;
      bUseControllerRotationYaw=true;
      GetCharacterMovement()->bOrientRotationToMovement=true;
      GetCharacterMovement()->bUseControllerDesiredRotation=false;
      GetCharacterMovement()->RotationRate=FRotator(0.f,360.f,0.f);;
      break;
   case EViewMode::FPerson_View:
      CameraArm->TargetArmLength=-100.f;
      CameraArm->SetRelativeRotation(FRotator(0.f,180.f,0.f));
      CameraArm->bUsePawnControlRotation=true;
      CameraArm->bInheritPitch=true;
      CameraArm->bInheritRoll=true;
      CameraArm->bInheritYaw=true;
      CameraArm->bDoCollisionTest=true;
      bUseControllerRotationYaw=true;
      GetCharacterMovement()->bOrientRotationToMovement=true;
      GetCharacterMovement()->bUseControllerDesiredRotation=false;
      GetCharacterMovement()->RotationRate=FRotator(0.f,360.f,0.f);
      break;
   }
}

void ACC_KangnamMain::ChangeViewMode()
{
   switch (CurrentViewMode)
   {
   case EViewMode::TopView:
      SetViewMode(EViewMode::TPerson_View);
      break;
   case EViewMode::TPerson_View:
      SetViewMode(EViewMode::FPerson_View);
      break;
   case EViewMode::FPerson_View:
      SetViewMode(EViewMode::TopView);
      break;
   }
}

void ACC_KangnamMain::ShowChat(FString chat)
{
   _chatWidget->SetVisibility(true);

   UW_ShowChat* widget = Cast<UW_ShowChat>(_chatWidget->GetUserWidgetObject());

   if (widget)
   {
      UE_LOG(LogTemp, Warning, TEXT("ShowChat"));
      widget->UpdateMessage(FText::FromString(chat));
   }

   float WaitTime = 3.f;
   if (!GetWorldTimerManager().IsTimerActive(waitHandle))
   {
      GetWorld()->GetTimerManager().SetTimer(waitHandle, FTimerDelegate::CreateLambda([&]()
         {
            _chatWidget->SetVisibility(false);
         }), WaitTime, false);
   }
   else
   {
      GetWorldTimerManager().ClearTimer(waitHandle);
      GetWorld()->GetTimerManager().SetTimer(waitHandle, FTimerDelegate::CreateLambda([&]()
         {
            _chatWidget->SetVisibility(false);
         }), WaitTime, false);
   }
}

void ACC_KangnamMain::BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
{
   if(CurrentViewMode!=EViewMode::TopView)
   {
      if (TouchItem.bIsPressed == true) {}
      else 
      {
         // Cache the finger index and touch location and flag we are processing a touch
         TouchItem.bIsPressed = true;
         TouchItem.FingerIndex = FingerIndex;
         TouchItem.Location = Location;
         TouchItem.bMoved = false;
      }
   }
}

void ACC_KangnamMain::EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
{
   if(CurrentViewMode!=EViewMode::TopView)
   {
      if((TouchItem.bIsPressed == false) || ( TouchItem.FingerIndex != FingerIndex) )
      {
         return;
      }
      TouchItem.bIsPressed = false;
   }
}

void ACC_KangnamMain::TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location)
{
   if(CurrentViewMode!=EViewMode::TopView)
   {
      if(CurrentViewMode!=EViewMode::TopView)
      {
         if ((TouchItem.bIsPressed == true) && (TouchItem.FingerIndex == FingerIndex))
         {
            if (GetWorld() != nullptr)
            {
               UGameViewportClient* ViewportClient = GetWorld()->GetGameViewport();
               if (ViewportClient != nullptr)
               {
                  FVector MoveDelta = Location - TouchItem.Location;
                  FVector2D ScreenSize;
                  ViewportClient->GetViewportSize(ScreenSize);
                  FVector2D ScaledDelta = FVector2D(MoveDelta.X, MoveDelta.Y) / ScreenSize;
                  if (FMath::Abs(ScaledDelta.X) >= (4.0f / ScreenSize.X))
                  {
                     TouchItem.bMoved = true;
                     float Value = ScaledDelta.X * BaseTurnRate;
                     AddControllerYawInput(Value);
                  }
                  if (FMath::Abs(ScaledDelta.Y) >= (4.0f / ScreenSize.Y))
                  {
                     TouchItem.bMoved = true;
                     float Value = ScaledDelta.Y* BaseTurnRate;
                     AddControllerPitchInput(Value);
                  }
                  TouchItem.Location = Location;
               }
               TouchItem.Location = Location;
            }
         }
      }
   }
}

void ACC_KangnamMain::TryEnableTouchscreenMovement(UInputComponent* PlayerInputComponent)
{
   PlayerInputComponent->BindTouch(EInputEvent::IE_Pressed, this, &ACC_KangnamMain::BeginTouch);
   PlayerInputComponent->BindTouch(EInputEvent::IE_Released, this, &ACC_KangnamMain::EndTouch);
   PlayerInputComponent->BindTouch(EInputEvent::IE_Repeat, this, &ACC_KangnamMain::TouchUpdate);  
}
반응형
저작자표시 (새창열림)

'Unreal Engine 4 > Unreal Engine 4' 카테고리의 다른 글

2022/04/12 백업본  (0) 2022.04.12
2022/04/07 로비메뉴 백업  (1) 2022.04.07
OrbitingMovement 컴포넌트 생성  (0) 2022.03.24
계층구조 만들기  (0) 2022.03.22
ConstructorHelpers를 이용한 Object 찾기와 그 에 대한 참조 획득  (0) 2022.03.22
'Unreal Engine 4/Unreal Engine 4' 카테고리의 다른 글
  • 2022/04/12 백업본
  • 2022/04/07 로비메뉴 백업
  • OrbitingMovement 컴포넌트 생성
  • 계층구조 만들기
숯불돼지왕갈비
숯불돼지왕갈비
  • 숯불돼지왕갈비
    게임 개발 공부기
    숯불돼지왕갈비
  • 전체
    오늘
    어제
    • 분류 전체보기 (302)
      • 학교수업 (165)
      • 취업강의 (6)
      • C++ (49)
        • 코딩 테스트 (4)
      • Unreal Engine 5 (25)
        • MMORPG 개발 (25)
      • Unreal Engine 4 (44)
        • Omak Project (3)
        • Unreal Engine 4 개발일지 (9)
        • Unreal Engine 4 (32)
      • Unity (1)
        • 개발 일지 (1)
      • 수학 (3)
        • 소프트웨어 공학용 수학 (3)
      • DirectX 11 (4)
      • 게임 디자인 패턴 (2)
      • 포트폴리오 (1)
      • 자격증 (1)
        • 정보처리기사 (0)
        • SQLD (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
숯불돼지왕갈비
2022/03/29 360도 캐릭터 백업
상단으로

티스토리툴바