-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-update.sml
307 lines (262 loc) · 8.56 KB
/
game-update.sml
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
structure GameUpdate =
struct
open GameTypes
(* Record update functions which just change the value of
* a couple of fields in a record. *)
local
fun invertBlockType (blockType: block_type) =
case blockType of
LIGHT => DARK
| DARK => LIGHT
in
fun invertBlock (block: block) : block =
let val {block = oldBlockType, vertexData} = block
in {block = invertBlockType oldBlockType, vertexData = vertexData}
end
end
fun ballWithXMove (ball: ball, newXMove) : ball =
let
val {player, xPos, yPos, xMove = _, yMove} = ball
in
{ xMove = newXMove
, player = player
, xPos = xPos
, yPos = yPos
, yMove = yMove
}
end
fun ballWithYMove (ball: ball, newYMove) =
let
val {player, xPos, yPos, xMove, yMove = _} = ball
in
{ yMove = newYMove
, player = player
, xPos = xPos
, yPos = yPos
, xMove = xMove
}
end
datatype collision_side =
BALL_ON_LEFT_SIDE
| BALL_ON_TOP_SIDE
| BALL_ON_RIGHT_SIDE
| BALL_ON_BOTTOM_SIDE
| NO_COLLIDE
datatype collision_result = RESULT of ball * block | NO_RESULT
local
fun help (min, pos, max) = pos >= min andalso pos <= max
in
fun isBetween (min, pos1, pos2, max) =
help (min, pos1, max) orelse help (min, pos2, max)
end
fun areOnDifferentSides (ball, block) =
case (#player ball, #block block) of
(DAY, DARK) => true
| (NIGHT, LIGHT) => true
| (_, _) => false
local
fun getCollisionSide (ball: ball, block: block, lineNum, rowIdx) =
let
(* Calculate rectangle area. *)
val ballStartX = #xPos ball
val ballEndX = ballStartX + 50
val ballStartY = #yPos ball
val ballEndY = ballStartY + 50
val blockStartX = (rowIdx - 10) * 50
val blockEndX = blockStartX + 50
val blockStartY = (~(lineNum - 10)) * 50
val blockEndY = blockStartY + 50
val isInHorizontalRange =
isBetween (blockStartX, ballStartX, ballEndX, blockEndX)
val isInVerticalRange =
isBetween (blockStartY, ballStartY, ballEndY, blockEndY)
in
if isInHorizontalRange andalso isInVerticalRange then
(* Collided; check which side next. *)
let
val (xSide, xAmt) =
if abs (ballStartX - blockEndX) < abs (ballEndX - blockStartX) then
(BALL_ON_LEFT_SIDE, abs (ballStartX - blockEndX))
else
(BALL_ON_RIGHT_SIDE, abs (ballEndX - blockStartX))
val (ySide, yAmt) =
if abs (ballStartY - blockEndY) < abs (ballEndY - blockStartY) then
(BALL_ON_TOP_SIDE, abs (ballStartY - blockEndY))
else
(BALL_ON_BOTTOM_SIDE, abs (ballEndY - blockStartY))
in
if xAmt < yAmt then xSide else ySide
end
else
NO_COLLIDE
end
fun hitLeftSideOfBlock (ball: ball, block: block) =
let
val newBlock = invertBlock block
val newBall = ballWithXMove (ball, 11)
in
RESULT (newBall, newBlock)
end
fun hitRightSideOfBLock (ball: ball, block: block) =
let
val newBlock = invertBlock block
val newBall = ballWithXMove (ball, ~11)
in
RESULT (newBall, newBlock)
end
fun hitTopSideOfBlock (ball: ball, block: block) =
let
val newBlock = invertBlock block
val newBall = ballWithYMove (ball, 11)
in
RESULT (newBall, newBlock)
end
fun hitBottomSideOfBLock (ball: ball, block: block) =
let
val newBlock = invertBlock block
val newBall = ballWithYMove (ball, ~11)
in
RESULT (newBall, newBlock)
end
in
fun updateOnCollision (ball: ball, block: block, lineNum, rowIdx) =
if areOnDifferentSides (ball, block) then
case getCollisionSide (ball, block, lineNum, rowIdx) of
BALL_ON_LEFT_SIDE => hitLeftSideOfBlock (ball, block)
| BALL_ON_TOP_SIDE => hitTopSideOfBlock (ball, block)
| BALL_ON_RIGHT_SIDE => hitRightSideOfBLock (ball, block)
| BALL_ON_BOTTOM_SIDE => hitBottomSideOfBLock (ball, block)
| NO_COLLIDE => NO_RESULT
else
NO_RESULT
end
local
fun helpFoldBlockRow
(pos, row, lineNum, dayBall, nightBall, newBlocks: block list) =
if pos < 0 then
(dayBall, nightBall, Vector.fromList newBlocks)
else
let
val block = Vector.sub (row, pos)
val dayBlockCollisionResult =
updateOnCollision (dayBall, block, lineNum, pos)
val (block, dayBall) =
case dayBlockCollisionResult of
NO_RESULT => (block, dayBall)
| RESULT (dayBall, block) => (block, dayBall)
val nightBlockCollisionResult =
updateOnCollision (nightBall, block, lineNum, pos)
val (block, nightBall) =
(case nightBlockCollisionResult of
NO_RESULT => (block, nightBall)
| RESULT (nightBall, block) => (block, nightBall))
val newBlocks = block :: newBlocks
in
helpFoldBlockRow
(pos - 1, row, lineNum, dayBall, nightBall, newBlocks)
end
fun helpFoldBlockLines
(pos, blocks, dayBall, nightBall, newBlocks: block vector list) :
ball * ball * block vector vector =
if pos < 0 then
(dayBall, nightBall, Vector.fromList newBlocks)
else
let
val row = Vector.sub (blocks, pos)
val (dayBall, nightBall, lineBlocks) = helpFoldBlockRow
(Vector.length row - 1, row, pos, dayBall, nightBall, [])
val newBlocks = lineBlocks :: newBlocks
in
helpFoldBlockLines (pos - 1, blocks, dayBall, nightBall, newBlocks)
end
in
fun updateBlocks (blocks: block vector vector, dayBall, nightBall) =
helpFoldBlockLines
(Vector.length blocks - 1, blocks, dayBall, nightBall, [])
end
fun checkLeftWallCollision (ball: ball) =
if #xPos ball <= ~500 then ballWithXMove (ball, 11) else ball
fun checkTopWallCollision (ball: ball) =
if #yPos ball >= 500 then ballWithYMove (ball, ~11) else ball
fun checkRightWallCollision (ball: ball) =
if #xPos ball + 50 >= 500 then ballWithXMove (ball, ~11) else ball
fun checkBottomWallCollision (ball: ball) =
if #yPos ball - 50 <= ~500 then ballWithYMove (ball, 11) else ball
fun checkWallCollisions (ball: ball) =
let
val ball = checkLeftWallCollision ball
val ball = checkRightWallCollision ball
val ball = checkTopWallCollision ball
in
checkBottomWallCollision ball
end
fun updateBall (ball: ball) : ball =
let
val {player, xPos, yPos, xMove, yMove} = ball
val xPos = xPos + xMove
val yPos = yPos + yMove
in
{player = player, xPos = xPos, yPos = yPos, xMove = xMove, yMove = yMove}
end
fun update (game: game_board) : game_board =
let
val
{ dayBall
, nightBall
, blocks
, dr
, dg
, db
, nr
, ng
, nb
, dayVertexBuffer
, dayVertexShader
, dayFragmentBuffer
, dayFragmentShader
, dayProgram
, nightVertexBuffer
, nightVertexShader
, nightFragmentBuffer
, nightFragmentShader
, nightProgram
, ballVertexBuffer
, ballVertexShader
, ballFragmentBuffer
, ballFragmentShader
, ballProgram
} = game
val dayBall = updateBall dayBall
val nightBall = updateBall nightBall
val (dayBall, nightBall, blocks) =
updateBlocks (blocks, dayBall, nightBall)
val dayBall = checkWallCollisions dayBall
val nightBall = checkWallCollisions nightBall
in
{ dayBall = dayBall
, nightBall = nightBall
, blocks = blocks
, dr = dr
, dg = dg
, db = db
, nr = nr
, ng = ng
, nb = nb
, dayVertexBuffer = dayVertexBuffer
, dayVertexShader = dayVertexShader
, dayFragmentBuffer = dayFragmentBuffer
, dayFragmentShader = dayFragmentShader
, dayProgram = dayProgram
, nightVertexBuffer = nightVertexBuffer
, nightVertexShader = nightVertexShader
, nightFragmentBuffer = nightFragmentBuffer
, nightFragmentShader = nightFragmentShader
, nightProgram = nightProgram
, ballVertexBuffer = ballVertexBuffer
, ballVertexShader = ballVertexShader
, ballFragmentBuffer = ballFragmentBuffer
, ballFragmentShader = ballFragmentShader
, ballProgram = ballProgram
}
end
end