-
Notifications
You must be signed in to change notification settings - Fork 14
/
Mario.elm
804 lines (666 loc) · 19.8 KB
/
Mario.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
module Mario exposing (main)
{-|
# Run around as Mario!
This example is primarily here to show how the sprite system works.
Essentially we're going to be animating like you would with a flip book!
But we're also going to be doing some physics calculations directly ourselves, so this example shows how you'd use `elm-animator` side-by-side with other manual animations if you need to.
(1) **Sprite Sheet** - A sprite sheet is single image file with a bunch of images at different positions in the file. [Here's the one we're using](https://github.com/mdgriffith/elm-animator/blob/master/examples/images/mario-sprites.png)
To animate this, we need to pick out our subimages from our image file by knowing their bounding boxes.
(2) **Animating the flipbook** - Here's the new place where we're animating Mario'd state.
-}
import Animator
import Browser
import Browser.Dom
import Browser.Events
import Html exposing (Html)
import Html.Attributes as Attr
import Json.Decode as Decode
import Task
import Time
{--}
type alias Model =
{ x : Float
, y : Float
, vx : Float
, vy : Float
, window : Window
, gamepad : GamePad
, mario : Animator.Timeline Mario
}
type alias GamePad =
{ left : Pressed
, right : Pressed
, jump : Pressed
, run : Pressed
, duck : Pressed
}
type Pressed
= NotPressed
| StartPressed
| HeldFor Milliseconds
type alias Milliseconds =
Float
type alias Keys =
{ x : Int
, y : Int
}
type Arrow
= ArrowDown
| ArrowUp
| ArrowLeft
| ArrowRight
type alias Window =
{ width : Int
, height : Int
}
type Mario
= Mario Action Direction
type Action
= Running
| Walking
| Standing
| Ducking
| Jumping
type Direction
= Left
| Right
type Msg
= Tick Time.Posix
| Frame Float
| Pressed Button
| Released Button
| WindowSize Int Int
type Button
= GoLeft
| GoRight
| Duck
| Jump
| Run
main =
Browser.document
{ init =
\() ->
( init
, Browser.Dom.getViewport
|> Task.attempt
(\viewportResult ->
case viewportResult of
Ok viewport ->
WindowSize
(round viewport.scene.width)
(round viewport.scene.height)
Err err ->
WindowSize
(round 800)
(round 600)
)
)
, view = view
, update = update
, subscriptions = subscriptions
}
init : Model
init =
{ x = 0
, y = 0
, vx = 0
, vy = 0
, window = { width = 800, height = 500 }
, gamepad =
{ left = NotPressed
, right = NotPressed
, jump = NotPressed
, run = NotPressed
, duck = NotPressed
}
, mario = Animator.init (Mario Standing Right)
}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg mario =
case msg of
Tick newTime ->
( mario
|> Animator.update newTime animator
, Cmd.none
)
Frame dt ->
( mario
|> holdButtons dt
|> gravity (dt / 10)
|> jump mario.gamepad
|> walk mario.gamepad
|> physics (dt / 10)
|> updateSprites
, Cmd.none
)
Pressed button ->
( { mario
| gamepad =
applyButtonToGamepad button True mario.gamepad
}
, Cmd.none
)
Released button ->
( { mario
| gamepad =
applyButtonToGamepad button False mario.gamepad
}
, Cmd.none
)
WindowSize width height ->
( { mario
| window =
{ width = width
, height = height
}
}
, Cmd.none
)
{-| -}
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ animator
|> Animator.toSubscription Tick model
, Browser.Events.onResize WindowSize
, Browser.Events.onKeyDown (Decode.map Pressed decodeButton)
, Browser.Events.onKeyUp (Decode.map Released decodeButton)
, Browser.Events.onAnimationFrameDelta Frame
]
animator : Animator.Animator Model
animator =
Animator.animator
-- we tell the animator how to get the checked timeline using .checked
-- and we tell the animator how to update that timeline with updateChecked
|> Animator.watching .mario (\mario m -> { m | mario = mario })
decodeButton : Decode.Decoder Button
decodeButton =
Decode.andThen toButton
(Decode.field "key" Decode.string)
toButton : String -> Decode.Decoder Button
toButton string =
case string of
"ArrowLeft" ->
Decode.succeed GoLeft
"ArrowRight" ->
Decode.succeed GoRight
" " ->
Decode.succeed Jump
"ArrowDown" ->
Decode.succeed Duck
"Shift" ->
Decode.succeed Run
_ ->
Decode.fail "Skip"
holdButtons : Float -> Model -> Model
holdButtons dt model =
let
gamepad =
model.gamepad
in
{ model
| gamepad = holdButtonsOnGamepad dt gamepad
}
holdButtonsOnGamepad dt gamepad =
{ left = hold dt gamepad.left
, right = hold dt gamepad.right
, jump = hold dt gamepad.jump
, run = hold dt gamepad.run
, duck = hold dt gamepad.duck
}
hold dt pressed =
case pressed of
NotPressed ->
NotPressed
StartPressed ->
HeldFor dt
HeldFor existingDt ->
HeldFor (existingDt + dt)
applyButtonToGamepad : Button -> Bool -> GamePad -> GamePad
applyButtonToGamepad button pressed gamepad =
case button of
GoLeft ->
{ gamepad
| left =
if pressed then
StartPressed
else
NotPressed
}
GoRight ->
{ gamepad
| right =
if pressed then
StartPressed
else
NotPressed
}
Duck ->
{ gamepad
| duck =
if pressed then
StartPressed
else
NotPressed
}
Jump ->
{ gamepad
| jump =
if pressed then
StartPressed
else
NotPressed
}
Run ->
{ gamepad
| run =
if pressed then
StartPressed
else
NotPressed
}
view : Model -> Browser.Document Msg
view model =
{ title = "Mario - Elm Animator"
, body =
[ stylesheet
, Html.div
[ Attr.style "position" "fixed"
, Attr.style "left" "0"
, Attr.style "top" "0"
, Attr.style "width" (String.fromInt model.window.width ++ "px")
, Attr.style "height" (String.fromInt model.window.height ++ "px")
]
[ Html.div
[ Attr.style "position" "absolute"
, Attr.style "top" "80px"
, Attr.style "left" "80px"
, Attr.style "user-select" "none"
, Attr.style "font-family" "'Roboto', sans-serif"
]
[ Html.h1 [] [ Html.text "Mario" ]
, Html.div [] [ Html.text "Arrows to move, shift to run, space to jump!" ]
]
, Html.div
[ Attr.class "positioner"
, Attr.style "position" "absolute"
, Attr.style "top" (String.fromFloat ((toFloat model.window.height / 2) - model.y) ++ "px")
, Attr.style "left" (String.fromFloat model.x ++ "px")
]
-- (2) - Animating Mario's state with sprites
-- We're watching te model.mario timeline, which has both a direction and an action that mario is currently doing.
--
[ viewSprite
(Animator.step model.mario <|
\(Mario action direction) ->
let
-- this is where we decide to show the left or the right sprite.
--
frame mySprite =
case direction of
Left ->
Animator.frame mySprite
Right ->
Animator.frame { mySprite | flipX = True }
in
case action of
-- for these first three states, we only have a single frame we care about.
Standing ->
frame sprite.tail.stand
Ducking ->
frame sprite.tail.duck
Jumping ->
frame sprite.tail.jump
Walking ->
-- when we're in a `Walking` state, we want to cycle through 3 frames.
-- And we can also specify our frames per secton
Animator.framesWith
-- `transition` are the frames we'd want to take when transitioning to this state.
{ transition = frame sprite.tail.stand
-- `resting` is what we want to do while we're in this state.
, resting =
Animator.cycle
(Animator.fps 15)
[ frame sprite.tail.step1
, frame sprite.tail.step2
, frame sprite.tail.stand
]
}
Running ->
-- In order to make mario go faster, we're upping the fps
-- and we're also changing the frames so that he puts his arms out.
Animator.framesWith
{ transition = frame sprite.tail.standArms
, resting =
Animator.cycle
(Animator.fps 30)
[ frame sprite.tail.runStep1
, frame sprite.tail.runStep2
, frame sprite.tail.standArms
]
}
)
]
]
]
}
viewSprite : Box -> Html msg
viewSprite box =
Html.div []
[ Html.div
[ Attr.style "position" "absolute"
, Attr.style "top" (String.fromInt box.adjustY ++ "px")
, Attr.style "left" (String.fromInt box.adjustX ++ "px")
, Attr.style "width" (String.fromInt box.width ++ "px")
, Attr.style "height" (String.fromInt box.height ++ "px")
, Attr.style "background-image" "url('http://mdgriffith.github.io/elm-animator/images/mario-sprites.png')"
, Attr.style "background-repeat" "no-repeat"
, Attr.style "transform-origin" "30% 50%"
, Attr.style "transform"
(if box.flipX then
"scaleX(-1) scale(2)"
else
"scaleX(1) scale(2)"
)
, Attr.style "background-position"
("-"
++ (String.fromInt box.x ++ "px -")
++ (String.fromInt box.y ++ "px")
)
-- we need to tell the browser to render our image and leave the pixels pixelated.
, Attr.class "pixel-art"
]
[]
]
isHeld : Pressed -> Bool
isHeld pressed =
case pressed of
NotPressed ->
False
StartPressed ->
True
HeldFor _ ->
True
walk : GamePad -> Model -> Model
walk pad mario =
let
run yes x =
if yes then
x * 2.0
else
x
newVx =
if isHeld pad.left && isHeld pad.right then
0
else if isHeld pad.left then
run (isHeld pad.run) -1.8
else if isHeld pad.right then
run (isHeld pad.run) 1.8
else
0
in
{ mario
| vx = newVx
}
jump : GamePad -> Model -> Model
jump pad mario =
if pad.jump == StartPressed && mario.vy == 0 then
{ mario | vy = 6.0 }
else
mario
gravity : Float -> Model -> Model
gravity dt mario =
{ mario
| vy =
if mario.y > 0 then
mario.vy - dt / 4
else
0
}
physics : Float -> Model -> Model
physics dt mario =
{ mario
| x =
(mario.x + dt * mario.vx)
|> min (toFloat mario.window.width - 40)
|> max 0
, y = max 0 (mario.y + dt * mario.vy)
}
updateSprites : Model -> Model
updateSprites model =
let
current =
Animator.current model.mario
direction =
if model.vx > 0 then
Right
else if model.vx < 0 then
Left
else
case current of
Mario _ currentDirection ->
currentDirection
action =
if model.y /= 0 then
Jumping
else if model.vx /= 0 then
if abs model.vx > 2 then
Running
else
Walking
else if isHeld model.gamepad.duck then
Ducking
else
Standing
newMario =
Mario action direction
in
if current /= newMario then
{ model
| mario =
model.mario
|> Animator.go Animator.immediately newMario
}
else
model
{- (1) - Sprite Sheet
x, y -> the coordinates of the image on the sprite sheet
width, height -> the size of the image I want
adjustX, adjustY -> adjustX and adjustY move the position of the rendered image so that we can line it up with the previous frames.
flipX, flipY -> The sprite sheet only shows mario looking in one direction. Though we can flip that image if we need to!
-}
type alias Box =
{ x : Int
, y : Int
, width : Int
, height : Int
, adjustX : Int
, adjustY : Int
, flipX : Bool
, flipY : Bool
}
sprite =
{ tail =
{ stand =
{ x = 0
, y = 240
, width = 27
, height = 30
, adjustX = 4
, adjustY = 0
, flipX = False
, flipY = False
}
, step1 =
{ x = 30
, y = 240
, width = 27
, height = 30
, adjustX = 3
, adjustY = 0
, flipX = False
, flipY = False
}
, step2 =
{ x = 60
, y = 240
, width = 27
, height = 30
, adjustX = 4
, adjustY = 0
, flipX = False
, flipY = False
}
, jump =
{ x = 90
, y = 240
, width = 27
, height = 30
, adjustX = 4
, adjustY = 0
, flipX = False
, flipY = False
}
, duck =
{ x = 120
, y = 235
, width = 27
, height = 30
, adjustX = 5
, adjustY = 0
, flipX = False
, flipY = False
}
, pivot =
{ x = 150
, y = 240
, width = 27
, height = 30
, adjustX = 0
, adjustY = 0
, flipX = False
, flipY = False
}
, kick =
{ x = 180
, y = 240
, width = 27
, height = 30
, adjustX = -1
, adjustY = 0
, flipX = False
, flipY = False
}
, bum =
{ x = 208
, y = 239
, width = 20
, height = 30
, adjustX = 3
, adjustY = 0
, flipX = False
, flipY = False
}
, standArms =
{ x = 0
, y = 280
, width = 25
, height = 30
, adjustX = 4
, adjustY = 0
, flipX = False
, flipY = False
}
, runStep1 =
{ x = 25
, y = 280
, width = 27
, height = 30
, adjustX = 3
, adjustY = 0
, flipX = False
, flipY = False
}
, runStep2 =
{ x = 52
, y = 280
, width = 25
, height = 30
, adjustX = 4
, adjustY = 0
, flipX = False
, flipY = False
}
, runJump1 =
{ x = 329
, y = 280
, width = 27
, height = 30
, adjustX = 3
, adjustY = 0
, flipX = False
, flipY = False
}
, runJump2 =
{ x = 359
, y = 280
, width = 27
, height = 30
, adjustX = 3
, adjustY = 0
, flipX = False
, flipY = False
}
, runJump3 =
{ x = 389
, y = 280
, width = 27
, height = 30
, adjustX = 3
, adjustY = 0
, flipX = False
, flipY = False
}
, fall1 =
{ x = 268
, y = 280
, width = 27
, height = 30
, adjustX = 3
, adjustY = 0
, flipX = False
, flipY = False
}
, fall2 =
{ x = 300
, y = 280
, width = 27
, height = 30
, adjustX = 3
, adjustY = 0
, flipX = False
, flipY = False
}
}
}
stylesheet : Html msg
stylesheet =
Html.node "style"
[]
[ Html.text """@import url('https://fonts.googleapis.com/css?family=Roboto&display=swap');
body, html {
margin: 0;
padding:0;
border:0;
display:block;
position: relative;
width: 100%;
height: 100%;
}
.pixel-art {
image-rendering: pixelated;
image-rendering: -moz-crisp-edges;
image-rendering: crisp-edges;
}
"""
]