
SceneNumMax=1;

StageBonusExp=50;
StageBonusMoney=25;
StageBonusMana=25;

StageTimeBorder=120;

loop
{
	alternative(SceneNum)

	SetStageData1;
	SetStageEnemy1;
	SetSceneChangecondition1;

	SetStage;
	StageMainTask;
	loop
	{
		if(SceneChange){break;}
		if(GetCommonData("StageClear",false)){break;}
		yield;
	}
	if(GetCommonData("StageClear",false)==false)
	{
	SceneChangeEffect;
	wait(60);
	SceneChange=false;
	SetCommonData("SceneChange",false);
	}
	SceneNum+=1;
	if(SceneNum==SceneNumMax){break;}
}

task SetStageData1
{
StagePlayerX=-208;
StagePlayerY=-208;

StagePlayerX+=11*32;
StagePlayerY+=35*32;

StageRow=22;
StageColumn=37;
StageObjectArray=[
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,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,-1,-1,0,
0,-1,-1,-1,-1,-1,-1,-1,0,1,1,1,1,1,0,-1,-1,-1,-1,-1,-1,0,
0,-1,-1,-1,-1,-1,-1,-1,0,1,1,1,1,1,0,-1,-1,-1,-1,-1,-1,0,
0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0
];


}

task SetSceneChangecondition1
{
	wait(120);
	loop
	{
		if(GetPlayerY<=21*32+16){break;}
		yield;
	}

	Alice(11*32,6*32);

	loop
	{
	if(GetCommonData("BossDefeat",false)){break;}
	yield;
	}

	if(SceneNum!=SceneNumMax-1)
	{
	SceneChange=true;
	SetCommonData("SceneChange",true);
	}
	else
	{
	SetCommonData("StageClear",true);
	}

}

task SetStageEnemy1
{

}

task Alice(abx,aby)
{
	let ex=abx+16;
	let ey=aby+16;
	let StopCount=0;

	while(ex-StagePlayerX<=-0 || ex-StagePlayerX>=GetStgFrameWidth || ey-StagePlayerY<=-0 || ey-StagePlayerY>=448+0)
	{
		if(SceneChange==true){return;}
		yield;
	}

	ascent(i in 9..14)
	{
	ascent(j in 21..22)
	{
	StageObjectArray[i+StageRow*j]=0;
	CreateMap(i,j,0);
	}
	ascent(j in 22..28)
	{
	StageObjectArray[i+StageRow*j]=-1;
	CreateMap(i,j,-1);
	}
	}

	let objEnemy=SetEnemyInfo(ex,ey,18000-17500*0,30,0,0,12,8,"img/alice.png");

	Obj_SetValue(objEnemy,"Knock_Right",0);
	Obj_SetValue(objEnemy,"Knock_Left",0);
	Obj_SetValue(objEnemy,"Knock_Up",0);
	Obj_SetValue(objEnemy,"Knock_Down",0);

	ObjMove_SetAngle(objEnemy,90);

	EnemyArray=EnemyArray~[objEnemy];
	let ArrayID=length(EnemyArray)-1;

	ObjMove_SetPosition(objEnemy, ex-StagePlayerX,ey-StagePlayerY);

	let Movespeed=0;
	let MoveAngle=GetAngleToPlayer(objEnemy);

	Pos;
	Obj_SetValue(objEnemy,"Appear",1);
	if(ex-StagePlayerX>=-32 && ex-StagePlayerX<=448+32 && ey-StagePlayerY>=0-32 && ey-StagePlayerY<=448+32)
	{
		loop
		{
			if(StagePlayerY<=11*32-192){break;}
			yield;
		}
		BossVictoryConditionEffect("アリス");
		BGMStop=true;
		wait(120);
		BGMStop=false;
		Obj_PlayBGM(GetMainStgScriptDirectory~"bgm/boss.wav");
		Obj_SetValue(objEnemy,"Appear",0);
	}

	EnemyFinalizeTask;
	Shot;
	Move;


	while(Obj_GetValue(objEnemy, "Life")>10000){yield;}

	Movespeed=0;
	MoveAngle=GetAngleToPlayer(objEnemy);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	Obj_SetValue(objEnemy, "IntersectionShot", 0);
	Obj_SetValue(objEnemy, "IntersectionPlayer", 0);
	SpellCardEffect(objEnemy,"騎士「ドールオブラウンドテーブル」");
	wait(150);
	Obj_SetValue(objEnemy, "IntersectionShot", 12);
	Obj_SetValue(objEnemy, "IntersectionPlayer", 8);

	Shot2;

	task Shot2
	{
		let DollTime=150;
		let DollSpeed=3.5;
		loop
		{
		if(Obj_GetValue(objEnemy, "Life")<=0){break;}
		if(Obj_IsDeleted(objEnemy)){break;}
		let ra=rand(0,360);
		ascent(i in 0..12)
		{
		Doll(ra+i*30);
		}
		wait(60);
		if(Obj_GetValue(objEnemy, "Life")<=0){break;}
		let Cx=StagePlayerX-192+ObjRender_GetX(objEnemy);
		let Cy=StagePlayerY-192+ObjRender_GetY(objEnemy);
		let ax;
		let ay;
		let dis;
		loop
		{
			ax=rand(5,16)*32+16-192;
			ay=rand(5,16)*32+16-192;
			dis=((Cy-ay)^2+(Cx-ax)^2)^0.5;
			if(dis<=180){break;}
		}
		Movespeed=dis/60;
		MoveAngle=atan2(ay-Cy,ax-Cx);
		ObjMove_SetAngle(objEnemy,MoveAngle);
		wait(60);
		if(Obj_GetValue(objEnemy, "Life")<=0){break;}
		Movespeed=0;
		MoveAngle=GetAngleToPlayer(objEnemy);
		ObjMove_SetAngle(objEnemy,MoveAngle);
		wait(DollTime-30);
		}

		task Doll(angle)
		{
		let obj=SetDollInfo(ex,ey,500,0,0,0,12,8,"img/char_th002.png");

		ObjRender_SetX(obj,ObjRender_GetX(objEnemy));
		ObjRender_SetY(obj,ObjRender_GetY(objEnemy));

		let speed=DollSpeed;
		let time=DollTime;
		ascent(i in 0..6)
		{
		Blade(i*60);
		}
		String;
		loop(time)
		{
			if(Obj_GetValue(objEnemy, "Life")<=0){break;}
			if(Obj_IsDeleted(objEnemy)){break;}
			MoveByPlayer(obj);
			ObjRender_SetX(obj,ObjRender_GetX(obj)+speed*cos(angle));
			ObjRender_SetY(obj,ObjRender_GetY(obj)+speed*sin(angle));
			speed-=DollSpeed/time;
			yield;
		}
		angle=atan2(ObjRender_GetY(objEnemy)-ObjRender_GetY(obj),ObjRender_GetX(objEnemy)-ObjRender_GetX(obj));
		let distance=((ObjRender_GetY(objEnemy)-ObjRender_GetY(obj))^2+(ObjRender_GetX(objEnemy)-ObjRender_GetX(obj))^2)^0.5;
		loop(time)
		{
			if(Obj_GetValue(objEnemy, "Life")<=0){break;}
			if(Obj_IsDeleted(objEnemy)){break;}
			MoveByPlayer(obj);
			ObjRender_SetX(obj,ObjRender_GetX(obj)+speed*cos(angle));
			ObjRender_SetY(obj,ObjRender_GetY(obj)+speed*sin(angle));
			speed+=(distance/(time/2))/time;
		yield;
		}
		Obj_Delete(obj);
	task Blade(Bangle)
	{
	let objShot = ObjShot_Create(OBJ_STRAIGHT_LASER);
	ObjMove_SetX(objShot, ObjRender_GetX(obj));//x座標設定
	ObjMove_SetY(objShot, ObjRender_GetY(obj));//y座標設定;
	//ObjMove_SetSpeed(objShot, speed);//速度設定
	//ObjMove_SetAngle(objShot, angle);
	ObjStLaser_SetAngle(objShot, Bangle);
	ObjShot_SetGraphic(objShot, DS_BALL_S_BLUE);//画像設定
	ObjShot_SetAutoDelete(objShot,false);
	ObjShot_SetSpellResist(objShot,true);
	ObjShot_Regist(objShot);

	let leng=40;
	ObjLaser_SetLength(objShot,leng);
	//ObjLaser_SetInvalidLength(objShot,4,4);
	ObjLaser_SetRenderWidth(objShot,6);

	
	while(!Obj_IsDeleted(obj))
	{
		leng=((36*cos(Bangle))^2+(18*sin(Bangle))^2)^0.5;
		let aBangle=atan2(18*sin(Bangle),36*cos(Bangle));
		ObjLaser_SetLength(objShot,leng);
		ObjStLaser_SetAngle(objShot,aBangle);
		ObjMove_SetX(objShot, ObjRender_GetX(obj));//x座標設定
		ObjMove_SetY(objShot, ObjRender_GetY(obj));//y座標設定;
		Bangle+=9;
		yield;
	}
	Obj_Delete(objShot);

	}

	task String
	{
		let imgWhite = GetCurrentScriptDirectory() ~ "../img/White.png";
		let obje = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjRender_SetX(obje, ObjRender_GetX(obj));//x座標設定
		ObjRender_SetY(obje, ObjRender_GetY(obj));//y座標設定;
		ObjPrim_SetTexture(obje, imgWhite);
		ObjPrim_SetPrimitiveType(obje, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obje, 4);
		ObjRender_SetBlendType(obje, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obje,20);
		ObjRender_SetAngleZ(obje,0);

		ObjPrim_SetVertexPosition(obje, 0, 0, -64, 0);
		ObjPrim_SetVertexPosition(obje, 1, 100, -64, 0);
		ObjPrim_SetVertexPosition(obje, 2, 100, 64, 0);
		ObjPrim_SetVertexPosition(obje, 3,0, 64, 0);

		ObjPrim_SetVertexUVT(obje, 0, 0, 0);
		ObjPrim_SetVertexUVT(obje, 1, 128, 0);
		ObjPrim_SetVertexUVT(obje, 2, 128, 128);
		ObjPrim_SetVertexUVT(obje, 3, 0, 128);

		ObjRender_SetColor(obje,128/8,128/8,255/8);

		while(!Obj_IsDeleted(obj))
		{
		let leng=((ObjRender_GetY(objEnemy)-ObjRender_GetY(obj))^2+(ObjRender_GetX(objEnemy)-ObjRender_GetX(obj))^2)^0.5;
		ObjPrim_SetVertexPosition(obje, 0, 0, -1, 0);
		ObjPrim_SetVertexPosition(obje, 1, leng, -1, 0);
		ObjPrim_SetVertexPosition(obje, 2, leng, 1, 0);
		ObjPrim_SetVertexPosition(obje, 3,0, 1, 0);
		ObjRender_SetAngleZ(obje,atan2(ObjRender_GetY(objEnemy)-ObjRender_GetY(obje),ObjRender_GetX(objEnemy)-ObjRender_GetX(obje)));
		ObjRender_SetX(obje, ObjRender_GetX(obj));//x座標設定
		ObjRender_SetY(obje, ObjRender_GetY(obj));//y座標設定;
		yield;
		}
		Obj_Delete(obje);
	}

		}



	}

	task Shot
	{
		ShotA;
		wait(360);
		if(Obj_GetValue(objEnemy, "Life")<=10000){return;}
		Movespeed=0.5;
		MoveAngle=90;
		ObjMove_SetAngle(objEnemy,90);
		ShotB;
		loop(240)
		{
		if(Obj_GetValue(objEnemy, "Life")<=10000){return;}
		yield;
		}

		Movespeed=0;
		wait(120);
		ShotC;
		wait(360);
		loop{
		ShotA;
		wait(360);
		if(Obj_GetValue(objEnemy, "Life")<=10000){break;}
		Movespeed=0.5;
		MoveAngle=GetAngleToPlayer(objEnemy);
		ObjMove_SetAngle(objEnemy,GetAngleToPlayer(objEnemy));
		ShotB;
		loop(240)
		{
		if(Obj_GetValue(objEnemy, "Life")<=10000)
		{
			return;
		}
		yield;
		}
		Movespeed=0;
		wait(120);
		ShotC;
		wait(390);
		}

		task ShotA
		{
			loop(5)
			{
			if(Obj_GetValue(objEnemy, "Life")<=10000){break;}
			ascent(i in -5..6)
			{
				ObjMove_SetAngle(objEnemy,GetAngleToPlayer(objEnemy));
				SP_CreateShotA1(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),1.5,GetAngleToPlayer(objEnemy)+i*5,DS_BALL_S_RED,0);
			}
			let ra=rand(0,360);
			ascent(i in 0..6)
			{
				SP_CreateShotA1(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),1.0,ra+i*60,DS_BALL_S_ORANGE,0);
			}
			wait(60);
			}
		}

		task ShotB
		{
			let dir=rand_int(0,1)*2-1;
			loop(4)
			{
			if(Obj_GetValue(objEnemy, "Life")<=10000){break;}
			let ra=rand(0,360);
			ascent(j in 0..15)
			{
			if(Obj_GetValue(objEnemy, "Life")<=10000){break;}
			ascent(i in 0..5)
			{
				SP_CreateShotA1(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),1.0+j*0.175,ra+i*72,DS_BALL_S_BLUE,0);
			}
			ra+=2*dir;
			wait(4);
			}
			wait(30);
			dir*=-1;
			}
		}

		task ShotC
		{
			loop(120)
			{
			if(Obj_GetValue(objEnemy, "Life")<=10000){break;}
				ObjMove_SetAngle(objEnemy,GetAngleToPlayer(objEnemy));
				SP_CreateShotA1(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),rand(0.5,2.5),rand(0,360),DS_BALL_S_YELLOW,0);
				wait(3);
			}
		}
	}

	task Pos
	{
		while(!Obj_IsDeleted(objEnemy))
		{
		//	ObjMove_SetAngle(objEnemy,GetAngleToPlayer(objEnemy));
			if(Obj_GetValue(objEnemy,"Knock_Right")!=0)
			{
				ex+=5;
				Obj_SetValue(objEnemy,"Knock_Right",0);
			}
			if(Obj_GetValue(objEnemy,"Knock_Left")!=0)
			{
				ex-=5;
				Obj_SetValue(objEnemy,"Knock_Left",0);
			}
			if(Obj_GetValue(objEnemy,"Knock_Up")!=0)
			{
				ey-=5;
				Obj_SetValue(objEnemy,"Knock_Up",0);
			}
			if(Obj_GetValue(objEnemy,"Knock_Down")!=0)
			{
				ey+=5;
				Obj_SetValue(objEnemy,"Knock_Down",0);
			}
			ObjMove_SetPosition(objEnemy, ex-StagePlayerX,ey-StagePlayerY);
			yield;
		}

	}
	task Move
	{
		let MoveCount=0;
		let MoveStop=0;
		while(!Obj_IsDeleted(objEnemy))
		{
			MoveByPlayer(objEnemy);
		//	ObjMove_SetAngle(objEnemy,MoveAngle);
		//	ObjMove_SetSpeed(objEnemy,Movespeed);
			ex+=Movespeed*cos(MoveAngle);
			ey+=Movespeed*sin(MoveAngle);
			MoveCount+=1;
			if(GetCommonData("StageClear",false)){break;}
			if(StopCount>0){StopCount-=1;}
		//	ObjRender_SetAlpha(objEnemy,255-StopCount/60*255);
			yield;
		}
	}

	task EnemyFinalizeTask
	{
//	while( ( Obj_GetValue(objEnemy, "Vanished")==0 && ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0 ) && SceneChange==false){yield;}
	while( ( Obj_GetValue(objEnemy, "Vanished")==0 && Obj_GetValue(objEnemy, "Life")>0) && SceneChange==false){yield;}
	yield;
	SetCommonData("SpellEndFlag",true);
	Obj_SetValue(objEnemy, "IntersectionShot", 0);
	Obj_SetValue(objEnemy, "IntersectionPlayer", 0);
	BossDefeatEffect(objEnemy);
	Movespeed=0;
	ascent(i in 0..90)
	{
		ObjRender_SetAlpha(objEnemy,255-i*255/90);
		yield;
	}
		if(TreasureDropProbability(4))
		{
		if(UniqueCarryCheck("初等魔法書")){break;}
		DrawGetItem;
		StageTreasure=StageTreasure~[["初等魔法書"]];
		}
		wait(60);
		SetCommonData("BossDefeat",true);
		Obj_Delete(objEnemy);

	}
}

