@@ -7,6 +7,7 @@ import List exposing (map, range)
77import String exposing (fromInt )
88import Svg
99import Svg.Attributes as SA
10+ import Time
1011
1112
1213
@@ -28,6 +29,14 @@ main =
2829
2930type alias Model =
3031 { board : Board
32+ , currentPiece : CurrentPiece
33+ }
34+
35+
36+ type alias CurrentPiece =
37+ { position : Position
38+ , tiles : List Position
39+ , color : FieldColor
3140 }
3241
3342
@@ -128,8 +137,16 @@ init _ =
128137
129138 emptyBoard =
130139 { rows = map mkEmptyRow <| range 1 boardHeight }
140+
141+ currentPiece =
142+ { position = ( 5 , boardHeight )
143+ , tiles = tPiece. tiles
144+ , color = tPiece. color
145+ }
131146 in
132- ( { board = setField ( 5 , 3 ) Red <| setField ( 1 , 1 ) Blue emptyBoard }
147+ ( { board = emptyBoard
148+ , currentPiece = currentPiece
149+ }
133150 , Cmd . none
134151 )
135152
@@ -141,11 +158,17 @@ init _ =
141158type Msg
142159 = Left
143160 | Right
161+ | GravityTick Time . Posix
144162
145163
146164update : Msg -> Model -> ( Model , Cmd Msg )
147165update msg model =
148166 case msg of
167+ GravityTick _ ->
168+ ( dropCurrentPiece model
169+ , Cmd . none
170+ )
171+
149172 Left ->
150173 ( model
151174 , Cmd . none
@@ -157,13 +180,25 @@ update msg model =
157180 )
158181
159182
183+ dropCurrentPiece : Model -> Model
184+ dropCurrentPiece ( { currentPiece } as model) =
185+ let
186+ ( x, y ) =
187+ currentPiece. position
188+
189+ droppedPiece =
190+ { currentPiece | position = ( x, y - 1 ) }
191+ in
192+ { model | currentPiece = droppedPiece }
193+
194+
160195
161196-- SUBSCRIPTIONS
162197
163198
164199subscriptions : Model -> Sub Msg
165200subscriptions model =
166- Sub . none
201+ Time . every 1000 GravityTick
167202
168203
169204
@@ -174,18 +209,21 @@ view : Model -> Html Msg
174209view model =
175210 div []
176211 [ div [] [ text " LOOK MUM, NO SERVER" ]
177- , boardView model. board
212+ , boardView model. board model . currentPiece
178213 ]
179214
180215
181- placePieceOnBoard : Position -> PieceDefinition -> Board -> Board
182- placePieceOnBoard ( x , y ) pieceDef oldBoard =
216+ placePieceOnBoard : CurrentPiece -> Board -> Board
217+ placePieceOnBoard currentPiece oldBoard =
183218 let
219+ ( x, y ) =
220+ currentPiece. position
221+
184222 translateTile ( tx, ty ) =
185223 ( x + tx, y + ty )
186224
187225 absoluteTilePositions =
188- map translateTile pieceDef . tiles
226+ map translateTile currentPiece . tiles
189227
190228 minRowTaken =
191229 Maybe . withDefault 0 <|
@@ -207,7 +245,7 @@ placePieceOnBoard ( x, y ) pieceDef oldBoard =
207245
208246 colorField columnIndex field =
209247 if containedInTakenPositions ( columnIndex, rowIndex ) then
210- Field pieceDef . color
248+ Field currentPiece . color
211249
212250 else
213251 field
@@ -227,17 +265,14 @@ placePieceOnBoard ( x, y ) pieceDef oldBoard =
227265 { oldBoard | rows = newRows }
228266
229267
230- boardView : Board -> Html Msg
231- boardView board =
268+ boardView : Board -> CurrentPiece -> Html Msg
269+ boardView board currentPiece =
232270 let
233- boardWithPiece =
234- placePieceOnBoard ( 2 , 9 ) tPiece board
235-
236- boardWithPieces =
237- placePieceOnBoard ( 5 , 13 ) jPiece boardWithPiece
271+ boardWithCurrentPiece =
272+ placePieceOnBoard currentPiece board
238273
239274 rowViews =
240- List . concat ( List . indexedMap rowView boardWithPieces . rows)
275+ List . concat ( List . indexedMap rowView boardWithCurrentPiece . rows)
241276 in
242277 Svg . svg
243278 [ SA . width " 150"
0 commit comments