let O_type = 0;

let B_Damage = 0.5;
let S_Damage = 2.0;

task TShot{
	Shot;
}

task WaveShot(x, y, speed, angle, damage, b_damage, b_penet){
	let obj = ObjShot_Create(OBJ_SHOT);
	ObjMove_SetPosition(obj, x, y);
	ObjMove_SetSpeed(obj, speed);
	ObjMove_SetAngle(obj, angle);
	ObjShot_SetGraphic(obj, 4);
	ObjShot_SetDamage(obj, damage);
	ObjShot_SetPenetration(obj, 1);
	ObjShot_Regist(obj);

	while(!Obj_IsDeleted(obj)){
		x = ObjMove_GetX(obj);
		y = ObjMove_GetY(obj);
		yield;
	}
	if(!IsOut(x, y, 64)){
		Burst(x, y, angle, b_damage, b_penet);
	}
}

task Burst(x, y, angle, damage, penet){
	BurstEffect(x, y, angle, 5);
	yield;
	let obj = ObjShot_Create(OBJ_SHOT);
	ObjMove_SetPosition(obj, x, y);
	ObjMove_SetSpeed(obj, 0);
	ObjMove_SetAngle(obj, angle);
	ObjShot_SetGraphic(obj, 5);
	ObjShot_SetDamage(obj, damage);
	ObjShot_SetPenetration(obj, 999);
	ObjShot_Regist(obj);
	loop(penet){
		yield;
	}
	Obj_Delete(obj);
}

task BurstEffect(x, y, angle, time){
	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);
	ObjRender_SetAngleZ(obj, angle);
	ObjSprite2D_SetSourceRect(obj, 64, 0, 96, 32);
	ObjSprite2D_SetDestRect(obj, -12, -16, 12, 16);
	let alpha = 128;
	ObjRender_SetColor(obj, alpha, alpha, alpha);
	descent(i in 1 .. time+1){
		x += 4*cos(angle);
		y += 4*sin(angle);
		ObjRender_SetColor(obj, alpha, alpha, alpha);
		ObjRender_SetPosition(obj, x, y, 0);
		alpha = 128*i/time;
		yield;
	}
	Obj_Delete(obj);
}

function OptionShot_Nway(objOpt, way, speed, angle, arc, damage, b_damage, penet){
	let i = -(way-1)/2;
	loop(way){
		WaveShot(ObjRender_GetX(objOpt), ObjRender_GetY(objOpt), speed, angle + arc*i, damage, b_damage, penet);
		i++;
	}
}

task OptionA(obj, x, y, x2, y2){
	let s_a = 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;
		let scale = 1.1+0.1*cos(s_a);
		ObjRender_SetScaleX(obj, scale);
		ObjRender_SetScaleY(obj, scale);
		ObjRender_SetX(obj, GetPlayerX+cx+rx*cos(36*a));
		ObjRender_SetY(obj, GetPlayerY+cy+ry*cos(36*a));
		yield;
	}
}

//パワー400(Max)の時のショット
sub Shot{
	loop(4){
 		objOpt = objOpt ~ [Option(O_type)];
	}
	OptionA(objOpt[0], 40*cos(240), 40*sin(240), 20*cos(255), 20*sin(255));
	OptionA(objOpt[1], 40*cos(300), 40*sin(300), 20*cos(285), 20*sin(285));
	OptionA(objOpt[2], 40*cos(135), 40*sin(135), 20*cos(225), 20*sin(225));
	OptionA(objOpt[3], 40*cos(45), 40*sin(45), 20*cos(315), 20*sin(315));
	let angle = 270;
	let a_r = 0;
	let a_r2 = 0;
	let arc = 0;
	let arc2 = 0;
	let way = 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 = 8;
			a_r2 =4;
			arc = 16;
			arc2 =8;
			way = 2;
		}else{
			a_r = 170;
			a_r2 = 10;
			arc = 16;
			arc2 =16;
			way = 3;
		}
		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);

			OptionShot_Nway(objOpt[0], way, 20, 270 - a_r2, arc2, 0.5, 0.25, 2);
			OptionShot_Nway(objOpt[1], way, 20, 270 + a_r2, arc2, 0.5, 0.25, 2);
			OptionShot_Nway(objOpt[2], way, 20, 270 - a_r, arc, 0.5, 0.25, 2);
			OptionShot_Nway(objOpt[3], way, 20, 270 + a_r, arc, 0.5, 0.25, 2);
		}
		yield;
	}
	descent(i in 0 .. 4){
		Obj_Delete(objOpt[i]);
	}
	objOpt = [];
}
