Game.Prefabs.Modes.PlaceableNaturalObjectGlobalMode
Assembly: Game
Namespace: Game.Prefabs.Modes
Type: class
Base: EntityQueryModePrefab
Summary:
PlaceableNaturalObjectGlobalMode is a mode prefab that modifies runtime data for "placeable natural objects" (e.g., trees and plants). It multiplies the construction cost and XP reward on PlaceableObjectData instances that match the query (trees/plants) while excluding buildings. The mode schedules a Burst-compiled chunk job to apply multipliers and provides a RestoreDefaultData implementation to reset changes back to prefab defaults.
Fields
-
public float m_ConstructionCostMultiplier
Controls the multiplier applied to PlaceableObjectData.m_ConstructionCost when this mode is applied. Default value is the field's value in the prefab/inspector. Values < 1 reduce cost, > 1 increase it. -
public float m_XPRewardMultiplier
Controls the multiplier applied to PlaceableObjectData.m_XPReward when this mode is applied. Values < 1 reduce XP reward, > 1 increase it. -
private struct ModeJob
(nested type)
A Burst-compiled IJobChunk implementation that applies the multipliers on chunks of PlaceableObjectData. -
public float m_ConstructionCostMultiplier
Multiplier forwarded from the mode instance into the job. -
public float m_XPRewardMultiplier
Multiplier forwarded from the mode instance into the job. -
public ComponentTypeHandle<PlaceableObjectData> m_PlaceableObjectType
Writable ComponentTypeHandle used to access PlaceableObjectData in the job. Note: not marked readonly because the job writes back modified data.
Properties
- None (no public properties declared on this type)
Constructors
public PlaceableNaturalObjectGlobalMode()
Default parameterless constructor (no special initialization beyond Unity/mono default). Typical usage is to configure the public multiplier fields in the inspector or via script.
Methods
public override EntityQueryDesc GetEntityQueryDesc()
Returns the EntityQueryDesc that selects entities to affect:- All: PlaceableObjectData (read), PlaceableInfoviewItem (read)
- Any: TreeData (read) OR PlantData (read)
-
None: BuildingData (read) AND BuildingExtensionData (read) This ensures only placeable natural objects (trees/plants) are targeted and building prefabs are excluded.
-
protected override void RecordChanges(EntityManager entityManager, Entity entity)
Reads the current PlaceableObjectData for the given entity by calling entityManager.GetComponentData(entity). This method is used by the mode system to record state/changes as part of applying modes. The implementation only reads the component (no modification). -
public override JobHandle ApplyModeData(EntityManager entityManager, EntityQuery requestedQuery, JobHandle deps)
Schedules and returns a parallel JobHandle for ModeJob that applies multipliers to matched PlaceableObjectData. The method creates a ModeJob instance with the current multipliers and a non-readonly ComponentTypeHandle obtained from the provided EntityManager, then calls JobChunkExtensions.ScheduleParallel(job, requestedQuery, deps). -
public override void RestoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
Restores construction cost and XP reward to the values stored in the original prefab for each entity in the passed array. For each entity: - Attempts to get the PrefabBase using prefabSystem.TryGetPrefab
(entity, out prefabBase). - Attempts to extract PlaceableObject (component) from the prefab via prefabBase.TryGetExactly
(out component). - If successful, reads the entity's current PlaceableObjectData, overwrites m_ConstructionCost and m_XPReward with the prefab values, and writes the component data back to the entity.
-
If prefab or component is missing, logs a warning using ComponentBase.baseLog.Warn.
-
ModeJob.Execute (IJobChunk implementation)
For each chunk, iterates over PlaceableObjectData elements and: - value.m_ConstructionCost = (uint)((float)value.m_ConstructionCost * m_ConstructionCostMultiplier)
- value.m_XPReward = (int)((float)value.m_XPReward * m_XPRewardMultiplier) Then writes the modified value back to the chunk's native array. The job is Burst-compiled and scheduled in parallel by ApplyModeData.
Usage Example
// Example: configure the mode's multipliers (can be done in code or in the prefab/inspector)
public class MyModeInitializer : MonoBehaviour
{
void Awake()
{
var mode = /* get your PlaceableNaturalObjectGlobalMode instance, e.g. from a prefab or system */;
mode.m_ConstructionCostMultiplier = 0.75f; // reduce tree/plant costs to 75%
mode.m_XPRewardMultiplier = 1.25f; // increase XP reward by 25%
// The game/mode system will call ApplyModeData and RestoreDefaultData as appropriate.
}
}
Notes and implementation details:
- The ModeJob is Burst-compiled for performance and uses JobChunk scheduling (ScheduleParallel).
- PlaceableObjectData is modified in-place and casted back to the component's field types (uint for cost, int for XP).
- RestoreDefaultData relies on PrefabSystem and PrefabBase.TryGetExactly