setup hits on character

This commit is contained in:
2025-12-06 17:19:02 -05:00
parent eb402e0458
commit a3a120491d
2 changed files with 62 additions and 8 deletions

View File

@@ -2,7 +2,10 @@
#include "DDICharacter.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "OpenConflict/Weapons/Projectiles/ProjectileBase.h"
// Sets default values
ADDICharacter::ADDICharacter()
@@ -17,6 +20,12 @@ ADDICharacter::ADDICharacter()
CameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f));
CameraComponent->bUsePawnControlRotation = true;
HealthComponent = CreateDefaultSubobject<UDDIHealth>(TEXT("HealthComponent"));
GetCapsuleComponent()->SetCollisionProfileName(TEXT("BlockAllDynamic"));
GetCapsuleComponent()->SetGenerateOverlapEvents(true);
GetCapsuleComponent()->SetNotifyRigidBodyCollision(true);
}
@@ -24,6 +33,11 @@ ADDICharacter::ADDICharacter()
void ADDICharacter::BeginPlay()
{
Super::BeginPlay();
if (GetCapsuleComponent())
{
GetCapsuleComponent()->OnComponentHit.AddDynamic(this, &ADDICharacter::OnHit);
}
}
@@ -34,4 +48,13 @@ void ADDICharacter::Tick(float DeltaTime)
}
void ADDICharacter::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if (OtherActor->Owner == this)
return;
AProjectileBase* Projectile = Cast<AProjectileBase>(OtherActor);
if (HealthComponent && Projectile)
HealthComponent->TakeDamage(10);
}

View File

@@ -4,6 +4,7 @@
#include "CoreMinimal.h"
#include "Camera/CameraComponent.h"
#include "Components/DDIHealth.h"
#include "GameFramework/Character.h"
#include "DDICharacter.generated.h"
@@ -12,20 +13,50 @@ UCLASS()
class OPENCONFLICT_API ADDICharacter : public ACharacter
{
GENERATED_BODY()
/*UPROPERTY and UFUNCTION declarations*/
private:
/*Properties*/
public:
// Sets default values for this character's properties
ADDICharacter();
/*Functions*/
protected:
/*Properties*/
/*Functions*/
public:
/*Properties*/
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera")
UCameraComponent* CameraComponent;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health")
UDDIHealth* HealthComponent;
/*Functions*/
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
/*C++ only declarations*/
private:
/*Properties*/
/*Functions*/
protected:
/*Properties*/
/*Functions*/
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
public:
/*Properties*/
/*Functions*/
// Sets default values for this character's properties
ADDICharacter();
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera")
UCameraComponent* CameraComponent;
};