﻿var alert = 0;

//! ヘリ
var Heli = Class.create();
Heli.prototype = {
	initialize : function() {
		this.x      = 0;		// 座標（経度）
		this.y      = 0;		// 座標（緯度）
		this.vx     = 0;		// 速度（経度方向）
		this.vy     = 0;		// 速度（緯度方向）
		this.speed  = 0;		// 速度（キー入力上）
		this.kangle = 0;		// 進行方向（キー入力上）
		this.rangle = 0;		// 進行方向（実際）
		this.prop   = 0;		// プロペラ角
		this.clip   = true;		// クリップしたかどうか
		this.elem   = null;		// キャラのエレメント
		this.kill   = false;	// タスクkill要求
		this.maxFwSpeed =  40;	// 前方最高速
		this.maxBwSpeed = -10;	// 後方最高速
	},

	move : function() {
		// 移動
		this.vx = -this.speed * sin16(this.rangle) * 0.000001;
		this.vy =  this.speed * cos16(this.rangle) * 0.000001;
		this.x += this.vx;
		this.y += this.vy;
		this.prop = (this.prop + 1) & 3;
	},

	render : function() {
		var sx = toScreenX(this.x);
		var sy = toScreenY(this.y);
		var icon = (this.rangle * 4 + this.prop) * -16;
		var clip = isClip(sx, sy);
		if (this.clip && !clip) Element.show(this.elem);
		if (!this.clip && clip) Element.hide(this.elem);
		this.clip = clip;
		var e = this.elem;
		e.childNodes[0].style.left = icon + "px";
		e.style.left = (sx-8) + "px";
		e.style.top  = (sy-8) + "px";
	},

	throttle : function(dst, acc) {
		if (this.speed < dst) this.speed += acc;
		else if (this.speed > dst) this.speed -= acc;
	}
};

//! 自ヘリ
var MyHeli = Class.create();
MyHeli.prototype = (new Heli).extend({
	initialize : function() {
		this.bombX  = 0;
		this.bombY  = 0;
		this.bombZ  = -1;
		this.bombVX = 0;
		this.bombVY = 0;
		this.bomb   = 0;
	},

	update : function() {
		// 加減速
		if (pad.lev & pad.UP)   this.speed += 2;
		if (pad.lev & pad.DOWN) this.speed -= 2;
		if (this.speed < 0) this.speed++;
		this.speed = this.speed < this.maxBwSpeed ? this.maxBwSpeed : this.speed > this.maxFwSpeed ? this.maxFwSpeed : this.speed;
		// 旋回
		if (pad.lev & pad.RIGHT) this.kangle--;
		if (pad.lev & pad.LEFT)  this.kangle++;
		this.kangle = (this.kangle >= 48 ? this.kangle - 48 : this.kangle < 0 ? this.kangle + 48 : this.kangle);
		this.rangle = Math.floor((this.kangle + 1) / 3);
		this.rangle = norm16(this.rangle);
		// 弾発射
		if (pad.trg & pad.A) shot.start(this);
		// 爆弾投下
		if ((pad.trg & pad.B) && this.bombZ < 0) {
		 	this.bombX  = this.x;
			this.bombY  = this.y;
		 	this.bombVX = this.vx;
			this.bombVY = this.vy;
			this.bombZ = 10;
		}
		// 工場追加
		if (pad.trg & pad.ONE)   this.addFactory(1);
		if (pad.trg & pad.TWO)   this.addFactory(2);
		if (pad.trg & pad.THREE) this.addFactory(3);
		if (pad.trg & pad.FOUR)  this.addFactory(4);
		// 挙動
		this.move();
		// 敵機との当たり判定
		for (var i = 0; i < enemTask.work.length; i++) {
			var e = enemTask.work[i];
	        var dx = e.x - this.x;
	        var dy = e.y - this.y;
			if (Math.abs(dx) <= 0.00012 && 
			    Math.abs(dy) <= 0.00012) {
				explode(e.x, e.y, 100);
				e.start();
				continue;
			}
	    }
		// 爆弾挙動
		if (this.bombZ >= 0) {
			this.bombX += this.bombVX;
			this.bombY += this.bombVY;
			if (this.bombZ-- == 0) {
				explode(this.bombX, this.bombY, 80);
				var m = area.getMarker();
				for(var i = m.length-1; i >= 0; i--) {
					var dx = Math.abs(m[i].x - this.bombX);
					var dy = Math.abs(m[i].y - this.bombY)
					if (dx < 0.00020 && dy < 0.00020) {
						m[i].damaged();
						if (m[i].hp <= 0) {
							area.remove(m[i]);
						}
						var r = Math.random() * 16;
						explode(this.bombX + sin16(r)*0.00007, this.bombY + cos16(r)*0.00007, 80);
						r = Math.random() * 16;
						explode(this.bombX + sin16(r)*0.00007, this.bombY + cos16(r)*0.00007, 80);
						break;
					}
				}
			}
		}
	},

	addFactory : function(kind) {
		area.add(this.x, this.y, kind, 10, $F('name'), $F('text'));
	}
});

//! 敵ヘリ（雑魚）
var E1Heli = Class.create();
E1Heli.prototype = (new Heli).extend({
	// カメラから見えない場所でスタート
	start : function() {
		Element.hide(this.elem);
		var r = Math.floor(Math.random() * 16);
		this.dist = span.width;
		this.x = heli0.x - sin16(r) * this.dist * 1;
		this.y = heli0.y + cos16(r) * this.dist * 1;
		this.mode = "far";
		this.kangle = Math.floor(Math.random() * 16);
	},

	far : function() {
		// 適当に旋回
		if (!alert) {
			if (Math.random() < 0.5) this.kangle++;
			this.throttle(20, 1);
			if (this.dist > span.width * 1.5) this.start();
			if (this.dist < span.width * 0.5) this.mode = 'find';
		}
		// 一直線
		else {
			this.throttle(40, 1);
			var rd = diff16(atan16(-this.dx, this.dy), this.rangle);
			if (rd > 0) this.kangle++;
			else if (rd < 0) this.kangle--;
		}
	},

	// 発見したので近づく
	find : function() {
		var rd = diff16(atan16(-this.dx, this.dy), this.rangle);
		if (rd > 0) this.kangle++;
		else if (rd < 0) this.kangle--;
		var ad = (rd >= 0) ? rd : -rd;
		if (this.dist > span.width) this.mode = "find";
		if (ad < 3) {
			this.throttle(30, 1);
			if (this.dist < span.width * 0.25) this.mode = "nearmiss";
		}
		else this.throttle(20, 1);

	},

	// ぶつかりそうなので避ける
	nearmiss : function() {
		var rd = diff16(atan16(-this.dx, this.dy), this.rangle);
		if (rd >= 0) this.kangle--;
		else if (rd < 0) this.kangle++;
		var ad = (rd >= 0) ? rd : -rd;
		if (ad > 2 && this.dist > span.width * 0.25) this.mode = "find";
		this.throttle(20, 1);
	},

	update : function() {
		this.dx = heli0.x - this.x;
		this.dy = heli0.y - this.y;
		this.dist = Math.sqrt(this.dx*this.dx + this.dy*this.dy);
		switch(this.mode) {
			default:
			case "far":		this.far();		break;
			case "find":	this.find();	break;
			case "nearmiss":this.nearmiss();break;
		}
		this.kangle = (this.kangle >= 80 ? this.kangle - 80 : this.kangle < 0 ? this.kangle + 80 : this.kangle);
		this.rangle = Math.floor((this.kangle + 2) / 5);
		this.rangle = norm16(this.rangle);
		this.speed = this.speed < this.maxBwSpeed ? this.maxBwSpeed : this.speed > this.maxFwSpeed ? this.maxFwSpeed : this.speed;

		this.move();
	}
});

function shout() {
	var text = $F('mic');
	if (text == "") return;
	alert = 300;
	$('voice').innerHTML = text;
	//$('mic').value = '';
	message.print("非常事態発生！！");
}
function voice() {
	$("voice").style.left = (-300*8 + 256 + alert*8) + "px";
}
