LEAP Documentation 40220
Documentation for the LEAP project
ItemSpawner.h
Go to the documentation of this file.
1// Copyright Blue Isle Studios Inc 2018. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "GameFramework/Actor.h"
7#include "ItemSpawner.generated.h"
8
9#define CLAIMED_SPAWN "ClaimedSpawn"
10#define ITEM_SPAWN "ItemSpawn"
11
12class USceneComponent;
13
14USTRUCT(Blueprintable)
16{
17 GENERATED_USTRUCT_BODY()
18
19 /* Which Item to spawn */
20 UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
21 TSubclassOf<class AActor> SpawnableItem = nullptr;
22 /* Limit spawning to spawnpoints containing this tag. If empty, it will spawn at any spawn point. */
23 UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
24 FName Tag = NAME_None;
25 /* The chance of this item spawning is SpawnWeight / AllSpawnWeights. If you have 3 Items with 100 weight, your chance to spawn would be 100/300 or 1 in 3. */
26 UPROPERTY(EditInstanceOnly, BlueprintReadWrite, meta = (ClampMin = "0.001", ClampMax = "1000.0", UIMin = "0.001", UIMax = "1000.0"))
27 float SpawnWeight = 100.0f;
28 /* What is the minimum and maximum number of items of this type that should be spawned? The SpawnMinMax of the group will supersede individual items should they conflict.
29 @ Example: 5 items all want to spawn at least 2 times but the spawn maximum of the group is 6. Only 6 items will be spawned. */
30 UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
31 FVector2D SpawnMinMax = FVector2D(1.0f, 2.0f);
32
33 uint32 SpawnedCount = 0;
34};
35
36USTRUCT(Blueprintable)
38{
39 GENERATED_USTRUCT_BODY()
40
41 FName GroupName = FName("New SpawnGroup");
42 /* List of actors used to spawn items at. Any actor components with the Tag "ItemSpawn" will also be considered as a viable spawn location.*/
43 UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
44 TArray<AActor*> Spawnpoints;
45 /* Which items can be spawned at SpawnPoints list? */
46 UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
47 TArray<FSpawnableItem> SpawnableItems;
48 /* What is the minimum and maximum number of items that should be spawned? This value will supersede any per item min/max settings.
49 @ Example: 5 SpawnableItems all want to spawn at least 2 times but the spawn maximum of the group is 6. Only 6 items will be spawned. */
50 UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
51 FVector2D SpawnMinMax = FVector2D(5.0f, 10.0f);
52 /* Each time an item randomly spawns, it's chance to spawn again will be multiplied by this value in order to lower its chance of dropping another time.*/
53 UPROPERTY(EditInstanceOnly, BlueprintReadWrite, meta = (ClampMin = "0.0", ClampMax = "1.0", UIMin = "0.0", UIMax = "1.0"))
54 float DuplicatesPenalty = 0.5f;
55};
56
57UCLASS()
58class PROJECTX_API AItemSpawner : public AActor
59{
60 GENERATED_BODY()
61
62public:
63 UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category = ItemSpawner)
64 TMap<FName, FSpawnGroup> SpawnGroups;
65
67
68protected:
69 virtual void BeginPlay() override;
70
71private:
72 UWorld* World = nullptr;
73
74 void BuildSpawnList(const FSpawnGroup& SpawnGroup, TArray<USceneComponent*>& SpawnLocations);
75 void SpawnItems(FSpawnGroup& SpawnGroup, const TArray<USceneComponent*>& SpawnLocations);
76 void SpawnItem(FSpawnableItem& Item, TArray<USceneComponent*>& SpawnLocations);
77 void BuildFilteredSpawnList(const FSpawnableItem& SpawnableItem, TArray<USceneComponent*>& FilteredSpawnList, const TArray<USceneComponent*>& SpawnLocations);
78 void ClampSpawnAmountsToGroup(FSpawnGroup& SpawnGroup, float& SpawnWeightTotal, uint32& TotalItemSpawnMinimums, uint32& TotalItemSpawnMaximums);
79 void Cleanup(TArray<USceneComponent*>& SpawnLocations);
80
81 USceneComponent* GetUnusedSpawnPoint(const TArray<USceneComponent*>& SpawnLocations) const;
82
83 bool IsSpawnableItemValid(FSpawnableItem& SpawnableItem, const FSpawnGroup& SpawnGroup, const TArray<USceneComponent*>& SpawnLocations);
84 bool IsSpawnGroupValid(FSpawnGroup& SpawnGroup, const TArray<USceneComponent*>& SpawnLocations);
85
86 FORCEINLINE const uint32 GetSpawnGroupMinimumSpawns(const FSpawnGroup& SpawnGroup) const { return (uint32)SpawnGroup.SpawnMinMax.X; }
87 FORCEINLINE const uint32 GetSpawnGroupMaximumSpawns(const FSpawnGroup& SpawnGroup) const { return (uint32)SpawnGroup.SpawnMinMax.Y; }
88 FORCEINLINE const uint32 GetItemMinimumSpawns(const FSpawnableItem& SpawnableItem) const { return (uint32)SpawnableItem.SpawnMinMax.X; }
89 FORCEINLINE const uint32 GetItemMaximumSpawns(const FSpawnableItem& SpawnableItem) const { return (uint32)SpawnableItem.SpawnMinMax.Y; }
90
91};
Definition: ItemSpawner.h:59
Definition: ItemSpawner.h:38
FVector2D SpawnMinMax
Definition: ItemSpawner.h:51
Definition: ItemSpawner.h:16
FVector2D SpawnMinMax
Definition: ItemSpawner.h:31