-
Notifications
You must be signed in to change notification settings - Fork 4
/
Game.elm
99 lines (68 loc) · 1.68 KB
/
Game.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module Game where
import Html exposing (Html, div, button, text)
import Graphics.Element exposing (Element)
import Graphics.Collage
import Keyboard
import Random
import Window
import Board
-- MODEL
type alias Model =
Board.Model
initialModel : Model
initialModel =
Board.init initialSeed 3 3 100 1
|> Board.update (Board.Shuffle 100)
-- UPDATE
type Action
= NoOp
| ArrowLeft
| ArrowRight
| ArrowUp
| ArrowDown
update : Action -> Model -> Model
update action model =
case action of
ArrowLeft ->
model |> Board.update (Board.Move Board.Left)
ArrowRight ->
model |> Board.update (Board.Move Board.Right)
ArrowUp ->
model |> Board.update (Board.Move Board.Up)
ArrowDown ->
model |> Board.update (Board.Move Board.Down)
_ ->
model
-- VIEW
view : (Int, Int) -> Model -> Html
view (windowWidth, windowHeight) model =
div [ ]
[ {--button [ ] [ text "Shuffle" ]
, --}Html.fromElement (Graphics.Collage.collage windowWidth (windowHeight - 20) (Board.view model))
]
-- PORTS
port initialSeed : Int
-- SIGNALS
arrows : Signal Action
arrows =
let
toAction arrow =
if | arrow == { x = -1, y = 0 } -> ArrowLeft
| arrow == { x = 1, y = 0 } -> ArrowRight
| arrow == { x = 0, y = 1 } -> ArrowUp
| arrow == { x = 0, y = -1 } -> ArrowDown
| otherwise -> NoOp
in
Keyboard.arrows
|> Signal.map toAction
|> Signal.filter (\a -> a /= NoOp) NoOp
input : Signal Action
input =
arrows -- Will add mouse clicks later
model : Signal Model
model =
Signal.foldp update initialModel input
-- MAIN
main : Signal Html
main =
Signal.map2 view Window.dimensions model