#include "./PlayerAnimation.dnh"
#include "./PlayerEffect.dnh"

let GetCenterX = GetStgFrameWidth()/2;
let GetCenterY = GetStgFrameHeight()/2;
let GetClipMinX = 0;
let GetClipMinY = 0;
let GetClipMaxX = GetStgFrameWidth();
let GetClipMaxY = GetStgFrameHeight();

let Texture = current ~ "./img/reimu_shots.png";
let TexSpirit = current ~ "./img/SpiritAttack.png";
let TexSpirit2 = current ~ "./img/Default_Player_MagicCircle.png";
let SE_GRAZE;
let SE_PDOWN;
let SE_CHARGE;
let SE_SENABLE;
let SE_PSHOT;
let SE_SPELL1;
let SE_SPELL2;
let SE_SPELL3;
let SE_SPELL4;

let charge = 0;
let spiritenable = false;
let SP_SPELL = 0;
let SP_USER = 1;

let OptionX = GetPlayerX();
let OptionY = GetPlayerY();

function LoadPlayerData{
	let path = current ~ "./lib/reimuShotData.dnh";
	LoadPlayerShotData(path); //自弾画像ロード
	LoadTexture(Texture);
	SE_GRAZE = _LoadSE(current ~ "./se/graze.wav");
	SE_PDOWN = _LoadSE(current ~ "./se/playerdown.wav");
	SE_CHARGE = _LoadSE(current ~ "./se/charge.wav");
	SE_SENABLE = _LoadSE(current ~ "./se/spiritEnable.wav");
	SE_PSHOT = _LoadSE(current ~ "./se/playershot.wav");
	SE_SPELL1 = _LoadSE(current ~ "./se/spellA1.wav");
	SE_SPELL2 = _LoadSE(current ~ "./se/spellA2.wav");
	SE_SPELL3 = _LoadSE(current ~ "./se/spellB1.wav");
	SE_SPELL4 = _LoadSE(current ~ "./se/spellB2.wav");

}

function _PlaySE(SE){
	ObjSound_SetVolumeRate(SE, Volume(GetAreaCommonData("OptionArea", "SE_VOLUME", 100)));
	ObjSound_Play(SE);
}

function _LoadSE(char){
	let obj = ObjSound_Create();
	ObjSound_Load(obj, char);
	ObjSound_SetSoundDivision(obj, SOUND_SE);
	return obj;
}

function _RemoveSE(obj){
	while(ObjSound_IsPlaying(obj)){yield;}
	Obj_Delete(obj);
}

function Volume(v){
	return 100-100*((100-v)/100)^2;
}


function init_system{
	SetPlayerLife(2);
	SetPlayerSpell(3);
	SetPlayerPower(100);
	SetPlayerDownStateFrame(60);
	SetPlayerRebirthFrame(20);
	SetPlayerRebirthLossFrame(0);
}

function main_system{
	let dx = GetPlayerX-OptionX;
	let dy = GetPlayerY-OptionY;
	OptionX += dx/2;
	OptionY += dy/2;
	if((GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
			&& GetPlayerState() == STATE_NORMAL){
		CollectItemsInCircle(GetPlayerX, GetPlayerY, 80);
	}
	yield;
}

function fin_system{
	_RemoveSE(SE_GRAZE);
	_RemoveSE(SE_PDOWN);
	_RemoveSE(SE_CHARGE);
	_RemoveSE(SE_SENABLE);
	_RemoveSE(SE_PSHOT);
	_RemoveSE(SE_SPELL1);
	_RemoveSE(SE_SPELL2);
	_RemoveSE(SE_SPELL3);
	_RemoveSE(SE_SPELL4);
}

function event_system{
	alternative(GetEventType())
	case(EV_REQUEST_SPELL){
		//スペルカード要求
		let spell = GetPlayerSpell();//残りスペル数
		if(spell >= 1){
			//通常ボム
			SetScriptResult(true);//スペル発動可能
			SetPlayerSpell(spell-1);//スペル数を1減らす
			if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
				TSpellB();//スペルタスク起動
			}else{
				TSpellA();//スペルタスク起動
			}
		}
		else{
			SetScriptResult(false);//スペル発動不可
		}
	}
	case(EV_HIT){
		//被弾
		_PlaySE(SE_PDOWN);
		TExplosion();
	}
	case(EV_PLAYER_SHOOTDOWN){
		//撃墜されたらpowerが100なくなる。
		//これをコメントアウトすると撃墜されてもパワーが減らなくなる
		SetPlayerPower(max(GetPlayerPower()-100, 100));
	}
	case(EV_PLAYER_REBIRTH){
		//復帰

		//残スペルが２未満なら補充
		if(GetPlayerSpell() < 3){
			SetPlayerSpell(3);
		}
		//パワーが100以下だったら復帰時に100に戻す
		//撃墜時にオプションを消すために入れてる処理
		if(GetPlayerPower < 100){
			SetPlayerPower(100);
		}
		//復帰後4秒は無敵
		SetPlayerInvincibilityFrame(240);
	}
	case(EV_GRAZE){
		//グレイズ通知
		_PlaySE(SE_GRAZE);
	}
	case(EV_GET_ITEM)
	{
		//アイテム取得通知
/*		let itemType = GetEventArgument(0);
		alternative(itemType)
		case(ITEM_1UP){
			_PlaySE(SE_EPLAYER);
			SetPlayerLife(GetPlayerLife()+1);
		}
		case(ITEM_SPELL){
			_PlaySE(SE_ESPELL);
			SetPlayerSpell(GetPlayerSpell()+1);
		}*/
	}
}

//ホーミング弾に使う処理
//RAINS3から引用させていただいてます
function Horming(toAngle, angle, dAngle){
	toAngle %= 360;
	angle %= 360;

	let ret = toAngle-angle;
	if(ret >= 180){ret -= 360;}//180°以上なら-180〜180内に補正
	if(ret < -180){ret += 360;}//-180°未満なら-180〜180内に補正

	if((|ret|) < dAngle){
		angle = toAngle;
	}else{
		if(sin(ret) > 0){
			angle += dAngle;
		}else{
			angle -= dAngle;
		}
	}
	if(angle >= 360){angle -= 360;}
	if(angle < 0){angle += 360;}

	return angle;
}

//指定位置が画面外から距離ｄ離れているかを調べる関数(boorean型)
function IsOut(x, y, d){
	if(absolute(GetCenterX-x) < (GetCenterX+d) &&
		absolute(GetCenterY-y) < (GetCenterY+d)){
		return false;
	}else{return true;}
}

//指定位置が自機から距離ｄ内かを調べる関数(boorean型)
function InCurcleToPlayer(x, y, d){
	if((GetPlayerX-x)^2 + (GetPlayerY-y)^2 < d^2){
		return true;
	}else{return false;}
}

//指定位置から自機までの距離を返す関数(real型)
function GetDistanceToPlayer(x, y){
	let d = ((GetPlayerX-x)^2 + (GetPlayerY-y)^2)^0.5;
	return d;
}

//指定位置から自機までの距離の２乗を返す関数(real型)
function GetDistanceToPlayer2(x, y){
	let d = (GetPlayerX-x)^2 + (GetPlayerY-y)^2;
	return d;
}
