Move generic pickup functionality to base Pickup class

This commit is contained in:
Louis Hobbs 2023-06-24 15:11:09 +01:00
parent b1eed2ff2e
commit 79d61a200e
4 changed files with 80 additions and 66 deletions

View File

@ -2,13 +2,25 @@
#include "Pickup.h"
#include "PlayerCharacter.h"
// Sets default values
APickup::APickup()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
// 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;
PrimaryActorTick.SetTickFunctionEnable(true);
PrimaryActorTick.bStartWithTickEnabled = true;
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
SphereComponent->SetSphereRadius(25.0f, true);
SphereComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe);
SphereComponent->SetupAttachment(RootComponent);
PointLightComponent = CreateDefaultSubobject<UPointLightComponent>(TEXT("PointLightComponent"));
PointLightComponent->SetLightColor(FLinearColor::FromSRGBColor(LightColor));
PointLightComponent->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
@ -16,12 +28,28 @@ void APickup::BeginPlay()
{
Super::BeginPlay();
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnOverlapBegin);
PointLightComponent->SetWorldLocation(this->GetActorLocation());
}
// Called every frame
void APickup::Tick(float DeltaTime)
void APickup::Tick(const float DeltaTime)
{
Super::Tick(DeltaTime);
PointLightComponent->MarkRenderStateDirty();
// We have to do this because Unreal doesn't like it when you create lights in c++ apparently ::pain::
const float Sin = FMath::Abs(FMath::Sin(GetWorld()->GetRealTimeSeconds() * (LightFadeSpeed / 2)));
//PointLightComponent->SetLightBrightness(sin * MaxLightBrightness);
PointLightComponent->SetLightBrightness(Sin * MaxLightBrightness);
}
void APickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (Cast<APlayerCharacter>(OtherActor))
{
this->Destroy();
}
}

View File

@ -2,6 +2,8 @@
#pragma once
#include <Components/PointLightComponent.h>
#include <Components/SphereComponent.h>
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Pickup.generated.h"
@ -11,6 +13,23 @@ class NAKATOMI_API APickup : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float LightFadeSpeed = 1.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float MaxLightBrightness = 5000.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
FColor LightColor = FColor::White;
private:
UPROPERTY()
USphereComponent* SphereComponent;
UPROPERTY()
UPointLightComponent* PointLightComponent;
public:
// Sets default values for this actor's properties
APickup();
@ -23,4 +42,8 @@ public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION()
virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};

View File