function SetDollInfo(x,y,life,reduce,cut,C_Resist,IRA,IRB,imgpath)
{
	//#include".\lib\lib_anime_fairy.txt"

	//let imgpath = GetCurrentScriptDirectory() ~ "img/v8-fairy01.png";

	let obj = ObjEnemy_Create(OBJ_ENEMY);
//	ObjMove_SetPosition(obj, x-StagePlayerX, y-StagePlayerY);
	Obj_SetValue(obj, "Vanished", 0);
	Obj_SetValue(obj, "Life", life);
	Obj_SetValue(obj, "MaxLife", life);
	Obj_SetValue(obj, "DrawDamage", 0);
	Obj_SetValue(obj, "DrawCount", 0);
	Obj_SetValue(obj, "Defence", reduce);
	Obj_SetValue(obj, "Resist", cut);
	Obj_SetValue(obj, "C_Resist", C_Resist);

	ObjEnemy_SetLife(obj, life*10);
	Obj_SetValue(obj, "IntersectionShot", IRA);
	Obj_SetValue(obj, "IntersectionPlayer", IRB);
	Obj_SetValue(obj,"Appear",0);
	ObjEnemy_Regist(obj);

	EnemyMainTask(obj);

	TImage;

	EnemyClearTask;
	return (obj);

task EnemyClearTask
{
let alpha=255;
while(!GetCommonData("StageClear",false)){yield;}
loop(60)
{
ObjRender_SetAlpha(obj,alpha);
alpha-=255/60;
yield;
}
Obj_SetValue(obj, "Vanished", 1);
}


task TImage
{
	let OnMove=false;

	ObjPrim_SetTexture(obj, csd~imgpath);
	let imgFrameCount=0;
	let FrameStep=0;

	loop
	{
	ObjSprite2D_SetSourceRect(obj,72, 196+4, 96,228+4);
	Obj_SetRenderPriorityI(obj,30);
	ObjSprite2D_SetDestCenter(obj);
	yield;
	}
}

task EnemyMainTask(obj)
{
	while(!Obj_IsDeleted(obj))
	{
		if(Obj_GetValue(obj, "IntersectionShot")!=0 && Obj_GetValue(obj,"Appear")==0)
		{
			ObjEnemy_SetIntersectionCircleToShot(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),Obj_GetValue(obj, "IntersectionShot"));
		}
		if(Obj_GetValue(obj, "IntersectionPlayer")!=0 && Obj_GetValue(obj,"Appear")==0)
		{
			ObjEnemy_SetIntersectionCircleToPlayer(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),Obj_GetValue(obj, "IntersectionPlayer"));
		}
		if(GetCommonData("StageClear",false)){break;}
		yield;
	}
}

}