HealthComponent

This commit is contained in:
2025-08-05 17:55:39 -04:00
parent bb8d95d8c2
commit d144e13084
12 changed files with 98 additions and 4 deletions

Binary file not shown.

View File

@@ -31,7 +31,7 @@ void ABasePawn::RotateTurret(FVector LookAtTarget)
FVector ToTarget = LookAtTarget - TurretMesh->GetComponentLocation(); FVector ToTarget = LookAtTarget - TurretMesh->GetComponentLocation();
FRotator LookAtRotation = FRotator(0.f, ToTarget.Rotation().Yaw, 0.f); FRotator LookAtRotation = FRotator(0.f, ToTarget.Rotation().Yaw, 0.f);
TurretMesh->SetWorldRotation( TurretMesh->SetWorldRotation(
FMath::RInterpTo( FMath::RInterpTo(
TurretMesh->GetComponentRotation(), TurretMesh->GetComponentRotation(),
LookAtRotation, LookAtRotation,
UGameplayStatics::GetWorldDeltaSeconds(this), UGameplayStatics::GetWorldDeltaSeconds(this),
@@ -44,6 +44,7 @@ void ABasePawn::Fire()
FVector Location = ProjectileSpawnPoint->GetComponentLocation(); FVector Location = ProjectileSpawnPoint->GetComponentLocation();
FRotator Rotation = ProjectileSpawnPoint->GetComponentRotation(); FRotator Rotation = ProjectileSpawnPoint->GetComponentRotation();
GetWorld()->SpawnActor<AProjectile>(ProjectileClass, Location, Rotation); GetWorld()->SpawnActor<AProjectile>(ProjectileClass, Location, Rotation);
/*DrawDebugSphere( /*DrawDebugSphere(

View File

@@ -30,7 +30,7 @@ public:
UPROPERTY(EditInstanceOnly) UPROPERTY(EditInstanceOnly)
int32 EditInstanceOnlyInt = 14; int32 EditInstanceOnlyInt = 14;
protected: protected:
void RotateTurret(FVector LookAtTarget); void RotateTurret(FVector LookAtTarget);

View File

@@ -0,0 +1,41 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "HealthComponent.h"
// Sets default values for this component's properties
UHealthComponent::UHealthComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UHealthComponent::BeginPlay()
{
Super::BeginPlay();
Health = MaxHealth;
GetOwner()->OnTakeAnyDamage.AddDynamic(this, &UHealthComponent::DamageTaken);
}
// Called every frame
void UHealthComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
void UHealthComponent::DamageTaken(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* Instigator, AActor* DamageCauser)
{
}

View File

@@ -0,0 +1,37 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "HealthComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class TOONTANKS_API UHealthComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UHealthComponent();
protected:
// Called when the game starts
virtual void BeginPlay() override;
private:
UPROPERTY(EditAnywhere, Category = "Health")
float MaxHealth = 100.f;
float Health = 0.f;
UFUNCTION()
void DamageTaken(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* Instigator, AActor* DamageCauser);
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};

View File

@@ -23,6 +23,8 @@ AProjectile::AProjectile()
void AProjectile::BeginPlay() void AProjectile::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
ProjectileMesh->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);
} }
// Called every frame // Called every frame
@@ -31,3 +33,13 @@ void AProjectile::Tick(float DeltaTime)
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
} }
void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
/*UE_LOG(LogTemp, Warning, TEXT("OnHit"));
UE_LOG(LogTemp, Warning, TEXT("HitComp: %s"), *HitComp->GetName());
UE_LOG(LogTemp, Warning, TEXT("OtherActor: %s"), *OtherActor->GetName());
UE_LOG(LogTemp, Warning, TEXT("OtherComp: %s"), *OtherComp->GetName());*/
}

View File

@@ -27,6 +27,9 @@ private:
UPROPERTY(VisibleAnywhere, Category = "Movement") UPROPERTY(VisibleAnywhere, Category = "Movement")
class UProjectileMovementComponent* ProjectileMovementComponent; class UProjectileMovementComponent* ProjectileMovementComponent;
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
public: public:
// Called every frame // Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;

View File

@@ -21,7 +21,7 @@ ATank::ATank()
void ATank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) void ATank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{ {
Super::SetupPlayerInputComponent(PlayerInputComponent); Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &ATank::Move); PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &ATank::Move);

View File

@@ -36,7 +36,7 @@ private:
UPROPERTY(EditAnywhere, Category = "Movement") UPROPERTY(EditAnywhere, Category = "Movement")
float Speed = 200.f; float Speed = 200.f;
UPROPERTY(EditAnywhere, Category = "Movement") UPROPERTY(EditAnywhere, Category = "Movement")
float TurnRate = 135.f; float TurnRate = 135.f;