Add Magic Wand Weapon

This commit is contained in:
baz 2024-07-31 01:58:09 +01:00
parent 6ef61f94b5
commit 0e81f48630
8 changed files with 220 additions and 2 deletions

BIN
Content/Weapons/MagicWand/BP_MagicWandProjectile.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Weapons/MagicWand/BP_MagicWandWeapon.uasset (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,34 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Projectile.h"
#include "EnemyCharacter.h"
#include "Weapons/MagicWandWeapon.h"
// Sets default values
AProjectile::AProjectile()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SphereComponent->SetupAttachment(RootComponent);
SphereComponent->SetSphereRadius(50.0f);
}
// Called when the game starts or when spawned
void AProjectile::BeginPlay()
{
Super::BeginPlay();
AMagicWandWeapon* OwnerWeapon = Cast<AMagicWandWeapon>(GetOwner());
SphereComponent->OnComponentBeginOverlap.AddDynamic(OwnerWeapon, &AMagicWandWeapon::OnProjectileBeginOverlap);
}
// Called every frame
void AProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
SetActorLocation(GetActorLocation() + (TargetDirection * ProjectileSpeed));
}

View File

@ -0,0 +1,35 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/SphereComponent.h"
#include "GameFramework/Actor.h"
#include "Projectile.generated.h"
UCLASS()
class VAMPIRES_API AProjectile : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
USphereComponent* SphereComponent = nullptr;
FVector TargetDirection = FVector::ZeroVector;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float ProjectileSpeed = 1.0f;
public:
// Sets default values for this actor's properties
AProjectile();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};

View File

@ -16,6 +16,8 @@ AWeapon::AWeapon()
void AWeapon::BeginPlay() void AWeapon::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
TArray<AWeapon*> example;
GetWorldTimerManager().SetTimer(WeaponTimerHandle, this, &AWeapon::FireWeaponAction, WeaponCooldown, true); GetWorldTimerManager().SetTimer(WeaponTimerHandle, this, &AWeapon::FireWeaponAction, WeaponCooldown, true);
UEXPComponent* expcomponent = GetOwner()->GetComponentByClass<UEXPComponent>(); UEXPComponent* expcomponent = GetOwner()->GetComponentByClass<UEXPComponent>();

View File

@ -12,10 +12,10 @@ class VAMPIRES_API AWeapon : public AActor
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties")
float WeaponCooldown = 1.0f; float WeaponCooldown = 1.0f;
UPROPERTY(BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties")
float Damage; float Damage;
private: private:

View File

@ -0,0 +1,91 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "MagicWandWeapon.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetMathLibrary.h"
#include "vampires/EnemyCharacter.h"
AMagicWandWeapon::AMagicWandWeapon()
{
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SphereComponent->SetupAttachment(RootComponent);
SphereComponent->SetSphereRadius(1000.0f);
}
void AMagicWandWeapon::BeginPlay()
{
Super::BeginPlay();
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AMagicWandWeapon::OnWeaponBeginOverlap);
SphereComponent->OnComponentEndOverlap.AddDynamic(this, &AMagicWandWeapon::OnWeaponEndOverlap);
}
void AMagicWandWeapon::FireWeaponAction_Implementation()
{
Super::FireWeaponAction_Implementation();
if (IsValid(ProjectileTemplate))
{
float distance = 0.0f;
AActor* nearestActor = UGameplayStatics::FindNearestActor(GetActorLocation(), OverlappedEnemies, distance);
if (nearestActor)
{
FActorSpawnParameters actorSpawnParameters;
actorSpawnParameters.Owner = this;
actorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
actorSpawnParameters.TransformScaleMethod = ESpawnActorScaleMethod::MultiplyWithRoot;
AProjectile* projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetTransform(),
actorSpawnParameters);
FVector direction = UKismetMathLibrary::GetDirectionUnitVector(
GetActorLocation(), nearestActor->GetActorLocation());
direction.Z = 0.0;
direction.Normalize();
projectile->TargetDirection = direction;
}
}
}
void AMagicWandWeapon::OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Add(Enemy);
}
}
void AMagicWandWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Remove(Enemy);
}
}
void AMagicWandWeapon::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
UHealthComponent* EnemyHealthComponent = Enemy->GetHealthComponent();
if (!EnemyHealthComponent->GetIsDead())
{
AController* ownerController = nullptr;
if (AVampireCharacter* character = Cast<AVampireCharacter>(GetOwner()))
{
ownerController = character->GetController();
}
EnemyHealthComponent->TakeDamage(Enemy, Damage, nullptr, ownerController, this);
OverlappedComponent->GetAttachmentRootActor()->Destroy();
}
}
}

View File

@ -0,0 +1,50 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "../Weapon.h"
#include "Components/SphereComponent.h"
#include "vampires/Projectile.h"
#include "MagicWandWeapon.generated.h"
/**
*
*/
UCLASS()
class VAMPIRES_API AMagicWandWeapon : public AWeapon
{
GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
USphereComponent* SphereComponent = nullptr;
UPROPERTY(EditAnywhere, Category = "Weapon Properties")
TSubclassOf<AProjectile> ProjectileTemplate = nullptr;
private:
TArray<AActor*> OverlappedEnemies = TArray<AActor*>();
public:
AMagicWandWeapon();
protected:
virtual void BeginPlay() override;
public:
virtual void FireWeaponAction_Implementation() override;
UFUNCTION()
void OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);
UFUNCTION()
void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex);
UFUNCTION()
void OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};