summaryrefslogtreecommitdiff
path: root/geometry2.html
blob: 15927c00204a5bcfbe0576f901807fe609e1669d (plain)
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
<!doctype html>
<html>
<head><title>GEOMETRY</title></head>
<style type="text/css">
*
	{
	// border: 1px solid red;
	}
body
	{
	background-color: #87CEEB;
	overflow: hidden;
	}
#frustrum
	{
	z-index: 666;
	}
#player
	{
	z-index: 980;
	background-color: #0f0;
	}
#grid
	{
	display: none;
	z-index: 950;
	}
#water
	{
	z-index: 800;
	background-color: #6666cc;
	}
#seafloor
	{
	z-index: 810;
	background-color: #8380ab;
	}
#background
	{
	position: fixed;
	overflow: visible;
	z-index: 900;
	}
#foreground
	{
	overflow: visible;
	z-index: 999;
	}
#hud
	{
	font-size: 14px;
	font-family: monospace;
	background-color: #333;
	z-index: 1000;
	color: #fff;
	opacity: 0.7;
	white-space: pre;
	}
#tests
	{
	position: fixed;
	right: 400px;
	z-index: 1001;
	}
.flip
	{
	-moz-transform: scaleX(-1);
	-o-transform: scaleX(-1);
	-webkit-transform: scaleX(-1);
	transform: scaleX(-1);
	filter: FlipH;
	-ms-filter: "FlipH";
	}
</style>

<body>
</body>

<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="jquery.gamequery-0.5.1.js"></script> 
<script type="text/javascript"> 
var SCREEN_WIDTH  = $(window).width()
var SCREEN_HEIGHT = $(window).height()
var REFRESH_RATE = 30
var ISLAND_HEIGHT = 576
var WATER_OFFSET = 458
var SKY_OFFSET = SCREEN_HEIGHT - ISLAND_HEIGHT
var FLOOR_OFFSET = SKY_OFFSET + WATER_OFFSET
var SEAFLOOR_DEPTH = 600

var FALL_THRESHOLD = 10

var ABOVE_SURFACE = 3
var ON_SURFACE = 2
var LANDING_ON_SURFACE = 1
var BELOW_SURFACE = 0
var BEYOND_SURFACE = -1
var OFF_MAP = -2

var ORIENTATION_RIGHT = 1
var ORIENTATION_LEFT = -1

var KEY_LEFT = 37
var KEY_UP = 38
var KEY_RIGHT = 39
var KEY_DOWN = 40
var KEYTRACK = {}
KEYTRACK[KEY_LEFT] = false
KEYTRACK[KEY_UP] = false
KEYTRACK[KEY_RIGHT] = false
KEYTRACK[KEY_DOWN] = false

function clamp (x, min, max)
	{
	return Math.max(min, Math.min(x, max))
	}
var HUD =
	{
	div: document.createElement("div"),
	surface: false,
	msg: "",
	update: function ()
		{
		var activity = Player.running ? "RUNNING" : ""
		activity += Player.jumping ? " JUMPING" : ""
		activity += Player.cannonball ? " CANNONBALL" : ""
		activity += Player.underwater ? " UNDERWATER" : ""
		var orientation = Player.orientation === ORIENTATION_RIGHT ? "RIGHT" : "LEFT"
		HUD.div.innerHTML =
			[
			"",
			"PLAYER", Player.x, Player.y, activity, orientation, "\n",
			"SPEED ", Player.dx, Player.dy, HUD.surface, "\n",
			"CANVAS", Canvas.x, Canvas.y, "\n",
			"KEY_UP", KEYTRACK[KEY_UP], "\n",
			"KEY_DOWN", KEYTRACK[KEY_DOWN], "\n",
			"KEY_LEFT", KEYTRACK[KEY_LEFT], "\n",
			"KEY_RIGHT", KEYTRACK[KEY_RIGHT], "\n",
			"SCREEN", SCREEN_WIDTH, SCREEN_HEIGHT, SKY_OFFSET, "\n",
			HUD.msg,
			].join(" ")
		},
	init: function ()
		{
		HUD.div.style.position = "fixed"
		HUD.div.style.top = 0
		HUD.div.style.right = 0
		HUD.div.style.width = 300 + "px"
		HUD.div.style.height = 300 + "px"
		HUD.div.setAttribute("id", "hud")
		document.body.appendChild(HUD.div)
		},
	}
var Canvas =
	{
	div: document.createElement("canvas"),
	background: document.createElement("div"),
	foreground: document.createElement("div"),
	water: document.createElement("div"),
	seafloor: document.createElement("div"),
	x: 0,
	y: 0,
	width: 2024+1224,
	height: 3000,
	surfaces: [],
	ground: [
		[0,-90],
		[700,-90],
		[1000,-70],
		[1200,0],
		[1210,SEAFLOOR_DEPTH],
		[1980,SEAFLOOR_DEPTH],
		[2080,0],
		[2090,-90],
		[2800,-90],
		[2024+1224,-90],
		],
	recalculate_surfaces: function ()
		{
		var calc = []
		for (var i = 1; i < Canvas.ground.length; i++)
			{
			var two = Canvas.ground[i]
			var one = Canvas.ground[i-1]
			// console.log(one,two)
			calc.push([one[0], one[1]+FLOOR_OFFSET, two[0], two[1]+FLOOR_OFFSET])
			}
		for (var i = 0; i < Canvas.surfaces.length; i++)
			{
			var s = Canvas.surfaces[i]
			calc.push([s[0], s[1]+FLOOR_OFFSET, s[2], s[3]+FLOOR_OFFSET])
			}
		Canvas.surfaces = calc
		},
	background_elements:
		[
		// x, y, width, height, image
		[0, 0, 1224, 576, "landscape/island1bg.png"],
		[0, 576, 1224, 1000, "#8380ab"],
		[2024, 576, 1224, 1000, "#8380ab"],
		[1224, WATER_OFFSET + SEAFLOOR_DEPTH - 152, 200, 152, "landscape/underwaterislandcorner.png",],
		[1824, WATER_OFFSET + SEAFLOOR_DEPTH - 152, 200, 152, "landscape/underwaterislandcorner.png", "flip"],
		[2024, 0, 1224, 576, "landscape/island1bg.png", "flip"],
		],
	foreground_elements:
		[
		// x, y, width, height, image
		[0, -34, 1224, 576, "landscape/island1fg.png"],
		],
	init: function ()
		{
		Canvas.background.style.position = "fixed"
		Canvas.background.style.top = 0 + "px"
		Canvas.background.style.left = 0 + "px"
		Canvas.background.style.width = Canvas.width + "px"
		Canvas.background.style.height = Canvas.height + "px"
		Canvas.background.setAttribute("id", "background")
		document.body.appendChild(Canvas.background)

		Canvas.foreground.style.position = "fixed"
		Canvas.foreground.style.top = 0 + "px"
		Canvas.foreground.style.left = 0 + "px"
		Canvas.foreground.style.width = Canvas.width + "px"
		Canvas.foreground.style.height = Canvas.height + "px"
		Canvas.foreground.setAttribute("id", "foreground")
		document.body.appendChild(Canvas.foreground)

		Canvas.water.style.position = "fixed"
		Canvas.water.style.top = SKY_OFFSET + WATER_OFFSET + "px"
		Canvas.water.style.left = 0 + "px"
		Canvas.water.style.width = Canvas.width + "px"
		Canvas.water.style.height = Canvas.height + "px"
		Canvas.water.setAttribute("id", "water")
		document.body.appendChild(Canvas.water)
 
		Canvas.seafloor.style.position = "fixed"
		Canvas.seafloor.style.top = SKY_OFFSET + SEAFLOOR_DEPTH + "px"
		Canvas.seafloor.style.left = 0 + "px"
		Canvas.seafloor.style.width = Canvas.width + "px"
		Canvas.seafloor.style.height = Canvas.height + "px"
		Canvas.seafloor.setAttribute("id", "seafloor")
		document.body.appendChild(Canvas.seafloor)

		ctx = Canvas.div.getContext("2d"),
		Canvas.div.style.position = "fixed"
		Canvas.div.style.top = 0
		Canvas.div.style.left = 0
		Canvas.div.setAttribute("width", Canvas.width+"px")
		Canvas.div.setAttribute("height", Canvas.height+"px")
		Canvas.div.setAttribute("id", "grid")
		document.body.appendChild(Canvas.div)

		ctx.beginPath()
		for (var x = 0.5; x < Canvas.width; x += 10)
			{
			ctx.moveTo(x, 0)
			ctx.lineTo(x, Canvas.height)
			}
		for (var y = 0.5; y < Canvas.height; y += 10)
			{
			ctx.moveTo(0, y)
			ctx.lineTo(Canvas.width, y)
			}
		ctx.strokeStyle = "rgba(255,255,255,0.2)"
		ctx.stroke()

		ctx.beginPath()
		ctx.moveTo(0, SKY_OFFSET+0.5)
		ctx.lineTo(Canvas.width+0.5, SKY_OFFSET+0.5)
		ctx.strokeStyle = "rgba(255,255,0,1)"
		ctx.stroke()

		ctx.beginPath()
		ctx.moveTo(0, FLOOR_OFFSET+0.5)
		ctx.lineTo(Canvas.width+0.5, FLOOR_OFFSET+0.5)
		ctx.strokeStyle = "rgba(255,255,0,1)"
		ctx.stroke()

		/*
		ctx.fillStyle = "rgba(0,20,255,0.5)"
		ctx.fillRect(0, Physics.water_level+0.5, Canvas.width, Canvas.height - Physics.water_level)
		*/

		Canvas.recalculate_surfaces ()
		for (var i = 0; i < Canvas.surfaces.length; i++)
			{
			var surface = Canvas.surfaces[i]
			ctx.beginPath()
			ctx.moveTo(surface[0] + 0.5, surface[1] + 0.5)
			ctx.lineTo(surface[2] + 0.5, surface[3] + 0.5)
			ctx.strokeStyle = "#0bf"
			ctx.stroke()
			}
		for (var i = 0; i < Canvas.background_elements.length; i++)
			{
			Frustrum.furniture.push (new Furniture (Canvas.background, Canvas.background_elements[i]))
			}
		for (var i = 0; i < Canvas.foreground_elements.length; i++)
			{
			Frustrum.furniture.push (new Furniture (Canvas.foreground, Canvas.foreground_elements[i]))
			}
		},
	}
function Furniture (container, row)
	{
	var div = document.createElement("div")
	div.style.position = "fixed"
	div.style.left = row[0] + "px"
	div.style.top = SKY_OFFSET + row[1] + "px"
	div.style.width = row[2] + "px"
	div.style.height = row[3] + "px"
	if (row[4].substr(0,1) === "#")
		div.style.background = row[4]
	else
		div.style.background = "url('" + row[4] + "')"
	if (row.length === 6)
		div.className = row[5]
	container.appendChild(div)

	// this.container = container 
	this.elem = div
	this.x = row[0]
	this.y = row[1]
	this.width = row[2]
	this.height = row[3]
	this.background = row[4]
	this.update = function ()
		{
		this.elem.style.top = SKY_OFFSET + this.y - Frustrum.y + "px"
		this.elem.style.left = this.x - Frustrum.x + "px"
		}
	}
var Frustrum =
	{
	div: document.createElement("div"),
	x: 0,
	y: 0,
	width: SCREEN_WIDTH,
	height: SCREEN_HEIGHT,
	furniture: [],
	redraw: function ()
		{
		Canvas.div.style.top =  Canvas.y - Frustrum.y + "px"
		Canvas.div.style.left = Canvas.x - Frustrum.x + "px"

		Player.div.style.top =  Player.y - Frustrum.y + "px"
		Player.div.style.left = Player.x - Frustrum.x + "px"

		Canvas.water.style.top = SKY_OFFSET + WATER_OFFSET + Canvas.y - Frustrum.y + "px"
		Canvas.seafloor.style.top = SKY_OFFSET + WATER_OFFSET + SEAFLOOR_DEPTH + Canvas.y - Frustrum.y + "px"
	
		for (var i = 0; i < Frustrum.furniture.length; i++)
			Frustrum.furniture[i].update()
		},
	update: function ()
		{
		if (Player.underwater)
			{
			Frustrum.dest_y = Player.y - Frustrum.halfheight + Player.height
			Frustrum.dest_x = Player.x - Frustrum.halfwidth + Player.width
			if (Player.y > Canvas.height - Frustrum.halfheight)
				Frustrum.dest_y = Canvas.height - Frustrum.height
			}
		else
			{
			Frustrum.dest_x = 0
			Frustrum.dest_y = 0
			}
		if (Player.x > Canvas.width - Frustrum.halfwidth)
			Frustrum.dest_x = Canvas.width - Frustrum.width
		else if (Player.x > Frustrum.halfwidth)
			Frustrum.dest_x = Player.x - Frustrum.halfwidth
		Frustrum.y = Math.floor( (Frustrum.dest_y + Frustrum.y * 9) / 10 )
		Frustrum.x = Math.floor( (Frustrum.dest_x + Frustrum.x * 9) / 10 )
		// Frustrum.div.style.top = Frustrum.y + "px"
		// Frustrum.div.style.left = Frustrum.x + "px"
		},
	init: function ()
		{
		Frustrum.div.style.position = "fixed"
		Frustrum.div.style.top = 0
		Frustrum.div.style.left = 0
		Frustrum.div.style.width = Frustrum.width + "px"
		Frustrum.div.style.height = Frustrum.height + "px"
		Frustrum.div.setAttribute("id", "frustrum")
		document.body.appendChild(Frustrum.div)
		Frustrum.halfwidth = Frustrum.width/2
		Frustrum.halfheight = Frustrum.height/2
		},
	}
var Physics =
	{
	gravity: 5,
	dx_walk: 50,
	dx_swim: 50,
	dy_swim_up: -20,
	dy_swim_down: 20,
	dy_resistance: 15,
	dx_resistance: 15,
	air_resistance: 15,
	water_resistance: 8,
	water_level: SKY_OFFSET + WATER_OFFSET,
	jump_power: -45,
	}
var Player =
	{
	div: document.createElement("div"),
	running: false,
	jumping: false,
	start_jump: false,
	swimming: false,
	cannonball: false,
	orientation: ORIENTATION_RIGHT,
	dx: 0,
	dy: 0,
	x: 0,
	y: 0,
	width: 100,
	height: 167,
	yIntercept: 0,
	surface: [0,0,0,0],
	surface_left: [0,0,0,0],
	surface_right: [0,0,0,0],
	findNearestSurface: function (feet)
		{
		for (var i = 0; i < Canvas.surfaces.length; i++)
			{
			var s = Canvas.surfaces[i]
			if (Player.onSurface(feet, s) > BELOW_SURFACE)
				{
				return s
				}
			}
		return OFF_MAP
		},
	onSurface: function (feet, s)
		{
		if (s === OFF_MAP)
			return OFF_MAP

		// within bounds
		if (Player.x < s[0] || Player.x > s[2])
			return BEYOND_SURFACE

		// above the line
		if (s[1] === s[3])
			{
			Player.slope = 0
			Player.yIntercept = s[1]
			if (feet < s[1])
				return ABOVE_SURFACE
			if (feet <= s[1] + Player.dy + Physics.gravity + Player.height)
				return ON_SURFACE
			// if (! Player.surface)
			// 	return ON_SURFACE
			return ON_SURFACE
			// return BELOW_SURFACE
			}
		var slope = (s[3] - s[1]) / (s[2] - s[0])
		Player.slope = slope
		Player.yIntercept = (Player.x - s[0]) * slope + s[1]

		if (feet < Player.yIntercept - 1)
			return ABOVE_SURFACE
		if (feet <= Player.yIntercept + Player.dy + Physics.gravity + Player.height)
			return ON_SURFACE
		if (Player.surfaceState === ON_SURFACE)
			return ON_SURFACE
		// if (! Player.surface)
		// 	return ON_SURFACE
		return ON_SURFACE
		// return BELOW_SURFACE
		},
	underwater: false,
	update: function ()
		{
		var feet = Player.y + Player.height
		Player.feet = feet
		Player.surfaceState = Player.onSurface(feet, Player.surface)
		switch (Player.surfaceState)
			{
			case ABOVE_SURFACE:
				HUD.surface = "ABOVE SURFACE"
				if (Player.underwater)
					Player.dy = Math.min(Player.dy + Physics.gravity, Physics.dy_resistance)
				else
					Player.dy = Player.dy + Physics.gravity
				if (Player.y + Player.dy + Player.height > Player.yIntercept)
					{
					Player.y = Player.yIntercept - Player.height
					Player.dy = 0
					Player.surfaceState = ON_SURFACE
					}
				break
			case ON_SURFACE:
				HUD.surface = "ON SURFACE"
				Player.dy = 0
				Player.y = Player.yIntercept - Player.height
				Player.jumping = false
				HUD.msg = [Player.y, Player.yIntercept, Player.height, "...", Player.surface]
				// clearInterval(Events.interval)
				// Tests.random_x()
				break
			case BELOW_SURFACE:
				HUD.surface = "BELOW SURFACE"
				Player.dy = 0
				Player.y = Player.yIntercept - Player.height
				Player.surfaceState = ON_SURFACE
				break
			case BEYOND_SURFACE:
				HUD.surface = "BEYOND SURFACE"
				Player.surface = Player.findNearestSurface(feet)
				Player.update()
				break
			case OFF_MAP:
				HUD.surface = "OFF MAP"
				break
			}
		// Player.div.style.left = Player.x + "px"
		// Player.div.style.top = Player.y + "px"
		if (Player.underwater)
			{
			if (Player.y > Physics.water_level - 2 && Player.y < Physics.water_level + 2)
				Player.dy = 0
			else if (Player.y < Physics.water_level)
				{
				Player.underwater = false
				Player.cannonball = false
				Physics.dx_resistance = Physics.air_resistance
				Physics.dy_resistance = Physics.air_resistance
				}
			if (KEYTRACK[KEY_UP])
				Player.dy = Physics.dy_swim_up
			if (KEYTRACK[KEY_DOWN])
				Player.dy = Physics.dy_swim_down
			if (KEYTRACK[KEY_LEFT] || KEYTRACK[KEY_RIGHT])
				Player.dx = Physics.dx_swim * Player.orientation
			else
				Player.dx = 0
			}
		else
			{
			if (Player.y > Physics.water_level)
				{
				Player.jumping = false
				Player.cannonball = false
				Player.underwater = true
				Player.yJump = Physics.water_level
				Physics.dx_resistance = Physics.water_resistance
				Physics.dy_resistance = Physics.water_resistance
				}
			else if (Player.yJump < Physics.water_level && Player.yIntercept > Physics.water_level)
				Player.cannonball = true
			else
				Player.cannonball = false
			if ((Player.start_jump || KEYTRACK[KEY_UP]) && Player.surfaceState === ON_SURFACE)
				{
				Player.start_jump = false
				Player.jumping = true
				Player.yJump = Player.y
				Player.dy = Physics.jump_power
				Player.y += Player.dy
				Player.surfaceState = ABOVE_SURFACE
				}
			if (KEYTRACK[KEY_LEFT])
				{
				Player.dx = Physics.dx_walk * Player.orientation
				}
			else if (KEYTRACK[KEY_RIGHT])
				{
				Player.dx = Physics.dx_walk * Player.orientation
				}
			else
				Player.dx = 0
			}
		Player.y += Player.dy
		Player.x = clamp (Player.x + Player.dx, 10, Canvas.width-Player.width - 20)
		},
	init: function ()
		{
		Player.x = 200
		Player.y = SKY_OFFSET + FLOOR_OFFSET - 350 - Player.height
		Player.div.style.position = "fixed"
		Player.div.style.top = Player.y + "px"
		Player.div.style.left = Player.x + "px"
		Player.div.style.width = Player.width + "px"
		Player.div.style.height = Player.height + "px"
		Player.div.setAttribute("id", "player")
		document.body.appendChild(Player.div)
		},
	}
var Events =
	{
	interval: false,
	keydown: function (e)
		{
		KEYTRACK[e.keyCode] = true
		switch (e.keyCode)
			{
			case KEY_UP:
				if (Player.underwater || Player.jumping)
					break
				Player.start_jump = true
				break
			case KEY_LEFT:
				Player.orientation = ORIENTATION_LEFT
				break
			case KEY_RIGHT:
				Player.orientation = ORIENTATION_RIGHT
				break
			case 71: // "G"
				$("#grid").toggle()
				break
			}
		},
	keyup: function (e)
		{
		KEYTRACK[e.keyCode] = false
		},
	loop: function ()
		{
		Player.update()
		Frustrum.update()
		Frustrum.redraw()
		HUD.update()
		},
	init: function ()
		{
		$(document).bind("keydown", Events.keydown)
		$(document).bind("keyup", Events.keyup)
		Events.interval = setInterval(Events.loop, REFRESH_RATE)
		},
	}
var Tests =
	{
	random_x: function ()
		{
		Player.x = Player.x+1 // Math.floor(Math.random () * 500)
		if (Player.x > Canvas.width)
			{
			Player.x = 0
			Player.y = 0
			}
		// Player.y = 50 // Math.floor(Math.random () * 50)
		Player.surface = Canvas.surfaces[0]
		//if (Math.random() > 0.3)
		//	{
		//	Player.dy = -5
		//	}
		},
	init: function ()
		{
		// $("#random_x").click(Tests.random_x)
		},
	}
var Main =
	{
	init: function ()
		{
		// Tests.init()
		Canvas.init()
		Player.init()
		Frustrum.init()
		HUD.init()
		Events.init()
		},
	}
Main.init ()
</script>
</html>