#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID[Mitori]
#Title["Ooama no Ake no Mitori"]
#ReplayName["Mitori"]
#Text["The Throne Dyed in Vermillion"]
#Image["./img/Mitori.png"]

let GCSD = GetCurrentScriptDirectory;

let playerID = GetPlayerObjectID;
let playerObj = ObjPrim_Create(OBJ_SPRITE_2D);
let hitboxObj = ObjPrim_Create(OBJ_SPRITE_2D);

let count = 0;

let isFocus = false;
let isShot = false;
let isDead = false;

let CanShoot = true;

let TeamMember = 0;

let bullet = GCSD ~ "./shot/shootmitori.txt";
let sprite = GCSD ~ "./img/mitorisprite.png";

let hitbox = GCSD ~ "./img/snowflake.png";


@Initialize{
	LoadPlayerShotData(bullet);

	SetPlayerSpeed(5,2);
	
	ObjPlayer_AddIntersectionCircleA1(playerID, 0,0,2,16);
	
	SetPlayerClip(0+1, 0+1, GetStgFrameWidth-1, GetStgFrameHeight-1);
	
	SetPlayerDownStateFrame(120);
	
	SetPlayerRebirthFrame(30);
	
	SetPlayerAutoItemCollectLine(120);
	
	
	RenderPlayer;
	RenderHitbox;
	Shooting;
	TMagicCircle;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_SPELL){
		if(GetPlayerSpell > 0){
		SetScriptResult(true);
		SetPlayerSpell(GetPlayerSpell - 1);
		Spell;
		}
		else{
		SetScriptResult(false);
		}
	}
	case(EV_GRAZE){
		SE_Play(GCSD ~ "./se/graze.wav", 255);
	}
	case(EV_HIT)
	{
		SE_Play(GCSD ~ "./se/death.wav", 255);
		ObjRender_SetAlpha(playerObj, 0);
		Shockwave;
		Expand;
	} 
	case(EV_PLAYER_SHOOTDOWN){
		SetForbidPlayerShot(true);
		SetForbidPlayerSpell(true);
		ObjRender_SetAlpha(playerObj, 0);
		isDead = true;
		CanShoot = false;
	}
	case(EV_PLAYER_REBIRTH){
		SetForbidPlayerShot(false);
		SetForbidPlayerSpell(false);
		SetPlayerInvincibilityFrame(130);
		SetPlayerSpell(2);
		ObjRender_SetAlpha(playerObj, 255);
		isDead = false;
		CanShoot = true;
	}
}

@MainLoop{
if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD){
	isFocus = true;
}
if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PULL){
	isFocus = false;
}
if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD){
	isShot = true;
}
if(GetVirtualKeyState(VK_SHOT) == KEY_PULL){
	isShot = false;
}
yield;
}

task RenderPlayer{
while(!Obj_IsDeleted(hitboxObj)){

	let x = 30;
	let y = 50;
	let f = 0;
	let fl = 0;
	let fr = 0;
	let repeat = 0;
	let move = "idle";

	ObjPrim_SetTexture(playerObj, sprite);
	Obj_SetRenderPriorityI(playerObj, 31);
	ObjRender_SetBlendType(playerObj, BLEND_ALPHA);
	ObjRender_SetAngleXYZ(playerObj, 0,0,0);
	ObjRender_SetScaleXYZ(playerObj, 1,1,0);
	ObjRender_SetAlpha(playerObj, 255);
	ObjSprite2D_SetSourceRect(playerObj,0,0,x,y);
	ObjSprite2D_SetDestCenter(playerObj);
	ObjRender_SetPosition(playerObj, GetPlayerX, GetPlayerY, 0);
	while(!Obj_IsDeleted(playerObj)){
		ObjRender_SetPosition(playerObj, GetPlayerX, GetPlayerY, 0);
		
		let speed = ObjMove_GetSpeed(playerObj);
		
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){move = "left"}
		if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){move = "right"}
		
		if(move == "idle"){
		if(f<8){ObjSprite2D_SetSourceRect(playerObj,0,0,x,y);}
		if(f>=8 && f<16){ObjSprite2D_SetSourceRect(playerObj,x*1,0,x*2,y);}
		if(f>=16 && f<24){ObjSprite2D_SetSourceRect(playerObj,x*2,0,x*3,y);}
		if(f>=24 && f<32){ObjSprite2D_SetSourceRect(playerObj,x*3,0,x*4,y);}
		if(f>=32 && f<40){ObjSprite2D_SetSourceRect(playerObj,x*4,0,x*5,y);}
		if(f>=40 && f<48){ObjSprite2D_SetSourceRect(playerObj,x*5,0,x*6,y);}
		if(f>=48 && f<56){ObjSprite2D_SetSourceRect(playerObj,x*6,0,x*7,y);}
		if(f>=56 && f<64){ObjSprite2D_SetSourceRect(playerObj,x*7,0,x*8,y);}
		fl=0;
		fr=0;
		}
		
		if(move == "left"){fl++;
		if(fl<4){ObjSprite2D_SetSourceRect(playerObj,0,y,x,y*2);}
		if(fl>=4 && fl<8){ObjSprite2D_SetSourceRect(playerObj,x*1,y,x*2,y*2);}
		if(fl>=8 && fl<12){ObjSprite2D_SetSourceRect(playerObj,x*2,y,x*3,y*2);}
		if(fl>=12 && fl<16){ObjSprite2D_SetSourceRect(playerObj,x*3,y,x*4,y*2);}
		if(fl>=16 && fl<20){ObjSprite2D_SetSourceRect(playerObj,x*4,y,x*5,y*2);}
		if(fl>=20 && fl<24){ObjSprite2D_SetSourceRect(playerObj,x*5,y,x*6,y*2);}
		if(fl>=24 && fl<28){ObjSprite2D_SetSourceRect(playerObj,x*6,y,x*7,y*2);}
		if(fl>=28 && fl<32){ObjSprite2D_SetSourceRect(playerObj,x*7,y,x*8,y*2);}
		if(fl>32){fl-=12;}
		}		

		if(move == "right"){fr++;
		if(fr<4){ObjSprite2D_SetSourceRect(playerObj,0,y*2,x,y*3);}
		if(fr>=4 && fr<8){ObjSprite2D_SetSourceRect(playerObj,x*1,y*2,x*2,y*3);}
		if(fr>=8 && fr<12){ObjSprite2D_SetSourceRect(playerObj,x*2,y*2,x*3,y*3);}
		if(fr>=12 && fr<16){ObjSprite2D_SetSourceRect(playerObj,x*3,y*2,x*4,y*3);}
		if(fr>=16 && fr<20){ObjSprite2D_SetSourceRect(playerObj,x*4,y*2,x*5,y*3);}
		if(fr>=20 && fr<24){ObjSprite2D_SetSourceRect(playerObj,x*5,y*2,x*6,y*3);}
		if(fr>=24 && fr<28){ObjSprite2D_SetSourceRect(playerObj,x*6,y*2,x*7,y*3);}
		if(fr>=28 && fr<32){ObjSprite2D_SetSourceRect(playerObj,x*7,y*2,x*8,y*3);}
		if(fr>32){fr-=12;}
		}						
		
		f++;
		if(f>64){f=0;}
		move = "idle";
		yield;
	}
}
}

task RenderHitbox{
	let alpha = 0;
	let spin = 0;
	
	ObjPrim_SetTexture(hitboxObj, hitbox);
	ObjRender_SetBlendType(hitboxObj, BLEND_ALPHA);
	Obj_SetRenderPriorityI(hitboxObj, 50);
	ObjRender_SetAlpha(hitboxObj, 0);
	ObjRender_SetScaleXYZ(hitboxObj, 1,1,0);
	ObjSprite2D_SetSourceRect(hitboxObj, 0,0,70,70);
	ObjSprite2D_SetDestCenter(hitboxObj);
	
	while(!Obj_IsDeleted(hitboxObj)){
	while(isDead){yield; Obj_SetVisible(hitboxObj, 0);}
	Obj_SetVisible(hitboxObj, 1);
	ObjRender_SetPosition(hitboxObj, GetPlayerX, GetPlayerY, 0);
	ObjRender_SetAngleZ(hitboxObj, spin);
	ObjRender_SetAlpha(hitboxObj, alpha);
	if(isFocus){if(isDead==false){alpha = 255}}
	else{alpha=0;}
	spin++;
	yield;
	}
}

task Shooting{
let a = 0;
let b = 0;
	while(!Obj_IsDeleted(hitboxObj)){
		if(CanShoot){if(isShot){
		
				if(isFocus){
				let aa = CreatePlayerShotA1(GetPlayerX(), GetPlayerY(), 15, 270+7*sin(a*25), 4, 1000, 1);
				let ba = CreatePlayerShotA1(GetPlayerX(), GetPlayerY(), 15, 270+7*sin(a*25+180), 4, 1000, 2);
				ObjRender_SetAlpha(aa, 122);
				ObjRender_SetAlpha(ba, 122);
				}
				else{
				let aa = CreatePlayerShotA1(GetPlayerX(), GetPlayerY(), 15, 270+33*sin(a*25), 7, 1000, 1);
				let ba = CreatePlayerShotA1(GetPlayerX(), GetPlayerY(), 15, 270+33*sin(a*25+180), 7, 1000, 2);
				ObjRender_SetAlpha(aa, 122);
				ObjRender_SetAlpha(ba, 122);
				}
			b++;
			SE_Play(GCSD ~ "./se/shoot.wav", 100);
		}
	}
	a++;
	if(b == 4){
	b = 0;
	Arrows;
	}
	wait(5);
}

	task Arrows{
	let mult = 2;
	SE_Play(GCSD ~ "./se/shoot2.wav", 55);
		let a = CreatePlayerShotA1(GetPlayerX(), GetPlayerY(), 2*mult, 90, 7, 1000, 3);
		let b = CreatePlayerShotA1(GetPlayerX(), GetPlayerY(), 2*mult, 90, 7, 1000, 3);
		ObjMove_AddPatternA2(a, 0, NO_CHANGE, NO_CHANGE, 0.01, 2*mult, 10);
		ObjMove_AddPatternA2(b, 0, NO_CHANGE, NO_CHANGE, 0.01, -2*mult, 10);
		ObjMove_AddPatternA2(a, 105/mult, NO_CHANGE, NO_CHANGE, 2, 0, 20);
		ObjMove_AddPatternA2(b, 105/mult, NO_CHANGE, NO_CHANGE, 2, 0, 20);
	}
}

task Spell{
	SetForbidPlayerSpell(true);
	CanShoot = false;
	SE_Play(GCSD ~ "./se/bomb.wav", 100);
	ObjRender_SetAlpha(playerObj, 255);
	
	if(isFocus){
	SpellBackground;
	SpellText("Red Plower");
	SpellUse2;
	SetPlayerInvincibilityFrame(60*6);
	wait(60*5.5);
	SetForbidPlayerSpell(false);
	CanShoot = true;
	Obj_Delete(GetSpellManageObject);
	}
	
	else{
	SpellBackground;
	SpellText("Momentous Flash");
	SpellUse;	
	SetPlayerInvincibilityFrame(60*2);
	CanShoot = true;
	wait(120);
	SetForbidPlayerSpell(false);
	Obj_Delete(GetSpellManageObject);
	}
	
}


task SpellUse{
	let angle = 0;
	SE_Play(GCSD ~ "./se/boop2.wav", 255);
	Fire;
	
	task Fire{
	ascent(i in 0..60*2){
		let obj = CreatePlayerShotA1(rand(0, 640), rand(0, 480), 0, 270, 1, 99999, 3);
		ObjShot_SetEraseShot(obj, true);
		ObjShot_SetAutoDelete(obj, 1);
		Manage(obj);
		ObjMove_AddPatternA1(obj, 90, 30, NO_CHANGE);
		}
	wait(90);
	SE_Play(GCSD ~ "./se/shoot2.wav", 100);
	}
	
	task Manage(obj){
	wait(90);
	ObjShot_SetDamage(obj, 80);
	}
}

