Game.UI.InGame.DestroyedBuildingSection
Assembly:
Assembly-CSharp
Namespace:
Game.UI.InGame
Type:
class
Base:
InfoSectionBase
Summary:
DestroyedBuildingSection is an Info UI section used for destroyed (burned/ruined) buildings. It reads the Destroyed component for the selected entity, determines the progress of clearing, whether the building has been fully cleared, and whether disaster response vehicles are en route. It integrates with the game's ToolSystem to allow toggling the upgrade/rebuild tool, exposes a UI binding for the rebuild toggle, and reports tooltip tags and localized tooltip keys representing the current state (Waiting, NoService, Searching, Rebuild). The section queries for available fire stations and service-dispatch vehicles using ECS EntityQuery and reads/writes properties for UI consumption (including JSON serialization). It carefully disposes of temporary NativeArray results created from queries.
Fields
-
private ToolSystem m_ToolSystem
m_ToolSystem is the global ToolSystem retrieved from the world. Used to inspect and change the currently active tool and to subscribe to tool-change events. -
private DefaultToolSystem m_DefaultToolSystem
Reference to the default (pointer/select) tool so the section can restore the default tool when toggling rebuild mode off. -
private UpgradeToolSystem m_UpgradeToolSystem
Reference to the upgrade/rebuild tool used when the user chooses to rebuild the destroyed building. The section sets this tool as active when toggling rebuild on. -
private ValueBinding<bool> m_Rebuilding
UI binding that reflects whether the rebuild/upgrade tool is currently active. Bound to the UI key "rebuilding" so the UI can show the rebuild toggle state. -
private EntityQuery m_FireStationQuery
An EntityQuery configured to find fire station buildings (Building + FireStation + PrefabRef, excluding Temp and Deleted). Used to check whether disaster-response capacity exists in the city. -
private EntityQuery m_ServiceDispatchQuery
An EntityQuery to find vehicles that have ServiceDispatch buffers (Vehicle + ServiceDispatch, excluding Deleted/Temp). Used to inspect currently dispatched vehicles and their service buffers.
Properties
-
protected override string group => "DestroyedBuildingSection"
Group identifier used for UI binding scoping and tooltip keys. -
private Entity destroyer { get; set; }
Entity (prefab) that caused the destruction (e.g., a disaster prefab). Populated from the Destroyed component's event PrefabRef during processing. Can be Entity.Null when there is no recorded destroyer. -
private bool cleared { get; set; }
True when clearing progress reaches or exceeds 100% (progress >= 1.0). Used to switch status to Rebuild when the site is cleared. -
private float progress { get; set; }
Clearing progress for the destroyed site (0..1). The UI exposes this multiplied by 100 in OnWriteProperties. -
private Status status { get; set; }
Current internal status of the destroyed site. Enum values: None, Waiting, NoService, Searching, Rebuild. The status is used to add tooltip keys and decide UI state. -
protected override bool displayForDestroyedObjects => true
Overrides base to indicate this section should display for destroyed objects. -
protected override bool displayForUpgrades => true
Overrides base to indicate this section should be shown in upgrade contexts as well.
Constructors
public DestroyedBuildingSection()
Default constructor. The class lifecycle relies on OnCreate to initialize systems and bindings; constructor is empty except for the [Preserve] attribute in the source (ensures it isn't stripped).
Methods
protected override void OnCreate()
Initializes internal system references (ToolSystem, DefaultToolSystem, UpgradeToolSystem), subscribes to ToolSystem.EventToolChanged, constructs EntityQuery instances (m_FireStationQuery, m_ServiceDispatchQuery), and adds UI bindings:- TriggerBinding("toggleRebuild", OnToggleRebuild)
-
ValueBinding
("rebuilding", initialValue: false) This method wires up the section to react to tool changes and to allow the user to toggle rebuild mode. -
protected override void OnDestroy()
Unsubscribes the OnToolChanged callback from ToolSystem.EventToolChanged and calls base.OnDestroy. Ensures no stale event subscriptions remain. -
private void OnToolChanged(ToolBaseSystem tool)
Event handler for tool changes. Updates m_Rebuilding binding to true when the active tool is the UpgradeToolSystem, false otherwise. Keeps UI toggle in sync with the actual active tool. -
private void OnToggleRebuild()
Called by the UI trigger binding to toggle rebuild mode: - If the active tool is already the UpgradeToolSystem, switch back to DefaultToolSystem.
-
Otherwise clear m_UpgradeToolSystem.prefab and set the active tool to the UpgradeToolSystem (enter rebuild mode). This method centralizes the logic for entering/exiting rebuild tool mode.
-
protected override void Reset()
Resets internal state when the section is reset (e.g., when selection changes). Sets destroyer to Entity.Null, status to None, cleared to false and progress to 0. -
private bool Visible()
Determines whether the UI section should be visible for the currently selected entity. Checks: - base.Destroyed flag true
- selectedEntity has Building component
- either no Owner component or ServiceUpgrade component exists
-
special-case for SpawnableBuildingData vs PlacedSignatureBuildingData: if spawnable and not placed-signature, only show if Attached component exists Returns true if all conditions satisfied.
-
protected override void OnUpdate()
Sets base.visible using Visible(). Called each frame/update to determine UI visibility. -
protected override void OnProcess()
Main per-frame/process logic for the section. Steps: - Reads Destroyed component for selectedEntity to get clearing progress and event PrefabRef.
- Sets destroyer from the event's PrefabRef (if present).
- Computes progress and cleared flag.
- If not cleared:
- Queries fire station prefabs (m_FireStationQuery) and checks whether any have disaster response capacity > 0.
- If none, sets status = NoService.
- Otherwise enumerates service-dispatch vehicles (m_ServiceDispatchQuery), reads their ServiceDispatch buffers and inspects referenced requests for FireRescueRequest of type Disaster targeting selectedEntity; uses VehicleAtTarget to check vehicle state (e.g., Rescueing flag). If a matching dispatch is found, sets status = Searching.
- If no conditions matched, sets status = Waiting.
- Disposes temporary NativeArray results from queries.
- If cleared, sets status = Rebuild.
- Adds the status.ToString() to base.tooltipKeys (for localized tooltip) when status != None.
- Adds TooltipTags.Destroyed to m_InfoUISystem.tooltipTags. Notes:
- Temporary NativeArray allocations are used; they are disposed at the end of the check.
-
The method relies on component reads and TryGetComponent calls that may fail; it guards appropriately.
-
private bool VehicleAtTarget(Entity vehicle)
Utility to determine if a vehicle is currently at the target and performing a rescue: tries to get the FireEngine component and checks the Rescueing flag in its state. Returns true when the vehicle is in the Rescueing state. -
public override void OnWriteProperties(IJsonWriter writer)
Serializes UI-visible properties to JSON for the UI: - "destroyer": writes the destroyer prefab name (via PrefabSystem.GetPrefab) or null if Entity.Null
- "progress": writes progress * 100 (percentage)
- "cleared": writes cleared boolean
- "status": writes status.ToString() Used by the UI system to obtain JSON properties for client-side widgets or external inspection.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_ToolSystem = base.World.GetOrCreateSystemManaged<ToolSystem>();
m_DefaultToolSystem = base.World.GetOrCreateSystemManaged<DefaultToolSystem>();
m_UpgradeToolSystem = base.World.GetOrCreateSystemManaged<UpgradeToolSystem>();
// Subscribe to tool changes so the UI toggle stays in sync
m_ToolSystem.EventToolChanged = (Action<ToolBaseSystem>)Delegate.Combine(
m_ToolSystem.EventToolChanged,
new Action<ToolBaseSystem>(OnToolChanged));
// Create queries to check for fire stations and service dispatch vehicles
m_FireStationQuery = GetEntityQuery(
ComponentType.ReadOnly<Building>(),
ComponentType.ReadOnly<Game.Buildings.FireStation>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.Exclude<Temp>(),
ComponentType.Exclude<Deleted>());
m_ServiceDispatchQuery = GetEntityQuery(
ComponentType.ReadOnly<Vehicle>(),
ComponentType.ReadOnly<ServiceDispatch>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Temp>());
// Bind the UI toggle and rebuilding flag
AddBinding(new TriggerBinding(group, "toggleRebuild", OnToggleRebuild));
AddBinding(m_Rebuilding = new ValueBinding<bool>(group, "rebuilding", initialValue: false));
}
Additional notes and tips: - The class uses ECS patterns (EntityQuery, component buffers, TryGetComponent), so ensure calls are made on the correct thread/context the entity world expects. - Temporary NativeArray allocations (ToComponentDataArray / ToEntityArray) are created per-process and must be disposed to avoid leaks — the code disposes them after use. - The status enum values correspond to localized tooltip keys used by base.tooltipKeys; if adding new statuses, ensure localization entries exist. - OnToggleRebuild clears m_UpgradeToolSystem.prefab before activating the upgrade tool; this ensures the user must pick a prefab to rebuild rather than reusing a stale selection.