harmboschloo / elm-ecs

Using the entity-component-system (ECS) pattern in elm.
https://package.elm-lang.org/packages/harmboschloo/elm-ecs/latest/
BSD 3-Clause "New" or "Revised" License
32 stars 0 forks source link

Composable selectors? #2

Open peacememories opened 5 years ago

peacememories commented 5 years ago

I think there's a case to be made for selectors to be composable (in my case, I would want to create a special selector that transforms two components, and would want to be able to use that in other selectors.

Some kind of selectWith : Selector comparable ecs a -> ComponentSpec comparable ecs a although I'm pretty sure it wouldn't work that way^^'

In my case, I want to be able to wrap another component in an Owned type if it has the component Owned attached

peacememories commented 5 years ago

For now the solution for my usecase is

selectOwned : Selector NeedId Ecs a -> Ecs -> List ( OwnedId, a )
selectOwned selector ecs =
    let
        isOwned =
            Ecs.Select.andHas components.owned
    in
    Ecs.selectList (isOwned selector) ecs
        |> List.map
            (\( id, value ) ->
                ( OwnedId id, value )
            )

which is not terribly reusable, but probably works for now

harmboschloo commented 5 years ago

I did make the mapping functions to make selectors composable but they are currently not exposed. My initial though was to keep selectors a simple as possible. But your use case might be a good reason to expose them anyway. 🤔

peacememories commented 5 years ago

Thanks for responding so quickly!

I took a look at the mapping functions, and they seem to only select a single entity?

What I would really love to see is some json-decode-pipeline-esque pipelining capability. Though I have to say that I am probably not the core target audience for this library, so you should probably not expose things for my sake alone ;) For games I think the current selectors are fine. I should probably workshop some implementations for our data storage and see where I end up, and if where I end up is close to the api of this library :)

harmboschloo commented 5 years ago

I took a look at the mapping functions, and they seem to only select a single entity?

A selector (including the mapping functions) can do both a single entity and a list.

What I would really love to see is some json-decode-pipeline-esque pipelining capability

I like that idea. I'll think about it. That would be nice for setting up the specs as well, but I'm not sure if that's feasible. For the selectors it shouldn't really be a problem. The only downside would be that it's a bit less performant I think.

harmboschloo commented 5 years ago

I published a small package to do dictionary intersections. It's basically what the selectors do under the hood. Maybe it's interesting if you want a bit more flexibility.

https://package.elm-lang.org/packages/harmboschloo/elm-dict-intersect/latest/