Advertisement
Guest User

TwinStickCPawn.cpp

a guest
Feb 27th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. // Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
  2.  
  3. #include "TwinStickC.h"
  4. #include "TwinStickCPawn.h"
  5. #include "TwinStickCProjectile.h"
  6. #include "TimerManager.h"
  7. #include "Engine/Blueprint.h"
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14. const FName ATwinStickCPawn::MoveForwardBinding("MoveForward");
  15. const FName ATwinStickCPawn::MoveRightBinding("MoveRight");
  16. const FName ATwinStickCPawn::FireForwardBinding("FireForward");
  17. const FName ATwinStickCPawn::FireRightBinding("FireRight");
  18.  
  19. ATwinStickCPawn::ATwinStickCPawn(const FObjectInitializer& ObjectInitializer)
  20.     : Super(ObjectInitializer)
  21. {  
  22.     static ConstructorHelpers::FObjectFinder<UStaticMesh> ShipMesh(TEXT("/Game/TwinStick/Meshes/TwinStickUFO.TwinStickUFO"));
  23.     // Create the mesh component
  24.     ShipMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ShipMesh"));
  25.     RootComponent = ShipMeshComponent;
  26.     ShipMeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
  27.     ShipMeshComponent->SetStaticMesh(ShipMesh.Object);
  28.    
  29.     // Cache our sound effect
  30.     static ConstructorHelpers::FObjectFinder<USoundBase> FireAudio(TEXT("/Game/TwinStick/Audio/TwinStickFire.TwinStickFire"));
  31.     FireSound = FireAudio.Object;
  32.  
  33.     static ConstructorHelpers::FObjectFinder<UBlueprint> ASD(TEXT("Blueprint'/Game/Blueprints/TestBP.TestBP'"));
  34.     if (ASD.Object != NULL) {
  35.         (UClass*)ASD.Object->GeneratedClass;
  36.     }
  37.  
  38.     // Create a camera boom...
  39.     CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  40.     CameraBoom->AttachTo(RootComponent);
  41.     CameraBoom->bAbsoluteRotation = true; // Don't want arm to rotate when ship does
  42.     CameraBoom->TargetArmLength = 1200.f;
  43.     CameraBoom->RelativeRotation = FRotator(-80.f, 0.f, 0.f);
  44.     CameraBoom->bDoCollisionTest = false; // Don't want to pull camera in when it collides with level
  45.  
  46.     // Create a camera...
  47.     CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("TopDownCamera"));
  48.     CameraComponent->AttachTo(CameraBoom, USpringArmComponent::SocketName);
  49.     CameraComponent->bUsePawnControlRotation = false;   // Camera does not rotate relative to arm
  50.  
  51.     // Movement
  52.     MoveSpeed = 1000.0f;
  53.     // Weapon
  54.     GunOffset = FVector(90.f, 0.f, 0.f);
  55.     FireRate = 0.1f;
  56.     bCanFire = true;
  57. }
  58.  
  59. void ATwinStickCPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  60. {
  61.     check(InputComponent);
  62.  
  63.     // set up gameplay key bindings
  64.     InputComponent->BindAxis(MoveForwardBinding);
  65.     InputComponent->BindAxis(MoveRightBinding);
  66.     InputComponent->BindAxis(FireForwardBinding);
  67.     InputComponent->BindAxis(FireRightBinding);
  68. }
  69.  
  70. void ATwinStickCPawn::Tick(float DeltaSeconds)
  71. {
  72.     // Find movement direction
  73.     const float ForwardValue = GetInputAxisValue(MoveForwardBinding);
  74.     const float RightValue = GetInputAxisValue(MoveRightBinding);
  75.  
  76.     // Clamp max size so that (X=1, Y=1) doesn't cause faster movement in diagonal directions
  77.     const FVector MoveDirection = FVector(ForwardValue, RightValue, 0.f).GetClampedToMaxSize(1.0f);
  78.  
  79.     // Calculate  movement
  80.     const FVector Movement = MoveDirection * MoveSpeed * DeltaSeconds;
  81.  
  82.     // If non-zero size, move this actor
  83.     if (Movement.SizeSquared() > 0.0f)
  84.     {
  85.         const FRotator NewRotation = Movement.Rotation();
  86.         FHitResult Hit(1.f);
  87.         RootComponent->MoveComponent(Movement, NewRotation, true, &Hit);
  88.        
  89.         if (Hit.IsValidBlockingHit())
  90.         {
  91.             const FVector Normal2D = Hit.Normal.GetSafeNormal2D();
  92.             const FVector Deflection = FVector::VectorPlaneProject(Movement, Normal2D) * (1.f - Hit.Time);
  93.             RootComponent->MoveComponent(Deflection, NewRotation, true);
  94.         }
  95.     }
  96.    
  97.     // Create fire direction vector
  98.     const float FireForwardValue = GetInputAxisValue(FireForwardBinding);
  99.     const float FireRightValue = GetInputAxisValue(FireRightBinding);
  100.     const FVector FireDirection = FVector(FireForwardValue, FireRightValue, 0.f);
  101.  
  102.     // Try and fire a shot
  103.     FireShot(FireDirection);
  104. }
  105.  
  106. void ATwinStickCPawn::FireShot(FVector FireDirection)
  107. {
  108.     // If we it's ok to fire again
  109.     if (bCanFire == true)
  110.     {
  111.         // If we are pressing fire stick in a direction
  112.         if (FireDirection.SizeSquared() > 0.0f)
  113.         {
  114.             const FRotator FireRotation = FireDirection.Rotation();
  115.             // Spawn projectile at an offset from this pawn
  116.             const FVector SpawnLocation = GetActorLocation() + FireRotation.RotateVector(GunOffset);
  117.  
  118.             UWorld* const World = GetWorld();
  119.             if (World != NULL)
  120.             {
  121.                 // spawn the projectile
  122.                 World->SpawnActor<AActor*>(BlueprintVar,SpawnLocation, FireRotation);
  123.             }
  124.  
  125.             bCanFire = false;
  126.             World->GetTimerManager().SetTimer(TimerHandle_ShotTimerExpired, this, &ATwinStickCPawn::ShotTimerExpired, FireRate);
  127.  
  128.             // try and play the sound if specified
  129.             if (FireSound != nullptr)
  130.             {
  131.                 UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
  132.             }
  133.  
  134.             bCanFire = false;
  135.         }
  136.     }
  137. }
  138.  
  139. void ATwinStickCPawn::ShotTimerExpired()
  140. {
  141.     bCanFire = true;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement