-
Notifications
You must be signed in to change notification settings - Fork 6
/
SnakeSignals.elm
50 lines (32 loc) · 1.11 KB
/
SnakeSignals.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module SnakeSignals where
import Char
import Keyboard
import Random
import Signal exposing ((<~), Signal, filter, mergeMany)
import SnakeModel exposing (..)
import SnakeView exposing (..)
import Time exposing (Time, fps)
timeSignal : Signal Time
timeSignal = fps 50
makeTick : Time -> Event
makeTick time =
let seed1 = Random.initialSeed (round time)
(x,seed2) = Random.generate (Random.int -boardSize boardSize) seed1
(y,_) = Random.generate (Random.int -boardSize boardSize) seed2
in
Tick { x = x, y = y }
tickSignal : Signal Event
tickSignal = makeTick <~ timeSignal
directionSignal : Signal Event
directionSignal =
let arrowsToDelta {x,y} =
if | x == 0 && y == 0 -> Ignore
| x /= 0 -> Direction { dx = x, dy = 0 }
| otherwise -> Direction { dx = 0, dy = y }
in
arrowsToDelta <~ Keyboard.arrows
newGameSignal : Signal Event
newGameSignal =
always NewGame <~ (filter identity False <| Keyboard.isDown (Char.toCode 'N'))
eventSignal : Signal Event
eventSignal = mergeMany [tickSignal, directionSignal, newGameSignal]