Game.Prefabs.EmergencyGenerator
Assembly: Assembly-CSharp
Namespace: Game.Prefabs
Type: class
Base: ComponentBase, IServiceUpgrade
Summary:
EmergencyGenerator is a building component intended to turn a battery-style building into an emergency electricity generator. It exposes an integer electricity production value and an activation threshold (Bounds1) that controls when the emergency generator starts and stops producing power. The component writes its configuration into ECS component data (EmergencyGeneratorData) during prefab initialization and declares the ECS components required for runtime.
Fields
-
public int m_ElectricityProduction
Controls how much emergency electricity this generator produces when active. Set this in the prefab (Inspector) or via code before initialization to determine the production amount that is stored to the EmergencyGeneratorData ECS component. -
public Bounds1 m_ActivationThreshold = new Bounds1(0.05f, 0.1f)
Tooltip: "The emergency generator is activated when the charge drops below Min. It it disabled again when the charge reaches Max."
Defines the activation hysteresis for the emergency generator: when the battery charge drops below the Min value it will be activated, and when the charge reaches the Max value it will be deactivated. Defaults to (0.05, 0.1).
Properties
- (none)
Constructors
public EmergencyGenerator()
Default MonoBehaviour-style constructor. Typical usage is to configure fields using the Inspector or set them from code before the prefab is initialized.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Asserts that a ServiceUpgrade component is present on the same prefab (Assert.IsNotNull(GetComponent(), "Only battery building service upgrades can function as emergency generators")). Adds the ECS component type EmergencyGeneratorData (read/write) to the provided components set. This tells the prefab -> entity conversion to include EmergencyGeneratorData for entities created from this prefab. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Adds ServiceUsage (read/write) to the archetype components set. This ensures the runtime archetype includes ServiceUsage for entities of this prefab. -
public void GetUpgradeComponents(HashSet<ComponentType> components)
Implements IServiceUpgrade. Adds Game.Buildings.EmergencyGenerator (read/write) to the upgrade components set. This is the upgrade-specific ECS component type expected on buildings that use this upgrade. -
public override void Initialize(EntityManager entityManager, Entity entity)
Writes the configured values into the ECS EmergencyGeneratorData component on the supplied entity: - m_ElectricityProduction <- this.m_ElectricityProduction
- m_ActivationThreshold <- this.m_ActivationThreshold This is called during prefab-to-entity initialization to transfer authoring data to runtime ECS data.
Usage Example
// Example: configure an EmergencyGenerator on a prefab (executed in authoring code)
var prefab = GetComponent<EmergencyGenerator>();
prefab.m_ElectricityProduction = 500; // set production in editor or in code
prefab.m_ActivationThreshold = new Bounds1(0.10f, 0.20f);
// During prefab->entity conversion, Initialize will be invoked and the ECS component will be set:
[SomeAuthoringConversionStep]
public void Convert(EntityManager entityManager, Entity entity)
{
// the framework will call EmergencyGenerator.Initialize(entityManager, entity)
// which does:
// entityManager.SetComponentData(entity, new EmergencyGeneratorData {
// m_ElectricityProduction = prefab.m_ElectricityProduction,
// m_ActivationThreshold = prefab.m_ActivationThreshold
// });
}
Additional notes and tips: - GetPrefabComponents asserts the presence of a ServiceUpgrade component. Ensure the prefab also has ServiceUpgrade; otherwise initialization will assert in builds with UNITY_ASSERTIONS enabled. - EmergencyGenerator writes and requires specific ECS components: EmergencyGeneratorData (stores the authoring values), ServiceUsage, and Game.Buildings.EmergencyGenerator (upgrade-specific data). Make sure any systems that consume these components expect the provided fields. - The activation hysteresis uses Bounds1 (Min, Max). Configure the values to avoid frequent on/off cycling (i.e., ensure Max > Min with sufficient gap).