-
Notifications
You must be signed in to change notification settings - Fork 0
/
Msg.elm
71 lines (65 loc) · 1.61 KB
/
Msg.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
module Msg exposing (Msg (..), update)
import Array exposing (Array)
import Model exposing (Model, Lifecycle (..), Universe, Body)
import Physics exposing (advanceUniverse)
import Utils exposing (repeatFn)
type Msg
= NoOp
| ResizeWindow Int Int
| MakeStars ( List ( Float, Float ) )
| Genesis ( List Body )
| Apocalypse
| Tick Float
| TogglePhysics
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
( model, Cmd.none )
ResizeWindow x y ->
( { model | window =
{ width = x
, height = y
}
}
, Cmd.none
)
MakeStars stars ->
( { model | stars = stars }
, Cmd.none
)
Genesis bodies ->
( { model | lifecycle = Run
{ bodies = Array.fromList bodies
, time = 0
, showPhysics = False
}
}
, Cmd.none
)
Apocalypse ->
( { model | lifecycle = Welcome }
, Cmd.none )
Tick δt ->
case model.lifecycle of
Welcome ->
( model, Cmd.none )
Run universe ->
let
-- use 1 frame (~16ms) as our Time Unit
δms = round ( δt / 16 )
newUniverse = repeatFn advanceUniverse δms universe
in
( { model | lifecycle = Run newUniverse }
, Cmd.none
)
TogglePhysics ->
case model.lifecycle of
Run universe ->
( { model | lifecycle = Run
{ universe | showPhysics = not universe.showPhysics }
}
, Cmd.none
)
_ ->
( model, Cmd.none )