Game.UI.InGame.AnimalSection
Assembly:
Game
Namespace:
Game.UI.InGame
Type:
class
Base:
InfoSectionBase
Summary:
AnimalSection is an in-game UI info section that displays information about animal entities (pets, livestock, wildlife) for the currently selected entity. It inspects ECS components on the selected entity to determine the animal type, its owner (if any), and where the animal is headed (destination). It adds an appropriate tooltip key for the UI and serializes these properties to JSON for the UI layer. The class relies on the game's EntityManager and several game components (HouseholdPet, Wildlife, CurrentTransport, Target, Owner, OutsideConnection) and uses the name system (m_NameSystem) to bind readable names when serializing.
Fields
private enum TypeKey { Pet, Livestock, Wildlife }
This nested enum categorizes animals into three types: Pet, Livestock and Wildlife. It is used internally to determine which tooltip/key to display and to serialize the "typeKey" property.
(No other explicit private fields are declared — ownerEntity and destinationEntity are implemented as private auto-properties.)
Properties
-
protected override string group => "AnimalSection"
Identifier for the UI group this section belongs to. Used by the base InfoSection system to group and manage UI sections. -
private TypeKey typeKey { get; set; }
Tracks the currently determined animal type for the selected entity. Set during processing (OnProcess) based on the presence of certain ECS components. -
private Entity ownerEntity { get; set; }
Holds the Entity reference for the owner (household) of a pet or livestock, or Entity.Null when there is no owner. Used by OnWriteProperties to serialize owner information and bind a readable name via m_NameSystem. -
private Entity destinationEntity { get; set; }
Holds the Entity reference for the destination the animal is heading toward (extracted from CurrentTransport -> Target or Owner or direct target). Entity.Null if no destination is determined. Used by OnWriteProperties to serialize destination info and bind a readable name.
Constructors
public AnimalSection()
Default constructor. Marked with [Preserve] so it's kept by Unity's linkers. No initialization beyond base constructor behavior; state is initialized/reset via Reset and updated in OnProcess.
Methods
-
private string GetTypeKeyString(TypeKey typeKey)
Returns the string key corresponding to a TypeKey value: "Pet", "Livestock", or "Wildlife". Used to add human-readable tooltip keys for the UI (added to base.tooltipKeys). -
protected override void Reset()
Clears the internal Entity references by setting ownerEntity and destinationEntity to Entity.Null. Called by the base section lifecycle when resetting state. -
private bool Visible()
Determines whether the AnimalSection should be visible for the currently selected entity. Returns true if the selected entity has a HouseholdPet component or a Wildlife component. Used by OnUpdate to set base.visible. -
[Preserve] protected override void OnUpdate()
Updates the visible flag of the section each frame (or each update tick) by calling Visible() and assigning the result to base.visible. Marked with [Preserve] to avoid stripping. -
protected override void OnProcess()
Main processing method called by the info section system when an entity is being inspected. Steps performed: - Determines the animal type (typeKey) via GetTypeKey().
- If the selected entity has a HouseholdPet component, sets ownerEntity to the household Entity stored in the component (otherwise Entity.Null).
- Determines destinationEntity via GetDestination().
-
Adds a tooltip key based on the typeKey by calling GetTypeKeyString and adding to base.tooltipKeys.
-
private Entity GetDestination()
Attempts to find the destination Entity for the selected entity: - If the selected entity has a CurrentTransport component, inspect its m_CurrentTransport to get a Target component; obtain the target Entity.
- If the found entity is an OutsideConnection, return that entity.
- If it has an Owner component, return the Owner.m_Owner.
- If the entity exists in the EntityManager, return the entity.
-
If none of the above yield a valid destination, return Entity.Null. This method encapsulates the logic to turn transport/target/owner relationships into a single destination Entity for display.
-
private TypeKey GetTypeKey()
Determines the TypeKey for the selectedEntity: - If selectedEntity has HouseholdPet -> Pet
- Else if selectedEntity has Wildlife -> Wildlife
-
Else -> Livestock Used by OnProcess to set typeKey.
-
public override void OnWriteProperties(IJsonWriter writer)
Serializes the section's properties to JSON for the UI layer: - Writes "typeKey" as the enum name string (e.g., "Pet").
- Writes "owner" as a bound name (via m_NameSystem.BindName) or null if ownerEntity is Entity.Null.
- Writes "ownerEntity" as the raw Entity value or null.
- Writes "destination" as a bound name or null.
- Writes "destinationEntity" as the raw Entity value or null. This method depends on m_NameSystem being available on the base class to resolve readable names for entities.
Usage Example
// The AnimalSection is normally instantiated and managed by the UI system.
// Example: how the section determines visibility and exposes properties for serialization.
[Preserve]
protected override void OnUpdate()
{
base.OnUpdate();
// base.visible will be set by AnimalSection.OnUpdate() based on whether
// the selected entity has HouseholdPet or Wildlife.
if (base.visible)
{
// The UI system will call OnProcess() and OnWriteProperties() to populate UI data.
// You can inspect ownerEntity/destinationEntity (internally) or rely on the serialized JSON:
// {
// "typeKey": "Pet",
// "owner": "Household #123",
// "ownerEntity": 12345,
// "destination": "Park",
// "destinationEntity": 67890
// }
}
}
Notes and integration tips: - This class expects the game's ECS to provide the listed components (HouseholdPet, Wildlife, CurrentTransport, Target, Owner, OutsideConnection). - Entity.Null is used throughout to indicate missing/unknown owner or destination. - The m_NameSystem on the base InfoSectionBase is used to convert Entity references into user-facing names when writing JSON. If you extend or reuse this class, ensure m_NameSystem is available and initialized. - Tooltip keys added via base.tooltipKeys correspond to the type string returned by GetTypeKeyString; ensure localization/UI mapping is present for "Pet", "Livestock", and "Wildlife".