Advertisement
Guest User

Untitled

a guest
May 27th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.71 KB | None | 0 0
  1. //////////////////////////////////////////// Jetpack Header
  2.  
  3. class TOWER_API AJetpack : public AActor
  4. {
  5.     GENERATED_BODY()
  6.    
  7. public:
  8.     // Sets default values for this actor's properties
  9.     AJetpack(const class FObjectInitializer& OI);
  10.  
  11.     // Called when the game starts or when spawned
  12.     virtual void BeginPlay() override;
  13.    
  14.     // Called every frame
  15.     virtual void Tick( float DeltaSeconds ) override;
  16.  
  17.     UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Jetpack)
  18.     UStaticMeshComponent* jpmc;
  19.  
  20.     UStaticMesh* jpm;
  21. };
  22.  
  23. ///////////////////////////////////// Jetpack CPP
  24.  
  25.  
  26. // Fill out your copyright notice in the Description page of Project Settings.
  27.  
  28. #include "Tower.h"F
  29. #include "Jetpack.h"
  30.  
  31.  
  32. // Sets default values
  33. AJetpack::AJetpack(const class FObjectInitializer& OI) : Super(OI)
  34. {
  35.     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  36.     PrimaryActorTick.bCanEverTick = true;
  37.  
  38.     static ConstructorHelpers::FObjectFinder<UStaticMesh> jpm(TEXT("StaticMesh '/Game/Content/Models/jet.jet'"));
  39.  
  40.     jpmc = OI.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Jetpack Mesh"));
  41.     jpmc->SetStaticMesh(jpm.Object);
  42.  
  43.     jpmc->AttachTo(RootComponent);
  44.  
  45. }
  46.  
  47. // Called when the game starts or when spawned
  48. void AJetpack::BeginPlay()
  49. {
  50.     Super::BeginPlay();
  51.  
  52. }
  53.  
  54. // Called every frame
  55. void AJetpack::Tick( float DeltaTime )
  56. {
  57.     Super::Tick( DeltaTime );
  58.  
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65. ///////////////////////////// Character header
  66.  
  67. // Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
  68. #pragma once
  69. #include "GameFramework/Character.h"
  70. #include "Jetpack.h"
  71. #include "TowerCharacter.generated.h"
  72.  
  73. UCLASS(config=Game)
  74. class ATowerCharacter : public ACharacter
  75. {
  76.     GENERATED_BODY()
  77.  
  78.     /** Camera boom positioning the camera behind the character */
  79.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
  80.     class USpringArmComponent* CameraBoom;
  81.  
  82.     /** Follow camera */
  83.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
  84.     class UCameraComponent* FollowCamera;
  85.  
  86. public:
  87.     ATowerCharacter(const FObjectInitializer& ObjectInitializer);
  88.  
  89.     /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */
  90.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
  91.     float BaseTurnRate;
  92.  
  93.     /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */
  94.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
  95.     float BaseLookUpRate;
  96.  
  97.     AJetpack* jet;
  98.  
  99.  
  100. protected:
  101.  
  102.     /** Called for forwards/backward input */
  103.     void MoveForward(float Value);
  104.  
  105.     /** Called for side to side input */
  106.     void MoveRight(float Value);
  107.  
  108.     /**
  109.      * Called via input to turn at a given rate.
  110.      * @param Rate  This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
  111.      */
  112.     void TurnAtRate(float Rate);
  113.  
  114.     /**
  115.      * Called via input to turn look up/down at a given rate.
  116.      * @param Rate  This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
  117.      */
  118.     void LookUpAtRate(float Rate);
  119.  
  120.     /** Handler for when a touch input begins. */
  121.     void TouchStarted(ETouchIndex::Type FingerIndex, FVector Location);
  122.  
  123.     /** Handler for when a touch input stops. */
  124.     void TouchStopped(ETouchIndex::Type FingerIndex, FVector Location);
  125.  
  126. protected:
  127.     // APawn interface
  128.     virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
  129.     // End of APawn interface
  130.  
  131. public:
  132.     /** Returns CameraBoom subobject **/
  133.     FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
  134.     /** Returns FollowCamera subobject **/
  135.     FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
  136. };
  137.  
  138.  
  139. //////////////////////////////////// Character CPP
  140.  
  141. // Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
  142.  
  143. #include "Tower.h"
  144. #include "TowerCharacter.h"
  145.  
  146. //////////////////////////////////////////////////////////////////////////
  147. // ATowerCharacter
  148.  
  149. ATowerCharacter::ATowerCharacter(const FObjectInitializer& ObjectInitializer)
  150.     : Super(ObjectInitializer)
  151. {
  152.     // Set size for collision capsule
  153.     GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
  154.  
  155.     // set our turn rates for input
  156.     BaseTurnRate = 45.f;
  157.     BaseLookUpRate = 45.f;
  158.  
  159.     // Don't rotate when the controller rotates. Let that just affect the camera.
  160.     bUseControllerRotationPitch = false;
  161.     bUseControllerRotationYaw = false;
  162.     bUseControllerRotationRoll = false;
  163.  
  164.     // Configure character movement
  165.     GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...  
  166.     GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
  167.     GetCharacterMovement()->JumpZVelocity = 600.f;
  168.     GetCharacterMovement()->AirControl = 0.2f;
  169.  
  170.     // Create a camera boom (pulls in towards the player if there is a collision)
  171.     CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  172.     CameraBoom->AttachTo(RootComponent);
  173.     CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character  
  174.     CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
  175.  
  176.     // Create a follow camera
  177.     FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  178.     FollowCamera->AttachTo(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
  179.     FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  180.  
  181.     jet->AttachRootComponentTo(this->GetMesh(), "BackSocket", EAttachLocation::SnapToTarget);
  182.  
  183.     // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
  184.     // are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
  185.  
  186. }
  187.  
  188. //////////////////////////////////////////////////////////////////////////
  189. // Input
  190.  
  191. void ATowerCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  192. {
  193.     // Set up gameplay key bindings
  194.     check(InputComponent);
  195.     InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  196.     InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  197.  
  198.     InputComponent->BindAxis("MoveForward", this, &ATowerCharacter::MoveForward);
  199.     InputComponent->BindAxis("MoveRight", this, &ATowerCharacter::MoveRight);
  200.  
  201.     // We have 2 versions of the rotation bindings to handle different kinds of devices differently
  202.     // "turn" handles devices that provide an absolute delta, such as a mouse.
  203.     // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
  204.     InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  205.     InputComponent->BindAxis("TurnRate", this, &ATowerCharacter::TurnAtRate);
  206.     InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  207.     InputComponent->BindAxis("LookUpRate", this, &ATowerCharacter::LookUpAtRate);
  208.  
  209.     // handle touch devices
  210.     InputComponent->BindTouch(IE_Pressed, this, &ATowerCharacter::TouchStarted);
  211.     InputComponent->BindTouch(IE_Released, this, &ATowerCharacter::TouchStopped);
  212. }
  213.  
  214.  
  215. void ATowerCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
  216. {
  217.     // jump, but only on the first touch
  218.     if (FingerIndex == ETouchIndex::Touch1)
  219.     {
  220.         Jump();
  221.     }
  222. }
  223.  
  224. void ATowerCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)
  225. {
  226.     if (FingerIndex == ETouchIndex::Touch1)
  227.     {
  228.         StopJumping();
  229.     }
  230. }
  231.  
  232. void ATowerCharacter::TurnAtRate(float Rate)
  233. {
  234.     // calculate delta for this frame from the rate information
  235.     AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  236. }
  237.  
  238. void ATowerCharacter::LookUpAtRate(float Rate)
  239. {
  240.     // calculate delta for this frame from the rate information
  241.     AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  242. }
  243.  
  244. void ATowerCharacter::MoveForward(float Value)
  245. {
  246.     if ((Controller != NULL) && (Value != 0.0f))
  247.     {
  248.         // find out which way is forward
  249.         const FRotator Rotation = Controller->GetControlRotation();
  250.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  251.  
  252.         // get forward vector
  253.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  254.         AddMovementInput(Direction, Value);
  255.     }
  256. }
  257.  
  258. void ATowerCharacter::MoveRight(float Value)
  259. {
  260.     if ( (Controller != NULL) && (Value != 0.0f) )
  261.     {
  262.         // find out which way is right
  263.         const FRotator Rotation = Controller->GetControlRotation();
  264.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  265.    
  266.         // get right vector
  267.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  268.         // add movement in that direction
  269.         AddMovementInput(Direction, Value);
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement