Add Magic Wand Weapon
This commit is contained in:
parent
6ef61f94b5
commit
0e81f48630
Binary file not shown.
Binary file not shown.
|
@ -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));
|
||||
}
|
||||
|
|
@ -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;
|
||||
};
|
|
@ -16,6 +16,8 @@ AWeapon::AWeapon()
|
|||
void AWeapon::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
TArray<AWeapon*> example;
|
||||
GetWorldTimerManager().SetTimer(WeaponTimerHandle, this, &AWeapon::FireWeaponAction, WeaponCooldown, true);
|
||||
UEXPComponent* expcomponent = GetOwner()->GetComponentByClass<UEXPComponent>();
|
||||
|
||||
|
|
|
@ -12,10 +12,10 @@ class VAMPIRES_API AWeapon : public AActor
|
|||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
UPROPERTY(BlueprintReadWrite)
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties")
|
||||
float WeaponCooldown = 1.0f;
|
||||
|
||||
UPROPERTY(BlueprintReadWrite)
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties")
|
||||
float Damage;
|
||||
|
||||
private:
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
};
|
Loading…
Reference in New Issue