9
9
*/
10
10
11
11
#include " CsvSpawnerEditorComponent.h"
12
- #include " AzCore/Debug/Trace.h"
13
12
#include " CsvSpawnerComponent.h"
14
13
#include " CsvSpawnerCsvParser.h"
15
14
#include " CsvSpawnerUtils.h"
16
15
17
16
#include < AzCore/Component/TransformBus.h>
17
+ #include < AzCore/Debug/Trace.h>
18
18
#include < AzCore/IO/Path/Path.h>
19
19
#include < AzCore/Serialization/EditContext.h>
20
20
#include < AzFramework/Physics/Common/PhysicsTypes.h>
21
+ #include < AzFramework/Physics/PhysicsScene.h>
21
22
#include < AzToolsFramework/API/EditorAssetSystemAPI.h>
22
23
#include < AzToolsFramework/Viewport/ViewportMessages.h>
23
- #include < thread>
24
24
25
25
namespace CsvSpawner
26
26
{
27
-
28
27
void CsvSpawnerEditorComponent::Reflect (AZ::ReflectContext* context)
29
28
{
30
29
AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
31
30
if (serializeContext)
32
31
{
33
- serializeContext->Class <CsvSpawnerEditorComponent, AzToolsFramework::Components::EditorComponentBase>()
32
+ CsvSpawnerEditorTerrainSettingsConfig::Reflect (context);
33
+
34
+ serializeContext->Class <CsvSpawnerEditorComponent, EditorComponentBase>()
34
35
->Version (2 )
35
36
->Field (" CsvAssetId" , &CsvSpawnerEditorComponent::m_csvAssetId)
36
37
->Field (" NumberOfEntries" , &CsvSpawnerEditorComponent::m_numberOfEntries)
37
38
->Field (" SpawnableAssetConfigurations" , &CsvSpawnerEditorComponent::m_spawnableAssetConfigurations)
38
39
->Field (" DefaultSeed" , &CsvSpawnerEditorComponent::m_defaultSeed)
39
- ->Field (" ShowLabels" , &CsvSpawnerEditorComponent::m_showLabels);
40
+ ->Field (" ShowLabels" , &CsvSpawnerEditorComponent::m_showLabels)
41
+ ->Field (" TerrainSettingsConfig" , &CsvSpawnerEditorComponent::m_terrainSettingsConfig);
40
42
41
43
AZ::EditContext* editContext = serializeContext->GetEditContext ();
42
44
if (editContext)
@@ -45,12 +47,12 @@ namespace CsvSpawner
45
47
->ClassElement (AZ::Edit::ClassElements::EditorData, " CsvSpawnerEditorComponent" )
46
48
->Attribute (AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE (" Game" ))
47
49
->Attribute (AZ::Edit::Attributes::Category, " CsvSpawner" )
48
- ->Attribute (AZ::Edit::Attributes::AutoExpand, true )
49
50
->DataElement (
50
51
AZ::Edit::UIHandlers::Default,
51
52
&CsvSpawnerEditorComponent::m_spawnableAssetConfigurations,
52
53
" Asset Config" ,
53
54
" Asset configuration" )
55
+ ->Attribute (AZ::Edit::Attributes::AutoExpand, false )
54
56
->DataElement (AZ::Edit::UIHandlers::Default, &CsvSpawnerEditorComponent::m_csvAssetId, " CSV Asset" , " CSV asset" )
55
57
->UIElement (AZ::Edit::UIHandlers::Button, " Reload Csv" , " Reload Csv" )
56
58
->Attribute (AZ::Edit::Attributes::NameLabelOverride, " " )
@@ -60,47 +62,56 @@ namespace CsvSpawner
60
62
->Attribute (AZ::Edit::Attributes::ReadOnly, true )
61
63
->DataElement (AZ::Edit::UIHandlers::Default, &CsvSpawnerEditorComponent::m_defaultSeed, " Default seed" , " " )
62
64
->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." );
64
71
}
65
72
}
66
73
}
67
74
68
- void CsvSpawnerEditorComponent::OnOnShowLabelsChanged ()
75
+ void CsvSpawnerEditorComponent::Activate ()
69
76
{
77
+ EditorComponentBase::Activate ();
78
+ AzFramework::Terrain::TerrainDataNotificationBus::Handler::BusConnect ();
79
+
70
80
if (m_showLabels)
71
81
{
72
82
AzFramework::ViewportDebugDisplayEventBus::Handler::BusConnect (AzToolsFramework::GetEntityContextId ());
73
83
}
74
- else
75
- {
76
- AzFramework::ViewportDebugDisplayEventBus::Handler::BusDisconnect ();
77
- }
78
- }
79
84
80
- void CsvSpawnerEditorComponent::Activate ()
81
- {
82
- AzToolsFramework::Components::EditorComponentBase::Activate ();
83
- if (m_showLabels)
85
+ if (m_terrainSettingsConfig.m_spawnOnComponentActivated && !m_terrainSettingsConfig.m_flagSpawnEntitiesOnStartOnce )
84
86
{
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
+ });
86
98
}
87
-
88
- AZ::TickBus::Handler::BusConnect ();
89
99
}
90
100
91
101
void CsvSpawnerEditorComponent::Deactivate ()
92
102
{
93
103
m_spawnedTickets.clear ();
104
+ m_terrainSettingsConfig.m_flagSpawnEntitiesOnStartOnce = false ;
94
105
106
+ AzFramework::Terrain::TerrainDataNotificationBus::Handler::BusDisconnect ();
95
107
AzFramework::ViewportDebugDisplayEventBus::Handler::BusDisconnect ();
96
- AZ::TickBus::Handler::BusDisconnect ();
97
- AzToolsFramework::Components::EditorComponentBase::Deactivate ();
108
+ EditorComponentBase::Deactivate ();
98
109
}
99
110
100
111
void CsvSpawnerEditorComponent::BuildGameEntity (AZ::Entity* gameEntity)
101
112
{
102
113
// Create Game component
103
- const auto config = CsvSpawnerUtils:: GetSpawnableAssetFromVector (m_spawnableAssetConfigurations);
114
+ const auto config = GetSpawnableAssetFromVector (m_spawnableAssetConfigurations);
104
115
105
116
using AssetSysReqBus = AzToolsFramework::AssetSystemRequestBus;
106
117
AZ::Data::AssetInfo sourceAssetInfo;
@@ -114,28 +125,33 @@ namespace CsvSpawner
114
125
115
126
AZ_Printf (" CsvSpawnerEditorComponent" , " Source of CSV file path: %s" , sourcePath.c_str ());
116
127
117
- auto spawnableEntityInfo = CsvSpawner::CsvSpawnerUtils:: GetSpawnableEntityInfoFromCSV (sourcePath.String ());
128
+ auto spawnableEntityInfo = GetSpawnableEntityInfoFromCSV (sourcePath.String ());
118
129
119
130
gameEntity->CreateComponent <CsvSpawnerComponent>(config, spawnableEntityInfo, m_defaultSeed);
131
+
120
132
// Destroy Editor's spawned entities
121
133
m_spawnedTickets.clear ();
122
134
}
123
135
124
- void CsvSpawnerEditorComponent::OnTick (float deltaTime, AZ::ScriptTimePoint time)
136
+ void CsvSpawnerEditorComponent::OnTerrainDataChanged (
137
+ [[maybe_unused]] const AZ::Aabb& dirtyRegion, TerrainDataChangedMask dataChangedMask)
125
138
{
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 ))
129
141
{
130
- SpawnEntities ();
131
- AZ::TickBus::Handler::BusDisconnect ();
132
- m_frameCounter = 0 ;
142
+ return ;
133
143
}
134
- }
135
144
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
+ }
139
155
}
140
156
141
157
void CsvSpawnerEditorComponent::SpawnEntities ()
@@ -145,6 +161,7 @@ namespace CsvSpawner
145
161
AZ_Error (" CsvSpawnerEditorComponent" , false , " CSV asset is not set" );
146
162
return ;
147
163
}
164
+
148
165
m_spawnedTickets.clear ();
149
166
using AssetSysReqBus = AzToolsFramework::AssetSystemRequestBus;
150
167
AZ::Data::AssetInfo sourceAssetInfo;
@@ -158,16 +175,28 @@ namespace CsvSpawner
158
175
159
176
AZ_Printf (" CsvSpawnerEditorComponent" , " Source of CSV file path: %s" , sourcePath.c_str ());
160
177
161
- m_spawnableEntityInfo = CsvSpawner::CsvSpawnerUtils:: GetSpawnableEntityInfoFromCSV (sourcePath.String ());
178
+ m_spawnableEntityInfo = GetSpawnableEntityInfoFromCSV (sourcePath.String ());
162
179
m_numberOfEntries = m_spawnableEntityInfo.size ();
163
180
164
181
AZ_Printf (" CsvSpawnerEditorComponent" , " Spawning spawnables, %d" , m_numberOfEntries);
165
182
166
- const auto config = CsvSpawnerUtils:: GetSpawnableAssetFromVector (m_spawnableAssetConfigurations);
183
+ const auto config = GetSpawnableAssetFromVector (m_spawnableAssetConfigurations);
167
184
m_spawnedTickets =
168
185
CsvSpawnerUtils::SpawnEntities (m_spawnableEntityInfo, config, m_defaultSeed, AzPhysics::EditorPhysicsSceneName, GetEntityId ());
169
186
}
170
187
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
+
171
200
void CsvSpawnerEditorComponent::OnSpawnButton ()
172
201
{
173
202
SpawnEntities ();
@@ -196,5 +225,4 @@ namespace CsvSpawner
196
225
debugDisplay.PopMatrix ();
197
226
debugDisplay.SetState (stateBefore);
198
227
}
199
-
200
228
} // namespace CsvSpawner
0 commit comments