This repository has been archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adventure.elm
185 lines (127 loc) · 4.15 KB
/
Adventure.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
module Adventure exposing
( Model
, Msg(..)
, init
, subscriptions
, update
, view
)
-- IMPORTS
import Adventure.Character exposing (Character)
import Adventure.Screen exposing (Screen)
import Adventure.SvgView
import Adventure.WebGLView
import Adventure.Window exposing (Window)
import Browser exposing (Document)
import Browser.Events
import Html exposing (Html)
import Html.Attributes
import Json.Decode
import Set
import Util.Fps exposing (Time)
import Util.Keyboard exposing (Controls)
import Util.View
-- MODEL
type alias Model =
{ character : Character
, deltaTime : Time
, playerKeyPress : Controls
, screen : Screen
, window : Window
}
-- INIT
initialModel : Model
initialModel =
{ character = Adventure.Character.initialCharacter
, deltaTime = 0.0
, playerKeyPress = Util.Keyboard.initialKeys
, screen = Adventure.Screen.screen01
, window = Adventure.Window.initialWindow
}
initialCommand : Cmd Msg
initialCommand =
Cmd.none
init : () -> ( Model, Cmd Msg )
init _ =
( initialModel, initialCommand )
-- UPDATE
type Msg
= BrowserAdvancedAnimationFrame Time
| PlayerPressedKeyDown String
| PlayerReleasedKey String
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
BrowserAdvancedAnimationFrame deltaTime ->
( { model | character = updateCharacter model.playerKeyPress deltaTime model.character }
, Cmd.none
)
PlayerPressedKeyDown key ->
( updateKeyPress key model, Cmd.none )
PlayerReleasedKey _ ->
( { model | playerKeyPress = Set.empty }, Cmd.none )
updateCharacter : Controls -> Time -> Character -> Character
updateCharacter playerKeyPress deltaTime character =
if Util.Keyboard.playerPressedArrowUpKey playerKeyPress then
{ character | y = character.y - character.vy * deltaTime }
else if Util.Keyboard.playerPressedArrowDownKey playerKeyPress then
{ character | y = character.y + character.vy * deltaTime }
else if Util.Keyboard.playerPressedArrowLeftKey playerKeyPress then
{ character | x = character.x - character.vx * deltaTime }
else if Util.Keyboard.playerPressedArrowRightKey playerKeyPress then
{ character | x = character.x + character.vx * deltaTime }
else
character
updateKeyPress : String -> Model -> Model
updateKeyPress key model =
if Set.member key Util.Keyboard.validKeys then
{ model | playerKeyPress = Set.insert key model.playerKeyPress }
else
model
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.batch
[ browserAnimationSubscription
, keyDownSubscription
, keyUpSubscription
]
browserAnimationSubscription : Sub Msg
browserAnimationSubscription =
Browser.Events.onAnimationFrameDelta <| handleAnimationFrames
handleAnimationFrames : Time -> Msg
handleAnimationFrames milliseconds =
BrowserAdvancedAnimationFrame <| milliseconds / 1000
keyDownSubscription : Sub Msg
keyDownSubscription =
Browser.Events.onKeyDown <| Json.Decode.map PlayerPressedKeyDown <| Util.Keyboard.keyDecoder
keyUpSubscription : Sub Msg
keyUpSubscription =
Browser.Events.onKeyUp <| Json.Decode.map PlayerReleasedKey <| Util.Keyboard.keyDecoder
-- VIEW
view : (Msg -> msg) -> Model -> Document msg
view msg model =
{ title = "⚔️ Adventure"
, body = List.map (Html.map msg) [ viewMain model, Util.View.footer ]
}
viewMain : Model -> Html Msg
viewMain model =
Html.main_
[ Html.Attributes.class "h-full p-8"
, Html.Attributes.style "background-color" "lightgray"
]
[ viewHeader
, viewGame model
]
viewHeader : Html msg
viewHeader =
Html.header [ Html.Attributes.class "flex justify-center" ]
[ Html.h1 [ Html.Attributes.class "font-black text-5xl" ]
[ Html.text "Adventure" ]
]
viewGame : Model -> Html Msg
viewGame model =
Html.section [ Html.Attributes.class "flex flex-row my-4" ]
[ Adventure.SvgView.view model.window model.screen model.character
, Adventure.WebGLView.view model.window
]