Game.Prefabs.Modes.MapTileMode
Assembly:
Game (assembly inferred)
Namespace:
Game.Prefabs.Modes
Type:
class MapTileMode
Base:
LocalModePrefab
Summary:
MapTileMode is a mode prefab component used to apply and restore per-tile data from a MapTilePrefab onto the ECS prefab entity. It holds a reference to a MapTilePrefab and an array of FeatureInfo overrides. At runtime it records which prefab components are relevant (so changes are tracked), writes map feature costs into the prefab's MapFeatureData buffer, and sets the TilePurchaseCostFactor component on the prefab entity. It also can restore the prefab entity to the defaults stored on the referenced MapTilePrefab.
Fields
-
public MapTilePrefab m_Prefab
Reference to the MapTilePrefab that provides default values and the target prefab entity. This must be assigned (usually via the inspector). If null, methods log a critical error and return early. -
public MapTilePrefab.FeatureInfo[] m_MapFeatures
An array of feature overrides. Each FeatureInfo is expected to contain at least a m_MapFeature (an enum/index) and m_Cost (the cost value). These overrides are written into the prefab's MapFeatureData buffer by ApplyModeData.
Properties
- (none defined)
This class does not expose properties; it uses public fields for configuration.
Constructors
public MapTileMode()
Default constructor (inherited behavior). No custom initialization logic is present in the class; fields are expected to be set via the inspector or by other code.
Methods
public override void RecordChanges(EntityManager entityManager, PrefabSystem prefabSystem)
Records which components on the MapTilePrefab's entity are relevant so the prefab system knows what to persist/track. Implementation:- Validates m_Prefab is not null; logs a critical message and returns if it is.
- Resolves the ECS Entity for m_Prefab via prefabSystem.GetEntity.
- Calls entityManager.GetComponentData
(entity) to touch the TilePurchaseCostFactor component. - Calls entityManager.GetBuffer
(entity) to touch the MapFeatureData buffer. -
Purpose: ensures those component types are considered changed/important when saving or applying mode changes.
-
public override void ApplyModeData(EntityManager entityManager, PrefabSystem prefabSystem)
Applies the mode-specific overrides stored on this MapTileMode to the prefab entity: - Validates m_Prefab is not null; logs and returns on null.
- Resolves the entity for m_Prefab.
- Gets the DynamicBuffer
for the entity. - Iterates m_MapFeatures and writes each override into the buffer at the index given by featureInfo.m_MapFeature (casts enum to int).
- Creates a new TilePurchaseCostFactor using mapTilePrefab.m_PurchaseCostFactor and sets it on the entity with SetComponentData.
-
Note: This replaces only the entries present in m_MapFeatures; unspecified features retain their existing values in the buffer unless explicitly set.
-
public override void RestoreDefaultData(EntityManager entityManager, PrefabSystem prefabSystem)
Restores the prefab entity data to the defaults stored in the referenced MapTilePrefab: - Validates m_Prefab is not null; logs and returns on null.
- Resolves the entity for m_Prefab.
- Gets the DynamicBuffer
for the entity. - Iterates mapTilePrefab.m_MapFeatures (the defaults stored on the MapTilePrefab) and writes each default into the buffer at the index given by featureInfo.m_MapFeature.
- Sets TilePurchaseCostFactor on the entity to mapTilePrefab.m_PurchaseCostFactor.
- Use this to revert changes made by ApplyModeData.
Notes about related types used: - MapTilePrefab — container for default map feature data and default purchase cost factor. Contains an array m_MapFeatures (FeatureInfo[]) and m_PurchaseCostFactor. - MapTilePrefab.FeatureInfo — expected to contain: - m_MapFeature (enum/index used to select the buffer slot) - m_Cost (value stored in MapFeatureData) - MapFeatureData — component/data stored in the prefab's buffer, constructed here with new MapFeatureData(cost). - TilePurchaseCostFactor — component storing purchase cost factor for the tile; set via SetComponentData.
Concurrency / safety: - The class assumes the EntityManager contains the expected components/buffers for the referenced prefab entity. GetComponentData/GetBuffer will throw if the component is missing; RecordChanges intentionally touches those components to ensure the prefab system tracks them ahead of modifications. - The index used to write into the buffer is derived from the feature enum value; ensure the enum values correspond to buffer layout/length.
Usage Example
// Example: from an editor script or initializer where you have access to entityManager and prefabSystem.
// Assume mapTileMode is a MapTileMode instance assigned via inspector or created at runtime.
mapTileMode.m_Prefab = someMapTilePrefab; // assign the MapTilePrefab asset
mapTileMode.m_MapFeatures = new MapTilePrefab.FeatureInfo[]
{
new MapTilePrefab.FeatureInfo { m_MapFeature = MapFeature.Rock, m_Cost = 500 },
new MapTilePrefab.FeatureInfo { m_MapFeature = MapFeature.Forest, m_Cost = 200 }
};
// Record that these components are relevant for prefab changes:
mapTileMode.RecordChanges(entityManager, prefabSystem);
// Apply the mode overrides to the prefab entity:
mapTileMode.ApplyModeData(entityManager, prefabSystem);
// To revert to the original defaults stored on the MapTilePrefab:
mapTileMode.RestoreDefaultData(entityManager, prefabSystem);