Advertisement
Dieton

Bird.cpp

Nov 8th, 2023 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | Gaming | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "Pawns/Bird.h"
  4. #include "Components/CapsuleComponent.h"
  5. #include "Components/SkeletalMeshComponent.h"
  6. #include "Components/InputComponent.h"
  7. #include "EnhancedInputSubsystems.h"
  8. #include "EnhancedInputComponent.h"
  9.  
  10. // Sets default values
  11. ABird::ABird()
  12. {
  13.     // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  14.     PrimaryActorTick.bCanEverTick = true;
  15.  
  16.     //Capule Collider Value
  17.     Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
  18.     Capsule->SetCapsuleHalfHeight(20.f);
  19.     Capsule->SetCapsuleRadius(15.f);
  20.     SetRootComponent(Capsule);
  21.     //RootComponent = Capsule;
  22.  
  23.     //Skeletal Mesh Value
  24.     BirdMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("BirdMesh"));
  25.     BirdMesh->SetupAttachment(GetRootComponent());//RootComponent, Capsule,
  26.  
  27.     AutoPossessPlayer = EAutoReceiveInput::Player0;
  28. }
  29.  
  30. // Called when the game starts or when spawned
  31. void ABird::BeginPlay()
  32. {
  33.     Super::BeginPlay();
  34.  
  35.     APlayerController* PlayerController = Cast<APlayerController>(GetController());//Controller
  36.     UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());
  37.     if (PlayerController)
  38.     {
  39.         if (Subsystem)
  40.         {
  41.             Subsystem->AddMappingContext(BirdMappingContext,0);
  42.         }
  43.     }
  44.    
  45.     /*
  46. ///Avg<int32>(1,3);
  47. int32 AvgInt = Avg<int32>(1, 3);
  48.  
  49. UE_LOG(LogTemp, Warning, TEXT("Avg of 1 and 3: %d"), AvgInt);//%d = descret value for integers
  50.  
  51. float AvgFloat = Avg<float>(3.45f, 7.86f);
  52. UE_LOG(LogTemp, Warning, TEXT("Avg of 3.45 and 7.86: %f"), AvgFloat);//%f is for float
  53.  
  54.  
  55. UE_LOG(LogTemp, Warning, TEXT("Begin Play Call!"));
  56.  
  57. if (GEngine)
  58. {
  59.     GEngine->AddOnScreenDebugMessage(1, 60.f, FColor::Green, FString("Item OnScreen Message!"));
  60. }
  61.  
  62. UWorld* World = GetWorld();
  63.  
  64. //SetActorLocation(FVector(0.f, 0.f, 50.f));
  65. //SetActorRotation(FRotator(0.f,45.f,0.f));
  66.  
  67. if (World)
  68. {
  69.     DrawDebugLine(World, Location, Location+Foward * 100.f, FColor::Red,true,-1.f,0,1.f);
  70. }
  71.  
  72. if (World)
  73. {
  74.     FVector Location = GetActorLocation();
  75.     DrawDebugSphere(World, Location, 25.f, THIRTY, FColor::Red,false, 30.f);
  76. }
  77.  
  78. if (World)
  79. {
  80.     DrawDebugPoint(World,Location+Forward * 100.f, 15.f, FColor::Red, true);
  81. }
  82.  
  83. FVector Location = GetActorLocation();
  84. FVector Forward = GetActorForwardVector();
  85.  
  86. DRAW_SPHERE(Location);
  87. DRAW_LINE(Location, Location + Forward * 100.f);
  88. DRAW_POINT(Location + Forward * 100.f);
  89. DRAW_VECTOR(Location, Location + Forward * 100.f);
  90.  
  91. */
  92. }
  93.  
  94. void ABird::Move(const FInputActionValue& Value)
  95. {
  96.     const float DirectionValue = Value.Get<float>();
  97.  
  98.     if (Controller && (DirectionValue != 0.f))
  99.     {
  100.         FVector Forward = GetActorForwardVector();
  101.         AddMovementInput(Forward, DirectionValue);
  102.         UE_LOG(LogTemp, Warning, TEXT("IA_Move: %f"), DirectionValue);
  103.     }
  104. }
  105.  
  106. // Called every frame
  107. void ABird::Tick(float DeltaTime)
  108. {
  109.     Super::Tick(DeltaTime);
  110.    
  111.     /*
  112. DRAW_SPHERE_SingleFrame(GetActorLocation());
  113. DRAW_VECTOR_SingleFrame(GetActorLocation(), GetActorLocation() + GetActorForwardVector() * 100.f);
  114.  
  115. FVector AvgVector = Avg<FVector>(GetActorLocation(), FVector::ZeroVector);
  116. DRAW_POINT_SingleFrame(AvgVector);
  117. //FRotator AvgRotator = Avg<FRotator>(GetActorRotation(), FRotator::ZeroRotator);
  118. //float DeltaZ = Amplitude * FMath::Sin(RuningTime * TimeConstant); // period = 2*pi/k
  119.  
  120. //AddActorWorldOffset(FVector(0.f, 0.f, DeltaZ));
  121.  
  122. //movemnt rate in unitys of cm/s
  123. float MovementRate = 50.f;
  124. float RotationRate = 45.f;
  125.  
  126. //MovementRate * DeltatTime (cm/s) * (s/frame) = (cm/frame)
  127. AddActorWorldOffset(FVector(MovementRate * DeltaTime,0.f,0.f));
  128.  
  129. //Rotation
  130. AddActorWorldRotation(FRotator(0.f, RotationRate * DeltaTime, 0.f));
  131.  
  132.  
  133. UE_LOG(LogTemp, Warning, TEXT("Begin Play Call!: %f"), DeltaTime);
  134.  
  135. if (GEngine)
  136. {
  137.     FString Name = GetName();
  138.     FString Message1 = FString::Printf(TEXT("Item Name: %s"), *Name);
  139.     FString Message2 = FString::Printf(TEXT("DeltaTime: %f"), DeltaTime);
  140.     GEngine->AddOnScreenDebugMessage(1, 60.f, FColor::Green, Message2);
  141.  
  142.     UE_LOG(LogTemp, Warning, TEXT("Item Name: %s"), *Name);
  143. }
  144. */
  145. }
  146.  
  147. // Called to bind functionality to input
  148. void ABird::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  149. {
  150.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  151.  
  152.     if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
  153.     {
  154.         EnhancedInputComponent->BindAction(MoveAction,ETriggerEvent::Triggered,this, &ABird::Move);
  155.     }
  156. }
  157.  
  158.  
Tags: ue5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement