Project Type | Persoanl project |
Software Used | Unreal Engine 4, Visual Studio |
Languages Used | C++, Blueprint |
Primary Role(s) | Gameplay programming |
void ASprungWheel::BeginPlay()
{
Super::BeginPlay();
Wheel->SetNotifyRigidBodyCollision(true);
// Regestring on hit event
Wheel->OnComponentHit.AddDynamic(this, &ASprungWheel::OnHit);
SetupSuspension();
}
// Called every frame
void ASprungWheel::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (GetWorld()->TickGroup == TG_PostPhysics)
{
TotalForceMagnitudeThisFrame = 0.0f;
}
}
void ASprungWheel::SetupSuspension()
{
if (!GetAttachParentActor()) { return; }
UPrimitiveComponent* RootComponent = Cast(GetAttachParentActor()->GetRootComponent());
if (!RootComponent) { return; }
MassWheelConstraint->SetConstrainedComponents(RootComponent, NAME_None, Axle, NAME_None);
AxleWheelConstraint->SetConstrainedComponents(Axle, NAME_None, Wheel, NAME_None);
}
void ASprungWheel::AddDrivingForce(float ForceMagnitude)
{
TotalForceMagnitudeThisFrame += ForceMagnitude;
}
void ASprungWheel::OnHit(UPrimitiveComponent * HitComponent, AActor * OtherActor,
UPrimitiveComponent * OtherComp, FVector NormalImpulse, const FHitResult & Hit)
{
ApplyForce();
}
void ASprungWheel::ApplyForce()
{
Wheel->AddForce(Axle->GetForwardVector() * TotalForceMagnitudeThisFrame);
}
#include "BattleTank.h"
#include "TankAimingComponent.h"
#include "TankAIController.h"
#include "Tank.h" // So we can impliment OnDeath
// Depends on movement component via pathfinding system
void ATankAIController::BeginPlay()
{
Super::BeginPlay();
}
void ATankAIController::SetPawn(APawn* InPawn)
{
Super::SetPawn(InPawn);
if (InPawn)
{
auto PossessedTank = Cast(InPawn);
if (!PossessedTank) { return; }
// Subscribe our local method to the tank's death event
PossessedTank->OnDeath.AddUniqueDynamic(this, &ATankAIController::OnPossedTankDeath);
}
}
void ATankAIController::OnPossedTankDeath()
{
if (!ensure(GetPawn())) { return; } // TODO remove if ok
GetPawn()->DetachFromControllerPendingDestroy();
}
// Called every frame
void ATankAIController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
auto PlayerTank = GetWorld()->GetFirstPlayerController()->GetPawn();
auto ControlledTank = GetPawn();
if (!PlayerTank && !ControlledTank) { return; }
// Move towards the player
MoveToActor(PlayerTank, AcceptanceRadius); // TODO check radius is in cm
// Aim towards the player
auto AimingComponent = ControlledTank->FindComponentByClass();
AimingComponent->AimAt(PlayerTank->GetActorLocation());
if (AimingComponent->GetFiringState() == EFiringState::Locked)
{
AimingComponent->Fire(); // TODO limit firing rate
}
}