Commit 6097ba05 authored by Ridel Morgan's avatar Ridel Morgan
Browse files

update

parent 5232210e
......@@ -7,7 +7,7 @@
ATank::ATank()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.bCanEverTick = false;
TankAimingComponent = CreateDefaultSubobject<UTankAimingComponent>(FName("Aiming Component"));
}
......@@ -22,6 +22,11 @@ void ATank::SetBarrelReference(UTankBarrel * BarrelToSet)
TankAimingComponent->SetBarrelReference(BarrelToSet);
}
void ATank::SetTurretReference(UTankTurret * TurretToSet)
{
TankAimingComponent->SetTurretReference(TurretToSet);
}
// Called when the game starts or when spawned
void ATank::BeginPlay()
{
......@@ -29,13 +34,6 @@ void ATank::BeginPlay()
}
// Called every frame
void ATank::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ATank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
......
......@@ -5,6 +5,7 @@
#include "Engine/StaticMesh.h"
#include "Kismet/GameplayStatics.h"
#include "TankBarrel.h"
#include "TankTurret.h"
// Sets default values for this component's properties
......@@ -23,13 +24,19 @@ void UTankAimingComponent::SetBarrelReference(UTankBarrel * BarrelToSet)
Barrel = BarrelToSet;
}
void UTankAimingComponent::MoveBarrelTowards(FVector AimDirection)
void UTankAimingComponent::SetTurretReference(UTankTurret * TurretToSet)
{
Turret = TurretToSet;
}
void UTankAimingComponent::MoveTurretTowards(FVector AimDirection)
{
auto BarrelRotation = Barrel->GetForwardVector().Rotation();
auto AimAsRotator = AimDirection.Rotation();
auto DeltaRotation = AimAsRotator - BarrelRotation;
Barrel->Elevate(5);
Barrel->Elevate(DeltaRotation.Pitch);
Turret->Rotate(DeltaRotation.Yaw);
}
// Called when the game starts
......@@ -53,16 +60,21 @@ void UTankAimingComponent::TickComponent(float DeltaTime, ELevelTick TickType, F
void UTankAimingComponent::AimAt(FVector HitLocation, float LaunchSpeed)
{
if (!Barrel) { return; }
if (!Turret) { return; }
FVector TossVelocity;
FVector StartLocation = Barrel->GetSocketLocation(FName("Projectile"));
if (UGameplayStatics::SuggestProjectileVelocity(this, TossVelocity, StartLocation, HitLocation, LaunchSpeed, false, 0.0f, 0.0f, ESuggestProjVelocityTraceOption::TraceFullPath, FCollisionResponseParams::DefaultResponseParam,TArray<AActor*>(),true))
if (UGameplayStatics::SuggestProjectileVelocity(this, TossVelocity, StartLocation, HitLocation, LaunchSpeed, false, 0.0f, 0.0f, ESuggestProjVelocityTraceOption::DoNotTrace))
{
auto AimDirection = TossVelocity.GetSafeNormal();
UE_LOG(LogTemp, Warning, TEXT("%s aiming at %s from %s. Launch direction %s"), *GetOwner()->GetName(), *HitLocation.ToString(), *Barrel->GetComponentLocation().ToString(), *AimDirection.ToString())
MoveBarrelTowards(AimDirection);
UE_LOG(LogTemp, Warning, TEXT("%f : %s aiming at %s from %s. Launch direction %s"), GetWorld()->GetTimeSeconds(), *GetOwner()->GetName(), *HitLocation.ToString(), *Barrel->GetComponentLocation().ToString(), *AimDirection.ToString())
MoveTurretTowards(AimDirection);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("%f : No projectile direction found"), GetWorld()->GetTimeSeconds())
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#include "TankBarrel.h"
#include "Engine/World.h"
void UTankBarrel::Elevate(float DegreePerSecond)
void UTankBarrel::Elevate(float RelativeSpeed)
{
UE_LOG(LogTemp, Warning, TEXT("Elevatin barrel at %f degree per sec"), DegreePerSecond)
RelativeSpeed = FMath::Clamp<float>(RelativeSpeed, -1, 1);
UE_LOG(LogTemp, Warning, TEXT("Elevatin barrel at %f relative speed"), RelativeSpeed)
auto ElevationChange = RelativeSpeed * MaxDegreesPerSecond * GetWorld()->GetDeltaSeconds();
auto RawNewElevation = RelativeRotation.Pitch + ElevationChange;
auto ClampedElevation = FMath::Clamp(RawNewElevation, MinElevationDegree, MaxElevationDegree);
SetRelativeRotation(FRotator(ClampedElevation, 0, 0));
}
// Fill out your copyright notice in the Description page of Project Settings.
#include "TankTurret.h"
void UTankTurret::Rotate(float RelativeSpeed)
{
RelativeSpeed = FMath::Clamp<float>(RelativeSpeed, -1, 1);
UE_LOG(LogTemp, Warning, TEXT("Rotation Turret at %f relative speed"), RelativeSpeed)
auto RotationChange = RelativeSpeed * MaxDegreesPerSecond * GetWorld()->GetDeltaSeconds();
auto RawNewRotation = RelativeRotation.Yaw + RotationChange;
SetRelativeRotation(FRotator(0, RawNewRotation, 0));
}
......@@ -8,6 +8,7 @@
#include "Tank.generated.h"
class UTankBarrel;
class UTankTurret;
UCLASS()
class BATTLETANKS_API ATank : public APawn
......@@ -23,6 +24,9 @@ public:
UFUNCTION(BlueprintCallable, Category = Setup)
void SetBarrelReference(UTankBarrel* BarrelToSet);
UFUNCTION(BlueprintCallable, Category = Setup)
void SetTurretReference(UTankTurret* TurretToSet);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
......@@ -30,14 +34,12 @@ protected:
UTankAimingComponent* TankAimingComponent = nullptr;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UPROPERTY(EditAnywhere, Category = Firing)
float LaunchSpeed = 100000.0f;
float LaunchSpeed = 4000.0f;
};
......@@ -7,6 +7,7 @@
#include "TankAimingComponent.generated.h"
class UTankBarrel; //forward declaration
class UTankTurret;
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
......@@ -20,7 +21,10 @@ public:
void SetBarrelReference(UTankBarrel* BarrelToSet);
void MoveBarrelTowards(FVector AimDirection);
void SetTurretReference(UTankTurret* TurretToSet);
void MoveTurretTowards(FVector AimDirection);
protected:
// Called when the game starts
......@@ -34,5 +38,6 @@ public:
private:
UTankBarrel* Barrel = nullptr;
UTankTurret* Turret = nullptr;
};
......@@ -15,11 +15,11 @@ class BATTLETANKS_API UTankBarrel : public UStaticMeshComponent
GENERATED_BODY()
public:
void Elevate(float DegreePerSecond);
void Elevate(float RelativeSpeed);
private:
UPROPERTY(EditAnywhere)
float MaxDegreesPerSecond = 20.0f;
float MaxDegreesPerSecond = 10.0f;
UPROPERTY(EditAnywhere)
float MaxElevationDegree = 40.0f;
......
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/StaticMeshComponent.h"
#include "TankTurret.generated.h"
/**
*
*/
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class BATTLETANKS_API UTankTurret : public UStaticMeshComponent
{
GENERATED_BODY()
public:
void Rotate(float RelativeSpeed);
private:
UPROPERTY(EditAnywhere)
float MaxDegreesPerSecond = 25.0f;
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment