January 2019
Intermediate to advanced
298 pages
7h 46m
English
The groundwork we've done so far will make sure that we can easily handle game events in a clean and maintainable way. The main idea behind having a stream representing the game keys is that now we can partition it much like we would a normal list.
For instance, if we're interested in all events where the key being pressed is UP, we would run the following code:
(->> active-keys-stream
(r/filter (partial some #{UP}))
(r/map (fn [_] (.log js/console "Pressed up..."))))
Similarly, for events involving the FIRE key, we could do the following:
(->> active-keys-stream
(r/filter (partial some #{FIRE}))
(r/map (fn [_] (.log js/console "Pressed fire..."))))
This works because, in Clojure, sets can be used as ...