<commit_before><!doctype html>

<html>
<head>

	<title>Climbing test</title>

	<style>
	
		body { margin: 0; padding: 0; overflow: hidden; background-color: #000; }
		canvas { position: fixed; }
	
	</style>

</head>
<body>	

	<script src="../deps/jquery.min.js"></script>
	<script src="../deps/jszip.min.js"></script>
	<script src="../deps/three.min.js"></script>

	<script src="../common/Base.js"></script>
	<script src="../common/Extensions.js"></script>
	<script src="../common/InputHelper.js"></script>
	<script src="../common/MathHelper.js"></script>
	<script src="../common/Canvas2d.js"></script>

	<script src="../game/js/ui/DebugUI.js"></script>

	<script>

		var cvs;

		var ledges = [
			{ start: 0, end: 32, height: 48 },
			{ start: 32, end: 36, height: 44 },
			{ start: 36, end: 40, height: 40 },
			{ start: 40, end: 70, height: 36 },
			{ start: 70, end: 100, height: 10 },
		];

		var player = {
			pos: 54, size: 8, ledge: 3
		};

		var zoom = window.innerHeight / 100;

		var keys = {
			Left: 37,
			Right: 39,
			Up: 38,
			Down: 40,
		};

		function init() {
			cvs = new GS.Canvas2d();
			cvs.init();
		}

		function update() {
			var dx = 0;

			if (GS.InputHelper.isKeyDown(keys.Left)) {
				dx--;
			}
			if (GS.InputHelper.isKeyDown(keys.Right)) {
				dx++;
			}

			move(dx);
		}

		function move(dx) {
			var newPos = player.pos + dx;

			var startX = newPos - player.size;
			var endX = newPos + player.size;

			var maxHeight = -Infinity;
			var maxLedge = -1;

			for (var i = 0; i < ledges.length; i++) {
				var ledge = ledges[i];

				if (startX >= ledge.start && startX <= ledge.end ||
					endX >= ledge.start && endX <= ledge.end) {

					if (ledge.height > maxHeight) {
						maxHeight = ledge.height;
						maxLedge = i;
					}
				}
			}

			if (maxLedge > -1) {				
				player.pos = newPos;
				player.ledge = maxLedge;
			}
		}

		function draw() {
			cvs.clear();

			drawLedges();
			drawPlayer();

			cvs.flip();
		}

		function drawLedges() {
			for (var i = 0; i < ledges.length; i++) {
				var ledge = ledges[i];

				var start = new THREE.Vector2(ledge.start * zoom, (100 - ledge.height) * zoom);
				var end = new THREE.Vector2(ledge.end * zoom, (100 - ledge.height) * zoom);

				cvs.line(start, end, "#fff", 3);

				if (i + 1 < ledges.length) {
					ledge = ledges[i + 1];
					start.copy(end);
					end.set(ledge.start * zoom, (100 - ledge.height) * zoom);

					cvs.line(start, end, "#fff", 3);
				}
			}<commit_msg>working climb logic (2d)<commit_after><!doctype html>

<html>
<head>

	<title>Climbing test</title>

	<style>
	
		body { margin: 0; padding: 0; overflow: hidden; background-color: #000; }
		canvas { position: fixed; }
	
	</style>

</head>
<body>	

	<script src="../deps/jquery.min.js"></script>
	<script src="../deps/jszip.min.js"></script>
	<script src="../deps/three.min.js"></script>

	<script src="../common/Base.js"></script>
	<script src="../common/Extensions.js"></script>
	<script src="../common/InputHelper.js"></script>
	<script src="../common/MathHelper.js"></script>
	<script src="../common/Canvas2d.js"></script>

	<script src="../game/js/ui/DebugUI.js"></script>

	<script>

		var cvs;

		var ledges = [
			{ start: 0, end: 28, height: 52 },
			{ start: 28, end: 32, height: 48 },
			{ start: 32, end: 36, height: 44 },
			{ start: 36, end: 40, height: 40 },
			{ start: 40, end: 70, height: 36 },
			{ start: 70, end: 100, height: 10 },
		];

		var player = {
			pos: 54, size: 8, ledge: 4
		};

		var zoom = window.innerHeight / 100;

		var keys = {
			Left: 37,
			Right: 39,
			Up: 38,
			Down: 40,
		};

		function init() {
			cvs = new GS.Canvas2d();
			cvs.init();
		}

		function update() {
			var dx = 0;

			if (GS.InputHelper.isKeyDown(keys.Left)) {
				dx--;
			}
			if (GS.InputHelper.isKeyDown(keys.Right)) {
				dx++;
			}

			move(dx);
		}

		function move(dx) {
			move2(dx);
			return;

			var newPos = player.pos + dx;

			var startX = newPos - player.size;
			var endX = newPos + player.size;

			var maxHeight = -Infinity;
			var maxLedge = -1;

			for (var i = 0; i < ledges.length; i++) {
				var ledge = ledges[i];

				if (startX >= ledge.start && startX <= ledge.end ||
					endX >= ledge.start && endX <= ledge.end) {

					if (ledge.height > maxHeight) {
						maxHeight = ledge.height;
						maxLedge = i;
					}
				}
			}

			if (maxLedge > -1) {
				player.pos = newPos;
				player.ledge = maxLedge;
			}
		}

		function move2(dx) {
			var newPos = player.pos + dx;

			var startX = newPos - player.size;
			var endX = newPos + player.size;

			var heights = [];

			var maxHeight = -Infinity;
			var maxLedge = -1;

			for (var i = 0; i < ledges.length; i++) {
				var ledge = ledges[i];

				if (startX <= ledge.end && endX >= ledge.start) {
					if (ledge.height > maxHeight) {
						maxHeight = ledge.height;
						maxLedge = i;
					}
					heights.push(ledge.height);
				}
			}

			if (maxLedge > -1) {
				heights.sort();

				var maxDistance = 0;
				var distance;
				for (var i = 0; i < heights.length - 1; i++) {
					distance = heights[i + 1] - heights[i];
					if (distance > maxDistance) {
						maxDistance = distance;
					}
				}

				if (maxDistance <= 6) {
					player.pos = newPos;
					player.ledge = maxLedge;
				} else {
					GS.DebugUI.setStaticLine("heights", heights.join(" "));
				}	
			}
		}

		function draw() {
			cvs.clear();

			drawLedges();
			drawPlayer();

			cvs.flip();
		}

		function drawLedges() {
			for (var i = 0; i < ledges.length; i++) {
				var ledge = ledges[i];

				var start = new THREE.Vector2(ledge.start * zoom, (100 - ledge.height) * zoom);
				var end = new THREE.Vector2(ledge.end * zoom, (100 - ledge.height) * zoom);				

				cvs.line(start, end, "#fff", 3);

				var center = start.clone().add(end).multiplyScalar(0.5);
				center.y += 10;
				cvs.text(center, ledge.height, "#fff", 16, "top", "center");

				if (i + 1 < ledges.length) {
					ledge = ledges[i + 1];
					start.copy(end);
					end.set(ledge.start * zoom, (100 - ledge.height) * zoom);

					cvs.line(start, end, "#fff", 3);
				}
			}
{'fraction_non_alphanumeric': 0.18562972706346598, 'fraction_numerical': 0.027458073002301873, 'mean_word_length': 2.388857938718663, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3841376', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2274, 'n_words': 549}