Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
- #include "Engine.h"
- #include "Test.h"
- #include "TestCharacter.h"
- //////////////////////////////////////////////////////////////////////////
- // ATestCharacter
- ATestCharacter::ATestCharacter()
- {
- // Set size for collision capsule
- GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
- // set our turn rates for input
- BaseTurnRate = 45.f;
- BaseLookUpRate = 45.f;
- /**/
- // Don't rotate when the controller rotates. Let that just affect the camera.
- bUseControllerRotationPitch = false;
- bUseControllerRotationYaw = false;
- bUseControllerRotationRoll = false;
- // Configure character movement
- GetCharacterMovement()->bOrientRotationToMovement = false; // Character moves in the direction of input...
- GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
- GetCharacterMovement()->JumpZVelocity = 600.f;
- GetCharacterMovement()->AirControl = 0.2f;
- // Create a camera boom (pulls in towards the player if there is a collision)
- CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
- CameraBoom->AttachTo(RootComponent);
- CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
- CameraBoom->bUsePawnControlRotation = false; // Rotate the arm based on the controller
- CameraBoom->bInheritYaw = false;
- CameraBoom->bInheritRoll = false;
- CameraBoom->bInheritPitch = false;
- // Create a follow camera
- FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
- FollowCamera->AttachTo(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
- FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
- // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
- // are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
- bRightMouseDown = false;
- APlayerController *PlayerController = Cast<APlayerController>(GetController());
- if (PlayerController)
- {
- PlayerController->bEnableClickEvents = true;
- PlayerController->bEnableTouchEvents = true;
- PlayerController->bEnableMouseOverEvents = true;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- // Input
- void ATestCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
- {
- // Set up gameplay key bindings
- check(InputComponent);
- // add mouse right click event
- InputComponent->BindAction("RightMouse", IE_Pressed, this, &ATestCharacter::RightMousePressed);
- InputComponent->BindAction("RightMouse", IE_Released, this, &ATestCharacter::RightMouseReleased);
- InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
- InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
- InputComponent->BindAxis("MoveForward", this, &ATestCharacter::MoveForward);
- InputComponent->BindAxis("Rotate", this, &ATestCharacter::Rotate);
- InputComponent->BindAxis("LookHorizontal", this, &ATestCharacter::TurnAtRate);
- InputComponent->BindAxis("LookVertical", this, &ATestCharacter::LookUpAtRate);
- InputComponent->BindAction("MouseWheelDown", IE_Pressed, this, &ATestCharacter::MouseWheelDown);
- InputComponent->BindAction("MouseWheelUp", IE_Released, this, &ATestCharacter::MouseWheelUp);
- InputComponent->BindAxis("StrafeRight", this, &ATestCharacter::StrafeRight);
- MaxBoomLength = 1000.0f;
- MinBoomLength = 50.0f;
- ZoomAmount = 20.0;
- }
- void ATestCharacter::Rotate(float Rate)
- {
- auto NewRotation = FRotator(0.0f, Rate, 0.0f);
- CameraBoom->AddWorldRotation(NewRotation);
- GetController()->SetControlRotation(FRotator(0.0f, CameraBoom->RelativeRotation.Yaw, 0.0f));
- }
- void ATestCharacter::MouseWheelUp()
- {
- float NewArmLength = CameraBoom->TargetArmLength;
- float ZoomSensitivity = 1.0f;// NewArmLength / 100.0f;
- NewArmLength -= (ZoomAmount * ZoomSensitivity);
- if (NewArmLength < MinBoomLength)
- NewArmLength = MinBoomLength;
- CameraBoom->TargetArmLength = NewArmLength; // The camera follows at this distance behind the character
- }
- void ATestCharacter::MouseWheelDown()
- {
- float NewArmLength = CameraBoom->TargetArmLength;
- float ZoomSensitivity = 1.0f;// NewArmLength / 100.0f;
- NewArmLength += (ZoomAmount * ZoomSensitivity);
- if (NewArmLength > MaxBoomLength)
- NewArmLength = MaxBoomLength;
- CameraBoom->TargetArmLength = NewArmLength; // The camera follows at this distance behind the character
- }
- void ATestCharacter::TurnAtRate(float Rate)
- {
- // calculate delta for this frame from the rate information
- //AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
- if (!bRightMouseDown)
- return;
- CameraBoom->AddRelativeRotation(FRotator(0.0f, Rate, 0.0f));
- // CameraBoom->AddLocalRotation(FRotator(0.0f, Rate, 0.0f));
- AddControllerYawInput(Rate);
- }
- void ATestCharacter::LookUpAtRate(float Rate)
- {
- if (!bRightMouseDown)
- return;
- CameraBoom->AddRelativeRotation(FRotator(Rate, 0.0f, 0.0f));
- //CameraBoom->AddLocalRotation(FRotator(Rate, 0.0f, 0.0f));
- AddControllerPitchInput(Rate);
- // calculate delta for this frame from the rate information
- //AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
- }
- void ATestCharacter::MoveForward(float Value)
- {
- if ((Controller != NULL) && (Value != 0.0f))
- {
- // find out which way is forward
- const FRotator Rotation = Controller->GetControlRotation();
- const FRotator YawRotation(0, Rotation.Yaw, 0);
- // get forward vector
- FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
- if (Value < 0)
- {
- Direction = GetActorForwardVector();
- Value *= 0.5f;
- }
- AddMovementInput(Direction, Value);
- }
- }
- void ATestCharacter::StrafeRight(float Value)
- {
- if ( (Controller != NULL) && (Value != 0.0f) )
- {
- // find out which way is right
- const FRotator Rotation = Controller->GetControlRotation();
- const FRotator YawRotation(0, Rotation.Yaw, 0);
- // get right vector
- const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
- // add movement in that direction
- AddMovementInput(Direction, Value);
- }
- }
- void ATestCharacter::RightMousePressed()
- {
- bRightMouseDown = true;
- APlayerController* PlayerController = Cast<APlayerController>(GetController());
- if (PlayerController)
- {
- FInputModeGameOnly InputMode;
- PlayerController->SetInputMode(InputMode);
- PlayerController->bEnableClickEvents = false;
- PlayerController->bEnableTouchEvents = false;
- PlayerController->bEnableMouseOverEvents = false;
- PlayerController->bShowMouseCursor = false;
- }
- }
- void ATestCharacter::RightMouseReleased()
- {
- bRightMouseDown = false;
- APlayerController* PlayerController = Cast<APlayerController>(GetController());
- if (PlayerController)
- {
- FInputModeGameAndUI InputMode;
- InputMode.SetLockMouseToViewport(true);
- InputMode.SetHideCursorDuringCapture(false);
- PlayerController->SetInputMode(InputMode);
- PlayerController->bEnableClickEvents = true;
- PlayerController->bEnableTouchEvents = true;
- PlayerController->bEnableMouseOverEvents = true;
- PlayerController->bShowMouseCursor = true;
- const ULocalPlayer* LocalPlayer = PlayerController->GetLocalPlayer();
- if (LocalPlayer && LocalPlayer->ViewportClient)
- {
- FViewport* Viewport = LocalPlayer->ViewportClient->Viewport;
- if (Viewport)
- {
- FVector2D ViewportSize;
- LocalPlayer->ViewportClient->GetViewportSize(ViewportSize);
- const int32 X = static_cast<int32>(ViewportSize.X * 0.5f);
- const int32 Y = static_cast<int32>(ViewportSize.Y * 0.5f);
- Viewport->SetMouse(X, Y);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment