#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["CharmingPast_Seija"]
#Title["Seija Kijin"]
#Text["Regular shot:[r]&nbsp;&nbsp;&nbsp;&nbsp;&quot;Dream Bow of[r]&nbsp;&nbsp;&nbsp;&nbsp;Heaven and Earth&quot;[r]Bomb:[r]&nbsp;&nbsp;&nbsp;&nbsp;&quot;Traitorous Weaponry&quot;[r]&nbsp;[r]by Misaki Rindou[r]&nbsp;[r](All graphic rendering[r]code by Kirbio)"]
#Image["./img_Select.png"]
#ReplayName["Seija"]

// Patched v1.01
	// Raised DPS. Fixed forbid shot.

let objPlayer = GetPlayerObjectID();
let objSlowShot = ID_INVALID;
let current = GetCurrentScriptDirectory();

let seShot = current ~ "se_plst00.wav";
let seGraze = current ~ "se_graze.wav";
let seBomb = current ~ "se_nep00.wav";
let seDead = current ~ "se_pldead00.wav";

@Initialize {
	let path = current ~ "Rindou_SeijaShot.txt";
	LoadPlayerShotData(path);

	LoadSound(seShot);
	LoadSound(seGraze);
	LoadSound(seBomb);
	LoadSound(seDead);

	ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 1, 20);

	SetPlayerClip(16, 16, GetStgFrameWidth-16, GetStgFrameHeight-16);

	TOption(-40, 0, -20, 10, 1);
	TOption(40, 0, 20, 10, -1);
	TImage();
	THitbox;
	TShot();
}

@MainLoop {
	yield;
}

@Event {
	alternative(GetEventType())
	case(EV_REQUEST_SPELL) {
		let spell = GetPlayerSpell();
		if(spell >= 1) {
			SetScriptResult(true);
			SetPlayerSpell(spell-1);
			PlaySE(seBomb);
			TSpell();
		} else {
			SetScriptResult(false);
		}
	}
	case(EV_HIT) {
		TExplosion(-45);
		TExplosion(45);
		PlaySE(seDead);
	}
	case(EV_PLAYER_REBIRTH) {
		SetPlayerSpell(3);
		SetPlayerInvincibilityFrame(180);
	}
	case(EV_GRAZE) {
		PlaySE(seGraze);
	}
	case(EV_GET_ITEM) {
		let itemType = GetEventArgument(0);
		alternative(itemType)
		case(ITEM_SPELL) {
		}
	}

}

task TImage {
	let objp = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objp, GetCurrentScriptDirectory() ~ "img_Player_Seija.png");
	Obj_SetRenderPriorityI(objp, 30);
	let sourcex = 0;
	let sourceidlex = 0;
	let sourceleftx = 0;
	let sourcerightx = 0;
	let sourcey = 0;
	let turnbackleft = 1;
	let turnbackright = 1;
	let death = 0;
	let invincibilityblink = 0;
	task TSources {
		loop {
			if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD)
			{
				sourcey = 1;
				turnbackright = 1;
				sourcerightx = 0;
				sourcex = sourceleftx;
				if(turnbackleft == 1) {
					sourceleftx++;
					if(sourceleftx > 7) { sourceleftx = 6; turnbackleft = -1; }
				} else if(turnbackleft == -1) {
					sourceleftx--;
					if(sourceleftx > 4) { sourceleftx = 5; turnbackleft = 1; }
				}
			}
			else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
			{
				sourcey = 2;
				turnbackleft = 1;
				sourceleftx = 0;
				sourcex = sourcerightx;
				if(turnbackright == 1) {
					sourcerightx++;
					if(sourcerightx > 7) { sourcerightx = 6; turnbackright = -1; }
				} else if(turnbackright == -1) {
					sourcerightx--;
					if(sourcerightx > 4) { sourcerightx = 5; turnbackright = 1; }
				}
			}
			else
			{
				sourcey = 0;
				sourcex = sourceidlex;
			}
			sourceidlex++;
			if(sourceidlex>=8) { sourceidlex = 0; }
			let waittime = 1;
			if(sourcey == 0 || sourcex > 4) { waittime = 4; }
			loop(waittime) { yield; }
		}
	}
	TSources;
	loop {
		ObjSprite2D_SetSourceRect(objp, 0+32*sourcex, 0+48*sourcey, 32+32*sourcex, 48+48*sourcey);
		ObjSprite2D_SetDestCenter(objp);
		ObjRender_SetPosition(objp, GetPlayerX, GetPlayerY, NULL);
		if(GetPlayerState == STATE_NORMAL || GetPlayerState == STATE_HIT) {
			death = 0;
		} else {
			if(death < 10) { death++; }
		}
		ObjRender_SetAlpha(objp, 255-death*255/10);
		ObjRender_SetScaleX(objp, 1+2*(death/10));
		ObjRender_SetScaleY(objp, 1-death/10);
		if(GetPlayerInvincibilityFrame > 0) {
			invincibilityblink++;
			invincibilityblink %= 4;
		} else {
			invincibilityblink = 0;
		}
		if(invincibilityblink >= 2) {
			ObjRender_SetColor(objp, 0, 0, 128);
		} else {
			ObjRender_SetColor(objp, 255, 255, 255);
		}
		yield;
	}

}

