Skip to content

Game.Prefabs.SwimmingAnimal

Assembly: Assembly-CSharp
Namespace: Game.Prefabs

Type: class

Base: ComponentBase

Summary:
SwimmingAnimal is a prefab component intended for aquatic creatures. It exposes configurable swim speed and preferred swim depth (min/max). At initialization it writes those values into the ECS AnimalData component on the entity (converting the swim speed from km/h to m/s). The class is annotated with a ComponentMenu attribute so it is available under the Creatures menu in the editor.


Fields

  • public System.Single m_SwimSpeed
    Default: 20f. Represents the swim speed value used in the prefab. The value stored here is interpreted as km/h in the inspector/code and converted to meters per second when written into AnimalData (division by 3.6f) during Initialize.

  • public Colossal.Mathematics.Bounds1 m_SwimDepth
    Default: new Bounds1(5f, 20f). Represents the allowable swim depth (min/max). The value is written directly into the AnimalData component.

Properties

  • None.
    This class does not declare any C# properties.

Constructors

  • public SwimmingAnimal()
    Default parameterless constructor (compiler-provided). The public fields are initialized with their declared defaults.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Intended to add required ECS ComponentType entries that must be present on the prefab entity. In this implementation the method is empty; if you create a custom prefab that relies on specific components (for example AnimalData), you should add them here: Example: components.Add(ComponentType.ReadWrite());

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    Intended to add component types that should be part of the entity archetype. Empty in this implementation. Use this to ensure the entity archetype contains the ECS components your prefab requires.

  • public override void Initialize(EntityManager entityManager, Entity entity)
    Called when the prefab component is applied to an entity. This implementation:

  • Calls base.Initialize(entityManager, entity).
  • Reads the existing AnimalData from the entity.
  • Sets AnimalData.m_SwimSpeed = m_SwimSpeed / 3.6f (converts km/h to m/s).
  • Sets AnimalData.m_SwimDepth = m_SwimDepth.
  • Writes the modified AnimalData back to the entity via entityManager.SetComponentData. Note: Initialize assumes an AnimalData component already exists on the entity. Ensure GetPrefabComponents/GetArchetypeComponents include AnimalData (or otherwise ensure the component is present).

Usage Example

// Typical usage: configure the prefab fields in the inspector or via code.
// Ensure the prefab's entity has an AnimalData component so Initialize can write to it.

[ComponentMenu("Creatures/", new Type[] { typeof(AnimalPrefab) })]
public class MySwimmingAnimal : SwimmingAnimal
{
    // You can override or extend behavior here. For example, ensure AnimalData is part of the prefab:
    public override void GetPrefabComponents(HashSet<ComponentType> components)
    {
        base.GetPrefabComponents(components);
        components.Add(ComponentType.ReadWrite<AnimalData>());
    }
}

// Alternatively, set fields on the prefab in code or inspector:
var prefab = /* obtain prefab GameObject or component */;
prefab.m_SwimSpeed = 12f;               // km/h in editor
prefab.m_SwimDepth = new Bounds1(3f, 15f);

{{ Additional notes: - m_SwimSpeed in this class is expected to be in km/h (common for editor-facing values). The Initialize method converts it to m/s for runtime use by dividing by 3.6. - Bounds1 is a simple min/max struct used across the codebase to represent a range (here: depth range). - If you implement custom logic that requires more ECS components (e.g., movement, collision), add them in GetPrefabComponents/GetArchetypeComponents to ensure the prefab entity gets the proper components at spawn. }}