-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
494 lines (400 loc) · 13.7 KB
/
Main.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
module Main exposing (..)
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import String
import Random
import Time exposing (Time)
import AnimationFrame
import Direction
import Direction exposing (Direction(..))
import Location exposing (Location)
import Map
import Map exposing (Map, GameState(..))
import Actor exposing (Actor, ActorType(..))
-- CONSTANTS
animationTime : number
animationTime =
500
mapSize : Int
mapSize =
9
-- MODEL
type alias Model =
{ map : Map
, randomSeed : Int
, timeSinceMove : Time
, enableInput : Bool
, gameState : GameState
, message : Maybe String
, whirlpoolAngle : Int
}
newLevelCmd : Cmd Msg
newLevelCmd =
Random.generate NewLevel (Random.int Random.minInt Random.maxInt)
newSeedCmd : Cmd Msg
newSeedCmd =
Random.generate NewSeed (Random.int Random.minInt Random.maxInt)
init : ( Model, Cmd Msg )
init =
( Model (Map.initMap mapSize 1 0) 0 0 True Playing Nothing 0
--(Just "Level 1!")
, newLevelCmd
)
-- UPDATE
type Msg
= TileClicked Int Int
| ActorClicked Actor
| NewLevel Int
| NewSeed Int
| HandleTimeDiff Time
| None
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
TileClicked x y ->
let
playerMovedMap =
Map.movePlayer x y model.map
playerUpdated =
(Map.getPlayer model.map /= Map.getPlayer playerMovedMap)
( updatedMap, enableInput ) =
if playerUpdated then
( Map.movePirates playerMovedMap, False )
else
( model.map, True )
updatedMap2 =
if model.enableInput then
updatedMap
else
model.map
gameState =
Map.getGameState updatedMap2
in
( { model
| map = updatedMap2
, gameState = gameState
, enableInput = enableInput
}
, Cmd.none
)
ActorClicked actor ->
let
( updatedMap, enableInput ) =
case actor.subtype of
PLAYER ->
( Map.fireCannons actor model.map, False )
WHIRLPOOL ->
( Map.movePlayer actor.location.x actor.location.y model.map, False )
_ ->
( model.map, True )
updatedMap2 =
if model.enableInput then
updatedMap
else
model.map
gameState =
Map.getGameState updatedMap2
in
( { model
| map = updatedMap2
, gameState = gameState
, enableInput = enableInput
}
, Cmd.none
)
NewLevel seedNum ->
( { model
| map = Map.initMap mapSize model.map.level seedNum
, randomSeed = seedNum
}
, Cmd.none
)
NewSeed seedNum ->
( { model | randomSeed = seedNum }
, Cmd.none
)
HandleTimeDiff time ->
let
updatedTime =
if model.timeSinceMove + time > animationTime then
0
else
model.timeSinceMove + time
map =
model.map
level =
map.level
( updatedModel, updatedCommand ) =
if updatedTime == 0 then
case model.gameState of
Playing ->
( { model | enableInput = True, message = Nothing }, Cmd.none )
GameOver ->
( { model | gameState = GameOver2 }, Cmd.none )
GameOver2 ->
( { model
| message = (Just "Game Over!")
, map = { map | level = 1 }
, gameState = Loading
}
, newLevelCmd
)
NextLevel ->
( { model | gameState = NextLevel2 }, Cmd.none )
NextLevel2 ->
( { model
| message = (Just ("Level " ++ (toString (level + 1)) ++ "!"))
, map = { map | level = level + 1 }
, gameState = Loading
}
, newLevelCmd
)
WhirlpoolSpin ->
( { model | gameState = WhirlpoolLand }
, newSeedCmd
)
WhirlpoolLand ->
( { model
| map = (Map.whirlpoolPlayer map model.randomSeed)
, gameState = Loading
}
, Cmd.none
)
FireCannons ->
( { model
| map = (Map.advanceCannons map)
, gameState = FireCannons2
}
, Cmd.none
)
FireCannons2 ->
( { model
| map = (Map.advanceCannons map)
, gameState = FireCannons3
}
, Cmd.none
)
FireCannons3 ->
( { model
| map = (Map.advanceCannons map)
, gameState = FireCannons4
}
, Cmd.none
)
FireCannons4 ->
( { model
| map = (Map.removeCannons map)
, gameState = MovePirates
}
, Cmd.none
)
MovePirates ->
( { model
| map = (Map.movePirates model.map)
, gameState = Map.getGameState (Map.movePirates model.map)
}
, Cmd.none
)
Loading ->
( { model | gameState = Playing }, Cmd.none )
else
( model, Cmd.none )
in
( { updatedModel
| timeSinceMove = updatedTime
, whirlpoolAngle = model.whirlpoolAngle + 5
}
, updatedCommand
)
None ->
( model, Cmd.none )
-- VIEW
viewMapTile : Int -> Int -> Html Msg
viewMapTile x y =
div
[ class "map-tile"
, attribute "x" (toString x)
, attribute "y" (toString y)
, onClick (TileClicked x y)
, style
[ ( "flex", "1" )
, ( "background-color", "#069" )
]
]
[]
viewMapTileRow : List Location -> Html Msg
viewMapTileRow row =
div
[ class "map-row"
, style
[ ( "display", "flex" )
, ( "flex", "1" )
]
]
(List.map
(\loc -> viewMapTile loc.x loc.y)
row
)
viewMapTiles : List (List Location) -> Html Msg
viewMapTiles listOfRows =
div
[ class "map-tiles"
, style
[ ( "width", "100vmin" )
, ( "height", "100vmin" )
, ( "marginLeft", "calc((100vw - 100vmin)/2)" )
, ( "marginTop", "calc((100vh - 100vmin)/2)" )
, ( "display", "flex" )
, ( "flex-direction", "column" )
, ( "background-color", "#069" )
]
]
(List.map
(\row -> viewMapTileRow row)
listOfRows
)
getOffset : Int -> Int -> String
getOffset size pos =
"calc( 100vmin * " ++ toString pos ++ " / " ++ toString size ++ " )"
getTileSize : Int -> String
getTileSize size =
"calc( 100vmin / " ++ toString size ++ " )"
viewActor : Actor -> Int -> Int -> Html Msg
viewActor actor mapSize whirlpoolAngle =
let
onWhirlpool =
(Actor.onWhirlpool actor mapSize) && (actor.subtype == PLAYER)
timeLabel =
(toString (animationTime / 1000)) ++ "s"
defaultTransition =
"top "
++ timeLabel
++ ", left "
++ timeLabel
whirlpoolTransition =
defaultTransition
++ ", transform "
++ timeLabel
actorAngle =
(Direction.getAngle actor.direction)
defaultTransform =
"rotate(" ++ (toString actorAngle) ++ "deg)"
whirlpoolTransform =
"rotate(" ++ (toString whirlpoolAngle) ++ "deg)"
spinningTransform =
"rotate(" ++ (toString (actorAngle + 360)) ++ "deg)"
transition =
if onWhirlpool then
whirlpoolTransition
else
defaultTransition
transform =
if onWhirlpool then
spinningTransform
else if actor.subtype == WHIRLPOOL then
whirlpoolTransform
else
defaultTransform
in
(img
[ src ("img/" ++ (String.toLower (toString actor.subtype)) ++ ".svg")
, style
[ ( "width", getTileSize mapSize )
, ( "height", getTileSize mapSize )
, ( "position", "absolute" )
, ( "top", getOffset mapSize actor.location.y )
, ( "left", getOffset mapSize actor.location.x )
, ( "transition", transition )
, ( "transform", transform )
]
, onClick (ActorClicked actor)
]
[]
)
viewActors : List Actor -> Int -> Int -> Int -> List (Html Msg)
viewActors actors mapSize whirlpoolAngle randomInt =
if randomInt /= 0 then
List.map
(\actor -> (viewActor actor mapSize whirlpoolAngle))
actors
else
[]
view : Model -> Html Msg
view model =
let
player =
Map.getPlayer model.map
mapSize =
model.map.size
actors =
Map.getActorsFromRecord model.map.actors
in
div
[ class "game"
, attribute "seed" (toString model.randomSeed)
, style
[ ( "height", "100%" )
, ( "overflow", "hidden" )
, ( "cursor", "pointer" )
, ( "background-color", "black" )
]
]
[ viewMapTiles model.map.tiles
, div
[ class "actors"
, style
[ ( "position", "absolute" )
, ( "top", "0" )
, ( "left", "0" )
, ( "marginLeft", "calc((100vw - 100vmin)/2)" )
, ( "marginTop", "calc((100vh - 100vmin)/2)" )
, ( "width", "100vmin" )
, ( "height", "100vmin" )
, ( "visibility", "hidden" )
]
]
[ div
[ style [ ( "visibility", "visible" ) ] ]
(viewActors actors mapSize model.whirlpoolAngle model.randomSeed)
]
, h3
[ style
[ ( "position", "absolute" )
, ( "top", "0" )
, ( "left", "0" )
, ( "margin", "0" )
, ( "cursor", "default" )
, ( "width", "100%" )
, ( "height", "100%" )
, ( "justify-content", "center" )
, ( "align-items", "center" )
, ( "background-color", "#069" )
, ( "color", "white" )
, ( "font-family", "Arial" )
, ( "display"
, (case model.message of
Just str ->
"flex"
Nothing ->
"none"
)
)
]
]
[ text (Maybe.withDefault "" model.message) ]
]
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
AnimationFrame.diffs HandleTimeDiff
-- MAIN
main : Program Never
main =
App.program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}