amethyst icon indicating copy to clipboard operation
amethyst copied to clipboard

Make Facet take a specific command and be composable

Open BobG1983 opened this issue 5 years ago • 9 comments

This lets us not send commands to things that can't take them, which lets us build global actors that can take AddEntity and RemoveEntity commands without adding ever increasing cost to addentity/removeentity for entities that don't care about those commands.

Example:

/**
 * A [Facet] is a [System] which performs actions based on the
 * [Command]s they receive.
 */
interface Facet<C : Context, T : Command<out EntityType, C>> : System<C> {

    /**
     * Performs the given [Command].
     * @return the [Response] to the given [command].
     * @see [Response] for more info.
     */
    fun executeCommand(command: T): Response

}

and then the compose function:

fun compose(other: Facet<C, Command<out EntityType, C>>): Facet<C, Command<out EntityType, C>>

This also means that facets can only take a single command. Which is a good thing.

What happens if I try and dispatch a command at runtime and an entity has no facets that intercept it?

BobG1983 avatar May 01 '19 22:05 BobG1983

would the API be facet.compose(otherFacet)

Or would it be better to have a CompositeFacet class that is a facet and has a constructor that takes two facets?

BobG1983 avatar May 02 '19 19:05 BobG1983

I'd do this similar to how Behaviors are composed now. The reason for this is that we can take advantage of operator overloading and we can also define boolean operations like facet0 + facet1 facet0 or facet1, things like that.

adam-arold avatar May 03 '19 09:05 adam-arold

Okay. So basically. If I’m going to implement this look at how behavior composition works.

BobG1983 avatar May 04 '19 14:05 BobG1983

That's a good idea, yes. But first let's think about the kinds of compositions we might want to implement. + can be used to compose but what about and and or? Do you think that would make sense here?

adam-arold avatar May 04 '19 17:05 adam-arold

Okay. Let’s think about an example.

StairAscender StairDescender

Compose into StairTraverser. That’s an additive composition.

For And and Or compositions I can’t think of an example off the top of my head. Both I think would require the two composited facets to take the same command though. An And/Or that takes different facets doesn’t make sense to me.

BobG1983 avatar May 05 '19 08:05 BobG1983

So StairAscender takes GoUp and StairDescender takes GoDown so StairTraverser takes both GoUp and GoDown so it is an additive composition. Job well done, we can go home, right? 😄

adam-arold avatar May 06 '19 09:05 adam-arold

Yeah. I mean sure. My question was, is there a case where we’d want and/or style composition . What would that be useful for if anything?

BobG1983 avatar May 06 '19 21:05 BobG1983

or/and I think is not really meaningful here. We have it with Behaviors and it is useful because they return a Boolean value so we can determine whether it performed an action or not. or short-circuits so if the first Behavior returns true the second is not run at all.

With Facets this is a bit more fuzzy because each Facet responds to a single Command so the composite Facet we get with + is just doing dynamic dispatch. As a corollary the or/and overloads seem useless here because if we have a Command we already know which Facet responds to that so there is no point in short-circuiting.

adam-arold avatar May 07 '19 09:05 adam-arold

Agreed. Okay cool.

If you don’t take a cut first I’ll take a cut at it once I get back from vacation.

On Tue, May 7, 2019 at 10:43 AM Adam Arold [email protected] wrote:

and I think is not really meaningful here. We have it with Behaviors and it is useful because they return a Boolean value so we can determine whether it performed an action or not. or short-circuits so if the first Behavior returns true the second is not run at all.

With Facets this is a bit more fuzzy because each Facet responds to a single Command so the composite Facet we get with + is just doing dynamic dispatch. As a corollary the or/and overloads seem useless here because if we have a Command we already know which Facet responds to that.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Hexworks/amethyst/issues/1#issuecomment-490011504, or mute the thread https://github.com/notifications/unsubscribe-auth/AAWAGGGTANYVCLE4XWLF4OLPUFFMVANCNFSM4HJZL2KQ .

BobG1983 avatar May 07 '19 21:05 BobG1983