How to Make Behavior Contain Other Behaviors in a Jet Banana

Everyone seems to be making a computer game with a reactive banana FRP structure, so I had to try this too. However, I was stuck with the early steps trying to describe the game data as types of Behavior reactive banana.

Basically, I'm trying to have a (changing) list of game characters (such as Frodo or Sam). Game characters can be updated on every tick of the game (modeled as a tick event). The idea is that whenever a game symbol changes, a single Event (Character) triggered, and the symbol update is finally sent over the network. The advantage of this in comparison with Asteroids.hs should be that the full state of the game (the entire list of game characters) does not need to be sent over the network, as there will be a number of events having one game symbol instead of one event with a game list of symbols.

For one game character, this works great! I created a Behavior (Character) using mapaccum so that the symbol update signal is sent when the symbol is updated. The problem that I cannot solve is how to make this work with the Behavior list of game characters.

I am trying to simulate a list of game characters as Behavior , as characters can come and go during the game. Do I need to use dynamic event switching here? Or, if I do not use Behavior with individual game symbols (and use only Behavior in the list of game symbols), is there a way to conditionally trigger update events when I look at the list of symbols? Or do I understand nothing here?

+6
source share
1 answer

To manage a dynamic collection of behavior, you need to use dynamic event switching. See BarTab.hs Example for a demo.

However, dynamic event switching can be a bit cumbersome and can often be avoided. Two common situations:

  • A collection of behaviors is known statically. TwoCounters.hs demonstrates that you can simply use combinators of applicative functors to calculate a value that depends on any behavior.
  • A dynamic collection can be modeled as a collection behavior ( Behavior [a] ) rather than a set of behaviors ( [Behavior a] ). The Asteroids.hs example demonstrates how to use this. This is a stylistic compromise: individual collection entries can no longer be formulated using FRP, but collection becomes easier to manage.
+5
source

Source: https://habr.com/ru/post/947679/


All Articles