-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathside_scroller.html
More file actions
443 lines (406 loc) · 11.1 KB
/
Copy pathside_scroller.html
File metadata and controls
443 lines (406 loc) · 11.1 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<html>
<head>
<title>Test</title>
<script type="text/javascript" src="Common.js"></script>
<script type="text/javascript" src="Sprite.js"></script>
<script type="text/javascript" src="TileSet.js"></script>
<script type="text/javascript" src="CollisionBlock.js"></script>
<script type="text/javascript" src="SceneObject.js"></script>
<script type="text/javascript" src="SceneObserver.js"></script>
<script type="text/javascript" src="CollisionObserver.js"></script>
<script type="text/javascript" src="InputHandler.js"></script>
<script type="text/javascript" src="Scene.js"></script>
<script type="text/javascript" src="PhysicsObject.js"></script>
<script type="text/javascript" src="TileObject.js"></script>
<script type="text/javascript" src="Controller.js"></script>
<script type="text/javascript" src="InputController.js"></script>
<script type="text/javascript" src="Actor.js"></script>
<script type="text/javascript" src="Viewport.js"></script>
</head>
<body style="margin: 0px; padding: 0px; scrolling: none;">
<canvas id="canvas" width="500" height="500"></canvas>
<script type="text/javascript" >
//Initializing the canvas
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
//Canvas dimensions
var W = window.innerWidth;
var H = window.innerHeight;
canvas.style.width = W;
canvas.style.height = H;
context.canvas.width = W;
context.canvas.height = H;
var spr = new Sprite({
src:'Test Resources/triceratops.png',
});
spr.addAnimation({
name : 'WalkLeft',
startX : 0,
startY : 0,
width : 78,
height : 40,
padding : 0,
frames : 8,
speed : 3
});
spr.addAnimation({
name : 'WalkRight',
startX : 0,
startY : 40,
width : 78,
height : 40,
padding : 0,
frames : 8,
speed : 3
});
spr.setState({state : 'WalkLeft'});
spr.pauseAnimation();
var c = new CollisionBlock({
width: 78,
height: 37,
matrix: [[0],[0,1,1,0]]
});
var a = new Actor({
x: 10,
y: 70,
z: 0,
yAcceleration: 30,
sprite: spr,
collisionBlock: c
});
a.addState({
name: 'StartWalkLeft',
spriteState: {state:'WalkLeft'},
xAcceleration: -50
});
a.addState({
name: 'StartWalkRight',
spriteState: {state:'WalkRight'},
xAcceleration: 50
});
a.addState({
name: 'WalkLeft',
spriteState: {state:'WalkLeft'},
});
a.addState({
name: 'WalkRight',
spriteState: {state:'WalkRight'},
});
a.addState({
name: 'StopWalkLeft',
xAcceleration: 50
});
a.addState({
name: 'StopWalkRight',
xAcceleration: -50
});
a.addState({
name: 'Still',
spritePause: true,
xVelocity: 0
});
a.addState({
name: 'Jump',
spritePause: true,
yVelocity: -50
});
a.addState({
name: 'FreeFall',
spritePause: true
});
a.setState('FreeFall');
var ts = new TileSet({
src: 'Test Resources/tileset.png',
tileWidth: 32,
tilesPerRow: 16,
rows: 16,
cols: 64
});
ts.setLayer({
zIndex: 0,
layer:
[
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[1,1,1,1,1,1,1,1,1,1,-1,-1,1,1,1,1,1,1,1,1,1,1,-1,-1,1,1,1,1,1,1,1,1,1,1],
[2,2,2,2,2,2,2,2,2,2,-1,-1,2,2,2,2,2,2,2,2,2,2,-1,-1,2,2,2,2,2,2,2,2,2,2],
[2,2,2,2,2,2,2,2,2,2,-1,-1,2,2,2,2,2,2,2,2,2,2,-1,-1,2,2,2,2,2,2,2,2,2,2],
[2,2,2,2,2,2,2,2,2,2,-1,-1,2,2,2,2,2,2,2,2,2,2,-1,-1,2,2,2,2,2,2,2,2,2,2],
[2,2,2,2,2,2,2,2,2,2,-1,-1,2,2,2,2,2,2,2,2,2,2,-1,-1,2,2,2,2,2,2,2,2,2,2]
]
});
var ts2 = new TileSet({
src: 'Test Resources/tileset.png',
tileWidth: 32,
tilesPerRow: 16,
rows: 16,
cols: 64
});
ts2.setLayer({
zIndex: 0,
layer:
[
[],
[],
[],
[],
[],
[],
[1],
[2],
[2],
[2],
[2],
[2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2],
[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],
[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],
[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],
[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
]
});
var scene = new Scene({
width: W,
height: H,
tileset: ts
});
scene.addSceneObject(a);
var scene2 = new Scene({
width: W,
height: H,
tileset: ts2
});
//the function for the update method of our InputController is the same regardless of input.
//Really, one should extend the InputController class (or modify it) with their own movement state code in the update() function.
var movementStateFunction = function(args) {
var a = args.object;
if(a.y_vel < 4 && a.y_vel > -4) { //if we're jumping or falling, we obviously can't actually walk anywhere or come to a stop
if(args.stateObject.walking_left) {
if(a.getState() !== 'WalkLeft' && a.getState() !== 'StartWalkLeft') a.setState('StartWalkLeft');
if(a.getState() === 'StartWalkLeft' && a.x_vel < -20) a.setState('WalkLeft');
} else if(args.stateObject.walking_right) {
if(a.getState() !== 'WalkRight' && a.getState() !== 'StartWalkRight') a.setState('StartWalkRight');
if(a.getState() === 'StartWalkRight' && a.x_vel > 20) a.setState('WalkRight');
} else {
if(a.getState() === 'WalkLeft' || a.getState() === 'StartWalkLeft') a.setState('StopWalkLeft');
if(a.getState() === 'WalkRight' || a.getState() === 'StartWalkRight') a.setState('StopWalkRight');
if(a.getState() === 'StopWalkLeft' && a.x_vel >= 0) { a.x_vel = 0; a.setState('Still'); }
if(a.getState() === 'StopWalkRight' && a.x_vel <= 0) { a.x_vel = 0; a.setState('Still'); }
}
}
if(a.getState() == 'Jump') a.setState('FreeFall');
}
var ih = new InputHandler();
var ic = new InputController();
ih.registerController(ic);
a.addController(ic);
ic.addInputBinding({
type: 'keydown',
keyCode: 65,
action: function(args) {
args.stateObject.walking_left = true;
args.stateObject.update = movementStateFunction;
}
});
ic.addInputBinding({
type: 'keydown',
keyCode: 68,
action: function(args) {
args.stateObject.walking_right = true;
args.stateObject.update = movementStateFunction;
}
});
ic.addInputBinding({
type: 'keyup',
keyCode: 65,
action: function(args) {
args.stateObject.walking_left = false;
args.stateObject.update = movementStateFunction;
}
});
ic.addInputBinding({
type: 'keyup',
keyCode: 68,
action: function(args) {
args.stateObject.walking_right = false;
args.stateObject.update = movementStateFunction;
}
});
ic.addInputBinding({
type: 'keydown',
keyCode: 32,
action: function(args) {
var a = args.object;
if(a.getState() !== 'FreeFall' && a.getState() !== 'Jump') a.setState('Jump');
args.stateObject.update = movementStateFunction;
}
});
var c2 = new CollisionBlock({
width: 320,
height: 32,
matrix: [[1]]
});
var c3 = new CollisionBlock({
width: 32,
height: 160,
matrix: [[1]]
});
var c4 = new CollisionBlock({
width: 960,
height: 32,
matrix: [[1]]
});
var to1 = new TileObject({
x: 0,
y: 352,
z: 0,
collisionBlock: c2
});
var to2 = new TileObject({
x: 384,
y: 352,
z: 0,
collisionBlock: c2
});
var to3 = new TileObject({
x: 768,
y: 352,
z: 0,
collisionBlock: c2
});
scene.addSceneObject(to1);
scene.addSceneObject(to2);
scene.addSceneObject(to3);
var to4 = new TileObject({
x: 0,
y: 192,
z: 0,
collisionBlock: c3
});
var to5 = new TileObject({
x: 960,
y: 192,
z: 0,
collisionBlock: c3
});
var to6 = new TileObject({
x: 0,
y: 352,
z: 0,
collisionBlock: c4
});
scene2.addSceneObject(to4);
scene2.addSceneObject(to5);
scene2.addSceneObject(to6);
var to7 = new TileObject({
x: 1056,
y: 192,
z: 0,
collisionBlock: c3,
solid: false
});
scene.addSceneObject(to7);
//In order for the Dino Actor to actually stop when it hits the floor, we need something to tell it that it hit the floor.
//For this, we use a CollisionObserver object and register all SceneObjects which may collide.
var observer = new CollisionObserver();
observer.registerObject(a);
observer.registerObject(to1);
observer.registerObject(to2);
observer.registerObject(to3);
scene.addObserver(observer);
scene2.addObserver(observer); //re-using observer--not the best practice...
var OffscreenObserver = SceneObserver.extend();
OffscreenObserver.prototype.update = function(args) {
for(var i=0; i<this.scene_objects.length; i++) {
var so = this.scene_objects[i];
if(so.x < -2 * so.width || so.x > scene.width || so.y < 0 || so.y > scene.height) { so.setState('FreeFall'); so.x = 10; so.y = 10; so.x_acc = 0; so.x_vel = 0; so.y_vel = 0; }
}
}
var oso = new OffscreenObserver();
oso.registerObject(a);
scene.addObserver(oso);
var LevelChangeObserver = SceneObserver.extend(function(args) {
SceneObserver.prototype.constructor.call(this,args);
this.viewport = args.viewport;
});
LevelChangeObserver.prototype.update = function(args) {
for(var i=0; i<this.scene_objects.length; i++) {
var so = this.scene_objects[i];
for(var j=0; j<this.scene_objects.length; j++) {
if(i == j) continue;
if(so.isOverlapping(this.scene_objects[j])) {
vp.getCurrentScene().removeSceneObject(a.name);
a.x = 40;
a.y = 314;
vp.loadScene({
name: 'scene2',
scene: scene2
});
observer.deregisterObject(to1);
observer.deregisterObject(to2);
observer.deregisterObject(to3);
observer.registerObject(to4);
observer.registerObject(to5);
observer.registerObject(to6);
vp.setCurrentScene({name:'scene2'});
vp.getCurrentScene().addSceneObject(a);
vp.center();
}
}
}
}
var lco = new LevelChangeObserver({
viewport: vp
});
lco.registerObject(a);
lco.registerObject(to7);
scene.addObserver(lco);
var vp = new Viewport({
width: 300,
height: 200,
target: a
});
vp.center();
vp.loadScene({
name: 'scene',
scene: scene
});
vp.setCurrentScene({name: 'scene'});
var vp2 = new Viewport({
width: 1200,
height: 500,
drawX: 300
});
vp2.loadScene({
name: 'scene',
scene: scene
});
vp2.setCurrentScene({name: 'scene'});
function draw() {
vp.getCurrentScene().update({ms:33});
vp.update();
//scene.draw({context:context});
vp.draw({context: context});
vp2.update();
vp2.draw({context:context});
}
//draw();
window.setInterval(draw, 33);
//Never do this. This is just because Ihaven't finished implementing this behaviour at this point
document.addEventListener('keydown', function(evt) {
ih.keyDown(evt);
});
document.addEventListener('keyup', function(evt) {
ih.keyUp(evt);
});
</script>
</body>
</html>