Advertisement
Guest User

Default APlayerCharacter

a guest
Jan 14th, 2015
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | None | 0 0
  1. // Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
  2.  
  3. #include "TemplatePlatformer.h"
  4. #include "TemplatePlatformerCharacter.h"
  5. #include "PaperFlipbookComponent.h"
  6.  
  7. //////////////////////////////////////////////////////////////////////////
  8. // ATemplatePlatformerCharacter
  9.  
  10. ATemplatePlatformerCharacter::ATemplatePlatformerCharacter(const FObjectInitializer& ObjectInitializer)
  11.     : Super(ObjectInitializer)
  12. {
  13.     // Setup the assets
  14.     struct FConstructorStatics
  15.     {
  16.         ConstructorHelpers::FObjectFinderOptional<UPaperFlipbook> RunningAnimationAsset;
  17.         ConstructorHelpers::FObjectFinderOptional<UPaperFlipbook> IdleAnimationAsset;
  18.         FConstructorStatics()
  19.             : RunningAnimationAsset(TEXT("/Game/Sprites/RunningAnimation.RunningAnimation"))
  20.             , IdleAnimationAsset(TEXT("/Game/Sprites/IdleAnimation.IdleAnimation"))
  21.         {
  22.         }
  23.     };
  24.     static FConstructorStatics ConstructorStatics;
  25.  
  26.     RunningAnimation = ConstructorStatics.RunningAnimationAsset.Get();
  27.     IdleAnimation = ConstructorStatics.IdleAnimationAsset.Get();
  28.     GetSprite()->SetFlipbook(IdleAnimation);
  29.  
  30.     // Use only Yaw from the controller and ignore the rest of the rotation.
  31.     bUseControllerRotationPitch = false;
  32.     bUseControllerRotationYaw = true;
  33.     bUseControllerRotationRoll = false;
  34.  
  35.     // Set the size of our collision capsule.
  36.     GetCapsuleComponent()->SetCapsuleHalfHeight(96.0f);
  37.     GetCapsuleComponent()->SetCapsuleRadius(40.0f);
  38.  
  39.     // Create a camera boom attached to the root (capsule)
  40.     CameraBoom = ObjectInitializer.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("CameraBoom"));
  41.     CameraBoom->AttachTo(RootComponent);
  42.     CameraBoom->TargetArmLength = 500.0f;
  43.     CameraBoom->SocketOffset = FVector(0.0f, 0.0f, 75.0f);
  44.     CameraBoom->bAbsoluteRotation = true;
  45.     CameraBoom->RelativeRotation = FRotator(0.0f, -90.0f, 0.0f);
  46.  
  47.     // Create an orthographic camera (no perspective) and attach it to the boom
  48.     SideViewCameraComponent = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("SideViewCamera"));
  49.     SideViewCameraComponent->ProjectionMode = ECameraProjectionMode::Perspective;
  50.     SideViewCameraComponent->OrthoWidth = 2048.0f;
  51.     SideViewCameraComponent->AttachTo(CameraBoom, USpringArmComponent::SocketName);
  52.  
  53.     // Prevent all automatic rotation behavior on the camera, character, and camera component
  54.     CameraBoom->bAbsoluteRotation = true;
  55.     SideViewCameraComponent->bUsePawnControlRotation = false;
  56.     GetCharacterMovement()->bOrientRotationToMovement = false;
  57.  
  58.     // Configure character movement
  59.     GetCharacterMovement()->GravityScale = 2.0f;
  60.     GetCharacterMovement()->AirControl = 0.80f;
  61.     GetCharacterMovement()->JumpZVelocity = 1000.f;
  62.     GetCharacterMovement()->GroundFriction = 3.0f;
  63.     GetCharacterMovement()->MaxWalkSpeed = 600.0f;
  64.     GetCharacterMovement()->MaxFlySpeed = 600.0f;
  65.  
  66.     // Lock character motion onto the XZ plane, so the character can't move in or out of the screen
  67.     GetCharacterMovement()->bConstrainToPlane = true;
  68.     GetCharacterMovement()->SetPlaneConstraintNormal(FVector(0.0f, -1.0f, 0.0f));
  69.  
  70.     // Behave like a traditional 2D platformer character, with a flat bottom instead of a curved capsule bottom
  71.     // Note: This can cause a little floating when going up inclines; you can choose the tradeoff between better
  72.     // behavior on the edge of a ledge versus inclines by setting this to true or false
  73.     GetCharacterMovement()->bUseFlatBaseForFloorChecks = true;
  74.  
  75.     // Enable replication on the Sprite component so animations show up when networked
  76.     GetSprite()->SetIsReplicated(true);
  77.     bReplicates = true;
  78. }
  79.  
  80. //////////////////////////////////////////////////////////////////////////
  81. // Animation
  82.  
  83. void ATemplatePlatformerCharacter::UpdateAnimation()
  84. {
  85.     const FVector PlayerVelocity = GetVelocity();
  86.     const float PlayerSpeed = PlayerVelocity.Size();
  87.  
  88.     // Are we moving or standing still?
  89.     UPaperFlipbook* DesiredAnimation = (PlayerSpeed > 0.0f) ? RunningAnimation : IdleAnimation;
  90.  
  91.     GetSprite()->SetFlipbook(DesiredAnimation);
  92. }
  93.  
  94. //////////////////////////////////////////////////////////////////////////
  95. // Input
  96.  
  97. //void ATemplatePlatformerCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  98. //{
  99. //  // Note: the 'Jump' action and the 'MoveRight' axis are bound to actual keys/buttons/sticks in DefaultInput.ini (editable from Project Settings..Input)
  100. //  /*InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  101. //  InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  102. //  InputComponent->BindAxis("MoveRight", this, &ATemplatePlatformerCharacter::MoveRight);
  103. //
  104. //  InputComponent->BindTouch(IE_Pressed, this, &ATemplatePlatformerCharacter::TouchStarted);
  105. //  InputComponent->BindTouch(IE_Released, this, &ATemplatePlatformerCharacter::TouchStopped);*/
  106. //}
  107.  
  108. void ATemplatePlatformerCharacter::MoveRight(float Value)
  109. {
  110.     // Update animation to match the motion
  111.     UpdateAnimation();
  112.  
  113.     // Set the rotation so that the character faces his direction of travel.
  114.     if (Controller != nullptr)
  115.     {
  116.         if (Value < 0.0f)
  117.         {
  118.             Controller->SetControlRotation(FRotator(0.0, 180.0f, 0.0f));
  119.         }
  120.         else if (Value > 0.0f)
  121.         {
  122.             Controller->SetControlRotation(FRotator(0.0f, 0.0f, 0.0f));
  123.         }
  124.     }
  125.  
  126.     // Apply the input to the character motion
  127.     AddMovementInput(FVector(1.0f, 0.0f, 0.0f), Value);
  128. }
  129.  
  130. void ATemplatePlatformerCharacter::TouchStarted(const ETouchIndex::Type FingerIndex, const FVector Location)
  131. {
  132.     // jump on any touch
  133.     Jump();
  134. }
  135.  
  136. void ATemplatePlatformerCharacter::TouchStopped(const ETouchIndex::Type FingerIndex, const FVector Location)
  137. {
  138.     StopJumping();
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement