Skip to content

Component that gives any entity Trigger-like behaviour #1116

@swoolcock

Description

@swoolcock

I am planning on implementing this myself

Yes

Describe your request

Essentially, the code modder can add a component to their entity that gives the OnEnter/OnStay/OnLeave functionality as if it were a Trigger. This means modders will no longer need to manually track CollideChecks across frames in the entity Update.

I've currently named the component Triggerable to be similar in naming to Holdable, and the actions OnEnter/OnStay/OnLeave, with their virtual invocation methods called InvokeOnEnter/InvokeOnStay/InvokeOnLeave in case the modder wants to override the component instead of assigning actions.

These actions (or method groups) can be passed into the constructor and/or assigned directly in the object initialiser. The first argument is an optional collider to use instead of the entity's Collider property.

Since colliders can be specified, this means multiple trigger regions can be assigned to the same entity if desired, each with different functionality.

This is an example where I've added three separate trigger regions to a gravity field, using different instantiation styles.

// custom hitbox, method groups
Add(new Triggerable(new Hitbox(16, Height, -16, 0), null, onStay, onLeave));

// custom hitbox, object initialiser
Add(new Triggerable(new Hitbox(16, Height, Width, 0)) {
    OnEnter = _ => Audio.Play(SFX.char_gran_laugh_firstphrase),
    OnLeave = player => player.Die(Vector2.Zero)
});

// default collider, object initialiser
Add(new Triggerable {
    OnEnter = player => player.SuperBounce(Top)
});

private void onStay(Player player) => player.Sprite.Scale.X = 2;
private void onLeave(Player player) => player.Sprite.Scale.X = 1;
triggerable.mp4

Additional context

No response

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions