let O_type = 1;

task TShot{
	let playerPower;
	loop{
		playerPower = truncate(min(GetPlayerPower(),400)/100);
		alternative(playerPower)
		case(1){ShotPower1;}
		case(2){ShotPower2;}
		case(3){ShotPower3;}
		case(4){ShotPower4;}
		while(GetPlayerState != STATE_NORMAL){
			yield;
		}
	}
}

function init_laser{
	let obj = ObjShot_Create(OBJ_STRAIGHT_LASER);
	ObjStLaser_SetAngle(obj, 270);
	ObjLaser_SetLength(obj, 0);
	ObjLaser_SetRenderWidth(obj, 15);
	ObjLaser_SetIntersectionWidth(obj, 15);
	ObjShot_SetGraphic(obj, 3);
	ObjLaser_SetInvalidLength(obj, 0, 0);
	return obj
}

//レーザー
task TLaser(objOpt, obj, Damage){
	ObjShot_SetDamage(obj, Damage);
	ObjShot_Regist(obj);

	LaserEffect(objOpt);
	LaserFlash(objOpt);
	while(!Obj_IsDeleted(obj) && ObjLaser_GetLength(obj) < 480){
		ObjMove_SetPosition(obj, ObjRender_GetX(objOpt), ObjRender_GetY(objOpt));
		ObjLaser_SetLength(obj, ObjLaser_GetLength(obj)+ 24);
		yield;
	}
	while(!Obj_IsDeleted(obj)){
		ObjMove_SetPosition(obj, ObjRender_GetX(objOpt), ObjRender_GetY(objOpt));
		yield;
	}


	task LaserEffect(objOpt){
		let objEfc = ObjPrim_Create(OBJ_SPRITE_2D);
		let RectX = 0;
		ObjPrim_SetTexture(objEfc, Texture);
		Obj_SetRenderPriority(objEfc, 0.28);
		ObjRender_SetBlendType(objEfc, BLEND_ALPHA);

		let Rect = 512;
		let L_Length = 0;
		while(!Obj_IsDeleted(obj) && ObjLaser_GetLength(obj) < 480){
			 L_Length = ObjLaser_GetLength(obj);
			
			ObjSprite2D_SetSourceRect(objEfc, 24*RectX , Rect, 24*(RectX+1), Rect+ 512*ObjLaser_GetLength(obj)/480);
			ObjSprite2D_SetDestRect(objEfc,-10, -L_Length, 10, 0);
			ObjRender_SetPosition(objEfc, ObjRender_GetX(objOpt), ObjRender_GetY(objOpt), 0);
			Rect += 12;
			if(Rect > 512){Rect -= 512;}
			yield;
		}

		while(!Obj_IsDeleted(obj)){
			ObjSprite2D_SetSourceRect(objEfc, 24*RectX , Rect, 24*(RectX+1), Rect+ 512*ObjLaser_GetLength(obj)/480);
			ObjSprite2D_SetDestRect(objEfc,-10, -480, 10, 0);
			ObjRender_SetPosition(objEfc, ObjRender_GetX(objOpt), ObjRender_GetY(objOpt), 0);
			Rect += 12;
			if(Rect > 512){Rect -= 512;}
			yield;
		}
		descent(i in 1 .. 10){
			L_Length += 24;
			ObjSprite2D_SetSourceRect(objEfc, 24*RectX , Rect, 24*(RectX+1), Rect+ 512*ObjLaser_GetLength(obj)/480);
			ObjSprite2D_SetDestRect(objEfc,-i, -L_Length, i, 0);
			ObjRender_SetPosition(objEfc, ObjRender_GetX(objOpt), ObjRender_GetY(objOpt), 0);
			Rect += 12;
			if(Rect > 512){Rect -= 512;}
			yield;
		}
		Obj_Delete(objEfc);
	}

	task LaserFlash(objOpt){
		let objEfc = ObjPrim_Create(OBJ_SPRITE_2D);
		let angleY = 90;
		let angleZ = 0;
		ObjPrim_SetTexture(objEfc, Texture);
		Obj_SetRenderPriority(objEfc, 0.29);
		ObjRender_SetBlendType(objEfc, BLEND_ADD_RGB);
		ObjSprite2D_SetSourceRect(objEfc, 32, 0, 64, 32);
		ObjSprite2D_SetDestRect(objEfc,-12,-12, 12, 12);

		while(!Obj_IsDeleted(obj) && angleY > 0){
			ObjRender_SetPosition(objEfc, ObjRender_GetX(objOpt), ObjRender_GetY(objOpt), 0);
			ObjRender_SetAngleY(objEfc, angleY);
			ObjRender_SetAngleZ(objEfc, angleZ);
			angleY -= 10;
			angleZ += 48;
			if(angleZ > 360){angleZ -= 360;}
			yield;
		}
		ObjRender_SetAngleY(objEfc, 0);
		while(!Obj_IsDeleted(obj)){
			ObjRender_SetPosition(objEfc, ObjRender_GetX(objOpt), ObjRender_GetY(objOpt), 0);
			ObjRender_SetAngleZ(objEfc, angleZ);
			angleZ += 48;
			if(angleZ > 360){angleZ -= 360;}
			yield;
		}
		while(angleY < 90){
			ObjRender_SetPosition(objEfc, ObjRender_GetX(objOpt), ObjRender_GetY(objOpt), 0);
			ObjRender_SetAngleY(objEfc, angleY);
			ObjRender_SetAngleZ(objEfc, angleZ);
			angleY += 10;
			angleZ += 48;
			if(angleZ > 360){angleZ -= 360;}
			yield;
		}
		Obj_Delete(objEfc);
	}
}

let OptionDelete = false;

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;
	}
}

task OptionB(obj, r, r2, angle){
	let s_a = 0;
	let rr = (r-r2)/2;
	let cr = (r+r2)/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);
		}
		r = cr+rr*cos(36*a);
		s_a += 4;
		let scale = 1.1+0.1*cos(s_a);
		ObjRender_SetScaleX(obj, scale);
		ObjRender_SetScaleY(obj, scale);
		ObjRender_SetX(obj, GetPlayerX + r*cos(angle));
		ObjRender_SetY(obj, GetPlayerY+20 + r/4*sin(angle));
		angle += 4;
		yield;
	}
}

//パワー100〜199の時のショット
sub ShotPower1{
	let optLength = 1;
	loop(optLength){
 		objOpt = objOpt ~ [Option(O_type)];
	}
	ascent(i in 0 .. optLength){
		OptionA(objOpt[i], 0, 20, 0, 20);
	}

	let shotCount = -1;
	let objLaser = [];

	let playerPower = optLength;
	while(playerPower == optLength){
		if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD)
		 && GetPlayerState() == STATE_NORMAL && IsPermitPlayerShot && !IsPlayerSpellActive){
			shotCount++;
		}else{
			descent(i in 0 .. length(objLaser)){
				Obj_Delete(objLaser[i]);
			}
			objLaser = [];
			shotCount = -1;
		}
		if(shotCount == 0){
			loop(optLength){
 				objLaser = objLaser ~ [init_laser];
			}
			ascent(i in 0 .. optLength){
				TLaser(objOpt[i], objLaser[i], 0.60);
			}
		}
		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);
		}
		playerPower = truncate(GetPlayerPower/100);
		yield;
	}
	descent(i in 0 .. optLength){
		Obj_Delete(objOpt[i]);
	}
	descent(i in 0 .. length(objLaser)){
		Obj_Delete(objLaser[i]);
	}
	objOpt = [];
	objLaser = [];
}

//パワー200〜299の時のショット
sub ShotPower2{
	let optLength = 2;
	loop(optLength){
 		objOpt = objOpt ~ [Option(O_type)];
	}
	OptionA(objOpt[0], 45, 20, 15, 20);
	OptionA(objOpt[1], -45, 20, -15, 20);

	let shotCount = -1;
	let objLaser = [];

	let playerPower = optLength;
	while(playerPower == optLength){
		if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD)
		 && GetPlayerState() == STATE_NORMAL && IsPermitPlayerShot && !IsPlayerSpellActive){
			shotCount++;
		}else{
			descent(i in 0 .. length(objLaser)){
				Obj_Delete(objLaser[i]);
			}
			objLaser = [];
			shotCount = -1;
		}
		if(shotCount == 0){
			loop(optLength){
 				objLaser = objLaser ~ [init_laser];
			}
			ascent(i in 0 .. optLength){
				TLaser(objOpt[i], objLaser[i], 0.55);
			}
		}
		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);
		}
		playerPower = truncate(GetPlayerPower/100);
		yield;
	}
	descent(i in 0 .. optLength){
		Obj_Delete(objOpt[i]);
	}
	descent(i in 0 .. length(objLaser)){
		Obj_Delete(objLaser[i]);
	}
	objOpt = [];
	objLaser = [];
}

//パワー300〜399の時のショット
sub ShotPower3{
	let optLength = 3;
	loop(optLength){
 		objOpt = objOpt ~ [Option(O_type)];
	}
	ascent(i in 0 .. optLength){
		OptionB(objOpt[i], 45, 15, -90+360*i/optLength);
	}

	let shotCount = -1;
	let objLaser = [];

	let playerPower = optLength;
	while(playerPower == optLength){
		if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD)
		 && GetPlayerState() == STATE_NORMAL && IsPermitPlayerShot && !IsPlayerSpellActive){
			shotCount++;
		}else{
			descent(i in 0 .. length(objLaser)){
				Obj_Delete(objLaser[i]);
			}
			objLaser = [];
			shotCount = -1;
		}
		if(shotCount == 0){
			loop(optLength){
 				objLaser = objLaser ~ [init_laser];
			}
			ascent(i in 0 .. optLength){
				TLaser(objOpt[i], objLaser[i], 0.55);
			}
		}
		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);
		}
		playerPower = truncate(GetPlayerPower/100);
		yield;
	}
	descent(i in 0 .. optLength){
		Obj_Delete(objOpt[i]);
	}
	descent(i in 0 .. length(objLaser)){
		Obj_Delete(objLaser[i]);
	}
	objOpt = [];
	objLaser = [];
}

//パワー400(Max)の時のショット
sub ShotPower4{
	let optLength = 4;
	loop(optLength){
 		objOpt = objOpt ~ [Option(O_type)];
	}
	ascent(i in 0 .. optLength){
		OptionB(objOpt[i], 45, 15, -90+360*i/optLength);
	}

	let shotCount = -1;
	let objLaser = [];

	let playerPower = optLength;
	while(playerPower == optLength){
		if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD)
		 && GetPlayerState() == STATE_NORMAL && IsPermitPlayerShot && !IsPlayerSpellActive){
			shotCount++;
		}else{
			descent(i in 0 .. length(objLaser)){
				Obj_Delete(objLaser[i]);
			}
			objLaser = [];
			shotCount = -1;
		}
		if(shotCount == 0){
			loop(optLength){
 				objLaser = objLaser ~ [init_laser];
			}
			ascent(i in 0 .. optLength){
				TLaser(objOpt[i], objLaser[i], 0.55);
			}
		}
		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);
		}
		playerPower = truncate(GetPlayerPower/100);
		yield;
	}
	descent(i in 0 .. optLength){
		Obj_Delete(objOpt[i]);
	}
	descent(i in 0 .. length(objLaser)){
		Obj_Delete(objLaser[i]);
	}
	objOpt = [];
	objLaser = [];
}
