From 2d48d7c7034d18da66128c244c6efc475f97bbe0 Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Fri, 6 Jan 2023 01:33:26 +0000 Subject: [PATCH] Add barebones weapon pickup --- .../Weapons/Pickups/TestWeaponPickup.uasset | 3 ++ Content/Weapons/TestWeapon.uasset | 3 ++ Source/Nakatomi/WeaponPickup.cpp | 49 +++++++++++++++++ Source/Nakatomi/WeaponPickup.h | 53 +++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 Content/Weapons/Pickups/TestWeaponPickup.uasset create mode 100644 Content/Weapons/TestWeapon.uasset create mode 100644 Source/Nakatomi/WeaponPickup.cpp create mode 100644 Source/Nakatomi/WeaponPickup.h diff --git a/Content/Weapons/Pickups/TestWeaponPickup.uasset b/Content/Weapons/Pickups/TestWeaponPickup.uasset new file mode 100644 index 0000000..770c14f --- /dev/null +++ b/Content/Weapons/Pickups/TestWeaponPickup.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bf48f8fcb8b8c619d770f454eb062baf14a456070d8fe3a8d2fb041902ca45c2 +size 22240 diff --git a/Content/Weapons/TestWeapon.uasset b/Content/Weapons/TestWeapon.uasset new file mode 100644 index 0000000..f2fd9a0 --- /dev/null +++ b/Content/Weapons/TestWeapon.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:19a6e31e88cb9074bb9d596836448b1b73fa06b4c9c8e34e905ba64b54f6be9e +size 27234 diff --git a/Source/Nakatomi/WeaponPickup.cpp b/Source/Nakatomi/WeaponPickup.cpp new file mode 100644 index 0000000..09fc449 --- /dev/null +++ b/Source/Nakatomi/WeaponPickup.cpp @@ -0,0 +1,49 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "WeaponPickup.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->SetupAttachment(RootComponent); +} + +// Called when the game starts or when spawned +void AWeaponPickup::BeginPlay() +{ + Super::BeginPlay(); + + FActorSpawnParameters SpawnParameters; + SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; + + WeaponComponent = GetWorld()->SpawnActor(Weapon, SpawnParameters); + + FAttachmentTransformRules TransformRules = FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true); + WeaponComponent->AttachToComponent(RootComponent, TransformRules); + WeaponComponent->SetActorRelativeLocation(FVector(0.0f, 0.0f, 5.0f)); + WeaponComponent->SetActorEnableCollision(false); + + WeaponStartingLocation = WeaponComponent->GetActorLocation(); +} + +// Called every frame +void AWeaponPickup::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + + // Rotate Weapon in desired direction + WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime); + + // Bob weapon up and down + float Time = GetWorld()->GetRealTimeSeconds(); + float Sine = FMath::Sin(Time * MovementSpeed); + WeaponComponent->SetActorLocation(WeaponStartingLocation + (MovementDirection * Sine)); +} + diff --git a/Source/Nakatomi/WeaponPickup.h b/Source/Nakatomi/WeaponPickup.h new file mode 100644 index 0000000..70e1429 --- /dev/null +++ b/Source/Nakatomi/WeaponPickup.h @@ -0,0 +1,53 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "Components/SphereComponent.h" +#include "CoreMinimal.h" +#include "GameFramework/Actor.h" +#include "Weapon.h" +#include "WeaponPickup.generated.h" + +UCLASS() +class NAKATOMI_API AWeaponPickup : public AActor +{ + GENERATED_BODY() + +public: + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + TSubclassOf Weapon; + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + FVector MovementDirection = FVector(0.0f, 1.0f, 0.0f); + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + float MovementSpeed = 1.0f; + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + FRotator SpinRotation = FRotator(0, 1, 0); + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + float RotationSpeed = 1.0f; + +private: + + USphereComponent* SphereComponent; + + AWeapon* WeaponComponent; + + FVector WeaponStartingLocation; + +public: + // Sets default values for this actor's properties + AWeaponPickup(); + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + +public: + // Called every frame + virtual void Tick(float DeltaTime) override; + +};