Fighting with FRP

淺唱寂寞╮ 提交于 2019-12-04 07:50:34

I've had similar experiences when trying to write games with Bacon and RxJs. Things that have a self-dependency (like player's position) are tough to handle in a "pure FRP" way.

For example, in my early Worzone game I included a mutable targets object that can be queried for positions of players and monsters.

Another approach is to do as the Elm guys do: model the full game state as a single Property (or Signal as it's called in Elm) and calculate the next state based on that full state.

So far my conclusion is that FRP is not so well-suited for game programming, at least in a "pure" way. After all, mutable state might be the more composable approach for some things. In some game projects, like the Hello World Open car race, I've used mutable state, like the DOM for storing state and EventStreams for passing events around.

So, Bacon.js is not a silver bullet. I suggest you find out yourself, where to apply FRP and where not to!

I have a similar filling sometimes. For me the experience of programming with FRP is mostly solving puzzles. Some of them are easy, some not. And those that I find easy might be harder form my colleagues and vice versa. And I don't like this about FRP.

Don't get me wrong, I like solving puzzles, this is a lot fun! But I think programming at payed work should be more... boring. More predictable. And code should be as straightforward as possible, even primitive.

But of course global mutable state also not the way we should go. I think we should find a way to make FRP more boring :)


Also a remark on your code, I think this will be more FRP'ish (a not tested draft):

var otherPlayerPositions = players
    .filter(nEq(me))
    .map(function(player){return player.position});

otherPlayerPositions = Bacon.combineAsArray(otherPlayerPositions);

me.position = otherPlayerPositions
    .sampledBy(movement, function(otherPositions, move) {
        return {otherPositions: otherPositions, move: move};
    })
    .scan(initPos, function(myCurPosition, restArgs) {
        var myNextPosition = myCurPosition + restArgs.move;
        if (!restArgs.otherPositions.some(eq(myNextPosition))) {
            return myNextPosition;
        } else {
            return myCurPosition;
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!