Skip to content

Commit 18e58c2

Browse files
authored
Merge branch 'main' into wc/fps_profiler
2 parents c3074cf + 402e524 commit 18e58c2

File tree

76 files changed

+1718
-141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1718
-141
lines changed

Gems/CsvSpawner/Code/Include/CsvSpawner/CsvSpawnerTypeIds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace CsvSpawner
1818
inline constexpr const char* CsvSpawnableEntityInfoTypeId = "{6acce47e-9cac-4a6e-8726-ea614fb2d30d}";
1919
inline constexpr const char* CsvSpawnableAssetConfigurationTypeId = "{d04ea4de-b4dc-4e76-b742-c1d77203bc3e}";
2020
inline constexpr const char* CsvSpawnerEditorComponentTypeId = "{8b016a40-c585-48a1-a3e5-2814f599418c}";
21+
inline constexpr const char* CsvSpawnerEditorTerrainSettingsConfigTypeId = "{589B418A-A2A5-4DDE-9722-2952F79037B0}";
2122
inline constexpr const char* CsvSpawnerComponentTypeId = "{59b31372-1f3c-4733-b61b-0fe94b5a8f3e}";
2223

2324
// Interface TypeIds

Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerComponent.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,48 @@ namespace CsvSpawner
4040
{
4141
}
4242

43-
void CsvSpawnerComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
43+
void CsvSpawnerComponent::Activate()
4444
{
45-
m_spawnedTickets = CsvSpawnerUtils::SpawnEntities(
46-
m_spawnableEntityInfo, m_spawnableAssetConfigurations, m_defaultSeed, AzPhysics::DefaultPhysicsSceneName, this->GetEntityId());
47-
AZ::TickBus ::Handler::BusDisconnect();
45+
if (IsTerrainAvailable())
46+
{
47+
AzFramework::Terrain::TerrainDataNotificationBus::Handler::BusConnect();
48+
}
49+
else
50+
{
51+
OnTerrainDataCreateEnd();
52+
}
4853
}
4954

50-
int CsvSpawnerComponent::GetTickOrder()
55+
void CsvSpawnerComponent::Deactivate()
5156
{
52-
return AZ::TICK_LAST;
57+
OnTerrainDataDestroyBegin();
5358
}
5459

55-
void CsvSpawnerComponent::Activate()
60+
void CsvSpawnerComponent::OnTerrainDataCreateEnd()
5661
{
57-
AZ::TickBus::Handler::BusConnect();
62+
if (m_terrainCreatedOnlyOnce)
63+
{
64+
return;
65+
}
66+
67+
AZ::TickBus::QueueFunction(
68+
[this]()
69+
{
70+
m_spawnedTickets = CsvSpawnerUtils::SpawnEntities(
71+
m_spawnableEntityInfo,
72+
m_spawnableAssetConfigurations,
73+
m_defaultSeed,
74+
AzPhysics::DefaultPhysicsSceneName,
75+
this->GetEntityId());
76+
});
77+
78+
// Init only once, even if level have multiple terrains
79+
m_terrainCreatedOnlyOnce = true;
5880
}
5981

60-
void CsvSpawnerComponent::Deactivate()
82+
void CsvSpawnerComponent::OnTerrainDataDestroyBegin()
6183
{
62-
AZ::TickBus::Handler::BusDisconnect();
84+
m_terrainCreatedOnlyOnce = false;
85+
AzFramework::Terrain::TerrainDataNotificationBus::Handler::BusDisconnect();
6386
}
64-
6587
} // namespace CsvSpawner

Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerComponent.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#pragma once
1212

13+
#include "AzFramework/Terrain/TerrainDataRequestBus.h"
1314
#include "CsvSpawnerUtils.h"
1415

1516
#include <AzCore/Component/Component.h>
@@ -25,7 +26,7 @@ namespace CsvSpawner
2526
//! dependent on a CSV file).
2627
class CsvSpawnerComponent
2728
: public AZ::Component
28-
, private AZ::TickBus::Handler
29+
, protected AzFramework::Terrain::TerrainDataNotificationBus::Handler
2930
{
3031
public:
3132
AZ_COMPONENT(CsvSpawnerComponent, CsvSpawnerComponentTypeId);
@@ -37,23 +38,26 @@ namespace CsvSpawner
3738
const AZStd::vector<CsvSpawnableEntityInfo>& spawnableEntityInfo,
3839
AZ::u64 defaultSeed);
3940

