Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ammo spawner #161

Merged
merged 5 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Config/DefaultGame.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ ProjectID = BACFB3F848AE10EDFB548D9F7BDABCA4
ProjectName = Cloud9
CopyrightNotice = Copyright (c) 2023 Alexei Gladkikh
CopyrightNotice=Copyright (c) 2024 Alexei Gladkikh
ProjectVersion=0.11.5
LicensingTerms=MIT

[/Script/Cloud9.Cloud9Character]
FixedCameraPitch = -45.0
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Content/Maps/warmup.umap
Binary file not shown.
Binary file modified Content/Vefects/VFX/Misc/NS_RingGlow_OneTimePrimaryWeapon.uasset
Binary file not shown.
Binary file added Importing/case_heave_weapon.blend
Binary file not shown.
Binary file added Importing/case_heave_weapon.fbx
Binary file not shown.
Binary file added Importing/case_light_weapon.blend
Binary file not shown.
Binary file added Importing/case_light_weapon.fbx
Binary file not shown.
Binary file added Importing/case_pistol.blend
Binary file not shown.
Binary file added Importing/case_pistol.fbx
Binary file not shown.
Binary file added Importing/case_pistol_heavy.blend
Binary file not shown.
Binary file added Importing/case_pistol_heavy.fbx
Binary file not shown.
Binary file added Importing/case_tools_heavy.blend
Binary file not shown.
Binary file added Importing/case_tools_heavy.fbx
Binary file not shown.
Binary file added Importing/case_weapon_rifles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Importing/case_weapon_rifles.psd
Binary file not shown.
Binary file added Importing/case_weapon_sniper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Importing/case_weapon_sniper.psd
Binary file not shown.
Binary file added Importing/coop_ammo_stash_full.blend
Binary file not shown.
Binary file added Importing/coop_ammo_stash_full.fbx
Binary file not shown.
Binary file added Importing/crate_ammobox.blend
Binary file not shown.
Binary file added Importing/crate_ammobox.fbx
Binary file not shown.
Binary file added Importing/textures/materials_dev_flat_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Source/Cloud9/Character/Components/Cloud9Inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class CLOUD9_API UCloud9Inventory
UFUNCTION(BlueprintCallable)
ACloud9WeaponBase* GetWeaponAt(EWeaponSlot Slot) const;

template <typename WeaponType>
WeaponType* GetWeaponAt(EWeaponSlot Slot) const { return Cast<WeaponType>(GetWeaponAt(Slot)); }

/**
* Function shoves existed weapon into inventory
*
Expand Down
12 changes: 12 additions & 0 deletions Source/Cloud9/Weapon/Classes/Cloud9WeaponFirearm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ int ACloud9WeaponFirearm::GetCurrentAmmo() const { return CurrentAmmo; }

int ACloud9WeaponFirearm::GetAmmoInReserve() const { return AmmoInReserve; }

bool ACloud9WeaponFirearm::AddAmmoInReserve(int Count)
{
if (let NewAmmoInReserve = FMath::Min(AmmoInReserve + Count, GetWeaponInfo()->MaxAmmoInReserve);
NewAmmoInReserve != AmmoInReserve)
{
AmmoInReserve = NewAmmoInReserve;
return true;
}

return false;
}

EFirearmFireStatus ACloud9WeaponFirearm::Fire(
const FFirearmWeaponInfo* WeaponInfo,
const FFirearmCommonData& FirearmCommonData)
Expand Down
1 change: 1 addition & 0 deletions Source/Cloud9/Weapon/Classes/Cloud9WeaponFirearm.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class CLOUD9_API ACloud9WeaponFirearm : public ACloud9WeaponBase

int GetCurrentAmmo() const;
int GetAmmoInReserve() const;
bool AddAmmoInReserve(int Count);

protected:
virtual bool OnInitialize(const FWeaponId& NewWeaponId, FName NewWeaponSkin) override;
Expand Down
2 changes: 1 addition & 1 deletion Source/Cloud9/Weapon/Classes/Cloud9WeaponGrenade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

#include "DrawDebugHelpers.h"

#include "Cloud9/Tools/Extensions/TVariant.h"
#include "Cloud9/Character/Cloud9Character.h"
#include "Cloud9/Game/Cloud9DeveloperSettings.h"
#include "Cloud9/Game/Cloud9GameInstance.h"
#include "Cloud9/Game/Cloud9PlayerController.h"
#include "Cloud9/Tools/Extensions/TVariant.h"
#include "Cloud9/Weapon/Sounds/Cloud9SoundPlayer.h"
#include "Cloud9/Weapon/Tables/WeaponTableGrenade.h"
#include "PhysicsEngine/RadialForceComponent.h"
Expand Down
3 changes: 0 additions & 3 deletions Source/Cloud9/Weapon/Classes/Cloud9WeaponGrenade.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#pragma once

#include "CoreMinimal.h"
#include "Cloud9/Game/Cloud9DeveloperSettings.h"

#include "Cloud9/Weapon/Classes/Cloud9WeaponBase.h"
#include "Cloud9/Weapon/Enums/GrenadeNames.h"
Expand Down Expand Up @@ -99,5 +98,3 @@ class CLOUD9_API ACloud9WeaponGrenade : public ACloud9WeaponBase
UPROPERTY(Category=Weapon, BlueprintReadOnly, meta=(AllowPrivateAccess))
FTimerHandle ActiveTimerHandle;
};

struct FGrenadeWeaponInfo;
70 changes: 70 additions & 0 deletions Source/Cloud9/Weapon/Utils/AmmoSpawner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2024 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "AmmoSpawner.h"

#include "Cloud9/Character/Cloud9Character.h"
#include "Cloud9/Weapon/Classes/Cloud9WeaponBase.h"
#include "Cloud9/Weapon/Classes/Cloud9WeaponFirearm.h"
#include "Engine/StaticMeshActor.h"

AAmmoSpawner::AAmmoSpawner()
{
WeaponType = EWeaponType::NoWeapon;
AmmoCount = 0;
SampleMesh = nullptr;
}

bool AAmmoSpawner::ActivateSpawner(ACloud9Character* Character)
{
if (let Inventory = Character->GetInventory(); IsValid(Inventory))
{
if (let Weapon = Inventory->GetWeaponAt<ACloud9WeaponFirearm>(EWeaponSlot::Main);
IsValid(Weapon) and Weapon->GetWeaponType() == WeaponType)
{
return Weapon->AddAmmoInReserve(AmmoCount);
}

if (let Weapon = Inventory->GetWeaponAt<ACloud9WeaponFirearm>(EWeaponSlot::Pistol);
IsValid(Weapon) and Weapon->GetWeaponType() == WeaponType)
{
return Weapon->AddAmmoInReserve(AmmoCount);
}
}

return false;
}

AActor* AAmmoSpawner::CreateChildActor()
{
if (IsValid(SampleMesh))
{
let ItemSample = CreateItemSample<AStaticMeshActor>();
let StaticMeshComponent = ItemSample->GetStaticMeshComponent();
StaticMeshComponent->SetMobility(EComponentMobility::Movable);
StaticMeshComponent->SetStaticMesh(SampleMesh);
return ItemSample;
}

return nullptr;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Alexei Gladkikh
// Copyright (c) 2024 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand All @@ -24,20 +24,29 @@
#pragma once

#include "CoreMinimal.h"
#include "Components/ChildActorComponent.h"
#include "ItemSpawner.h"
#include "Cloud9/Weapon/Enums/WeaponType.h"
#include "AmmoSpawner.generated.h"

#include "WeaponInitializerComponent.generated.h"
UCLASS()
class CLOUD9_API AAmmoSpawner : public AItemSpawner
{
GENERATED_BODY()

struct FWeaponConfig;
public:
AAmmoSpawner();

// !DEPRECATED!
virtual AActor* CreateChildActor() override;

UCLASS(Blueprintable, meta=(BlueprintSpawnableComponent))
class CLOUD9_API UWeaponInitializerComponent : public UChildActorComponent
{
GENERATED_BODY()
virtual bool ActivateSpawner(ACloud9Character* Character) override;

protected:
UPROPERTY(EditDefaultsOnly, Category=Config)
EWeaponType WeaponType;

UPROPERTY(EditDefaultsOnly, Category=Config)
UStaticMesh* SampleMesh;

public: // functions
UFUNCTION(BlueprintCallable)
void SetWeaponConfig(const FWeaponConfig& Config);
UPROPERTY(EditAnywhere, Category=Config)
int AmmoCount;
};
165 changes: 165 additions & 0 deletions Source/Cloud9/Weapon/Utils/ItemSpawner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "ItemSpawner.h"

#include "NiagaraComponent.h"
#include "Components/BoxComponent.h"
#include "Cloud9/Character/Cloud9Character.h"
#include "Cloud9/Game/Cloud9DeveloperSettings.h"

#include "Cloud9/Weapon/Classes/Cloud9WeaponBase.h"
#include "Cloud9/Weapon/Sounds/Cloud9SoundPlayer.h"

FName AItemSpawner::RootComponentName = "RootComponent";
FName AItemSpawner::TriggerBoxComponentName = "TriggerBox";
FName AItemSpawner::ItemSampleComponentName = "ItemSample";
FName AItemSpawner::GlowingEffectComponentName = "GlowingEffect";
FName AItemSpawner::SpriteComponentName = "Sprite";

AItemSpawner::AItemSpawner()
{
PrimaryActorTick.bCanEverTick = true;

RootComponent = CreateDefaultSubobject<USceneComponent>(RootComponentName);

RootComponent->SetMobility(EComponentMobility::Movable);

TriggerBoxComponent = CreateDefaultSubobject<UBoxComponent>(TriggerBoxComponentName);
ItemSampleComponent = CreateDefaultSubobject<UChildActorComponent>(ItemSampleComponentName);
GlowingEffectComponent = CreateDefaultSubobject<UNiagaraComponent>(GlowingEffectComponentName);

TriggerBoxComponent->SetupAttachment(RootComponent);
ItemSampleComponent->SetupAttachment(RootComponent);
GlowingEffectComponent->SetupAttachment(RootComponent);

log(Error, "ATTACHEMENT DONE %s", *GetName())

bIsDestroyOnActivation = false;
GlowingEffect = nullptr;
bIsGlowingEffectPreview = true;
SampleOffset = FVector::ZeroVector;
SampleRotator = FRotator::ZeroRotator;
SampleScale = 1.5f;
RotationSpeedInDegree = 0.0f;
bIsRandomInitialRotation = false;
ZoneSize = {20.0f, 20.0f, 20.0f};

TriggerBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AItemSpawner::OnBeginOverlap);
}

AActor* AItemSpawner::CreateChildActor() { return nullptr; }

bool AItemSpawner::ActivateSpawner(ACloud9Character* Character) { return false; }

void AItemSpawner::OnConstruction(const FTransform& Transform)
{
Super::OnConstruction(Transform);

let ItemSample = CreateChildActor();

if (not IsValid(ItemSample))
{
log(Error, "[ItemSpawner=%s] Can't create spawner sample", *GetName());
return;
}

ItemSample->SetActorTickEnabled(false);
ItemSample->UpdateComponentTransforms();
ItemSample->SetActorEnableCollision(false);
ItemSample->SetActorRelativeLocation(SampleOffset);
ItemSample->SetActorRelativeRotation(SampleRotator);
ItemSample->SetActorScale3D({SampleScale, SampleScale, SampleScale});

TriggerBoxComponent->SetBoxExtent(ZoneSize);

// Remove previous set glowing effect (required if preview disabled)
GlowingEffectComponent->SetAsset(nullptr);

if (IsValid(GlowingEffect) and bIsGlowingEffectPreview)
{
GlowingEffectComponent->SetAsset(GlowingEffect);
}
}

void AItemSpawner::BeginPlay()
{
Super::BeginPlay();

let ItemSample = ItemSampleComponent->GetChildActor();

if (not IsValid(ItemSample))
{
log(Error, "[AItemSpawnerBase=%s] Invalid spawner", *GetName());
return;
}

if (bIsRandomInitialRotation)
{
var Rotation = ItemSample->GetActorRotation();
Rotation.Yaw = FMath::RandRange(0.0f, 360.0f);
ItemSample->SetActorRotation(Rotation);
}

if (IsValid(GlowingEffect) and not bIsGlowingEffectPreview)
{
GlowingEffectComponent->SetAsset(GlowingEffect);
}
}

void AItemSpawner::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

if (let ItemSample = ItemSampleComponent->GetChildActor(); IsValid(ItemSample))
{
var Rotation = ItemSample->GetActorRotation();
Rotation.Yaw += DeltaTime * RotationSpeedInDegree;
ItemSample->SetActorRotation(Rotation);
}
}

void AItemSpawner::OnBeginOverlap(
UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComponent,
int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult& Hit)
{
if (let Character = Cast<ACloud9Character>(OtherActor);
IsValid(Character) and IsValid(ItemSampleComponent->GetChildActor()))
{
if (ActivateSpawner(Character))
{
let Settings = UCloud9DeveloperSettings::Get();

UCloud9SoundPlayer::PlayRandomSound(ActivationSounds, GetActorLocation(), Settings->Volume);

if (bIsDestroyOnActivation)
{
Destroy();
}
}
}
}
Loading