text: // Fill out your copyright notice in the Description page of Project Settings.
  
  
  #include "PawnTank.h"
  #include "GameFramework/SpringArmComponent.h"
  #include "Camera/CameraComponent.h"
  
  
  APawnTank::APawnTank()
  {
  	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
  	SpringArm->SetupAttachment(RootComponent);
  
  	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
  	Camera->SetupAttachment(SpringArm);
  }
  
  // Called when the game starts or when spawned
  void APawnTank::BeginPlay()
  {
  	Super::BeginPlay();
  	PlayerControllerRef = Cast<APlayerController>(GetController());
  }
  
  void APawnTank::HandleDestruction()
  {
  	Super::HandleDestruction();
  	// Hide Player. TODO - Create new function to do this
  
  	bAlive = false;
  
  	SetActorHiddenInGame(true);
  	SetActorTickEnabled(false);
  }
  
  // Called every frame
  void APawnTank::Tick(float DeltaTime)
  {
  	Super::Tick(DeltaTime);
  	Rotate();
  	Move();
  
  	if(PlayerControllerRef)
  	{
  		PlayerControllerRef->GetHitResultUnderCursor(ECC_Visibility, true, TraceHitResult);
  		FVector HitLocation = TraceHitResult.ImpactPoint;
  
  		RotateTurret(HitLocation);
  	}
  }
  
  // Called to bind functionality to input
  void APawnTank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  {
  	Super::SetupPlayerInputComponent(PlayerInputComponent);
  
  	PlayerInputComponent->BindAxis("MoveForward", this, &APawnTank::CalculateMoveInput);
  	PlayerInputComponent->BindAxis("Turn", this, &APawnTank::CalculateRotateInput);
  	PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &APawnTank::Fire);
  }
  
  bool APawnTank::GetPlayerAlive()
  {
  	return bAlive;
  }
  
  void APawnTank::CalculateMoveInput(float Value)
  {
  	MoveDirection = FVector(Value * MoveSpeed * GetWorld()->DeltaTimeSeconds, 0, 0);
  }
  
  void APawnTank::CalculateRotateInput(float Value)
  {
  	float RotateAmount = Value * RotateSpeed * GetWorld()->DeltaTimeSeconds;
  	FRotator Rotation = FRotator(0, RotateAmount, 0);
  	RotationDirection = FQuat(Rotation);
  }
  
  void APawnTank::Move()
  {
  	AddActorLocalOffset(MoveDirection, true);
  }
  
  void APawnTank::Rotate()
  {
  	AddActorLocalRotation(RotationDirection, true);
  }