74 lines
1.9 KiB
C++
74 lines
1.9 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Tank.h"
|
|
|
|
#include "Camera/CameraComponent.h"
|
|
#include "GameFramework/SpringArmComponent.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
|
|
ATank::ATank()
|
|
{
|
|
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
|
|
SpringArm->SetupAttachment(RootComponent);
|
|
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
|
|
Camera->SetupAttachment(SpringArm);
|
|
}
|
|
|
|
void ATank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
|
{
|
|
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
|
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &ATank::Move);
|
|
PlayerInputComponent->BindAxis(TEXT("Turn"), this, &ATank::Turn);
|
|
PlayerInputComponent->BindAction(TEXT("Fire"), EInputEvent::IE_Pressed, this, &ATank::Fire);
|
|
}
|
|
|
|
// Called every frame
|
|
void ATank::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
// Rotate Tank to Mouse Pointer Location
|
|
if (PlayerControllerRef)
|
|
{
|
|
FHitResult HitResult;
|
|
PlayerControllerRef->GetHitResultUnderCursor(
|
|
ECollisionChannel::ECC_Visibility,
|
|
false,
|
|
HitResult
|
|
);
|
|
DrawDebugSphere(
|
|
GetWorld(),
|
|
HitResult.ImpactPoint,
|
|
25,
|
|
12,
|
|
FColor::Green
|
|
);
|
|
RotateTurret(HitResult.ImpactPoint);
|
|
}
|
|
}
|
|
|
|
// Called when the game starts or when spawned
|
|
void ATank::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
PlayerControllerRef = Cast<APlayerController>(GetController());
|
|
}
|
|
|
|
// Move operation through W/S key
|
|
void ATank::Move(float Value)
|
|
{
|
|
FVector DeltaLocation = FVector::ZeroVector;
|
|
float DeltaTime = UGameplayStatics::GetWorldDeltaSeconds(this);
|
|
DeltaLocation.X = Value * DeltaTime * Speed;
|
|
this->AddActorLocalOffset(DeltaLocation, true);
|
|
}
|
|
|
|
// Turn operation through A/D key
|
|
void ATank::Turn(float Value)
|
|
{
|
|
FRotator DeltaRotation = FRotator::ZeroRotator;
|
|
float DeltaTime = UGameplayStatics::GetWorldDeltaSeconds(this);
|
|
DeltaRotation.Yaw = Value * DeltaTime * TurnRate;
|
|
this->AddActorLocalRotation(DeltaRotation, true);
|
|
} |