-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.elm
320 lines (260 loc) · 8.11 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
module Main exposing (..)
import AnimationFrame exposing (..)
import Collage exposing (..)
import Element exposing (..)
import Html exposing (..)
import Html.Attributes exposing (style)
import Keyboard
import Time exposing (Time)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
type alias Window =
{ w : Int, h : Int }
type alias KeyPressed =
{ left : Bool
, right : Bool
}
type alias Ball =
{ x : Int
, y : Int
, w : Int
, h : Int
, init : Bool
, velX : Int
, velY : Int
}
type alias Player =
{ x : Int
, y : Int
, height : Int
, width : Int
, velocity : Int
}
type alias Game =
{ player : Player
, window : Window
, keyPressed : KeyPressed
, ball : Ball
}
type alias Model =
Game
init =
( { player =
{ x = 0
, y = round (-480 / 2) + 10
, height = 20
, width = 140
, velocity = 10
}
, window =
{ w = 640
, h = 480
}
, keyPressed =
{ left = False
, right = False
}
, ball =
{ x = 0
, y = 0
, w = 20
, h = 20
, init = True
, velX = 5
, velY = -5
}
}
, Cmd.none
)
-- UPDATE
type Msg
= KeyDown Keyboard.KeyCode
| KeyUp Keyboard.KeyCode
| Tick Time
update msg model =
let
updateLeftKey : KeyPressed -> Bool -> KeyPressed
updateLeftKey data val =
{ data | left = val }
updateRightKey : KeyPressed -> Bool -> KeyPressed
updateRightKey data val =
{ data | right = val }
updateSpaceKey data =
{ data | init = False }
in
case msg of
KeyDown code ->
if code == 37 then
( { model | keyPressed = updateLeftKey model.keyPressed True }, Cmd.none )
else if code == 39 then
( { model | keyPressed = updateRightKey model.keyPressed True }, Cmd.none )
else if code == 32 then
( { model | ball = updateSpaceKey model.ball }, Cmd.none )
else
( model, Cmd.none )
KeyUp code ->
if code == 37 then
( { model | keyPressed = updateLeftKey model.keyPressed False }, Cmd.none )
else if code == 39 then
( { model | keyPressed = updateRightKey model.keyPressed False }, Cmd.none )
else
( model, Cmd.none )
Tick newTime ->
( model
|> moveBall
|> ballCollisionWallX
|> ballCollisionWallY
|> playerCollision
|> movePlayer
, Cmd.none
)
movePlayer model =
let
leftWallX =
round ((toFloat model.window.w / 2) * (-1))
rightWallX =
round (toFloat model.window.w / 2) - model.player.width
updatePlayerX data val =
{ data | x = val }
in
if (model.keyPressed.left == True && (model.player.x - model.player.velocity) > leftWallX) then
{ model | player = updatePlayerX model.player (model.player.x - model.player.velocity) }
else if (model.keyPressed.left == True && (model.player.x - model.player.velocity) <= leftWallX) then
{ model | player = updatePlayerX model.player (round ((toFloat model.window.w / 2) * (-1))) }
else if (model.keyPressed.right == True && (model.player.x + model.player.velocity) <= rightWallX) then
{ model | player = updatePlayerX model.player (model.player.x + model.player.velocity) }
else
model
ballCollisionWallX model =
let
updateRec data posVal velVal =
{ data | x = posVal, velX = velVal }
rightWall =
round (toFloat model.window.w / 2) - model.ball.w
leftWall =
round (toFloat model.window.w / 2) * (-1)
ballHalfWidth =
round (toFloat model.ball.w / 2)
in
if model.ball.x + model.ball.velX <= leftWall then
{ model
| ball =
updateRec
model.ball
leftWall
(model.ball.velX * (-1))
}
else if model.ball.x + model.ball.velX >= rightWall then
{ model
| ball =
updateRec
model.ball
rightWall
(model.ball.velX * (-1))
}
else
model
ballCollisionWallY model =
let
updateRec data posVal velVal =
{ data | y = posVal, velY = velVal }
topWall =
(round (toFloat model.window.h / 2) - round (toFloat model.ball.h))
bottomWall =
((round (toFloat model.window.h / 2)) * (-1) - round (toFloat model.ball.h / 2))
resetBall data =
{ data | init = True }
in
if (model.ball.y - model.ball.velY >= topWall) then
{ model
| ball =
updateRec
model.ball
topWall
(model.ball.velY * (-1))
}
else if (model.ball.y - model.ball.velY <= bottomWall) then
{ model | ball = resetBall model.ball }
else
model
playerCollision model =
let
updateVelY data dirVal =
{ data | velY = dirVal }
ballX =
model.ball.x
playerLeftSide =
model.player.x
playerRightSide =
(model.player.x + model.player.width)
playerTopSide =
model.player.y
playerBottomSide =
model.player.y + model.player.height
in
if
((model.ball.x <= model.player.x + model.player.width)
&& (model.ball.x + model.ball.w >= model.player.x)
&& (model.ball.y <= model.player.y + model.player.height)
&& (model.ball.h + model.ball.y >= model.player.y)
)
then
{ model | ball = updateVelY model.ball (model.ball.velY * (-1)) }
else
model
moveBall model =
let
updateBallPos data valX valY =
{ data | x = valX, y = valY }
in
if (model.ball.init == True) then
{ model
| ball =
updateBallPos
model.ball
(model.player.x + round (toFloat model.player.width / 2) - round (toFloat model.ball.w / 2))
(model.player.y + model.player.height + 1)
}
else
{ model
| ball =
updateBallPos
model.ball
(model.ball.x + model.ball.velX)
(model.ball.y - model.ball.velY)
}
view model =
div
[ style
[ ( "height", toString model.window.h ++ "px" )
, ( "width", toString model.window.w ++ "px" )
, ( "background", "url(assets/bg_stone.png)" )
]
]
[ toHtml
(collage model.window.w
model.window.h
[ drawObj model.player.x model.player.y model.player.width model.player.height "assets/spr_player.png"
, drawObj model.ball.x model.ball.y model.ball.w model.ball.h "assets/spr_ball.png"
]
)
]
drawObj : Int -> Int -> Int -> Int -> String -> Form
drawObj posX posY width height img =
move ( toFloat (posX + round (toFloat width / 2)), toFloat (posY + round (toFloat height / 2)) )
(toForm
(image (width) (height) img)
)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Keyboard.downs KeyDown
, Keyboard.ups KeyUp
, times Tick
]