Skip to content

Game.UI.Tooltip.NaturalResourcesTooltipSystem

Assembly:
Game

Namespace:
Game.UI.Tooltip

Type:
public class

Base:
TooltipSystemBase

Summary:
NaturalResourcesTooltipSystem displays mouse tooltips for natural resource heatmaps (fertility, oil, ore, fish — wood is declared but not used in the heatmap handling) when the Default tool and an infoview heatmap are active. It raycasts the terrain under the mouse cursor using the CameraUpdateSystem/ RaycastSystem, samples water depth (via WaterSystem) when needed, reads natural resource maps (via NaturalResourceSystem) with proper JobHandle completion, and adds IntTooltip entries for the available (base - used) resource amount using TooltipSystemBase.AddMouseTooltip. Methods and fields are [Preserve]-annotated to avoid stripping.


Fields

  • private PrefabSystem m_PrefabSystem
    Holds the game's PrefabSystem (obtained in OnCreate). Not used directly in the shown logic but typical for UI systems that may need prefab data.

  • private ToolSystem m_ToolSystem
    Reference to ToolSystem used to check the active tool and active infoview.

  • private DefaultToolSystem m_DefaultTool
    Reference to the default tool system; NaturalResourcesTooltipSystem only runs when this is the active tool.

  • private RaycastSystem m_RaycastSystem
    Used to add raycast inputs and obtain raycast results for the mouse position.

  • private CameraUpdateSystem m_CameraUpdateSystem
    Used to get the current viewer/camera to calculate the raycast line.

  • private NaturalResourceSystem m_NaturalResourceSystem
    Provides access to natural resource maps and helper methods (GetMap, GetFertilityAmount, GetOilAmount, GetOreAmount, GetFishAmount).

  • private WaterSystem m_WaterSystem
    Used to get water surface data which is sampled to determine if a point is over water/depth for fish/fertility checks.

  • private EntityQuery m_ActiveInfomodeQuery
    EntityQuery that matches active infomode heatmap infoviews. Constructed to require PrefabData, InfomodeData, InfomodeActive, InfoviewHeatmapData and to exclude InfomodeGroup.

  • private IntTooltip m_Fertility
    Prepared IntTooltip instance for fertility; configured with path, icon, localized label, and unit.

  • private IntTooltip m_Wood
    Prepared IntTooltip instance for wood (declared/configured but not referenced in OnUpdate).

  • private IntTooltip m_Oil
    Prepared IntTooltip instance for oil.

  • private IntTooltip m_Ore
    Prepared IntTooltip instance for ore.

  • private IntTooltip m_Fish
    Prepared IntTooltip instance for fish.

  • private RaycastResult m_RaycastResult
    Cached last RaycastResult returned from RaycastSystem. Used by the private property raycastResult.

Properties

  • private RaycastResult raycastResult { get; set; }
    Getter: If cached m_RaycastResult.m_Owner is Entity.Null and CameraUpdateSystem can provide a viewer, constructs a RaycastInput (TypeMask.Terrain, CollisionMask.OnGround|Overground), adds it to RaycastSystem, reads the result array, and caches the first result. Setter: writes into m_RaycastResult. This property centralizes obtaining/updating the terrain raycast under the cursor.

Constructors

  • public NaturalResourcesTooltipSystem()
    Default constructor. Marked with [Preserve] in source to prevent stripping. No custom initialization logic beyond what OnCreate handles.

Methods

  • protected override void OnCreate() : System.Void
    Called when the system is created. Obtains references to several managed systems from World (PrefabSystem, ToolSystem, DefaultToolSystem, RaycastSystem, CameraUpdateSystem, NaturalResourceSystem, WaterSystem). Builds the m_ActiveInfomodeQuery to select active infoview heatmaps and initializes the IntTooltip instances (m_Fertility, m_Wood, m_Oil, m_Ore, m_Fish) with icon paths and localized labels. The method is [Preserve]-annotated.

Notes: - InfoviewHeatmapData.m_Type determines which resource heatmap the infoview is showing (HeatmapData.Fertility/Oil/Ore/Fish). - LocalizedString.Id(...) is used to set the tooltip label keys.

  • protected override void OnUpdate() : System.Void
    Main runtime logic performed each frame (when scheduled by the entity system). Early-return conditions:
  • If the active tool is not the default tool.
  • If there is no active infoview.
  • If the active infomode query is empty.

Behavior: - Resets raycastResult to default and enumerates entities from m_ActiveInfomodeQuery. - For each matching InfoviewHeatmapData entity, switches on m_Type: - Fertility: - Requires a valid raycast result. - Gets water surface data (JobHandle), completes it, samples depth; only proceeds if not over water (depth <= 0). - Gets NaturalResourceSystem map (JobHandle), completes it, obtains fertility amount at the hit position. - If (base > used) sets m_Fertility.value to base - used and calls AddMouseTooltip(m_Fertility). - Oil: - Requires valid raycast. - Gets resource map, completes it, obtains oil amount. If available, sets m_Oil.value and AddMouseTooltip(m_Oil). - Ore: - Similar to oil: get map, complete, check ore amount, show m_Ore tooltip. - Fish: - Requires valid raycast. - Gets water surface data, completes it, requires depth > 0 (i.e., water present). - Gets resource map, completes it, obtains fish amount and shows m_Fish if available. - Disposes the temporary native entity array.

Important implementation details: - All NativeArray/Native collections returned from system GetMap / GetSurfaceData are accompanied by JobHandles which this system explicitly completes before reading to ensure memory safety. - Early returns avoid unnecessary work if the raycast result is empty. - The code shows correct disposal of the temporary NativeArray created from the EntityQuery. - m_Wood tooltip is configured in OnCreate but never used in OnUpdate — likely reserved for future heatmap support.

Usage Example

[Preserve]
protected override void OnUpdate()
{
    // Example: minimal logic that mirrors how NaturalResourcesTooltipSystem shows fertility tooltips.
    if (m_ToolSystem.activeTool != m_DefaultTool || m_ToolSystem.activeInfoview == null || m_ActiveInfomodeQuery.IsEmptyIgnoreFilter)
        return;

    // ensure we have a raycast result for the cursor
    if (raycastResult.m_Owner == Entity.Null)
        return;

    // read the natural resource map safely (complete the returned JobHandle before accessing the map)
    JobHandle deps;
    NativeArray<NaturalResourceCell> map = m_NaturalResourceSystem.GetMap(readOnly: true, out deps);
    deps.Complete();

    // sample fertility at the raycast hit position
    NaturalResourceAmount fert = NaturalResourceSystem.GetFertilityAmount(raycastResult.m_Hit.m_HitPosition, map);
    if (fert.m_Base > fert.m_Used)
    {
        m_Fertility.value = fert.m_Base - fert.m_Used;
        AddMouseTooltip(m_Fertility); // TooltipSystemBase helper
    }
    // dispose of 'map' if required by the API (GetMap usage in original code indicates system manages lifetime here)
}

Additional notes and best practices: - Always Complete() job handles returned by other systems before reading NativeArray data on the main thread. - Dispose any NativeArray you allocate (the EntityQuery.ToEntityArray call is disposed in the original code). - Use CameraUpdateSystem.TryGetViewer to avoid null references when computing raycasts. - Respect water depth checks (WaterUtils.SampleDepth) when showing fish/fertility tooltips to avoid showing inappropriate tooltips over water/land. - The system relies on localized string keys; mods should use the same keys or provide localized entries if adding new tooltip types.