task THitbox {
	let objp = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objp, GetCurrentScriptDirectory() ~ "Hitbox.png");
	Obj_SetRenderPriorityI(objp, 55);
	ObjRender_SetScaleX(objp, 0);
	let angle = 0;
	let focus = 0;
	loop {
		ObjSprite2D_SetSourceRect(objp, 0, 0, 64, 64);
		ObjSprite2D_SetDestCenter(objp);
		ObjRender_SetPosition(objp, GetPlayerX, GetPlayerY, NULL);
		if(GetPlayerState == STATE_NORMAL) {
			if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE) {
				if(focus > 0) { focus--; }
			} else {
				if(focus < 5) { focus++; }
			}
		}
		ObjRender_SetScaleXYZ(objp, focus/5, focus/5, NULL);
		ObjRender_SetAngleZ(objp, angle);
		angle+=2;
		angle%=360;
		yield;
	}

}

task TOption (UX, UY, FX, FY, way) {
	let objp = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objp, GetCurrentScriptDirectory() ~ "img_Player_Seija.png");
	Obj_SetRenderPriorityI(objp, 30);
	ObjRender_SetScaleX(objp, way);
	let angle = 0;
	let x = 0;
	let y = 0;
	let focus = 0;
	loop {
		ObjSprite2D_SetSourceRect(objp, 0, 192, 30, 222);
		ObjSprite2D_SetDestCenter(objp);
		ObjRender_SetPosition(objp, GetPlayerX+x, GetPlayerY+y, NULL);
		if(GetPlayerState == STATE_NORMAL) {
			if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE) {
				if(focus > 0) { focus--; }
			} else {
				if(focus < 5) { focus++; }
			}
		}
		x = UX+(FX-UX)*focus/5;
		y = UY+(FY-UY)*focus/5;
		ObjRender_SetAngleZ(objp, angle);
		angle+=3*way;
		angle%=360;
		yield;
	}

}

task TShot {

	let shotCount = -1;
	let shotCount2 = -1;

	task WarpShot(x, y, angle, dmg, graphic, warped) {
		let objShot = CreatePlayerShotA1(x, y, 18, angle, dmg, 1, graphic);
		let deleteX = 0;
		let deleteY = 0;
		while(!Obj_IsDeleted(objShot)) {
			if(warped == 0 && ObjMove_GetY(objShot) > GetStgFrameHeight) {
				ObjMove_SetY(objShot, ObjMove_GetY(objShot)-GetStgFrameHeight);
				ObjShot_SetGraphic(objShot, graphic+1);
				graphic++;
				warped = 1;
			}
			deleteX = ObjMove_GetX(objShot);
			deleteY = ObjMove_GetY(objShot);
			yield;
		}
		if(deleteY < 0 || deleteY > GetStgFrameHeight) { return; }
		let objp = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(objp, GetCurrentScriptDirectory() ~ "img_Player_Seija.png");
		Obj_SetRenderPriorityI(objp, 40);
		ObjRender_SetBlendType(objp, BLEND_ADD_ARGB);
		ObjRender_SetPosition(objp, deleteX, deleteY, NULL);
		ObjSprite2D_SetSourceRect(objp, 128+16*(graphic-1), 192, 128+16+16*(graphic-1), 192+32);
		ObjSprite2D_SetDestRect(objp, -8, -16, 8, 16);
		ObjRender_SetAngleZ(objp, 90+angle);
		ascent(i in 0..10) {
			let scaleX = 1-i/10;
			let scaleY = 1+i/5;
			ObjRender_SetScaleXYZ(objp, scaleX, scaleY, NULL);
			ObjRender_SetAlpha(objp, 255-i*20);
			ObjSprite2D_SetDestRect(objp, -8, -16-i*1, 8, 16-i*1);
			yield;
		}
		Obj_Delete(objp);
	}

	loop {
		while(!(GetPlayerState == STATE_NORMAL || GetPlayerState == STATE_HIT)) {
			yield;
		}

		let dist = GetStgFrameHeight-GetPlayerY;

		if(GetVirtualKeyState(VK_SHOT) != KEY_FREE && IsPermitPlayerShot) {
			shotCount++;
			shotCount = shotCount % 4;
			shotCount2++;
			shotCount2 = shotCount2 % 7;
		} else {
			shotCount = -1;
			shotCount2 = -1;
		}

		if(shotCount == 0) {
			WarpShot(GetPlayerX-10, GetPlayerY, -90, 5, 5, 1);
			WarpShot(GetPlayerX+10, GetPlayerY, -90, 5, 5, 1);
			PlaySE(seShot);
		}

		if(shotCount2 == 0) {
			if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_FREE) {
				let initial = 10;
				let offset = 14-dist*(14-6)/448;
				WarpShot(GetPlayerX-40, GetPlayerY, 90-initial, 6, 3, 0);
				WarpShot(GetPlayerX+40, GetPlayerY, 90+initial, 6, 3, 0);
				WarpShot(GetPlayerX-40, GetPlayerY, 90-initial+offset, 6, 3, 0);
				WarpShot(GetPlayerX+40, GetPlayerY, 90+initial-offset, 6, 3, 0);
				WarpShot(GetPlayerX-40, GetPlayerY, 90-initial+offset*2, 7-dist*(7-2)/448, 3, 0);
				WarpShot(GetPlayerX+40, GetPlayerY, 90+initial-offset*2, 7-dist*(7-2)/448, 3, 0);
				WarpShot(GetPlayerX-40, GetPlayerY, 90-initial+offset*3, 7-dist*(7-2)/448, 3, 0);
				WarpShot(GetPlayerX+40, GetPlayerY, 90+initial-offset*3, 7-dist*(7-2)/448, 3, 0);
				WarpShot(GetPlayerX-40, GetPlayerY, 90-initial+offset*4, 7-dist*(7-2)/448, 3, 0);
				WarpShot(GetPlayerX+40, GetPlayerY, 90+initial-offset*4, 7-dist*(7-2)/448, 3, 0);
			} else {
				WarpShot(GetPlayerX-20, GetPlayerY+10, 90-10, 5, 1, 0);
				WarpShot(GetPlayerX-20, GetPlayerY+10, 90, 4.5, 1, 0);
				WarpShot(GetPlayerX-20, GetPlayerY+10, 90+10, 4, 1, 0);
				WarpShot(GetPlayerX+20, GetPlayerY+10, 90+10, 5, 1, 0);
				WarpShot(GetPlayerX+20, GetPlayerY+10, 90, 4.5, 1, 0);
				WarpShot(GetPlayerX+20, GetPlayerY+10, 90-10, 4, 1, 0);
			}
		}
		yield;
	}

}

task TExplosion (angle) {
	let path = GetCurrentScriptDirectory() ~ "img_Effect.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.60);
	ObjPrim_SetTexture(obj, path);
	ObjSprite2D_SetSourceRect(obj, 0, 256, 256, 512);
	ObjSprite2D_SetDestRect(obj, -128, -128, 128, 128);

	let alpha = 255;
	let scale = 0;
	let sdown = 0.1;
	loop(45)
	{
		scale += sdown;
		if(sdown > 0.01) { sdown-=1/450; }
		alpha -= 255/45;
		ObjRender_SetColor(obj, alpha, alpha*0.5, alpha*0.5);
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetScaleXYZ(obj, scale*0.50, scale, 1);
		ObjRender_SetAngleZ(obj, angle);
		yield;
	}
	Obj_Delete(obj);
}

task TSpell
{
	let current = GetCurrentScriptDirectory();
	let imgEffect = current ~ "bomb.png";
	
	let bombx = GetPlayerX;
	let bomby = GetPlayerY;

	let starty = GetPlayerY/GetStgFrameHeight;

	SetPlayerInvincibilityFrame(240);//無敵時間を設定
	let bSlowMove = GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD;
	let objManage = GetSpellManageObject();

	let size = 0;

	effect(0);
	effect(1);
	WaveOut;

	loop(10) { size += 1/10; yield; }

	loop(300) {

		let allshots = GetShotIdInCircleA2(bombx, bomby, 40, TARGET_ENEMY);
		ascent(this in 0..length(allshots)) {
			let thisobj = allshots[this];
			if( ObjShot_IsSpellResist(thisobj) ) { break; }
			let d = 0;
			if(this < 20) { d = 5; }
			else if(this < 50) { d = 0.75; }
			else if(this < 100) { d = 0.2; }
			else if(this < 200) { d = 0.1; }
			else { d = 0; }
			SpellBullet(ObjMove_GetX(thisobj), ObjMove_GetY(thisobj), d);
			ObjShot_ToItem(thisobj);
		}
		yield;
	}

	loop(10) { size -= 1/10; yield; }
	Obj_Delete(objManage);


	task effect(num)
	{
		let angle = 0;
		let pulse = 0;
		let alpha=255;
		let acc = 0;
		
		let obj = ObjSpell_Create();
		ObjPrim_SetVertexCount(obj, 4);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
		if(num == 1) { ObjRender_SetBlendType(obj, BLEND_SUBTRACT); }
		Obj_SetRenderPriorityI(obj, 29);
		ObjPrim_SetTexture(obj, imgEffect);
		ObjSpell_SetEraseShot(obj, false);
		ObjSpell_Regist(obj);

		let r = 144;
		let sc = 1;
		ascent(i in 0..4)
		{
			let angle = -45+i*90;
			let x = r*cos(angle);
			let y = r*sin(angle);

			ObjPrim_SetVertexUVT(obj, 0, 0, 0);
			ObjPrim_SetVertexUVT(obj, 1, 128, 0);
			ObjPrim_SetVertexUVT(obj, 2, 128, 128);
			ObjPrim_SetVertexUVT(obj, 3, 0, 128);
			ObjPrim_SetVertexPosition(obj, i, x, y, 0);
			ObjPrim_SetVertexColor(obj, i, 0, 0, 0);
		}
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, NULL);
		if(num == 0) {  Glimmer; }

		while(!Obj_IsDeleted(objManage)) {
			angle+=26; angle%=360;
			pulse+=11; pulse%=360;
			//if(num == 0) {
				effectfade(angle, pulse);
			//}
			ObjRender_SetScaleXYZ(obj, size*(sc*(1-num/4)+1/16*sin(pulse)), size*(sc*(1-num/4)+1/16*sin(pulse)), NULL);
			ObjRender_SetAngleZ(obj, angle);
			ObjRender_SetY(obj, ObjRender_GetY(obj)-acc);
			ObjSpell_SetIntersectionCircle(obj, ObjRender_GetX(obj), ObjRender_GetY(obj), 50*sc*size);
			ObjSpell_SetDamage(obj, 2);
			ascent(i in 0..4) { ObjPrim_SetVertexColor(obj, i, alpha, alpha, alpha); }
			acc+=starty/180;
			bomby = ObjRender_GetY(obj);
			sc-=1/800;
			yield;
		}
		Obj_Delete(obj);


	task effectfade (let angle, let pulse)
	{
		let alpha=192;
		let acc = 0;
		
		let obj2 = ObjSpell_Create();
		ObjPrim_SetVertexCount(obj2, 4);
		ObjPrim_SetPrimitiveType(obj2, PRIMITIVE_TRIANGLEFAN);
		ObjRender_SetBlendType(obj2, BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(obj2, 28);
		ObjPrim_SetTexture(obj2, imgEffect);
		ObjSpell_SetEraseShot(obj2, false);
		ObjSpell_Regist(obj2);

		let r = 144;
		ascent(i in 0..4)
		{
			let angle = -45+i*90;
			let x = r*cos(angle);
			let y = r*sin(angle);

			ObjPrim_SetVertexUVT(obj2, 0, 0, 0);
			ObjPrim_SetVertexUVT(obj2, 1, 128, 0);
			ObjPrim_SetVertexUVT(obj2, 2, 128, 128);
			ObjPrim_SetVertexUVT(obj2, 3, 0, 128);
			ObjPrim_SetVertexPosition(obj2, i, x, y, 0);
		}
		ObjRender_SetAngleZ(obj2, angle);
		let s = size*((1)+1/16*sin(pulse));
		let max = 1+1/4;

		descent(t in 0..10) {
			if(Obj_IsDeleted(obj)) { break; }
			ObjRender_SetPosition(obj2, ObjRender_GetX(obj), ObjRender_GetY(obj), NULL);
			ascent(i in 0..4) {
				ObjRender_SetScaleXYZ(obj2, s*(max-t/10*max), s*(max-t/10*max), NULL);
				//ObjPrim_SetVertexColor(obj2, i, alpha*t/10, alpha*t/10, alpha*t/10);
				ObjRender_SetAlpha(obj2, alpha*t/10);
			}
			yield;
		}
		Obj_Delete(obj2);
	}

	task Glimmer {
		let path = GetCurrentScriptDirectory() ~ "img_Effect.png";
		let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetBlendType(obj2, BLEND_ADD_ARGB);
		Obj_SetRenderPriority(obj2, 0.28);
		ObjPrim_SetTexture(obj2, path);
		ObjSprite2D_SetSourceRect(obj2, 0, 0, 128, 128);
		ObjSprite2D_SetDestRect(obj2, -128, -128, 128, 128);

		let alpha = 155;
		let scale = 1.5;
		
		ascent(i in 0..30) {
			ObjRender_SetPosition(obj2, ObjRender_GetX(obj), ObjRender_GetY(obj), 0);
			ObjRender_SetScaleXYZ(obj2, scale*i/30, scale*i/30, 0);
			ObjRender_SetColor(obj2, alpha, alpha*0.75, alpha*0.5);
			yield;
		}
		loop(270) { 
			ObjRender_SetPosition(obj2, ObjRender_GetX(obj), ObjRender_GetY(obj), 0);
			ObjRender_SetColor(obj2, alpha, alpha*0.75, alpha*0.5);
		yield; }
		loop(30) {
			if(Obj_IsDeleted(obj)) { break; }
			ObjRender_SetPosition(obj2, ObjRender_GetX(obj), ObjRender_GetY(obj), 0);
			alpha-=155/30;
			ObjRender_SetColor(obj2, alpha, alpha*0.75, alpha*0.5);
			yield;
		}
		Obj_Delete(obj2);
	}
	
	}
	

task WaveOut {
	let path = GetCurrentScriptDirectory() ~ "img_Effect.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.28);
	ObjPrim_SetTexture(obj, path);
	ObjSprite2D_SetSourceRect(obj, 0, 256, 256, 512);
	ObjSprite2D_SetDestRect(obj, -128, -128, 128, 128);

	let alpha = 155;
	let scale = 0;
	let sdown = 0.025;
	ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
	
	loop(300)
	{
		scale += sdown;
		alpha -= 155/300;
		ObjRender_SetColor(obj, alpha, alpha*0.75, alpha*0.5);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1);
		yield;
	}
	Obj_Delete(obj);
}

}

