<commit_before>// defines your module and loads any dependencies
define([
	'units/unit',
	'stage',
	'utility',
	'models/mapHitArea',
	'config'
], function(Unit,Stage,Utility,MapHitArea,Config)
{
	console.log("enemy.js loaded");

	// TODO: rename it back to Typhoon when Earthquake is created
	function Enemy(startX,startY,spriteSrc){
		//call super constructor.
		Unit.call(this,startX,startY,spriteSrc);
		this.direction 	= 	0 ;
		this.speed 		=	0 ;
		this.originalSpeed = 0;
		this.force 		= 	{dir:0,mag:0};
		this.max_hp 	= 	Math.random()*Config.maxTyphoonHP;
		this.hp 		= 	this.max_hp;

		this.isSlowed	=	0;
		this.typhoonID = Stage.addChild(this,'typhoons');

	}

	//subclass extends superclass
	Enemy.prototype = Object.create(Unit.prototype);
	Enemy.prototype.constructor = Enemy;

	// tick event handler
	Enemy.prototype.tick = function(dt){	// override
		this.updatePosition();
		if(! this.isWithinCanvas() ){
			this.remove();
		}
		console.log("Typhoon's tick completed.");
	};
	Enemy.prototype.render = function(ctx){
		if(this.spriteReady){
			//draw image
			ctx.globalAlpha = (this.hp/Config.maxTyphoonHP)*0.9;
			var drawX = this.x - this.spriteOrigin.x;
			var drawY = this.y - this.spriteOrigin.y;
			ctx.drawImage(this.sprite,drawX,drawY);
			ctx.globalAlpha = 1;	
			// ctx.fillText(this.typhoonID,this.x,this.y);
			//ctx.fillText(this.isSlowed,this.x,this.y);
		}

	};
	// <position functions>
	Enemy.prototype.updatePosition = function(){
		// force -> velocity
		if(this.force.mag!=0){
			this.addMotion(this.force.dir,this.force.mag);
		}
		// velocity -> displacement
		// apply slow effect
		var tempSpeed = this.speed;
		if(this.isSlowed>0) {
			this.isSlowed-=1;
			tempSpeed = this.speed* (1-this.isSlowed/150*0.8);
		}
		// move it
		var addX = Math.cos(this.direction/180*Math.PI) * tempSpeed;
		var addY = Math.sin(this.direction/180*Math.PI) * tempSpeed;
		this.x += addX;
		this.y += addY;

		if(MapHitArea.isLand(this.x,this.y))
			this.damage(this.max_hp*0.013);

	};
	/**
	 * get the current motion of the typhoon
	 * @return {object{dir,sp}} a force object containing direction and speed of the typhoon
	 */
	Enemy.prototype.getMotion = function(){
		return {
			dir:this.direction,
			sp:this.speed
		};
	};
	/**
	 * Sets the motion of the typhoon, ignoring  previous values
	 * @param {number} dir Direction of typhoon
	 * @param {number} sp  Speed of typhoon
	 */
	Enemy.prototype.setMotion = function(dir,sp){
		this.direction = dir;
		this.speed = sp;
		this.max_hp += (10-sp)*10;
		if(this.max_hp>Config.maxTyphoonHP)
			this.max_hp = Config.maxTyphoonHP;
		this.hp = this.max_hp;

	};
	/**
	 * alters the motion by a little bit
	 * @param {number} force_dir       direction of force applied<commit_msg>[CHANGE] Fine tuned enemy decay and recovery<commit_after>// defines your module and loads any dependencies
define([
	'units/unit',
	'stage',
	'utility',
	'models/mapHitArea',
	'config'
], function(Unit,Stage,Utility,MapHitArea,Config)
{
	console.log("enemy.js loaded");

	// TODO: rename it back to Typhoon when Earthquake is created
	function Enemy(startX,startY,spriteSrc){
		//call super constructor.
		Unit.call(this,startX,startY,spriteSrc);
		this.direction 	= 	0 ;
		this.speed 		=	0 ;
		this.originalSpeed = 0;
		this.force 		= 	{dir:0,mag:0};
		this.max_hp 	= 	Math.random()*Config.maxTyphoonHP;
		this.hp 		= 	this.max_hp;

		this.isSlowed	=	0;
		this.typhoonID = Stage.addChild(this,'typhoons');

	}

	//subclass extends superclass
	Enemy.prototype = Object.create(Unit.prototype);
	Enemy.prototype.constructor = Enemy;

	// tick event handler
	Enemy.prototype.tick = function(dt){	// override
		this.updatePosition();
		if(! this.isWithinCanvas() ){
			this.remove();
		}
		console.log("Typhoon's tick completed.");
	};
	Enemy.prototype.render = function(ctx){
		if(this.spriteReady){
			//draw image
			ctx.globalAlpha = (this.hp/Config.maxTyphoonHP)*0.9;
			var drawX = this.x - this.spriteOrigin.x;
			var drawY = this.y - this.spriteOrigin.y;
			ctx.drawImage(this.sprite,drawX,drawY);
			ctx.globalAlpha = 1;	
			// ctx.fillText(this.typhoonID,this.x,this.y);
			//ctx.fillText(this.isSlowed,this.x,this.y);
		}

	};
	// <position functions>
	Enemy.prototype.updatePosition = function(){
		// force -> velocity
		if(this.force.mag!=0){
			this.addMotion(this.force.dir,this.force.mag);
		}
		// velocity -> displacement
		// apply slow effect
		var tempSpeed = this.speed;
		if(this.isSlowed>0) {
			this.isSlowed-=1;
			tempSpeed = this.speed* (1-this.isSlowed/150*0.8);
		}
		// move it
		var addX = Math.cos(this.direction/180*Math.PI) * tempSpeed;
		var addY = Math.sin(this.direction/180*Math.PI) * tempSpeed;
		this.x += addX;
		this.y += addY;
 
		// Calculate if it will decade or recover
		if(MapHitArea.isLand(this.x,this.y))
			this.damage(this.max_hp*0.013);
		if(tempSpeed<0.1)
			this.damage(this.max_hp*0.005);
		else
			if(this.hp<Config.maxTyphoonHP)
			{
				this.hp += Config.maxTyphoonHP*0.003;
				if(this.hp > this.max_hp)
					this.max_hp = this.hp;
			}
	};
	/**
	 * get the current motion of the typhoon
	 * @return {object{dir,sp}} a force object containing direction and speed of the typhoon
	 */
	Enemy.prototype.getMotion = function(){
		return {
			dir:this.direction,
			sp:this.speed
		};
	};
	/**
	 * Sets the motion of the typhoon, ignoring  previous values
	 * @param {number} dir Direction of typhoon
	 * @param {number} sp  Speed of typhoon
	 */
	Enemy.prototype.setMotion = function(dir,sp){
		this.direction = dir;
		this.speed = sp;
		this.max_hp += (10-sp)*10;
		if(this.max_hp>Config.maxTyphoonHP)
			this.max_hp = Config.maxTyphoonHP;
		this.hp = this.max_hp;

	};
	/**
	 * alters the motion by a little bit
	 * @param {number} force_dir       direction of force applied
{'fraction_non_alphanumeric': 0.14948096885813147, 'fraction_numerical': 0.012802768166089966, 'mean_word_length': 3.453775038520801, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, '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': '4693336', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2158, 'n_words': 418}