Game.Buildings.ZoneCheckSystem
Assembly:
Namespace: Game.Buildings
Type: class
Base: GameSystemBase
Summary:
ZoneCheckSystem scans buildings in updated zone bounds and validates whether spawnable buildings are still allowed by the current zoning/cell state. It runs three main jobs to:
- find spawnable buildings inside updated zone bounds (FindSpawnableBuildingsJob),
- collect and deduplicate those building Entities (CollectEntitiesJob),
- validate each building's placement against zone blocks/cells and attached parents, then add or remove the Condemned component and relevant notifications (CheckBuildingZonesJob).
This system uses Unity DOTS jobs, NativeQuadTree searches, buffer/component lookups and integrates with other world systems (zone update/search, object search, tools, icon commands and modification barriers) to schedule work and manage dependencies.
Fields
-
private Game.Zones.UpdateCollectSystem m_ZoneUpdateCollectSystem
Used to obtain bounds that have been updated by zone changes. The system only runs when m_ZoneUpdateCollectSystem.isUpdated is true. -
private Game.Zones.SearchSystem m_ZoneSearchSystem
Provides a search tree for zone blocks/cells used by the zone validation job. -
private ModificationEndBarrier m_ModificationEndBarrier
Used to create an EntityCommandBuffer parallel writer for structural changes (adding/removing Condemned component). -
private Game.Objects.SearchSystem m_ObjectSearchSystem
Provides a static object search tree used to find buildings overlapping updated bounds. -
private ToolSystem m_ToolSystem
Used to check editor mode (m_EditorMode) so validation can be skipped when in editor. -
private IconCommandSystem m_IconCommandSystem
Used to add/remove notifications (icons) when buildings become condemned or are restored. -
private EntityQuery m_BuildingSettingsQuery
A query to fetch BuildingConfigurationData singleton; the system will not process if these settings are absent. -
private TypeHandle __TypeHandle
Container of ComponentLookup/BufferLookup handles used to access component data in jobs. It is initialized in OnCreateForCompiler and used via InternalCompilerInterface inside OnUpdate. -
private struct FindSpawnableBuildingsJob
(nested)
IJobParallelForDefer job that iterates over updated bounds and searches the object search tree for spawnable building Entities. Enqueues found Entities into a NativeQueue (parallel writer). -
private struct CollectEntitiesJob
(nested)
IJob that dequeues Entities from the NativeQueue, sorts and deduplicates them, and writes the result into a NativeListfor the validation job. -
private struct CheckBuildingZonesJob
(nested)
IJobParallelForDefer that validates each building from the collected list. Uses zone cell buffers and component lookups to determine if the building is still valid for its lot placement, handles attached parent validation, and updates Condemned component + notifications accordingly.
Properties
- None (no public properties defined in this system)
Constructors
public ZoneCheckSystem()
Default constructor. The system is a managed GameSystemBase; most initialization is done in OnCreate / OnCreateForCompiler.
Methods
protected override void OnCreate()
: System.Void
Initializes references to other world systems:- Gets UpdateCollectSystem, Zone SearchSystem, ModificationEndBarrier, ObjectSearchSystem, ToolSystem and IconCommandSystem.
-
Creates the EntityQuery used to fetch BuildingConfigurationData.
-
protected override void OnUpdate()
: System.Void
Main scheduling method. Runs when zone updates exist and BuildingConfigurationData is available. The method: - Allocates temporary NativeQueue and NativeList for entity discovery and collection.
- Gets updated bounds from m_ZoneUpdateCollectSystem.
- Creates and schedules FindSpawnableBuildingsJob to search the static object search tree.
- Schedules CollectEntitiesJob to gather and deduplicate entities from the queue.
- Builds and schedules CheckBuildingZonesJob to validate each building and apply/remove Condemned + notifications.
- Hooks job handles into other systems (AddBoundsReader, AddSearchTreeReader, AddCommandBufferWriter, AddJobHandleForProducer).
-
Disposes temporaries with proper job dependencies and updates base.Dependency.
-
protected override void OnCreateForCompiler()
: System.Void
Called by compiler initialization. It assigns entity queries and calls __TypeHandle.__AssignHandles to initialize ComponentLookup/BufferLookup handles for jobs. -
private void __AssignQueries(ref SystemState state)
: System.Void
Internal method used during OnCreateForCompiler to create any queries required by the compiler; here it creates (and immediately disposes) a temporary EntityQueryBuilder (placeholder for generated code). -
private bool CheckBuildingZonesJob.ValidateAttachedParent(Entity building, BuildingData prefabBuildingData, SpawnableBuildingData prefabSpawnableBuildingData)
If the building has an Attached component, checks whether its parent is a placeholder with a matching zone prefab. If so, the building is considered valid (attached to a correct placeholder). -
private bool CheckBuildingZonesJob.ValidateZoneBlocks(Entity building, BuildingData prefabBuildingData, SpawnableBuildingData prefabSpawnableBuildingData)
Performs the main per-building validation: - Computes oriented lot extents using the building transform and prefab lot size.
- Constructs a search bounds and iterates zone blocks overlapping the lot (through a nested Iterator that inspects cells).
- Builds a temporary NativeArray
validated array sized to the lot (x*y) and fills entries for visible cells matching the required zone type. - Tracks roadside direction flags to ensure at least one road adjacency condition is met.
-
Returns true if all lot cells are validated and road adjacency rules satisfied, false otherwise.
-
private struct FindSpawnableBuildingsJob.Iterator
Execute/Iterate helpers
Iterator used by the NativeQuadTree.Iterate call to check intersection between search bounds and updated bounds, and to test whether an object Entity is a building whose prefab has SpawnableBuildingData and is not a signature building. Matching entities are enqueued into the result queue. -
private struct CollectEntitiesJob.EntityComparer
Compare(Entity x, Entity y)
Comparer used to sort entities by Index before deduplication. -
private struct CheckBuildingZonesJob.Iterator
Iterate helper
Iterates block Entities in the zone search tree and inspects the block's cell buffer to mark validated lot cells and detect road-adjacency flags. Uses block/valid-area/cell data and math/zone utilities. The iterator writes into m_Validated and updates m_Directions flags.
Usage Example
// Typical use is automatic via the World; you can get the system and ensure it's created:
var world = World.DefaultGameObjectInjectionWorld;
var zoneCheckSystem = world.GetOrCreateSystemManaged<Game.Buildings.ZoneCheckSystem>();
// The system will run automatically each frame when zones are updated and BuildingConfigurationData exists.
// If you need to trigger a manual update in tests, ensure required singletons/systems are present and
// then call:
// zoneCheckSystem.Update(); // not generally recommended in production code — use the DOTS world update loop.
{{ Additional info: - This system is heavily jobified and designed to run on worker threads. Be careful when modifying component types used here (Building, PrefabRef, SpawnableBuildingData, Block/Cell buffers, etc.) as the TypeHandle must be kept in sync with generated job code. - The validation logic assumes a grid-based zone/cell layout and uses oriented lot iteration — rotation matters (transform.m_Rotation). - Condemned component is used to mark invalid buildings; when added, an icon notification (m_CondemnedNotification from BuildingConfigurationData) is created unless the building is destroyed/abandoned. }}