Game.Prefabs.Modes.EmergencyGeneratorMode
Assembly: Assembly-CSharp.dll
Namespace: Game.Prefabs.Modes
Type: class
Base: LocalModePrefab
Summary:
EmergencyGeneratorMode is a LocalModePrefab that applies mode-specific modifiers to EmergencyGenerator prefabs. It contains an array of ModeData entries, each pointing to a Prefab containing an EmergencyGenerator and supplying a multiplier for its electricity production. When modes are applied, this component modifies the EmergencyGeneratorData component on the prefab's ECS entity (multiplying the base production and writing it back). It also provides methods to record changes and restore the default values.
Fields
-
public ModeData[] m_ModeDatas
Holds one or more ModeData entries. Each entry references a PrefabBase that must contain an EmergencyGenerator component and a multiplier to apply to its electricity production. This array must be non-null at runtime; otherwise methods that iterate it will throw a NullReferenceException. -
public class ModeData
Inner serializable class used by m_ModeDatas. -
public PrefabBase m_Prefab
Prefab reference that is expected to contain an EmergencyGenerator MonoBehaviour/component. -
public float m_ElectricityProductionMultiplier
Multiplier applied to the EmergencyGeneratorData.m_ElectricityProduction value when ApplyModeData is invoked.
Properties
- (none)
Constructors
public EmergencyGeneratorMode()
Default public constructor. As a Unity component class, instances are typically created/serialized by the Unity editor rather than constructed directly in user code.
Methods
public override void RecordChanges(EntityManager entityManager, PrefabSystem prefabSystem)
Iterates over m_ModeDatas and, for each entry:- Attempts to get the EmergencyGenerator component from the referenced PrefabBase.
- If the component is missing, logs a critical message via ComponentBase.baseLog and continues.
-
Otherwise obtains the ECS Entity for the component's prefab via prefabSystem.GetEntity(component.prefab) and calls entityManager.GetComponentData
(entity). Purpose: ensure the EmergencyGeneratorData component exists / is read so the prefab system records that this mode will touch that component (used by the prefab change recording system). Note: RecordChanges does not mutate data but will throw if m_ModeDatas is null. -
public override void ApplyModeData(EntityManager entityManager, PrefabSystem prefabSystem)
Iterates over m_ModeDatas and, for each entry: - Gets the EmergencyGenerator component from the referenced PrefabBase; logs and continues if missing.
- Retrieves the ECS entity for the prefab and the EmergencyGeneratorData component value.
- Multiplies componentData.m_ElectricityProduction by the ModeData.m_ElectricityProductionMultiplier and casts the result to int: componentData.m_ElectricityProduction = (int)(componentData.m_ElectricityProduction * multiplier);
- Writes the updated EmergencyGeneratorData back to the entity via entityManager.SetComponentData. Notes:
- The multiplication result is truncated toward zero when cast to int; use carefully for small multipliers.
- If m_ModeDatas is null, this method will throw a NullReferenceException.
-
If the referenced prefab or component is missing, a critical log is emitted and that entry is skipped.
-
public override void RestoreDefaultData(EntityManager entityManager, PrefabSystem prefabSystem)
Iterates over m_ModeDatas and, for each entry: - Gets the EmergencyGenerator component from the referenced PrefabBase; logs and continues if missing.
- Retrieves the ECS entity for the prefab and the EmergencyGeneratorData component value.
- Restores componentData.m_ElectricityProduction to the value stored on the EmergencyGenerator MonoBehaviour (component.m_ElectricityProduction).
- Writes the restored EmergencyGeneratorData back to the entity via entityManager.SetComponentData. Purpose: revert changes previously applied by ApplyModeData so the prefab's ECS data matches the original MonoBehaviour defaults.
Usage Example
// Example: configure a mode in code (usually set in the Unity editor instead)
public class ExampleSetup
{
public void Configure(EmergencyGeneratorMode mode, PrefabBase generatorPrefab)
{
mode.m_ModeDatas = new EmergencyGeneratorMode.ModeData[]
{
new EmergencyGeneratorMode.ModeData
{
m_Prefab = generatorPrefab,
m_ElectricityProductionMultiplier = 1.5f // increase production by 50% in this mode
}
};
}
}
// Typical runtime flow (handled by the prefab/mode system):
// - RecordChanges(...) is called so the system knows which components will be modified.
// - ApplyModeData(...) is called to update EmergencyGeneratorData on the prefab's entity.
// - RestoreDefaultData(...) is called to put the values back to the MonoBehaviour defaults.
Additional notes and caveats: - This component expects the referenced PrefabBase to contain an EmergencyGenerator MonoBehaviour and that an ECS EmergencyGeneratorData component exists for the prefab's entity. - Because multiplication result is cast to int, fractional results are truncated. If exact rounding behavior is important, adjust calculations before casting. - Missing prefabs/components are logged with ComponentBase.baseLog.Critical; the code continues to process remaining entries.