text: // Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
  
  #include "City.h"
  #include "CityCharacter.h"
  #include "Animation/AnimInstance.h"
  #include "GameFramework/InputSettings.h"
  #include "Kismet/HeadMountedDisplayFunctionLibrary.h"
  #include "MotionControllerComponent.h"
  
  DEFINE_LOG_CATEGORY_STATIC(LogFPChar, Warning, All);
  
  //////////////////////////////////////////////////////////////////////////
  // ACityCharacter
  
  ACityCharacter::ACityCharacter()
  {
  	// Set size for collision capsule
  	GetCapsuleComponent()->InitCapsuleSize(55.f, 96.0f);
  
  	// set our turn rates for input
  	BaseTurnRate = 45.f;
  	BaseLookUpRate = 45.f;
  
  	// Create a CameraComponent	
  	FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
  	FirstPersonCameraComponent->SetupAttachment(GetCapsuleComponent());
  	FirstPersonCameraComponent->SetRelativeLocation(FVector(-39.56f, 1.75f, 64.f));
  	FirstPersonCameraComponent->bUsePawnControlRotation = true;
  
  	// Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)
  	Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));
  	Mesh1P->SetOnlyOwnerSee(true);
  	Mesh1P->SetupAttachment(FirstPersonCameraComponent);
  	Mesh1P->bCastDynamicShadow = false;
  	Mesh1P->CastShadow = false;
  
  	Mesh1P->SetRelativeRotation(FRotator(1.9f, -19.19f, 5.2f));
  	Mesh1P->SetRelativeLocation(FVector(-0.5f, -4.4f, -155.7f));
  
  }
  
  void ACityCharacter::BeginPlay()
  {
  	// Call the base class  
  	Super::BeginPlay();
  
  	//Attach gun mesh component to Skeleton, doing it here because the skeleton is not yet created in the constructor
  
  	// Show or hide the two versions of the gun based on whether or not we're using motion controllers.
  	if (bUsingMotionControllers)
  	{
  		Mesh1P->SetHiddenInGame(true, true);
  	}
  	else
  	{
  		Mesh1P->SetHiddenInGame(false, true);
  	}
  }
  
  //////////////////////////////////////////////////////////////////////////
  // Input
  
  void ACityCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
  {
  	// set up gameplay key bindings
  	check(PlayerInputComponent);
  
  	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  	PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  
  	//InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &ACityCharacter::TouchStarted);
  	if (EnableTouchscreenMovement(PlayerInputComponent) == false)
  	{
  		PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ACityCharacter::OnFire);
  	}
  
  
  	PlayerInputComponent->BindAxis("MoveForward", this, &ACityCharacter::MoveForward);
  	PlayerInputComponent->BindAxis("MoveRight", this, &ACityCharacter::MoveRight);
  
  	// We have 2 versions of the rotation bindings to handle different kinds of devices differently
  	// "turn" handles devices that provide an absolute delta, such as a mouse.
  	// "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
  	PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  	PlayerInputComponent->BindAxis("TurnRate", this, &ACityCharacter::TurnAtRate);
  	PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  	PlayerInputComponent->BindAxis("LookUpRate", this, &ACityCharacter::LookUpAtRate);
  }
  
  void ACityCharacter::OnFire()
  {
  
  }
  
  void ACityCharacter::BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
  {
  	if (TouchItem.bIsPressed == true)
  	{
  		return;
  	}
  	TouchItem.bIsPressed = true;
  	TouchItem.FingerIndex = FingerIndex;
  	TouchItem.Location = Location;
  	TouchItem.bMoved = false;
  }
  
  void ACityCharacter::EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
  {
  	if (TouchItem.bIsPressed == false)
  	{
  		return;
  	}
  	if ((FingerIndex == TouchItem.FingerIndex) && (TouchItem.bMoved == false))
  	{
  		OnFire();
  	}
  	TouchItem.bIsPressed = false;
  }
  
  
  void ACityCharacter::MoveForward(float Value)
  {
  	if (Value != 0.0f)
  	{
  		// add movement in that direction
  		AddMovementInput(GetActorForwardVector(), Value);
  	}
  }
  
  void ACityCharacter::MoveRight(float Value)
  {
  	if (Value != 0.0f)
  	{
  		// add movement in that direction
  		AddMovementInput(GetActorRightVector(), Value);
  	}
  }
  
  void ACityCharacter::TurnAtRate(float Rate)
  {
  	// calculate delta for this frame from the rate information
  	AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  }
  
  void ACityCharacter::LookUpAtRate(float Rate)
  {
  	// calculate delta for this frame from the rate information
  	AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  }
  
  bool ACityCharacter::EnableTouchscreenMovement(class UInputComponent* PlayerInputComponent)
  {
  	bool bResult = false;
  	if (FPlatformMisc::GetUseVirtualJoysticks() || GetDefault<UInputSettings>()->bUseMouseForTouch)
  	{
  		bResult = true;
  		PlayerInputComponent->BindTouch(EInputEvent::IE_Pressed, this, &ACityCharacter::BeginTouch);
  		PlayerInputComponent->BindTouch(EInputEvent::IE_Released, this, &ACityCharacter::EndTouch);
  
  		//Commenting this out to be more consistent with FPS BP template.
  		//PlayerInputComponent->BindTouch(EInputEvent::IE_Repeat, this, &ACityCharacter::TouchUpdate);
  	}
  	return bResult;
  }
  