40-
~CsvSpawnerComponent() = default;
41+
~CsvSpawnerComponent() override = default;
4142

4243
static void Reflect(AZ::ReflectContext* context);
4344

4445
// AZ::Component overrides
4546
void Activate() override;
4647
void Deactivate() override;
4748

48-
private:
49-
// AZ::TickBus::Handler overrides
50-
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
51-
int GetTickOrder() override;
49+
// AzFramework::Terrain::TerrainDataNotificationBus::Handler overrides
50+
void OnTerrainDataCreateEnd() override;
51+
void OnTerrainDataDestroyBegin() override;
5252

53+
private:
5354
AZStd::unordered_map<AZStd::string, CsvSpawnableAssetConfiguration> m_spawnableAssetConfigurations; //!< List of assets to spawn
5455
AZStd::vector<CsvSpawnableEntityInfo> m_spawnableEntityInfo; //!< List of entities to spawn
5556
AZ::u64 m_defaultSeed{ 0 }; //!< Default seed value
5657

5758
AZStd::unordered_map<int, AzFramework::EntitySpawnTicket> m_spawnedTickets;
59+
60+
// Terrain notify
61+
bool m_terrainCreatedOnlyOnce{ false }; //!< Is terrain fully generated once
5862
};
5963
} // namespace CsvSpawner

Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerEditorComponent.cpp

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,36 @@
99
*/
1010

1111
#include "CsvSpawnerEditorComponent.h"
12-
#include "AzCore/Debug/Trace.h"
1312
#include "CsvSpawnerComponent.h"
1413
#include "CsvSpawnerCsvParser.h"
1514
#include "CsvSpawnerUtils.h"
1615

1716
#include <AzCore/Component/TransformBus.h>
17+
#include <AzCore/Debug/Trace.h>
1818
#include <AzCore/IO/Path/Path.h>
1919
#include <AzCore/Serialization/EditContext.h>
2020
#include <AzFramework/Physics/Common/PhysicsTypes.h>
21+
#include <AzFramework/Physics/PhysicsScene.h>
2122
#include <AzToolsFramework/API/EditorAssetSystemAPI.h>
2223
#include <AzToolsFramework/Viewport/ViewportMessages.h>
23-
#include <thread>
2424

2525
namespace CsvSpawner
2626
{
27-
2827
void CsvSpawnerEditorComponent::Reflect(AZ::ReflectContext* context)
2928
{
3029
AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
3130
if (serializeContext)
3231
{
33-
serializeContext->Class<CsvSpawnerEditorComponent, AzToolsFramework::Components::EditorComponentBase>()
32+
CsvSpawnerEditorTerrainSettingsConfig::Reflect(context);
33+
34+
serializeContext->Class<CsvSpawnerEditorComponent, EditorComponentBase>()
3435
->Version(2)
3536
->Field("CsvAssetId", &CsvSpawnerEditorComponent::m_csvAssetId)
3637
->Field("NumberOfEntries", &CsvSpawnerEditorComponent::m_numberOfEntries)
3738
->Field("SpawnableAssetConfigurations", &CsvSpawnerEditorComponent::m_spawnableAssetConfigurations)
3839
->Field("DefaultSeed", &CsvSpawnerEditorComponent::m_defaultSeed)
39-
->Field("ShowLabels", &CsvSpawnerEditorComponent::m_showLabels);
40+
->Field("ShowLabels", &CsvSpawnerEditorComponent::m_showLabels)
41+
->Field("TerrainSettingsConfig", &CsvSpawnerEditorComponent::m_terrainSettingsConfig);
4042

4143
AZ::EditContext* editContext = serializeContext->GetEditContext();
4244
if (editContext)
@@ -45,12 +47,12 @@ namespace CsvSpawner
4547
->ClassElement(AZ::Edit::ClassElements::EditorData, "CsvSpawnerEditorComponent")
4648
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
4749
->Attribute(AZ::Edit::Attributes::Category, "CsvSpawner")
48-
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
4950
->DataElement(
5051
AZ::Edit::UIHandlers::Default,
5152
&CsvSpawnerEditorComponent::m_spawnableAssetConfigurations,
5253
"Asset Config",
5354
"Asset configuration")
55+
->Attribute(AZ::Edit::Attributes::AutoExpand, false)
5456
->DataElement(AZ::Edit::UIHandlers::Default, &CsvSpawnerEditorComponent::m_csvAssetId, "CSV Asset", "CSV asset")
5557
->UIElement(AZ::Edit::UIHandlers::Button, "Reload Csv", "Reload Csv")
5658
->Attribute(AZ::Edit::Attributes::NameLabelOverride, "")
@@ -60,47 +62,56 @@ namespace CsvSpawner
6062
->Attribute(AZ::Edit::Attributes::ReadOnly, true)
6163
->DataElement(AZ::Edit::UIHandlers::Default, &CsvSpawnerEditorComponent::m_defaultSeed, "Default seed", "")
6264
->DataElement(AZ::Edit::UIHandlers::Default, &CsvSpawnerEditorComponent::m_showLabels, "Show labels in Editor", "")
63-
->Attribute(AZ::Edit::Attributes::ChangeNotify, &CsvSpawnerEditorComponent::OnOnShowLabelsChanged);
65+
->Attribute(AZ::Edit::Attributes::ChangeNotify, &CsvSpawnerEditorComponent::OnShowLabelsChanged)
66+
->DataElement(
67+
AZ::Edit::UIHandlers::Default,
68+
&CsvSpawnerEditorComponent::m_terrainSettingsConfig,
69+
"Spawn Behaviour Settings",
70+
"Settings to configure spawn behaviour in editor.");
6471
}
6572
}
6673
}
6774

