diff --git a/Source/Nakatomi/Pickup.cpp b/Source/Nakatomi/Pickup.cpp index 329f487..aea5baa 100644 --- a/Source/Nakatomi/Pickup.cpp +++ b/Source/Nakatomi/Pickup.cpp @@ -2,26 +2,54 @@ #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(TEXT("SphereComponent")); + SphereComponent->SetSphereRadius(25.0f, true); + SphereComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe); + + SphereComponent->SetupAttachment(RootComponent); + + PointLightComponent = CreateDefaultSubobject(TEXT("PointLightComponent")); + PointLightComponent->SetLightColor(FLinearColor::FromSRGBColor(LightColor)); + PointLightComponent->SetupAttachment(RootComponent); } // Called when the game starts or when spawned 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(OtherActor)) + { + this->Destroy(); + } +} diff --git a/Source/Nakatomi/Pickup.h b/Source/Nakatomi/Pickup.h index 90d9778..7519ab3 100644 --- a/Source/Nakatomi/Pickup.h +++ b/Source/Nakatomi/Pickup.h @@ -2,6 +2,8 @@ #pragma once +#include +#include #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Pickup.generated.h" @@ -10,8 +12,25 @@ UCLASS() class NAKATOMI_API APickup : public AActor { GENERATED_BODY() - -public: + +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(); @@ -19,8 +38,12 @@ protected: // Called when the game starts or when spawned virtual void BeginPlay() override; -public: +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); }; diff --git a/Source/Nakatomi/WeaponPickup.cpp b/Source/Nakatomi/WeaponPickup.cpp index 3aa9c80..921cb06 100644 --- a/Source/Nakatomi/WeaponPickup.cpp +++ b/Source/Nakatomi/WeaponPickup.cpp @@ -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(TEXT("SphereComponent")); - SphereComponent->SetSphereRadius(25.0f, true); - SphereComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe); - - SphereComponent->SetupAttachment(RootComponent); - - PointLightComponent = CreateDefaultSubobject(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(OtherActor); - if (player && Weapon) + const auto Player = Cast(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 weapon) +void AWeaponPickup::SetWeapon(const TSubclassOf 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(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)); diff --git a/Source/Nakatomi/WeaponPickup.h b/Source/Nakatomi/WeaponPickup.h index 90f75ae..61617ce 100644 --- a/Source/Nakatomi/WeaponPickup.h +++ b/Source/Nakatomi/WeaponPickup.h @@ -2,22 +2,18 @@ #pragma once -#include -#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 Weapon; + TSubclassOf 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 weapon); + void SetWeapon(TSubclassOf NewWeapon); FWeaponProperties* GetWeaponProperties(); - void SetWeaponProperties(FWeaponProperties FWeaponProperties); + void SetWeaponProperties(const FWeaponProperties& FWeaponProperties) const; private: void SpawnWeapon();