Skip to content

Commit d1b908b

Browse files
committed
Episode 4: Add current piece and gravity
1 parent 89196d6 commit d1b908b

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

elm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
"elm/core": "1.0.2",
1111
"elm/html": "1.0.0",
1212
"elm/http": "2.0.0",
13-
"elm/svg": "1.0.1"
13+
"elm/svg": "1.0.1",
14+
"elm/time": "1.0.0"
1415
},
1516
"indirect": {
1617
"elm/bytes": "1.0.8",
1718
"elm/file": "1.0.5",
1819
"elm/json": "1.1.3",
19-
"elm/time": "1.0.0",
2020
"elm/url": "1.0.0",
2121
"elm/virtual-dom": "1.0.2"
2222
}

src/Main.elm

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import List exposing (map, range)
77
import String exposing (fromInt)
88
import Svg
99
import Svg.Attributes as SA
10+
import Time
1011

1112

1213

@@ -28,6 +29,14 @@ main =
2829

2930
type 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 _ =
141158
type Msg
142159
= Left
143160
| Right
161+
| GravityTick Time.Posix
144162

145163

146164
update : Msg -> Model -> ( Model, Cmd Msg )
147165
update 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

164199
subscriptions : Model -> Sub Msg
165200
subscriptions model =
166-
Sub.none
201+
Time.every 1000 GravityTick
167202

168203

169204

@@ -174,18 +209,21 @@ view : Model -> Html Msg
174209
view 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

Comments
 (0)