68-
void CsvSpawnerEditorComponent::OnOnShowLabelsChanged()
75+
void CsvSpawnerEditorComponent::Activate()
6976
{
77+
EditorComponentBase::Activate();
78+
AzFramework::Terrain::TerrainDataNotificationBus::Handler::BusConnect();
79+
7080
if (m_showLabels)
7181
{
7282
AzFramework::ViewportDebugDisplayEventBus::Handler::BusConnect(AzToolsFramework::GetEntityContextId());
7383
}
74-
else
75-
{
76-
AzFramework::ViewportDebugDisplayEventBus::Handler::BusDisconnect();
77-
}
78-
}
7984

80-
void CsvSpawnerEditorComponent::Activate()
81-
{
82-
AzToolsFramework::Components::EditorComponentBase::Activate();
83-
if (m_showLabels)
85+
if (m_terrainSettingsConfig.m_spawnOnComponentActivated && !m_terrainSettingsConfig.m_flagSpawnEntitiesOnStartOnce)
8486
{
85-
AzFramework::ViewportDebugDisplayEventBus::Handler::BusConnect(AzToolsFramework::GetEntityContextId());
87+
AZ::TickBus::QueueFunction(
88+
[this]()
89+
{
90+
// If there is no Terrain handlers (which means no active terrain in this level), just spawn entities on next available
91+
// tick. Since terrain is initiated on tick, IsTerrainAvailable will return real information when used inside tick.
92+
if (!IsTerrainAvailable() && !m_terrainSettingsConfig.m_spawnOnTerrainUpdate)
93+
{
94+
m_terrainSettingsConfig.m_flagSpawnEntitiesOnStartOnce = true;
95+
SpawnEntities();
96+
}
97+
});
8698
}
87-
88-
AZ::TickBus::Handler::BusConnect();
8999
}
90100

91101
void CsvSpawnerEditorComponent::Deactivate()
92102
{
93103
m_spawnedTickets.clear();
104+
m_terrainSettingsConfig.m_flagSpawnEntitiesOnStartOnce = false;
94105

106+
AzFramework::Terrain::TerrainDataNotificationBus::Handler::BusDisconnect();
95107
AzFramework::ViewportDebugDisplayEventBus::Handler::BusDisconnect();
96-
AZ::TickBus::Handler::BusDisconnect();
97-
AzToolsFramework::Components::EditorComponentBase::Deactivate();
108+
EditorComponentBase::Deactivate();
98109
}
99110

100111
void CsvSpawnerEditorComponent::BuildGameEntity(AZ::Entity* gameEntity)
101112
{
102113
// Create Game component
103-
const auto config = CsvSpawnerUtils::GetSpawnableAssetFromVector(m_spawnableAssetConfigurations);
114+
const auto config = GetSpawnableAssetFromVector(m_spawnableAssetConfigurations);
104115

105116
using AssetSysReqBus = AzToolsFramework::AssetSystemRequestBus;
106117
AZ::Data::AssetInfo sourceAssetInfo;
@@ -114,28 +125,33 @@ namespace CsvSpawner
114125

115126
AZ_Printf("CsvSpawnerEditorComponent", "Source of CSV file path: %s", sourcePath.c_str());
116127

117-
auto spawnableEntityInfo = CsvSpawner::CsvSpawnerUtils::GetSpawnableEntityInfoFromCSV(sourcePath.String());
128+
auto spawnableEntityInfo = GetSpawnableEntityInfoFromCSV(sourcePath.String());
118129

119130
gameEntity->CreateComponent<CsvSpawnerComponent>(config, spawnableEntityInfo, m_defaultSeed);
131+
120132
// Destroy Editor's spawned entities
121133
m_spawnedTickets.clear();
122134
}
123135

