Game.Prefabs.Modes.PowerPlantMode
Assembly: Assembly-CSharp
Namespace: Game.Prefabs.Modes
Type: class
Base: LocalModePrefab
Summary:
PowerPlantMode is a mode component (Unity component) used to modify PowerPlant entities' electricity production when a mode is applied. It holds an array of ModeData entries that pair a Prefab (PrefabBase) with a multiplier. At runtime this component interacts with the ECS (EntityManager / PrefabSystem) to read and modify the PowerPlantData component of the corresponding entity. It also provides a mechanism to restore the production value back to the default from the prefab.
Fields
-
public ModeData[] m_ModeDatas
Holds the list of ModeData entries. Each entry references a Prefab and a multiplier that will be applied to that prefab's PowerPlantData.m_ElectricityProduction when the mode is applied. -
public class ModeData
Inner serializable class describing one mode entry. -
public PrefabBase m_Prefab
Reference to the prefab that contains the PowerPlant MonoBehaviour. Used to locate the corresponding entity via PrefabSystem. -
public float m_ElectricityProductionMultiplier
Multiplier applied to the PowerPlantData.m_ElectricityProduction. The multiplier is applied to the current value and the result is cast to int when stored back into the component.
Properties
- (none)
This class does not expose any special C# properties; it uses public fields intended to be serialized/edited in the Unity inspector.
Constructors
public PowerPlantMode()
Implicit default constructor. The component is expected to be configured in the Unity editor; no special initialization logic is defined in the class.
Methods
public override void RecordChanges(EntityManager entityManager, PrefabSystem prefabSystem)
Iterates over m_ModeDatas. For each entry it:- Gets the PowerPlant MonoBehaviour from the referenced prefab (m_Prefab.GetComponent
()). - If the PowerPlant is missing, logs a critical message via ComponentBase.baseLog.Critical and continues.
- Looks up the ECS Entity for the prefab via prefabSystem.GetEntity(component.prefab).
-
Calls entityManager.GetComponentData
(entity). Purpose: touches/reads the ECS component so the change is registered (or to validate existence). It does not mutate data here. -
public override void ApplyModeData(EntityManager entityManager, PrefabSystem prefabSystem)
For each ModeData entry: - Resolves the corresponding PowerPlant MonoBehaviour and entity.
- Reads the current PowerPlantData from the entity.
- Multiplies componentData.m_ElectricityProduction by modeData.m_ElectricityProductionMultiplier and casts to int.
- Writes the modified componentData back using entityManager.SetComponentData. Notes and caveats:
- The multiplier is applied to the current stored production value; applying this method repeatedly will compound the effect.
- The multiplication result is cast to int (fractional part truncated).
-
If the target prefab or ECS component is missing, a critical log is emitted and that entry is skipped.
-
public override void RestoreDefaultData(EntityManager entityManager, PrefabSystem prefabSystem)
For each ModeData entry: - Resolves the PowerPlant MonoBehaviour and entity.
- Reads the current PowerPlantData from the entity.
- Sets componentData.m_ElectricityProduction to the original value from the PowerPlant MonoBehaviour (component.m_ElectricityProduction).
- Writes the updated componentData back to the entity. This restores the production value to the prefab's default value, undoing any multipliers previously applied.
Usage Example
Example: configuring in the Unity inspector - Add the PowerPlantMode component to a mode prefab. - In m_ModeDatas assign entries: set m_Prefab to the power plant prefab and set m_ElectricityProductionMultiplier to the desired multiplier (e.g., 1.25 for +25%).
Programmatic example (conceptual — called by the mode system at appropriate time):
// Assume entityManager and prefabSystem are available in context,
// and powerPlantMode is a configured PowerPlantMode component instance.
powerPlantMode.ApplyModeData(entityManager, prefabSystem); // Apply multipliers
// ...
powerPlantMode.RestoreDefaultData(entityManager, prefabSystem); // Restore defaults
Notes and best practices: - Ensure the referenced PrefabBase actually contains a PowerPlant MonoBehaviour; otherwise the entry is skipped and a critical log is produced. - Be aware that ApplyModeData multiplies the current production value. If your mode system calls ApplyModeData multiple times without restoring, values will compound. - The multiplier is a float but the stored production is an int — fractional results are truncated. If precise rounding is required, adjust the code to use rounding instead of a cast. - This component manipulates ECS component data (PowerPlantData) by reading/writing via EntityManager; ensure any required synchronization with jobs or the ECS system is handled by the caller.