-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyramidView.elm
412 lines (325 loc) · 9.63 KB
/
PyramidView.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
module PyramidView exposing (..)
import Html exposing (Html)
import Svg exposing (Svg, svg, polygon, Attribute, ellipse, g)
import Svg.Attributes exposing (..)
import Svg.Events exposing (onClick)
import Msg exposing (Msg(..))
import Model exposing (Model, Size(..), Stack(..), Board(..), Stash)
stashWidth =
500
stashHeight =
above Queen Queen 0
|> abs
|> (*) (Model.maxStashAmount - 1)
|> (+) (pyramidHeightConstant * getScale Queen)
|> (+) (2 * stashSpacing)
stashWidthString =
toString stashWidth
stashHeightString =
toString stashHeight
renderStash : Maybe Size -> Stash -> Html Msg
renderStash selected stash =
svg
[ width stashWidthString
, height stashHeightString
, viewBox ("0 0 " ++ stashWidthString ++ " " ++ stashHeightString)
]
<| [ Svg.rect
[ x "0"
, y "0"
, width stashWidthString
, height stashHeightString
, stroke "black"
, strokeWidth "2"
, fillOpacity "0"
]
[]
]
++ renderStashStack selected (stashWidth * 1 / 6) stash.queen Queen
++ renderStashStack selected (stashWidth * 3 / 6) stash.drone Drone
++ renderStashStack selected (stashWidth * 5 / 6) stash.pawn Pawn
renderStashStack : Maybe Size -> Float -> Int -> Size -> List (Svg Msg)
renderStashStack selected x amount size =
let
pyramidView =
case selected of
Just selectedSize ->
if size == selectedSize then
highlightedPyramid
else
clickablePyramid (Just size |> Select)
Nothing ->
clickablePyramid (Just size |> Select)
in
getHeights amount size
|> List.reverse
|> List.tail
|> Maybe.withDefault []
|> List.reverse
|> List.map (pyramidView size x)
getHeights : Int -> Size -> List Float
getHeights amount size =
List.repeat amount size
|> (List.scanl (above size) (stashHeight - stashSpacing))
stashSpacing =
5
renderStack : Stack -> ( Float, Float ) -> Svg Msg
renderStack stack ( x, y ) =
case stack of
EmptyStack ->
nullSvg
Single size ->
placedPyramid size x y
FullTree ->
g []
[ placedPyramid Queen x y
, placedPyramid Drone x (above Drone Queen y)
, placedPyramid Pawn x (above Pawn Drone (above Drone Queen y))
]
PartialTree ->
g []
[ placedPyramid Queen x y
, placedPyramid Drone x (above Drone Queen y)
]
DroneTree ->
g []
[ placedPyramid Drone x y
, placedPyramid Pawn x (above Pawn Drone y)
]
NoDroneTree ->
g []
[ placedPyramid Queen x y
, placedPyramid Pawn x (above Pawn Queen y)
]
FullNest ->
g []
[ placedPyramid Pawn x (below Pawn Drone (below Drone Queen y))
, placedPyramid Drone x (below Drone Queen y)
, placedPyramid Queen x y
]
PartialNest ->
g []
[ placedPyramid Pawn x (below Pawn Drone y)
, placedPyramid Drone x y
]
DroneNest ->
g []
[ placedPyramid Drone x (below Drone Queen y)
, placedPyramid Queen x y
]
NoDroneNest ->
g []
[ placedPyramid Pawn x (below Pawn Queen y)
, placedPyramid Queen x y
]
below lowerSize upperSize y =
if lowerSize == upperSize then
y
else
case ( lowerSize, upperSize ) of
( Pawn, Queen ) ->
y - (getScale upperSize * 2 / 5)
_ ->
y - (getScale upperSize * 1 / 5)
above upperSize lowerSize y =
if lowerSize == upperSize then
y - (getScale upperSize * 4 / 5)
else
case ( upperSize, lowerSize ) of
( Pawn, Queen ) ->
y - (getScale Pawn * 14 / 5)
_ ->
y - (getScale upperSize * 7 / 5)
pawnPyramidPathSuffix =
Pawn
|> getScale
|> pyramidPathSuffix
dronePyramidPathSuffix =
Drone
|> getScale
|> pyramidPathSuffix
queenPyramidPathSuffix =
Queen
|> getScale
|> pyramidPathSuffix
getScale : Size -> Float
getScale size =
case size of
Queen ->
(160 / 3)
Drone ->
40
Pawn ->
30
dotAttributes dotScale =
[ stroke "#EEEEEE"
, fillOpacity "0.0"
, pointerEvents "none"
, dotScale
/ 9
|> toString
|> rx
, dotScale
/ 7
|> toString
|> ry
]
middleY =
9 / 10
queen2ndX =
21 / 32
queen2ndY =
17 / 16
queen3rdX =
26 / 32
queen3rdY =
19 / 16
drone2ndX =
3 / 4
drone2ndY =
11 / 10
getDots : Size -> Float -> Float -> List (Svg Msg)
getDots size x y =
let
scale =
getScale size
pawnScale =
getScale Pawn
leftMiddleDot =
dot pawnScale
[ (x + (-scale / 2))
|> toString
|> cx
, (y + -(scale * middleY))
|> toString
|> cy
]
rightMiddleDot =
dot pawnScale
[ (x + (scale / 2))
|> toString
|> cx
, (y + -(scale * middleY))
|> toString
|> cy
]
in
case size of
Queen ->
[ leftMiddleDot
, dot (pawnScale * 31 / 32)
[ (x + (-scale * queen2ndX))
|> toString
|> cx
, (y - (scale * queen2ndY))
|> toString
|> cy
]
, dot (pawnScale * 30 / 32)
[ (x + (-scale * queen3rdX))
|> toString
|> cx
, (y - (scale * queen3rdY))
|> toString
|> cy
]
, rightMiddleDot
, dot (pawnScale * 33 / 32)
[ (x + (scale * (1 - queen2ndX)))
|> toString
|> cx
, (y - (scale * ((2 * middleY) - (queen2ndY))))
|> toString
|> cy
]
, dot (pawnScale * 34 / 32)
[ (x + (scale * (1 - queen3rdX)))
|> toString
|> cx
, (y - (scale * ((2 * middleY) - (queen3rdY))))
|> toString
|> cy
]
]
Drone ->
[ leftMiddleDot
, dot (pawnScale * 31 / 32)
[ (x + (-scale * drone2ndX))
|> toString
|> cx
, (y - (scale * drone2ndY))
|> toString
|> cy
]
, rightMiddleDot
, dot (pawnScale * 33 / 32)
[ (x + (scale * (1 - drone2ndX)))
|> toString
|> cx
, (y - (scale * ((2 * middleY) - drone2ndY)))
|> toString
|> cy
]
]
Pawn ->
[ leftMiddleDot
, rightMiddleDot
]
dot dotScale attributes =
ellipse
(attributes
++ dotAttributes dotScale
)
[]
pyramidPathSuffix : Float -> String
pyramidPathSuffix scale =
let
sclaeString =
toString scale
doubleSclaeString =
toString (2 * scale)
in
(" l " ++ sclaeString ++ " -" ++ sclaeString)
++ ("l -" ++ sclaeString ++ " -" ++ doubleSclaeString)
++ (" l -" ++ sclaeString ++ " " ++ doubleSclaeString)
++ (" l " ++ sclaeString ++ " " ++ sclaeString)
++ " Z "
++ (" l 0 " ++ toString (-pyramidHeightConstant * scale))
pyramidHeightConstant =
3
clickablePyramid : Msg -> Size -> Float -> Float -> Svg Msg
clickablePyramid msg =
pyramid [ onClick msg, stroke "grey" ]
highlightedPyramid =
pyramid [ stroke "white" ]
placedPyramid =
pyramid [ stroke "grey", pointerEvents "none" ]
pyramid : List (Attribute Msg) -> Size -> Float -> Float -> Svg Msg
pyramid attributes size x y =
let
dString =
"M "
++ toString x
++ " "
++ toString y
++ case size of
Queen ->
queenPyramidPathSuffix
Drone ->
dronePyramidPathSuffix
Pawn ->
pawnPyramidPathSuffix
in
[ Svg.path
([ d dString
, strokeWidth "2"
, fillOpacity "0.4"
]
++ attributes
)
[]
]
++ getDots size x y
|> g []
nullSvg =
Svg.polygon [] []