124-
void CsvSpawnerEditorComponent::OnTick(float deltaTime, AZ::ScriptTimePoint time)
136+
void CsvSpawnerEditorComponent::OnTerrainDataChanged(
137+
[[maybe_unused]] const AZ::Aabb& dirtyRegion, TerrainDataChangedMask dataChangedMask)
125138
{
126-
++m_frameCounter;
127-
128-
if (m_frameCounter == 2)
139+
// Ignore on update with selected flags
140+
if (static_cast<bool>(dataChangedMask & m_terrainSettingsConfig.m_terrainMasksToIgnore))
129141
{
130-
SpawnEntities();
131-
AZ::TickBus::Handler::BusDisconnect();
132-
m_frameCounter = 0;
142+
return;
133143
}
134-
}
135144

136-
int CsvSpawnerEditorComponent::GetTickOrder()
137-
{
138-
return AZ::TICK_LAST;
145+
if ((m_terrainSettingsConfig.m_spawnOnComponentActivated && !m_terrainSettingsConfig.m_flagSpawnEntitiesOnStartOnce) ||
146+
m_terrainSettingsConfig.m_spawnOnTerrainUpdate)
147+
{
148+
AZ::TickBus::QueueFunction(
149+
[this]()
150+
{
151+
SpawnEntities();
152+
m_terrainSettingsConfig.m_flagSpawnEntitiesOnStartOnce = true;
153+
});
154+
}
139155
}
140156

141157
void CsvSpawnerEditorComponent::SpawnEntities()
@@ -145,6 +161,7 @@ namespace CsvSpawner
145161
AZ_Error("CsvSpawnerEditorComponent", false, "CSV asset is not set");
146162
return;
147163
}
164+
148165
m_spawnedTickets.clear();
149166
using AssetSysReqBus = AzToolsFramework::AssetSystemRequestBus;
150167
AZ::Data::AssetInfo sourceAssetInfo;
@@ -158,16 +175,28 @@ namespace CsvSpawner
158175

159176
AZ_Printf("CsvSpawnerEditorComponent", "Source of CSV file path: %s", sourcePath.c_str());
160177

161-
m_spawnableEntityInfo = CsvSpawner::CsvSpawnerUtils::GetSpawnableEntityInfoFromCSV(sourcePath.String());
178+
m_spawnableEntityInfo = GetSpawnableEntityInfoFromCSV(sourcePath.String());
162179
m_numberOfEntries = m_spawnableEntityInfo.size();
163180

164181
AZ_Printf("CsvSpawnerEditorComponent", "Spawning spawnables, %d", m_numberOfEntries);
165182

166-
const auto config = CsvSpawnerUtils::GetSpawnableAssetFromVector(m_spawnableAssetConfigurations);
183+
const auto config = GetSpawnableAssetFromVector(m_spawnableAssetConfigurations);
167184
m_spawnedTickets =
168185
CsvSpawnerUtils::SpawnEntities(m_spawnableEntityInfo, config, m_defaultSeed, AzPhysics::EditorPhysicsSceneName, GetEntityId());
169186
}
170187

188+
void CsvSpawnerEditorComponent::OnShowLabelsChanged()
189+
{
190+
if (m_showLabels)
191+
{
192+
AzFramework::ViewportDebugDisplayEventBus::Handler::BusConnect(AzToolsFramework::GetEntityContextId());
193+
}
194+
else
195+
{
196+
AzFramework::ViewportDebugDisplayEventBus::Handler::BusDisconnect();
197+
}
198+
}
199+
171200
void CsvSpawnerEditorComponent::OnSpawnButton()
172201
{
173202
SpawnEntities();
@@ -196,5 +225,4 @@ namespace CsvSpawner
196225
debugDisplay.PopMatrix();
197226
debugDisplay.SetState(stateBefore);
198227
}
199-
200228
} // namespace CsvSpawner

0 commit comments

Comments
 (0)