let S_Damage = 5.0;
let O_type = 2.0;

let enemyCount;


task TShot{
	EnemyCount;
	ShotPower4;
}

task EnemyCount{
	loop{
		enemyCount = length(GetIntersectionRegistedEnemyID());
		yield;
	}
}

//お守り
task TAmulet(x, y, speed, angle, a_rate, Damage){
	if(enemyCount > 0){
		let Epos = GetEnemyIntersectionPosition(x, y, 1);
		angle = Horming(atan2(Epos[0][1]-y, Epos[0][0]-x), angle, 180);
	}
	let obj = ObjShot_Create(OBJ_SHOT);
	ObjMove_SetPosition(obj, x, y);
//	ObjMove_SetSpeed(obj, speed);
	ObjMove_SetAngle(obj, angle);
	ObjShot_SetGraphic(obj, 3);
	ObjShot_SetDamage(obj, Damage);
	ObjShot_SetPenetration(obj, 1);
	ObjShot_SetAutoDelete(obj, false);
	ObjShot_Regist(obj);
	let R_angle = 0;

	ascent(i in 0 .. 30){
		if(Obj_IsDeleted(obj)){break;}
		if(IsOut(x, y, 40)){Obj_Delete(obj);	return;}
		x = ObjMove_GetX(obj);
		y = ObjMove_GetY(obj);
		if(enemyCount > 0){
			let Epos = GetEnemyIntersectionPosition(x, y, 1);
			angle = Horming(atan2(Epos[0][1]-y, Epos[0][0]-x), angle, a_rate*i/30);
			ObjMove_SetAngle(obj, angle);
		}
		ObjMove_SetSpeed(obj, speed*i/30);
		ObjRender_SetAngleZ(obj, R_angle);
		R_angle += 12;
		yield;
	}
	ObjMove_SetSpeed(obj, speed);
	while(!Obj_IsDeleted(obj)){
		if(IsOut(x, y, 40)){Obj_Delete(obj);	return;}
		x = ObjMove_GetX(obj);
		y = ObjMove_GetY(obj);
		if(enemyCount > 0){
			let Epos = GetEnemyIntersectionPosition(x, y, 1);
			angle = Horming(atan2(Epos[0][1]-y, Epos[0][0]-x), angle, a_rate);
			ObjMove_SetAngle(obj, angle);
		}
		ObjMove_SetSpeed(obj, ObjMove_GetSpeed(obj) + 0.02);
		ObjRender_SetAngleZ(obj, R_angle);
		R_angle += 12;
		yield;
	}
	if(!IsOut(x, y, 40)){
		AmuletEffect(x, y, R_angle+angle);
	}
}

//爆風

task AmuletEffect(x, y, angle){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, Texture);
	Obj_SetRenderPriority(obj, 0.45);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	ObjRender_SetPosition(obj, x, y, 0);
	ObjSprite2D_SetSourceRect(obj, 32, 0, 48, 16);
	ObjSprite2D_SetDestRect(obj, -8, -8, 8, 8);
	let R_angle = angle+90;
	ObjRender_SetAngleZ(obj, R_angle);
	let time = 10;
	descent(i in 1 .. time+1){
		let alpha = 63 * i/time;
		let scale = 1.0+0.5*(time-i)/time;
		ObjRender_SetColor(obj, alpha, alpha, alpha);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1.0);
		ObjRender_SetAngleZ(obj, R_angle);
		R_angle += 12;
		yield;
	}
	Obj_Delete(obj);
}

task OptionA(obj, x, y, x2, y2){
	let s_a = 0;
	let angle = 0;
	let rx = (x-x2)/2;
	let ry = (y-y2)/2;
	let cx = (x+x2)/2;
	let cy = (y+y2)/2;

	let a = 0;
	while(!Obj_IsDeleted(obj)){
		if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
			a = min(a+1,5);
		}else{
			a = max(a-1,0);
		}
		s_a += 4;
		ObjRender_SetAngleZ(obj, angle);
		ObjRender_SetX(obj, GetPlayerX+cx+rx*cos(36*a));
		ObjRender_SetY(obj, GetPlayerY+cy+ry*cos(36*a));
		angle += 2;
		yield;
	}
}

//パワー400(Max)の時のショット
sub ShotPower4{
	loop(4){
 		objOpt = objOpt ~ [Option(O_type)];
	}
	OptionA(objOpt[0], 40*cos(60), 40*sin(60), 20*cos(60), 20*sin(60));
	OptionA(objOpt[1], 40*cos(120), 40*sin(120), 20*cos(120), 20*sin(120));
	OptionA(objOpt[2], 40*cos(180), 40*sin(180), 20*cos(150), 20*sin(150));
	OptionA(objOpt[3], 40*cos(0), 40*sin(0), 20*cos(30), 20*sin(30));
	let angle = 270;
	let a_r = 0;
	let a_rate = 0;
	let speed = 0;
	let shotCount = -1;

	loop{
		if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD)
		 && GetPlayerState() == STATE_NORMAL && IsPermitPlayerShot){
			shotCount++;
		}else{
			shotCount = -1;
		}
		if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
			a_r = 0;
			a_rate = 9.0;
			speed = 6;
		}else{
			a_r = 20;
			a_rate = 6.0;
			speed = 8;
		}
		if(shotCount%3 == 1){
			_PlaySE(SE_PSHOT);
			CreatePlayerShotA1(GetPlayerX()+8, GetPlayerY(), 20, 270, 3, 1, 1);
			CreatePlayerShotA1(GetPlayerX()-8, GetPlayerY(), 20, 270, 3, 1, 1);
		}
		if(shotCount%10 == 6){
			TAmulet(ObjRender_GetX(objOpt[0]), ObjRender_GetY(objOpt[0]), speed, angle+a_r/2, a_rate, S_Damage);
			TAmulet(ObjRender_GetX(objOpt[1]), ObjRender_GetY(objOpt[1]), speed, angle-a_r/2, a_rate, S_Damage);
			TAmulet(ObjRender_GetX(objOpt[2]), ObjRender_GetY(objOpt[2]), speed, angle-a_r, a_rate, S_Damage);
			TAmulet(ObjRender_GetX(objOpt[3]), ObjRender_GetY(objOpt[3]), speed, angle+a_r, a_rate, S_Damage);
		}
		yield;
	}
	descent(i in 0 .. 4){
		Obj_Delete(objOpt[i]);
	}
	objOpt = [];
}
