-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmot-replayer.html
339 lines (286 loc) · 12 KB
/
mot-replayer.html
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!DOCTYPE html>
<head>
<title>balls-n-walls replay</title>
</head>
<body>
<div id="replayDiv"></div>
</body>
<script>
var par = {
gameWidth: 650,
gameHeight: 650,
levelDuration: 1500
}
var h = 5, w = 10;
document.getElementById("replayDiv").innerHTML = "<!--main canvas where game happens:-->" + "test" +
"<canvas id='mainCanvas' height='" + h + "' width = '" + w + "'></canvas>"
+
"<!--overlay canvas that doesn't need to be refreshed constantly:-->" +
//it may be good to not hard-code the top and left value but rather use variables...this will be decided later when we do more styling
"<canvas id='overlay' style='position:absolute; left: 0; top: 0; z-index:3' height='" + h + "' width = '" + w + "'></canvas>"
+
"<!--occluder overlay canvas that doesn't need to be refreshed constantly:-->" +
//it may be good to not hard-code the top and left value but rather use variables...this will be decided later when we do more styling
"<canvas id='occluderCanvas' style='position:absolute; left: 0; top: 0; z-index:2' height='" + h + "' width = '" + w + "'></canvas>" +
"<!--selection canvas for ball selection in defusal mode:-->" +
//it may be good to not hard-code the top and left value but rather use variables...this will be decided later when we do more styling
"<canvas id='selectionCanvas' style='position:absolute; left: 0; top: 0; z-index:1' height='" + h + "' width = '" + w + "'></canvas>"
/*function makeAFakeModelObjectFromGivenReplayFrame(replayFrame){
return {
balls: replayFrame.balls,
getBalls: function(){return this.balls},
occluderRects: par.occluderRects,
getOccluderRects: function(){return this.occluderRects},
userObstacles: replayFrame.userObstacles,
wallThickness: par.wallThickness
}
}*/
var replayView = new view()
var replayTimer = new timer()
function update(currentSavedFrameIndex){
console.log([currentSavedFrameIndex, currentTime])
window.requestAnimationFrame(function(){
replayView.update(makeAFakeModelObjectFromGivenReplayFrame(savedModel[currentSavedFrameIndex]))
window.requestAnimationFrame(update, currentSavedFrameIndex+1)
})
currentSavedFrameIndex++
}
//does visualization of the fake models (and other items to be displayed) in the canvases
function view(mod) {
this.model = mod
//get canvas, context
this.can = document.getElementById("mainCanvas")
this.ctx = this.can.getContext("2d")
this.pointsToShow = new Array()
this.showPoint = function(pt) {this.pointsToShow.push(pt)}
this.initialized = false
/* uncomment for displaying an image for a ball
var imgElement = new Image();
imgElement.src = "ball.png";
ctx.drawImage(imgElement, ball.getX(), ball.getY());
*
ctx.closePath();
}*/
//not using commented parts anymore:
//for(var j = 0, numWalls = 4; j < numWalls; j++){
//var wall = model.walls[j];
//display the walls:
var color = "green"
ctx.beginPath();
ctx.fillStyle = color
//ctx.rect(wall.getX(), wall.getY(), wall.getW(), wall.getL())
var wallThickness = mod.wallThickness
ctx.rect(0,0,wallThickness, h)
ctx.rect(0,0,w,wallThickness)
ctx.rect(w-wallThickness,0,wallThickness,h)
ctx.rect(0,h-wallThickness,w,wallThickness)
ctx.fill();
ctx.closePath();
//}
//display the points/pixels in the user-defined wall as circles and draw line segments between them (except for before the first and after the last pixel)
for(var j = 0, obs = mod.userObstacles, numObs = obs.length; j<numObs; j++){
var ob = obs[j]
for(var o = 0, pix = ob.pixels, numPix = pix.length, rad = ob.getRadius(); o<numPix; o++){
//get x and y values of the pixel
var x = pix[o][0]
var y = pix[o][1]
var color = "maroon"
//draw circles of radius rad around each pixel
ctx.beginPath()
ctx.fillStyle = color
ctx.arc(x, y, rad, 0, 2*Math.PI)
ctx.fill()
//then draw a line from the pixel to the next pixel (if the next pixel exists)
if(o < numPix-1){
ctx.fillStyle = color
ctx.moveTo(pix[o][0], pix[o][1])
ctx.lineTo(pix[o+1][0], pix[o+1][1])
ctx.stroke();
}
ctx.closePath()
}
}
//show the points:
for(var j = 0, pix = this.pointsToShow, numPix = pix.length; j<numPix; j++){
var x = pix[j][0]
var y = pix[j][1]
var color = "black"
//draw circles of radius rad around each pixel
ctx.beginPath()
ctx.fillStyle = color
ctx.arc(x, y, 3, 0, 2*Math.PI)
ctx.fill()
ctx.closePath();
}
}
},
//img is a path to the image file. options is an object: {objectToNotifyWhenDoneDisplaying: value, customContext: value}
this.showImgAtFor = function(image, x, y, duration, options){
var ctx = (options === undefined || options.customContext === undefined) ? document.getElementById("overlay").getContext('2d') : options.customContext
console.assert(ctx instanceof CanvasRenderingContext2D)
var imgElement = new Image();
imgElement.src = image;
var width = null, height = null;
imgElement.onload = function(){
width = imgElement.width, height = imgElement.height;
ctx.drawImage(imgElement, x-width/2, y-height/2)
}
//clear it after the duration's up:
setTimeout(function(){
ctx.clearRect(x-width/2,y-height/2,width, height)
if(options !== undefined && options.objectToNotifyWhenDoneDisplaying !== undefined){
options.objectToNotifyWhenDoneDisplaying.respondToImageBeingCleared()
}
}, duration)
//imgElement.style.position = "absolute"
//imgElement.style.top = "300"
//document.body.appendChild(imgElement);
}
this.showOccluders = function(occluderRectangles){
var ctx = document.getElementById("occluderCanvas").getContext("2d")
var occluderPatternImg = new Image();
occluderPatternImg.src = "occluders/occluderpattern.png"
ctx.drawImage(occluderPatternImg, 100, 100)
var occluderPatter = null
occluderPatternImg.onload = function(){
occluderPattern = ctx.createPattern(occluderPatternImg, "repeat")
}
setTimeout(function(){ //setTimeout used because of onload delay
//loop through occluder rectangles:
for(var j = 0, rects = occluderRectangles, numRects = rects.length; j < numRects; j++){
ctx.beginPath()
ctx.fillStyle = occluderPattern
ctx.rect(rects[j].x, rects[j].y, rects[j].width, rects[j].height)
ctx.fill()
//this.showImgAtFor(occs[j].imgPath, occs[j].x, occs[j].y, curLevelreplayTimer.getTime(), {customContext:document.getElementById("occluderCanvas").getContext("2d")})
}
ctx.closePath()
}, 100)}
this.hideOccluders = function(){
var ctx = document.getElementById("occluderCanvas").getContext("2d")
ctx.clearRect(0,0,w,h)
}
} else {
//clear the rect aronud the ball
var totalDiameter = totalRadius * 2
ctx.beginPath()
ctx.clearRect(ball.getX()-totalRadius-1, ball.getY()-totalRadius-1, totalDiameter+2, totalDiameter+2)
ctx.stroke()
ctx.closePath()
}
}
}
this.displayTimer = function(timer){
var octx = document.getElementById("overlay").getContext("2d")
if(timer.hidden){
return null //exit the function if it's hidden, before the recursive call
}
//clear previously existing timer displays
octx.clearRect(timer.x-timer.fontSize,timer.y-timer.fontSize, timer.fontSize*2, timer.fontSize*2)
octx.font = timer.fontSize + "px Arial"
//time = Math.round((this.curTime % 60000)/1000)
var time = timer.getTime == null ? 0: Math.round(timer.getTime()/1000)
octx.fillStyle = timer.color
octx.textAlign = "center"
octx.fillText(time, timer.x, timer.y) //NOTE: assuming time counter should always be under a minute
}
//clears all displays:
this.clearView = function(){
var canvii = document.getElementsByTagName("CANVAS")
var ctxes = [];
for(var canCounter = 0; canCounter < canvii.length; canCounter++){ctxes.push(canvii[canCounter].getContext('2d'))}
for(var conCounter = 0; conCounter < ctxes.length; conCounter++){
var ctx = ctxes[conCounter]
var canWidth = canvii[conCounter].width
var canHeight = canvii[conCounter].height
ctx.clearRect(0,0,canWidth,canHeight);
}
}
}
//game timer. can be reset, told to count down, up, set coundtown time.
function timer(){
this.hidden = false, //toggle whether it's displayed
this.hide = function() {this.hidden = true},
this.unHide = function() {this.hidden = false}
this.paused = true
this.color = "red",
this.getColor = function(){return this.color},
this.fontSize =12,
this.x = w/2, // x positioning
this.y = h/22,
this.countdown = true, //default is countdown-mode, not countup-mode
this.ctdwnTime = par.levelDuration,
this.setCountdownTime = function(t){this.ctdwnTime = t},
this.startTime = 0, //null means it hasn't been set
this.curTime = 1000,
this.updateCurTime = function(){
//startDate must be set already for this to work properly.
this.curTime = new Date().valueOf() - this.startTime
this.curTimeInSeconds = Math.round(this.curTime % 60000/1000)
if(this.curTimeInSeconds == this.ctdwnTime){
curLevel.timeHasRunOut()
//this.ctdwnTime = -1000 //reset it so it doesn't call timeHasRunOut a million times
}
}
this.reset = function(countdownTime, color) {
this.setCountdownTime(countdownTime);
this.startTime = new Date().valueOf(); //0
this.color = color
this.updateCurTime()
}
//this is sensitive to counting up or down
this.getTime = function(noupdate/*optional parameter, if true will not update the time before returning the time*/){
if(noupdate != true) {this.updateCurTime()}
return (this.countdown) ? this.getTimeTilCountdownEnd() : this.timeElapsed(noupdate)
}
//returns how much time has elapsed since the timer has been reset
this.timeElapsed = function(noupdate){
if(noupdate != true) {this.updateCurTime()}
return this.curTime
}
this.getTimeTilCountdownEnd = function(noupdate){
if(noupdate != true) {this.updateCurTime()}
return this.ctdwnTime*1000 - this.curTime
}
this.start = function(){
this.paused = false;
this.run()
}
this.run = function(){
setTimeout(function(){
if(!(curLevel.gameOver() || this.paused)){
curLevel.timer.updateCurTime();curLevel.view.displayTimer(curLevel.timer); curLevel.timer.run()}}, 1000) //recursive call
}
this.pause = function(){this.paused = true}
}
displayObstacles(data.savedModel[frame].userObstacles)
function displayObstacles(obstacles){
/*var sm = data.savedModel
for(var m = 0, len = sm.length; m < len; m++){*/
var obstacles = sm[m].userObstacles
for(var o = 0, l = obstacles.length; o < l; o++){
var ob = obs[j]
for(var o = 0, pix = ob.pixels, numPix = pix.length, rad = ob.getRadius(); o<numPix; o++){
//get x and y values of the pixel
var x = pix[o][0]
var y = pix[o][1]
var color = "maroon"
//draw circles of radius rad around each pixel
ctx.beginPath()
ctx.fillStyle = color
ctx.arc(x, y, rad, 0, 2*Math.PI)
ctx.fill()
//then draw a line from the pixel to the next pixel (if the next pixel exists)
if(o < numPix-1){
ctx.fillStyle = color
ctx.moveTo(pix[o][0], pix[o][1])
ctx.lineTo(pix[o+1][0], pix[o+1][1])
ctx.stroke();
}
ctx.closePath()
}
}
}
//}
}
</script>