excaliburjs / Excalibur

🎮 Your friendly TypeScript 2D game engine for the web 🗡️
https://excaliburjs.com
BSD 2-Clause "Simplified" License
1.75k stars 188 forks source link

feat: Trigger - access to action's actor #3157

Closed eonarheim closed 1 week ago

eonarheim commented 1 month ago

Discussed in https://github.com/excaliburjs/Excalibur/discussions/3099

Originally posted by **KostarSf** June 17, 2024 Hi! It would be very useful to have access to the Actor, which calls Trigger's action callback. Something like this: ```ts const trigger = new Trigger({ // rest action: (actor) => { // do action } }) ``` I want to use triggers as areas of view for AI enemies, so it's important for me to know which Actor called action. Now I have to do my own implementation of the trigger, which takes this into account. It would be great if it appeared in Excalibur!
Autsider666 commented 1 month ago

I'm interested in giving this issue a try!

eonarheim commented 1 month ago

@Autsider666 all yours!

Does the scope of this issue also include the https://github.com/excaliburjs/Excalibur/discussions/3099#discussioncomment-9791793 about the type of the actor being returned being modified by the filter callback?

That's a tough one, I think the best we say is that TEntity extends Entity in some way for the filter? Since there are no runtime types I'm not sure we can capture the return, unless it's statically known, so we could allow folks to say triggers only operate on x types?

Rough possible sketch, may or may not compile, but types should be inferred?

class Trigger<TActionTarget extends Entity = Entity> {
    constructor(public targetType: typeof TActionTarget) => {}

    onFilter<TEntity extends Entity>(entity: TEntity): TActionTarget {
        if (entity instanceof this.targetType) {
           return entity
        }
       ..
    }

    onAction(actionTarget: TActionTarget) {

    } 
}