Game.Prefabs.Modes.BatteryMode
Assembly: Game
Namespace: Game.Prefabs.Modes
Type: class
Base: LocalModePrefab
Summary:
BatteryMode is a prefab-mode component used to modify runtime ECS BatteryData for one or more battery prefabs when a "mode" is applied. It contains a list of ModeData entries that reference a PrefabBase (the battery prefab) and multipliers for power output and capacity. When ApplyModeData is called the component multiplies the existing BatteryData values by the configured multipliers; RestoreDefaultData restores values to the prefab's default Battery settings; RecordChanges reads/records the existing BatteryData to ensure values are available before modification. The component logs and skips entries whose referenced Prefab does not contain a Battery component.
Fields
-
public ModeData[] m_ModeDatas
Holds the array of ModeData entries. Each entry references a target PrefabBase and two multipliers (m_PowerOutputMultiplier, m_CapacityMultiplier). The array can be empty; each element is processed in RecordChanges, ApplyModeData and RestoreDefaultData. -
public class ModeData
Nested serializable class that describes a single mode modification target and multipliers. -
public PrefabBase m_Prefab
Reference to the battery prefab that should be modified. -
public float m_PowerOutputMultiplier
Multiplier applied to the battery's m_PowerOutput (BatteryData) when ApplyModeData runs. -
public float m_CapacityMultiplier
Multiplier applied to the battery's m_Capacity (BatteryData) when ApplyModeData runs.
Properties
- (none)
Constructors
public BatteryMode()
Default parameterless constructor (inherited/implicit). No special initialization is performed by this class.
Methods
public override void RecordChanges(EntityManager entityManager, PrefabSystem prefabSystem)
Iterates m_ModeDatas and for each entry:- Resolves the Battery component from the referenced PrefabBase.
- If missing, logs a critical message and continues.
-
Uses prefabSystem.GetEntity(component.prefab) to get the Entity and reads BatteryData via entityManager.GetComponentData
(entity). Purpose: ensure the ECS component data exists/can be accessed and to "record" the current state before changes are applied. The method does not modify the data. -
public override void ApplyModeData(EntityManager entityManager, PrefabSystem prefabSystem)
For each ModeData entry: - Resolves the Battery component from the PrefabBase.
- If missing, logs a critical message and continues.
- Gets the Entity and reads BatteryData.
- Multiplies componentData.m_PowerOutput by m_PowerOutputMultiplier and componentData.m_Capacity by m_CapacityMultiplier, casting resulting values to int.
-
Writes the modified BatteryData back with entityManager.SetComponentData(entity, componentData). Effect: applies configured multipliers to runtime BatteryData of the prefab.
-
public override void RestoreDefaultData(EntityManager entityManager, PrefabSystem prefabSystem)
For each ModeData entry: - Resolves the Battery component from the PrefabBase.
- If missing, logs a critical message and continues.
- Gets the Entity and reads BatteryData.
- Restores componentData.m_PowerOutput and componentData.m_Capacity to the values from the Battery component instance on the prefab (component.m_PowerOutput, component.m_Capacity).
- Writes the restored BatteryData back to the entity. Purpose: revert runtime changes applied by ApplyModeData back to the prefab defaults.
Notes: - The class assumes the presence of a Battery component type and a BatteryData ECS component with fields m_PowerOutput and m_Capacity. - Prefab resolution uses PrefabSystem.GetEntity. If an entry's PrefabBase does not contain a Battery component, that entry is skipped and an error is logged via ComponentBase.baseLog.Critical. - Multiplication results are cast to int, so fractional results are truncated.
Usage Example
// Example: Configure a BatteryMode on a prefab (editor/initialization)
// Assume `myBatteryPrefabBase` is a PrefabBase reference to a battery prefab.
var batteryMode = new BatteryMode();
batteryMode.m_ModeDatas = new BatteryMode.ModeData[1];
batteryMode.m_ModeDatas[0] = new BatteryMode.ModeData
{
m_Prefab = myBatteryPrefabBase,
m_PowerOutputMultiplier = 1.25f, // +25% power output
m_CapacityMultiplier = 0.8f // 80% of original capacity
};
// At runtime, the game/mod system will call:
// batteryMode.RecordChanges(entityManager, prefabSystem);
// batteryMode.ApplyModeData(entityManager, prefabSystem);
// and later, to revert:
// batteryMode.RestoreDefaultData(entityManager, prefabSystem);