Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
- #include "TemplatePlatformer.h"
- #include "TemplatePlatformerCharacter.h"
- #include "PaperFlipbookComponent.h"
- //////////////////////////////////////////////////////////////////////////
- // ATemplatePlatformerCharacter
- ATemplatePlatformerCharacter::ATemplatePlatformerCharacter(const FObjectInitializer& ObjectInitializer)
- : Super(ObjectInitializer)
- {
- // Setup the assets
- struct FConstructorStatics
- {
- ConstructorHelpers::FObjectFinderOptional<UPaperFlipbook> RunningAnimationAsset;
- ConstructorHelpers::FObjectFinderOptional<UPaperFlipbook> IdleAnimationAsset;
- FConstructorStatics()
- : RunningAnimationAsset(TEXT("/Game/Sprites/RunningAnimation.RunningAnimation"))
- , IdleAnimationAsset(TEXT("/Game/Sprites/IdleAnimation.IdleAnimation"))
- {
- }
- };
- static FConstructorStatics ConstructorStatics;
- RunningAnimation = ConstructorStatics.RunningAnimationAsset.Get();
- IdleAnimation = ConstructorStatics.IdleAnimationAsset.Get();
- GetSprite()->SetFlipbook(IdleAnimation);
- // Use only Yaw from the controller and ignore the rest of the rotation.
- bUseControllerRotationPitch = false;
- bUseControllerRotationYaw = true;
- bUseControllerRotationRoll = false;
- // Set the size of our collision capsule.
- GetCapsuleComponent()->SetCapsuleHalfHeight(96.0f);
- GetCapsuleComponent()->SetCapsuleRadius(40.0f);
- // Create a camera boom attached to the root (capsule)
- CameraBoom = ObjectInitializer.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("CameraBoom"));
- CameraBoom->AttachTo(RootComponent);
- CameraBoom->TargetArmLength = 500.0f;
- CameraBoom->SocketOffset = FVector(0.0f, 0.0f, 75.0f);
- CameraBoom->bAbsoluteRotation = true;
- CameraBoom->RelativeRotation = FRotator(0.0f, -90.0f, 0.0f);
- // Create an orthographic camera (no perspective) and attach it to the boom
- SideViewCameraComponent = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("SideViewCamera"));
- SideViewCameraComponent->ProjectionMode = ECameraProjectionMode::Perspective;
- SideViewCameraComponent->OrthoWidth = 2048.0f;
- SideViewCameraComponent->AttachTo(CameraBoom, USpringArmComponent::SocketName);
- // Prevent all automatic rotation behavior on the camera, character, and camera component
- CameraBoom->bAbsoluteRotation = true;
- SideViewCameraComponent->bUsePawnControlRotation = false;
- GetCharacterMovement()->bOrientRotationToMovement = false;
- // Configure character movement
- GetCharacterMovement()->GravityScale = 2.0f;
- GetCharacterMovement()->AirControl = 0.80f;
- GetCharacterMovement()->JumpZVelocity = 1000.f;
- GetCharacterMovement()->GroundFriction = 3.0f;
- GetCharacterMovement()->MaxWalkSpeed = 600.0f;
- GetCharacterMovement()->MaxFlySpeed = 600.0f;
- // Lock character motion onto the XZ plane, so the character can't move in or out of the screen
- GetCharacterMovement()->bConstrainToPlane = true;
- GetCharacterMovement()->SetPlaneConstraintNormal(FVector(0.0f, -1.0f, 0.0f));
- // Behave like a traditional 2D platformer character, with a flat bottom instead of a curved capsule bottom
- // Note: This can cause a little floating when going up inclines; you can choose the tradeoff between better
- // behavior on the edge of a ledge versus inclines by setting this to true or false
- GetCharacterMovement()->bUseFlatBaseForFloorChecks = true;
- // Enable replication on the Sprite component so animations show up when networked
- GetSprite()->SetIsReplicated(true);
- bReplicates = true;
- }
- //////////////////////////////////////////////////////////////////////////
- // Animation
- void ATemplatePlatformerCharacter::UpdateAnimation()
- {
- const FVector PlayerVelocity = GetVelocity();
- const float PlayerSpeed = PlayerVelocity.Size();
- // Are we moving or standing still?
- UPaperFlipbook* DesiredAnimation = (PlayerSpeed > 0.0f) ? RunningAnimation : IdleAnimation;
- GetSprite()->SetFlipbook(DesiredAnimation);
- }
- //////////////////////////////////////////////////////////////////////////
- // Input
- //void ATemplatePlatformerCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
- //{
- // // Note: the 'Jump' action and the 'MoveRight' axis are bound to actual keys/buttons/sticks in DefaultInput.ini (editable from Project Settings..Input)
- // /*InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
- // InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
- // InputComponent->BindAxis("MoveRight", this, &ATemplatePlatformerCharacter::MoveRight);
- //
- // InputComponent->BindTouch(IE_Pressed, this, &ATemplatePlatformerCharacter::TouchStarted);
- // InputComponent->BindTouch(IE_Released, this, &ATemplatePlatformerCharacter::TouchStopped);*/
- //}
- void ATemplatePlatformerCharacter::MoveRight(float Value)
- {
- // Update animation to match the motion
- UpdateAnimation();
- // Set the rotation so that the character faces his direction of travel.
- if (Controller != nullptr)
- {
- if (Value < 0.0f)
- {
- Controller->SetControlRotation(FRotator(0.0, 180.0f, 0.0f));
- }
- else if (Value > 0.0f)
- {
- Controller->SetControlRotation(FRotator(0.0f, 0.0f, 0.0f));
- }
- }
- // Apply the input to the character motion
- AddMovementInput(FVector(1.0f, 0.0f, 0.0f), Value);
- }
- void ATemplatePlatformerCharacter::TouchStarted(const ETouchIndex::Type FingerIndex, const FVector Location)
- {
- // jump on any touch
- Jump();
- }
- void ATemplatePlatformerCharacter::TouchStopped(const ETouchIndex::Type FingerIndex, const FVector Location)
- {
- StopJumping();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement