<commit_before>
/* medea - an Open Source, WebGL-based 3d engine for next-generation browser games.
 * (or alternatively, for clumsy and mostly useless tech demos written solely for fun)
 *
 * medea is (c) 2011, <NAME>
 * licensed under the terms and conditions of a 3 clause BSD license.
 */

medea._addMod('terrainheightpath',['entity'],function(undefined) {
	"use strict";
	var medea = this;
	
	var TerrainHeightPath = medea.Entity.extend(
	{
		init : function(terrain, height_offset) {
			this.terrain = terrain;
			this.height_offset = height_offset === undefined ? 2.0 : height_offset;

// #ifdef DEBUG
			medea.DebugAssert(this.terrain instanceof medea.TerrainNode, "need valid terrain node");
// #endif
		},

		Render : function(viewport,entity,node,rqmanager) {
			// nothing to be done
		},

		Update : function(dtime,node) {
			var ppos = node.GetWorldPos();
			var h = this.terrain.GetWorldHeightForWorldPos(ppos[0],ppos[2]);

			if (h === null) {
				// outside the terrain or terrain not present yet, do not touch.
			}
			else {
				ppos[1] = this.height_offset + h;

				var t = vec3.create();
				mat4.multiplyVec3(node.parent.GetInverseGlobalTransform(),ppos,t);
				node.LocalPos(t);
			}
		},

		HeightOffset : function(h) {
			if (h === undefined) {
				return this.height_offset;
			}
			this.height_offset = h;
		},
	});
	
	medea.CreateTerrainHeightPathAnimator = function(terrain, ho) {
		return new TerrainHeightPath(terrain, ho);
	};
});<commit_msg>- TerrainHeightPath now supports a configurable smoothing factor to make the height transitions smoother.<commit_after>
/* medea - an Open Source, WebGL-based 3d engine for next-generation browser games.
 * (or alternatively, for clumsy and mostly useless tech demos written solely for fun)
 *
 * medea is (c) 2011, <NAME>
 * licensed under the terms and conditions of a 3 clause BSD license.
 */

medea._addMod('terrainheightpath',['entity'],function(undefined) {
	"use strict";
	var medea = this;
	
	var TerrainHeightPath = medea.Entity.extend(
	{
		init : function(terrain, height_offset, smooth_factor) {
			this.terrain = terrain;
			this.height_offset = height_offset === undefined ? 2.0 : height_offset;
			this.smooth_factor = smooth_factor || 0.06;
			this.seen = {};

// #ifdef DEBUG
			medea.DebugAssert(this.terrain instanceof medea.TerrainNode, "need valid terrain node");
// #endif
		},

		Render : function(viewport,entity,node,rqmanager) {
			// nothing to be done
		},

		Update : function(dtime,node) {
			var ppos = node.GetWorldPos();
			var h = this.terrain.GetWorldHeightForWorldPos(ppos[0],ppos[2]);

			if (h === null) {
				// outside the terrain or terrain not present yet, do not touch.
			}
			else {
				ppos[1] = this.height_offset + h;

				var t = vec3.create();
				mat4.multiplyVec3(node.parent.GetInverseGlobalTransform(),ppos,t);
				
				if (this.smooth_factor && node.id in this.seen) {
					var f = Math.pow(this.smooth_factor,dtime), oldh = t[1] - this.height_offset*0.5;
					t = vec3.add( vec3.scale(t,1.0 - f), vec3.scale(node.LocalPos(), f) );
					
					// add lower limit to make sure we don't fall below the terrain
					t[1] = Math.max(t[1], oldh);
				}
				
				node.LocalPos(t);
				this.seen[node.id] = true;
			}
		},

		HeightOffset : function(h) {
			if (h === undefined) {
				return this.height_offset;
			}
			this.height_offset = h;
		},
		
		SmoothFactor : function(h) {
			if (h === undefined) {
				return this.smooth_factor;
			}
			this.smooth_factor = h;
		},
	});
	
	medea.CreateTerrainHeightPathAnimator = function(terrain, ho) {
		return new TerrainHeightPath(terrain, ho);
	};
});
{'fraction_non_alphanumeric': 0.13079470198675497, 'fraction_numerical': 0.01131346578366446, 'mean_word_length': 3.4478527607361964, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5979767', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1198, 'n_words': 309}