task SpellUse2{
	let ang = 270;
	SE_Play(GCSD ~ "./se/boop2.wav", 255);
	
	let las = CreateStraightLaserA1(GetPlayerX, GetPlayerY, ang, 500, 25, 0, 4, 1999999);
	Manage(las);
	Fire;
	Rings;
	
	task Fire{
	wait(60);
	SE_Play(GCSD ~ "./se/bomb2.wav", 100);
		ascent(i in 0..60*1.5){
			ascent(j in 0..3){
			let obj = CreatePlayerShotA1(GetPlayerX+rand(-25, 25), GetPlayerY+rand(-25, 25), 30, ang, 8, 99999, 3);
			ObjShot_SetEraseShot(obj, true);
			ObjShot_SetAutoDelete(obj, 1);
			}
			SE_Play(GCSD ~ "./se/shoot2.wav", 50);
		wait(2);
		}
	}
	
	task Rings{
		loop(6){
			ascent(i in 0..12){
				ascent(j in 1..4){
				let obj = CreatePlayerShotA1(GetPlayerX, GetPlayerY, 10*j, i*360/12, 10, 99999, 3);
				ObjShot_SetEraseShot(obj, true);
				ObjShot_SetAutoDelete(obj, 1);		
				}
			}
		SE_Play(GCSD ~ "./se/shoot2.wav", 100);
		wait(45);
		}
	}
	
	task Manage(las){
	loop(60*2*2){
	if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){ang--;}
	if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){ang++;}
	ObjMove_SetPosition(las, GetPlayerX, GetPlayerY);
	ObjStLaser_SetAngle(las, ang);
	yield;
	}
	ascent(i in 0..255/5){
	ObjRender_SetAlpha(las, 255-i*255/5);
	yield;
	}
	Obj_Delete(las);
	}
	
}

task SpellText(text){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.80);
	ObjPrim_SetTexture(obj, GCSD ~ "./img/SpellAttack.png");
	ObjSprite2D_SetSourceRect(obj, 0, 0, 266, 34);
	ObjRender_SetAngleXYZ(obj, 0,180,0);
	ObjRender_SetPosition(obj, GetStgFrameWidth/2, GetStgFrameHeight-40, 0);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetColor(obj, 100,100,255);
	ObjRender_SetAlpha(obj, 0);
	
	let objtext = ObjText_Create;
	ObjText_SetText(objtext,text);
	Obj_SetRenderPriority(objtext, 0.80);
	ObjText_SetFontType(objtext, "Modern No. 20");
	ObjText_SetFontSize(objtext,12);
	ObjText_SetFontColorBottom(objtext,32,32,32);
	ObjText_SetFontColorTop(objtext, 255, 255, 255);
	ObjText_SetFontBorderType(objtext,BORDER_FULL);
	ObjText_SetFontBorderColor(objtext,32,32,32);
	ObjText_SetFontBorderWidth(objtext,1.5);
	ObjText_SetHorizontalAlignment(objtext, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objtext, 400);
	ObjRender_SetPosition(objtext, GetStgFrameWidth/2-200, GetStgFrameHeight-40-8, 0);
	ObjRender_SetAlpha(objtext, 0);
	
	ascent(i in 0..25){
	ObjRender_SetAlpha(objtext, i*10);
	ObjRender_SetAlpha(obj, i*10);
	yield;
	}
	
	while(!Obj_IsDeleted(obj) ){
	let inv = GetPlayerInvincibilityFrame;
	if(inv<10){break;}
	yield;
	}
	
	descent(i in 0..25){
	ObjRender_SetAlpha(objtext, i*10);
	ObjRender_SetAlpha(obj, i*10);
	yield;
	}
	
	Obj_Delete(obj);
	Obj_Delete(objtext);
}

task SpellBackground{
	let alpha = 255;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj, 1);
	ObjPrim_SetTexture(obj, GCSD ~ "./img/white.png");
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,4,4,1);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	ObjRender_SetAlpha(obj, alpha);
	ascent(i in 0..25){
	ObjRender_SetAlpha(obj,	alpha);
	alpha = 255-i*10;
	yield;
	}
	Obj_Delete(obj);
}

task Shockwave{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let Xscale = 0.2;
	let Yscale = 0.2;
	let alpha = 255;
	
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"./img/circles.png");
	ObjSprite2D_SetSourceRect(obj,0, 0, 64, 64);
	ObjRender_SetScaleXYZ(obj,Xscale,Yscale,0);
	Obj_SetRenderPriorityI(obj,50);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha(obj,alpha);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj,GetPlayerX, GetPlayerY,0);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	loop(30){
		ObjRender_SetScaleXYZ(obj,Xscale,Yscale,0);
		ObjRender_SetAlpha(obj,alpha);
		Xscale+=0.1;
		Yscale+=0.1;
		alpha-=10;
		yield;
	}
	Obj_Delete(obj);
}

task Expand{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let Xscale = 1;
	let Yscale = 1;
	let alpha = 255;
	
	ObjPrim_SetTexture(obj, sprite);
	ObjSprite2D_SetSourceRect(obj,0, 0, 30, 50);
	ObjRender_SetScaleXYZ(obj,Xscale,Yscale,0);
	Obj_SetRenderPriorityI(obj,50);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha(obj,alpha);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj,GetPlayerX, GetPlayerY,0);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	loop(30){
		ObjRender_SetScaleXYZ(obj,Xscale,Yscale,0);
		ObjRender_SetAlpha(obj,alpha);
		Xscale+=0.05;
		Yscale+=0.2;
		alpha-=10;
		yield;
	}
	Obj_Delete(obj);
}
	
task TMagicCircle{
	let countVertex = 64;
	let listRadius = [];
	loop(countVertex)
	{
		listRadius = listRadius ~ [0];
	}

	let path = GCSD ~ "Default_Player_MagicCircle.png";
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, countVertex);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.60);
	ObjPrim_SetTexture(obj, path);
	ascent(iVert in 0..countVertex / 2)
	{
		let left = iVert * 128;
		let indexVert = iVert * 2;
		ObjPrim_SetVertexUVT(obj, indexVert + 0, left, 0);
		ObjPrim_SetVertexUVT(obj, indexVert + 1, left, 64);		
	}

	let cx = 0;
	let cy = 0;
	let maxRadius = 128;
	let alpha = 0;
	let frame = 0;
	let angleRender = 0;
	let frameInvOld = 0;

	while(true)
	{
		let frameInv = GetPlayerInvincibilityFrame();

		if(frameInv <= 0)
		{
			Obj_SetVisible(obj, false);
			frameInvOld = 0;
		}
		else
		{
			if(frameInvOld == 0)
			{
				//初期化
				ascent(iVert in 0..countVertex)
				{
					listRadius[iVert] = 0;
				}
				alpha = 0;
				frame = 0;
			}
			frameInvOld = frameInv;

			let px = GetPlayerX();
			let py = GetPlayerY();

			alpha += 1 / 120;
			alpha = min(alpha, 1);
			angleRender += 360 / countVertex / 2;
			Obj_SetVisible(obj, true);

			frameInv = min(100, frameInv);
			let rRate = frameInv / 100;

			ascent(iVert in 0..countVertex / 2)
			{
				let indexVert = iVert * 2;
				let angle = 360 / (countVertex / 2 - 1) * iVert;

				let vx1 = listRadius[indexVert] * cos(angle);
				let vy1 = listRadius[indexVert] * sin(angle);
				ObjPrim_SetVertexPosition(obj, indexVert + 0, vx1, vy1, 0);

				let vx2 = listRadius[indexVert+1] * cos(angle);
				let vy2 = listRadius[indexVert+1] * sin(angle);
				ObjPrim_SetVertexPosition(obj, indexVert + 1, vx2, vy2, 0);

				//魔方陣拡大
				if(frame >= 0)
				{//外側
					let dr = (maxRadius * rRate - listRadius[indexVert]) / 8;
					listRadius[indexVert] = listRadius[indexVert] + dr;
				}
				if(frame > 10)
				{//内側
					let rRateIn = rRate - 0.12;
					if(rRateIn < 0){rRateIn=0;}
					let dr= (maxRadius * rRateIn - listRadius[indexVert + 1]) / 16;
					listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr;
				}

			}

			ObjRender_SetColor(obj, 192 * alpha, 192 * alpha, 255 * alpha);
			ObjRender_SetPosition(obj, px, py, 0);
			ObjRender_SetAngleZ(obj, angleRender);

			frame++;
		}

		yield;
	}
}

function wait(w) { loop(w) { yield; } } 

task SE_Play(let path, let vl){
let seobj = ObjSound_Create;
ObjSound_Load(seobj,path);
ObjSound_Play(seobj);
ObjSound_SetVolumeRate(seobj,vl);
loop(120){yield;}
RemoveSound(path);
Obj_Delete(seobj);
}