From 2c3a66cbd4cfc2cf8dbeeebc526f05f58a2d1e87 Mon Sep 17 00:00:00 2001 From: baz Date: Wed, 2 Apr 2025 23:48:25 +0100 Subject: [PATCH] Add Enemy Attacks --- Content/Enemy/BP_EnemyCharacter.uasset | 4 +- Content/Enemy/BP_TestEnemy.uasset | 4 +- Content/Pickups/EXP/BP_PickupTemplate.uasset | 4 +- Content/Pickups/Gold/BP_GoldPickup.uasset | 4 +- Content/Player/BP_PlayerCharacter.uasset | 4 +- .../Weapons/FireWand/BP_FireWandWeapon.uasset | 4 +- .../MagicWand/BP_MagicWandProjectile.uasset | 4 +- Source/vampires/EnemyCharacter.cpp | 40 +++++++++++++++++++ Source/vampires/EnemyCharacter.h | 25 ++++++++++++ Source/vampires/Pickup.cpp | 2 + Source/vampires/Projectile.cpp | 1 + Source/vampires/Weapons/GarlicWeapon.cpp | 2 + .../vampires/Weapons/LightningRingWeapon.cpp | 2 + Source/vampires/Weapons/PentagramWeapon.cpp | 2 + Source/vampires/Weapons/ProjectileWeapon.cpp | 1 + 15 files changed, 89 insertions(+), 14 deletions(-) diff --git a/Content/Enemy/BP_EnemyCharacter.uasset b/Content/Enemy/BP_EnemyCharacter.uasset index ab72b6a..ace2bca 100644 --- a/Content/Enemy/BP_EnemyCharacter.uasset +++ b/Content/Enemy/BP_EnemyCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:8babc608aaaefe05285db1c1b753c2eb28b1cebb40c69c9d22449b5f9f3cdf31 -size 33416 +oid sha256:07f0ef702fc39272390690a2e67dc86424d0dcba50c6173ffcd87e0df89e6aaa +size 33804 diff --git a/Content/Enemy/BP_TestEnemy.uasset b/Content/Enemy/BP_TestEnemy.uasset index 6b45aff..7b41b5a 100644 --- a/Content/Enemy/BP_TestEnemy.uasset +++ b/Content/Enemy/BP_TestEnemy.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:11476b84e8eb24be154d202ed87fe0b3b9da7fc1dc57ba46e953883980736c44 -size 27196 +oid sha256:686ccbb0e53ac5ae4803f2b90b06d63babb70a2bc9bd04c03a9f0b7c0fe3ed2c +size 28927 diff --git a/Content/Pickups/EXP/BP_PickupTemplate.uasset b/Content/Pickups/EXP/BP_PickupTemplate.uasset index 55645e6..b06b218 100644 --- a/Content/Pickups/EXP/BP_PickupTemplate.uasset +++ b/Content/Pickups/EXP/BP_PickupTemplate.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:eb30783d633a6f999da506bfff2521fa4fa4914cdafecf9c2651bfd42dff9401 -size 24513 +oid sha256:ca7d0740987168ab86fc76d832b83cc083cb8ae95a14f60b07ad7cf2d271a65f +size 27509 diff --git a/Content/Pickups/Gold/BP_GoldPickup.uasset b/Content/Pickups/Gold/BP_GoldPickup.uasset index f97eb9a..0872e88 100644 --- a/Content/Pickups/Gold/BP_GoldPickup.uasset +++ b/Content/Pickups/Gold/BP_GoldPickup.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:36bd6dcf623807d3b18df1f57ef68ecbc6b472d54218259318a11a81319c5052 -size 34394 +oid sha256:d596baeda4da7df442075b887f2ed011b3f9d6eb48bf3a3ddbd4a60e4bd53214 +size 36863 diff --git a/Content/Player/BP_PlayerCharacter.uasset b/Content/Player/BP_PlayerCharacter.uasset index dd94e69..7533b66 100644 --- a/Content/Player/BP_PlayerCharacter.uasset +++ b/Content/Player/BP_PlayerCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d5b466c2bf59a2872a43cf5d501368c2e1c676e85ec6e0167960561a48348a27 -size 56656 +oid sha256:6375a689dee17034a705afd794f0c8374fffd22927120433a4ef9fe297a36890 +size 56054 diff --git a/Content/Weapons/FireWand/BP_FireWandWeapon.uasset b/Content/Weapons/FireWand/BP_FireWandWeapon.uasset index b2dc8d0..1876d50 100644 --- a/Content/Weapons/FireWand/BP_FireWandWeapon.uasset +++ b/Content/Weapons/FireWand/BP_FireWandWeapon.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e64a0d18d7a13219d78e8bd814ef6666c42540970dce210c28c84cfdb0edff96 -size 23725 +oid sha256:b3144c811a307f21ecc7b915d375e59e4e75c6b51e1cd4c347a2712ebd15e1f2 +size 25300 diff --git a/Content/Weapons/MagicWand/BP_MagicWandProjectile.uasset b/Content/Weapons/MagicWand/BP_MagicWandProjectile.uasset index 0a9a550..360addc 100644 --- a/Content/Weapons/MagicWand/BP_MagicWandProjectile.uasset +++ b/Content/Weapons/MagicWand/BP_MagicWandProjectile.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2f8809a6fcba494cd880a6d71881d9b298e573ee7e7b180832e3c304f6beb111 -size 33337 +oid sha256:40ce9defb4a65134759df9b964e56df521b61d9b4012e719fe9931f97938a96e +size 35127 diff --git a/Source/vampires/EnemyCharacter.cpp b/Source/vampires/EnemyCharacter.cpp index eece1da..bfbcfbf 100644 --- a/Source/vampires/EnemyCharacter.cpp +++ b/Source/vampires/EnemyCharacter.cpp @@ -6,17 +6,23 @@ #include "EnemyDataAsset.h" #include "EXPPickup.h" #include "HealthComponent.h" +#include "MovieSceneTracksComponentTypes.h" #include "ObjectPoolComponent.h" #include "ObjectPoolManager.h" #include "PaperFlipbookComponent.h" #include "VampireAIController.h" #include "VampireGameMode.h" #include "Components/CapsuleComponent.h" +#include "Components/SphereComponent.h" #include "Kismet/GameplayStatics.h" AEnemyCharacter::AEnemyCharacter(const FObjectInitializer& ObjectInitializer) { ObjectPoolComponent = CreateDefaultSubobject(TEXT("Object Pool Component")); + + DamageSphere = CreateDefaultSubobject(TEXT("Damage Sphere")); + DamageSphere->SetupAttachment(RootComponent); + DamageSphere->SetSphereRadius(50.0f); } void AEnemyCharacter::BeginPlay() @@ -26,6 +32,10 @@ void AEnemyCharacter::BeginPlay() GetHealthComponent()->OnDeath.AddDynamic(this, &AEnemyCharacter::OnDeath); ObjectPoolComponent->OnRetrieve.BindUFunction(this, "ResetHealth"); + + DamageSphere->OnComponentBeginOverlap.AddDynamic(this, &AEnemyCharacter::OnDamageBeginOverlap); + DamageSphere->OnComponentEndOverlap.AddDynamic(this, &AEnemyCharacter::OnDamageEndOverlap); + DamageSphere->MoveIgnoreActors.Add(this); } void AEnemyCharacter::Tick(float DeltaTime) @@ -104,7 +114,37 @@ UHealthComponent* AEnemyCharacter::GetEnemyHealthComponent_Implementation() return GetHealthComponent(); } +void AEnemyCharacter::OnDamageBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) +{ + if (Cast(OtherActor) == UGameplayStatics::GetPlayerCharacter(GetWorld(), 0) && !Player.Contains(OtherActor)) + { + Player.Add(OtherActor); + + GetWorldTimerManager().SetTimer(DamageTimerHandle, this, &AEnemyCharacter::DamagePlayer, AttackCooldown, true); + } +} + +void AEnemyCharacter::OnDamageEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) +{ + if (Cast(OtherActor) == UGameplayStatics::GetPlayerCharacter(GetWorld(), 0) && Player.Contains(OtherActor)) + { + Player.Remove(OtherActor); + + GetWorldTimerManager().ClearTimer(DamageTimerHandle); + } +} + void AEnemyCharacter::ResetHealth() { GetHealthComponent()->ResetHealth(); } + +void AEnemyCharacter::DamagePlayer() +{ + for (auto player : Player) + { + UGameplayStatics::ApplyDamage(player, Damage, GetController(), this, nullptr); + } +} diff --git a/Source/vampires/EnemyCharacter.h b/Source/vampires/EnemyCharacter.h index e60f4f7..b4c928f 100644 --- a/Source/vampires/EnemyCharacter.h +++ b/Source/vampires/EnemyCharacter.h @@ -8,6 +8,7 @@ #include "Interfaces/Enemyable.h" #include "EnemyCharacter.generated.h" +class USphereComponent; class UObjectPoolComponent; class UBehaviorTree; class AEXPPickup; @@ -23,6 +24,15 @@ public: UPROPERTY(EditDefaultsOnly) TSubclassOf EXPPickupTemplate = nullptr; + UPROPERTY(EditDefaultsOnly) + TObjectPtr DamageSphere = nullptr; + + UPROPERTY(EditDefaultsOnly) + float Damage = 5.0f;; + + UPROPERTY(EditDefaultsOnly) + float AttackCooldown = 1.0f; + private: UPROPERTY(EditDefaultsOnly, Meta = (AllowPrivateAccess = "true")) UBehaviorTree* BehaviorTree = nullptr; @@ -31,6 +41,10 @@ private: UPickupDataAsset* PickupTemplate = nullptr; + TArray Player; + + FTimerHandle DamageTimerHandle; + public: AEnemyCharacter(const FObjectInitializer& ObjectInitializer); @@ -63,7 +77,18 @@ public: UFUNCTION() virtual UHealthComponent* GetEnemyHealthComponent_Implementation() override; + UFUNCTION() + void OnDamageBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, + const FHitResult& SweepResult); + + UFUNCTION() + void OnDamageEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, + int32 OtherBodyIndex); + private: UFUNCTION() void ResetHealth(); + + void DamagePlayer(); }; diff --git a/Source/vampires/Pickup.cpp b/Source/vampires/Pickup.cpp index d43fc50..1930751 100644 --- a/Source/vampires/Pickup.cpp +++ b/Source/vampires/Pickup.cpp @@ -21,10 +21,12 @@ APickup::APickup() InnerSphereComponent = CreateDefaultSubobject(TEXT("Inner Sphere Component")); SetRootComponent(InnerSphereComponent); InnerSphereComponent->SetSphereRadius(25.0f); + InnerSphereComponent->SetCollisionProfileName(TEXT("Pickup")); OuterSphereComponent = CreateDefaultSubobject(TEXT("Outer Sphere Component")); OuterSphereComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform); OuterSphereComponent->SetSphereRadius(250.0f); + OuterSphereComponent->SetCollisionProfileName(TEXT("Pickup")); StaticMeshComponent = CreateDefaultSubobject(TEXT("Static Mesh Component")); StaticMeshComponent->SetRelativeRotation(FRotator(0.0f, 90.0f, 0.0f)); diff --git a/Source/vampires/Projectile.cpp b/Source/vampires/Projectile.cpp index b6a9fad..8fa085b 100644 --- a/Source/vampires/Projectile.cpp +++ b/Source/vampires/Projectile.cpp @@ -20,6 +20,7 @@ AProjectile::AProjectile() SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); SetRootComponent(SphereComponent); SphereComponent->SetSphereRadius(50.0f); + SphereComponent->SetCollisionProfileName(TEXT("Weapon")); ProjectileMovement = CreateDefaultSubobject(TEXT("Projectile Movement")); ProjectileMovement->ProjectileGravityScale = 0.0f; diff --git a/Source/vampires/Weapons/GarlicWeapon.cpp b/Source/vampires/Weapons/GarlicWeapon.cpp index 81c6296..58e741d 100644 --- a/Source/vampires/Weapons/GarlicWeapon.cpp +++ b/Source/vampires/Weapons/GarlicWeapon.cpp @@ -11,6 +11,8 @@ AGarlicWeapon::AGarlicWeapon() SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); SphereComponent->SetupAttachment(RootComponent); SphereComponent->SetSphereRadius(150.0f); + SphereComponent->SetCollisionProfileName(TEXT("Weapon")); + Damage = 51.0f; Range = SphereComponent->GetScaledSphereRadius(); } diff --git a/Source/vampires/Weapons/LightningRingWeapon.cpp b/Source/vampires/Weapons/LightningRingWeapon.cpp index dfa5579..bbcba3a 100644 --- a/Source/vampires/Weapons/LightningRingWeapon.cpp +++ b/Source/vampires/Weapons/LightningRingWeapon.cpp @@ -11,6 +11,8 @@ ALightningRingWeapon::ALightningRingWeapon() SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); SphereComponent->SetupAttachment(RootComponent); SphereComponent->SetSphereRadius(1000.0f); + SphereComponent->SetCollisionProfileName(TEXT("Weapon")); + Damage = 51.0f; } diff --git a/Source/vampires/Weapons/PentagramWeapon.cpp b/Source/vampires/Weapons/PentagramWeapon.cpp index fe62fec..d218690 100644 --- a/Source/vampires/Weapons/PentagramWeapon.cpp +++ b/Source/vampires/Weapons/PentagramWeapon.cpp @@ -14,6 +14,8 @@ APentagramWeapon::APentagramWeapon() { BoxComponent = CreateDefaultSubobject(TEXT("Sphere Component")); BoxComponent->SetupAttachment(RootComponent); + BoxComponent->SetCollisionProfileName(TEXT("Weapon")); + WeaponCooldown = 90.0f; } diff --git a/Source/vampires/Weapons/ProjectileWeapon.cpp b/Source/vampires/Weapons/ProjectileWeapon.cpp index 9f9372a..bd03e68 100644 --- a/Source/vampires/Weapons/ProjectileWeapon.cpp +++ b/Source/vampires/Weapons/ProjectileWeapon.cpp @@ -14,6 +14,7 @@ AProjectileWeapon::AProjectileWeapon() { BoxComponent = CreateDefaultSubobject(TEXT("Box Component")); BoxComponent->SetupAttachment(RootComponent); + BoxComponent->SetCollisionProfileName(TEXT("Weapon")); } void AProjectileWeapon::BeginPlay()