@ -1,25 +1,11 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "WeaponPickup.h"
#include "PlayerCharacter.h"
// Sets default values
AWeaponPickup::AWeaponPickup()
{
// 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;
PrimaryActorTick.SetTickFunctionEnable(true);
PrimaryActorTick.bStartWithTickEnabled = true;
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
SphereComponent->SetSphereRadius(25.0f, true);
SphereComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe);
SphereComponent->SetupAttachment(RootComponent);
PointLightComponent = CreateDefaultSubobject<UPointLightComponent>(TEXT("PointLightComponent"));
PointLightComponent->SetLightColor(FLinearColor::FromSRGBColor(LightColor));
PointLightComponent->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
@ -31,13 +17,10 @@ void AWeaponPickup::BeginPlay()
{
SpawnWeapon();
}
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin);
PointLightComponent->SetWorldLocation(this->GetActorLocation());
}
// Called every frame
void AWeaponPickup::Tick(float DeltaTime)
void AWeaponPickup::Tick(const float DeltaTime)
{
Super::Tick(DeltaTime);
@ -47,15 +30,10 @@ void AWeaponPickup::Tick(float DeltaTime)
WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime);
// Bob weapon up and down
float Time = GetWorld()->GetRealTimeSeconds();
float Sine = FMath::Sin(Time * MovementSpeed);
const float Time = GetWorld()->GetRealTimeSeconds();
const float Sine = FMath::Sin(Time * MovementSpeed);
WeaponComponent->SetActorLocation(WeaponStartingLocation + ((MovementDirection * Sine) * MovementDistance));
}
PointLightComponent->MarkRenderStateDirty();
// We have to do this because Unreal doesn't like it when you create lights in c++ apparently ::pain::
float sin = FMath::Abs(FMath::Sin(GetWorld()->GetRealTimeSeconds() * (MovementSpeed / 2)));
PointLightComponent->SetLightBrightness(sin * MaxLightBrightness);
}
void AWeaponPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
@ -63,21 +41,22 @@ void AWeaponPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AAc
const FHitResult& SweepResult)
{
// TODO: Add extra checking here
auto player = Cast<APlayerCharacter>(OtherActor);
if (player && Weapon)
const auto Player = Cast<APlayerCharacter>(OtherActor);
if (Player && Weapon)
{
player->AddWeaponToInventory(Weapon);
player->WeaponInventory.Last()->SetWeaponProperties(*WeaponComponent->GetWeaponProperties());
this->Destroy();
Player->AddWeaponToInventory(Weapon);
Player->WeaponInventory.Last()->SetWeaponProperties(*WeaponComponent->GetWeaponProperties());
WeaponComponent->Destroy();
}
Super::OnOverlapBegin(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
}
void AWeaponPickup::SetWeapon(TSubclassOf<class AWeapon> weapon)
void AWeaponPickup::SetWeapon(const TSubclassOf<AWeapon> NewWeapon)
{
Weapon = weapon;
Weapon = NewWeapon;
if (WeaponComponent)
{
@ -92,7 +71,7 @@ FWeaponProperties* AWeaponPickup::GetWeaponProperties()
return &WeaponProperties;
}
void AWeaponPickup::SetWeaponProperties(FWeaponProperties FWeaponProperties)
void AWeaponPickup::SetWeaponProperties(const FWeaponProperties& FWeaponProperties) const
{
WeaponComponent->SetWeaponProperties(FWeaponProperties);
}
@ -102,7 +81,7 @@ void AWeaponPickup::SpawnWeapon()
FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
WeaponComponent = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParameters);
FAttachmentTransformRules TransformRules = FAttachmentTransformRules(
const FAttachmentTransformRules TransformRules = FAttachmentTransformRules(
EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true);
WeaponComponent->AttachToComponent(RootComponent, TransformRules);
WeaponComponent->SetActorRelativeLocation(FVector(0.0f, 0.0f, 5.0f));

View File

@ -2,22 +2,18 @@
#pragma once
#include <Components/PointLightComponent.h>
#include "Components/SphereComponent.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PlayerCharacter.h"
#include "Pickup.h"
#include "Weapon.h"
#include "WeaponPickup.generated.h"
UCLASS()
class NAKATOMI_API AWeaponPickup : public AActor
class NAKATOMI_API AWeaponPickup : public APickup
{
GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSubclassOf<class AWeapon> Weapon;
TSubclassOf<AWeapon> Weapon;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
FVector MovementDirection = FVector(0.0f, 0.0f, 1.0f);
@ -34,16 +30,7 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float RotationSpeed = 50.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float MaxLightBrightness = 5000.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
FColor LightColor = FColor::White;
private:
UPROPERTY()
USphereComponent* SphereComponent;
UPROPERTY()
AWeapon* WeaponComponent;
@ -52,9 +39,6 @@ private:
FWeaponProperties WeaponProperties;
UPROPERTY()
UPointLightComponent* PointLightComponent;
public:
// Sets default values for this actor's properties
AWeaponPickup();
@ -67,15 +51,15 @@ public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION()
void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override;
void SetWeapon(TSubclassOf<class AWeapon> weapon);
void SetWeapon(TSubclassOf<AWeapon> NewWeapon);
FWeaponProperties* GetWeaponProperties();
void SetWeaponProperties(FWeaponProperties FWeaponProperties);
void SetWeaponProperties(const FWeaponProperties& FWeaponProperties) const;
private:
void SpawnWeapon();