-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update.elm
119 lines (91 loc) · 3.07 KB
/
Update.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
module Update exposing (..)
import Model exposing (..)
import Time exposing (..)
import List exposing (..)
subscriptions : Model -> Sub Msg
subscriptions model =
case model of
Game gameModel ->
every (millisecond * 30) Tick
_ ->
Sub.none
update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
case model of
Title ->
updateTitle msg
Game gameModel ->
updateGame msg gameModel
updateTitle : Msg -> ( Model, Cmd msg )
updateTitle msg =
case msg of
TitleBegin ->
( initGameModel, Cmd.none )
_ ->
( Title, Cmd.none )
updateGame : Msg -> GameModel -> ( Model, Cmd msg )
updateGame msg game =
case msg of
-- Main game loop
Tick time ->
let
updatedInputState =
updateInputState game.inputState game.inputFrame
updatedGameState =
updateGameState game.gameState updatedInputState
updatedDrawState =
updateDrawState game.drawState game.gameState game.inputState
in
( Game
{ game
| inputFrame = newInputFrame
, inputState = updatedInputState
, gameState = updatedGameState
, drawState = updatedDrawState
}
, Cmd.none
)
-- Input messages
Click interactableID ->
( Game { game | inputFrame = interact game.inputFrame interactableID }, Cmd.none )
Space ->
( Game { game | inputFrame = pause game.inputFrame }, Cmd.none )
_ ->
( Game game, Cmd.none )
updateInputState : InputState -> InputFrame -> InputState
updateInputState inputState inputFrame =
{ inputState
| interactions = inputFrame.interactions
, pause = inputFrame.pause
, timer = inputState.timer + 1
}
updateGameState : GameState -> InputState -> GameState
updateGameState gameState inputState =
let
updatedPauseState =
if inputState.pause then
(not gameState.paused)
else
gameState.paused
updatedInteractables =
map
(\i ->
if member i.id inputState.interactions then
{ i | isHit = True }
else
i
)
gameState.interactables
in
{ gameState | interactables = updatedInteractables, paused = updatedPauseState }
updateDrawState : DrawState -> GameState -> InputState -> DrawState
updateDrawState drawState gameState inputState =
{ paused = gameState.paused
, interactables = gameState.interactables
}
interact : InputFrame -> InteractableID -> InputFrame
interact inputFrame interactableID =
{ inputFrame | interactions = interactableID :: inputFrame.interactions }
pause : InputFrame -> InputFrame
pause inputFrame =
{ inputFrame | pause = True }