task SpellBullet (x, y, damage) {
	let current = GetCurrentScriptDirectory();
	let imgEffect = current ~ "img_Player_Seija.png";
	
	let obj = ObjSpell_Create();
	ObjPrim_SetVertexCount(obj, 4);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjPrim_SetTexture(obj, imgEffect);
	ObjSpell_SetEraseShot(obj, false);
	ObjSpell_Regist(obj);
	
	ObjPrim_SetVertexUVT(obj, 0, 192, 192);
	ObjPrim_SetVertexUVT(obj, 1, 208, 192);
	ObjPrim_SetVertexUVT(obj, 2, 208, 256);
	ObjPrim_SetVertexUVT(obj, 3, 192, 256);
	ObjPrim_SetVertexPosition(obj, 0, 4, -8, 0);
	ObjPrim_SetVertexPosition(obj, 1, 4, 8, 0);
	ObjPrim_SetVertexPosition(obj, 2, -60, 8, 0);
	ObjPrim_SetVertexPosition(obj, 3, -60, -8, 0);
	ObjRender_SetScaleXYZ(obj, 1.5, 1.5, 0);
			
	ObjRender_SetPosition(obj, x, y, 0);
	
	let angle = -90+rand(-25, 25);
			
	let enemyX = 0;
	let enemyY = 0;
	let pos = GetEnemyIntersectionPosition(enemyX, enemyY, 1);
	if(length(pos) > 0)
	{
		enemyX = pos[0][0];
		enemyY = pos[0][1];
		angle = atan2(enemyY-y, enemyX-x);
	}
	
	ObjRender_SetAngleZ(obj, angle);
	
	let v = 2;
	let vacc = 0;
	let t = 0;
	loop(150) {
		x+=v*cos(angle);
		y+=v*sin(angle);
		ObjRender_SetPosition(obj, x, y, 0);
		ObjSpell_SetIntersectionCircle(obj, x, y, 30);
		ObjSpell_SetDamage(obj, damage*t/90);
		t++;
		v+=vacc; vacc+=1/15;
		yield;
	}
}