Skip to content

Behavior Class

Base class for behaviors — logic that operates on a single entity. Each entity gets its own behavior instance with constructor injection support.

public abstract class Behavior

Inheritance System.Object → Behavior

Remarks

Use behaviors for entity-specific logic (player movement, boss AI, etc.). For batch processing of many entities, use systems instead.

Execution order: During Brine2D.ECS.EntityWorld.Update(Brine2D.Core.GameTime), all IUpdateSystems run first (sorted by UpdateOrder), then all behaviors (sorted by UpdateOrder). During Brine2D.ECS.EntityWorld.FixedUpdate(Brine2D.Core.GameTime), all IFixedUpdateSystems run first (sorted by FixedUpdateOrder), then all behaviors (sorted by FixedUpdateOrder). During Brine2D.ECS.EntityWorld.Render(Brine2D.Rendering.IRenderer), all IRenderSystems run first (sorted by RenderOrder), then all behaviors (sorted by RenderOrder). Behaviors therefore always execute after system-rendered content. If you need to render between systems, move the render logic into a custom IRenderSystem with the appropriate RenderOrder.

public class PlayerMovementBehavior : Behavior
{
    private readonly IInputContext _input;
    private readonly IAudioService _audio;

    public PlayerMovementBehavior(IInputContext input, IAudioService audio)
    {
        _input = input;
        _audio = audio;
    }

    public override int UpdateOrder => 10;

    public override void Update(GameTime gameTime)
    {
        if (_input.IsKeyDown(Key.W))
            _audio.PlaySound(_footstep);
    }
}
Properties
Entity The entity this behavior is attached to.
FixedUpdateOrder Controls execution order relative to other behaviors during FixedUpdate. Lower values run first. Default is 0. Behaviors with the same order run in the order they were added.
IsEnabled Whether this behavior is enabled (affects Update, FixedUpdate, and Render calls).
RenderOrder Controls execution order relative to other behaviors during Render. Lower values run first. Default is 0. Behaviors with the same order run in the order they were added.
UpdateOrder Controls execution order relative to other behaviors during Update. Lower values run first. Default is 0. Behaviors with the same order run in the order they were added.
Methods
FixedUpdate(GameTime) Called at a fixed timestep if enabled. Use this for deterministic simulation logic (physics responses, AI ticks, etc.).
OnAttached() Called once when the behavior is attached to an entity. Use this to validate required components and cache references.
OnDetached() Called when the behavior is removed from an entity.
Render(IRenderer) Called every frame during the render pass if enabled. Runs \<b>after\</b> all IRenderSystems, so behavior-rendered content always draws on top of system-rendered content.
Update(GameTime) Called every frame if enabled.