#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["Orin"]
#Title["Rin Kaenbyou"]
#Text["Press C to switch between[r]humanoid and cat forms."]
#Image["./Image/Cutin.png"]
#ReplayName["Orin"]

let current = GetCurrentScriptDirectory();

let objPlayer = GetPlayerObjectID();
let renderPlayerObject = ObjPrim_Create(OBJ_SPRITE_2D);
let playerdead = false;
let IsPlayerSpawning = false;
let IsHitboxActive = false;
let optionAlive = true;
let CanShoot = true;

let kittyglompactive = false;

let isvisible = true;
let IsCat = false;
let cantransform = true;

let imgPlayer = current ~ "Image/Orin.png";
let snowflake = current ~ "Image/Focus.png";
let snowflake2 = current ~ "Image/Focus2.png";
let imgOption = current ~ "Image/Option.png";

@Initialize{
	LoadPlayerShotData(current ~ "Shotdata.dnh");
	SetPlayerAutoItemCollectLine(130);
	ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 3, 20); 

	SetPlayerSpeed(3.5, 1.5);
	SetPlayerClip(11,11,GetStgFrameWidth-11,GetStgFrameHeight-18);
	RenderPlayerImage(renderPlayerObject, 0);

	Shoot;
	TMagicCircle;
	Options;
	Forms;
}

@Event{
	alternative(GetEventType())
		case(EV_REQUEST_SPELL)
		{	
			let spell = GetPlayerSpell();
			if(spell >= 1 && IsPermitPlayerSpell()){
				if(!IsCat){
					BlazingWheel;
				}else{
					KittyGlomp;
				}
				SetScriptResult(true);
				SetPlayerSpell(spell-1);
			}else{
				SetScriptResult(false);
			}
		}
		case(EV_GRAZE){
			PlaySE(current~"Sound/se_graze.wav");
		}
		case(EV_HIT)
		{
			PlayerExplode(current ~ "Image/Explode.png");
			PlaySE(current~"Sound/se_pldead00.wav");
		}
		case(EV_PLAYER_SHOOTDOWN){
			playerdead = true;
			IsHitboxActive = false;
			optionAlive = false;
		}
		case(EV_PLAYER_REBIRTH)
		{
			SetPlayerSpell(3);
			playerdead = false;
			optionAlive = true;
			Options;
			SetPlayerInvincibilityFrame(180);
		}
		case(EV_GET_ITEM)
		{
			let itemType = GetEventArgument(0);
			alternative(itemType)
				case(ITEM_1UP){
					PlaySE(current~"Sound/se_extend.wav");
					SetPlayerLife(GetPlayerLife + 1);
					WriteLog("ITEM_1UP");
				}
				case(ITEM_1UP_S){
					PlaySE(current~"Sound/se_extend.wav");
					SetPlayerLife(GetPlayerLife + 1);
					WriteLog("ITEM_1UP_S");
				}
				case(ITEM_SPELL){
					PlaySE(current~"Sound/se_item00.wav");
					SetPlayerSpell(GetPlayerSpell + 1);
					WriteLog("ITEM_SPELL");
				}
				case(ITEM_SPELL_S){
					PlaySE(current~"Sound/se_item00.wav");
					SetPlayerSpell(GetPlayerSpell + 1);
					WriteLog("ITEM_SPELL_S");
				}
				case(ITEM_POWER){
					PlaySE(current~"Sound/se_item00.wav");
					WriteLog("ITEM_POWER");
				}
				case(ITEM_POWER_S){
					PlaySE(current~"Sound/se_item00.wav");
					WriteLog("ITEM_POWER_S");
				}
				case(ITEM_POINT){
					PlaySE(current~"Sound/se_item00.wav");
					WriteLog("ITEM_POWER");
				}
				case(ITEM_POINT_S){
					PlaySE(current~"Sound/se_item00.wav");
					WriteLog("ITEM_POWER_S");
				}
		}
}

@Loading{
	LoadSound(current~"Sound/se_plst00.wav");
	LoadSound(current~"Sound/se_nep00.wav");
	LoadSound(current~"Sound/se_pldead00.wav");
	LoadSound(current~"Sound/se_graze.wav");
	LoadSound(current~"Sound/se_item00.wav");
	LoadSound(current~"Sound/se_extend.wav");
	LoadSound(current~"Sound/se_cat01.wav");
	LoadSound(current ~ "Sound/se_boing.wav");
	LoadSound(current ~ "Sound/se_boingback.wav");
	LoadSound(current ~ "Sound/se_scratch.wav");
}

@MainLoop{
	if(GetPlayerState==STATE_DOWN || IsPermitPlayerShot==false){CanShoot = false;}
	if(GetPlayerState==STATE_NORMAL && IsPermitPlayerShot==true){CanShoot = true;}

	//if(GetPlayerState==STATE_DOWN){SetForbidPlayerShot(true); SetForbidPlayerSpell(true);}
	//if(GetPlayerState==STATE_NORMAL){SetForbidPlayerShot(false); SetForbidPlayerSpell(false);}
	SetPlayerClip(11,11,GetStgFrameWidth-11,GetStgFrameHeight-18);
	if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD&&!IsHitboxActive){RenderHitboxImage(objPlayer, 0, 1);}
	yield;
}

@Finalize{

}

task Shoot{
	let count = 0;
	loop{
		if(GetVirtualKeyState(VK_SHOT) != KEY_FREE && count%4 == 0){
			if(CanShoot){
				PlaySE(current~"Sound/se_plst00.wav");

				if(!IsCat){
					CreatePlayerShotA1(GetPlayerX - 10, GetPlayerY - 16, 20, 270, 3.5, 1.5, 1);
					CreatePlayerShotA1(GetPlayerX + 10, GetPlayerY - 16, 20, 270, 3.5, 1.5, 1);
				}else{
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 230, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 235, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 240, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 245, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 250, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 255, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 260, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 265, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 270, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 275, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 280, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 285, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 290, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 295, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 300, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 305, 2, 1, 2);
					CreatePlayerShotA1(GetPlayerX, GetPlayerY, 15, 310, 2, 1, 2);
				}
			}
		}
		count++;
		yield;
	}
}

task Options{
	Option(32, 0, 8, -20);
	Option(-32, 0, -8, -20);
}

//x/y and focx/focy are in relation to the player object
task Option(x, y, focx, focy){
	let objOption = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objOption, 30);
	ObjRender_SetBlendType(objOption, BLEND_ALPHA);
	ObjPrim_SetTexture(objOption, imgOption);
	ObjSprite2D_SetSourceRect(objOption, 0, 0, 32, 64);
	//ObjSprite2D_SetDestCenter(objOption);
	ObjSprite2D_SetDestRect(objOption, -16, -48, 16, 16);
	ObjRender_SetPosition(objOption, GetPlayerX, GetPlayerY, 0);
	ObjRender_SetScaleXYZ(objOption, 0.5, 0.5, 0);

	let destX;
	let destY;
	let dist;
	let dir;

	RenderOption;

	let count = 0;
	while(!Obj_IsDeleted(objOption)){
		if(!optionAlive || IsCat){Obj_Delete(objOption);}
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE){
			destX = GetPlayerX + x;
			destY = GetPlayerY + y;
		}else{
			destX = GetPlayerX + focx;
			destY = GetPlayerY + focy;
		}

		let opX = ObjRender_GetX(objOption);
		let opY = ObjRender_GetY(objOption);
		dist = GetDist(opX, opY, destX, destY);
		dir = GetAngle(opX, opY, destX, destY);
		opX += 0.25 * dist * cos(dir);
		opY += 0.25 * dist * sin(dir);
		ObjRender_SetPosition(objOption, opX, opY, 0);

		if(GetVirtualKeyState(VK_SHOT) != KEY_FREE && count%5 == 0){
			if(CanShoot){
				CreatePlayerShotA1(ObjRender_GetX(objOption), ObjRender_GetY(objOption), 20, 260, 3.25, 1, 3);
				CreatePlayerShotA1(ObjRender_GetX(objOption), ObjRender_GetY(objOption), 20, 270, 3.25, 1, 3);
				CreatePlayerShotA1(ObjRender_GetX(objOption), ObjRender_GetY(objOption), 20, 280, 3.25, 1, 3);
			}
		}
		count++;
		yield;
	}

	task RenderOption{
		let frame = 0;
		while(!Obj_IsDeleted(objOption)){
			if(frame < 5){ObjSprite2D_SetSourceRect(objOption, 0, 0, 32, 64);}
			if(frame >= 5 && frame < 10){ObjSprite2D_SetSourceRect(objOption, 32, 0, 64, 64);}
			if(frame >= 10 && frame < 15){ObjSprite2D_SetSourceRect(objOption, 64, 0, 96, 64);}
			if(frame >= 15 && frame < 20){ObjSprite2D_SetSourceRect(objOption, 96, 0, 128, 64);}
			if(frame >= 20 && frame < 25){ObjSprite2D_SetSourceRect(objOption, 128, 0, 160, 64);}
			if(frame >= 25 && frame < 30){ObjSprite2D_SetSourceRect(objOption, 160, 0, 192, 64);}
			if(frame >= 30 && frame < 35){ObjSprite2D_SetSourceRect(objOption, 192, 0, 224, 64);}
			if(frame >= 35){ObjSprite2D_SetSourceRect(objOption, 224, 0, 256, 64);}
			if(frame > 40){frame = 0;}

			frame++;
			yield;
		}
	}
}

task Forms{
	loop{
		if(GetKeyState(KEY_C) == KEY_PUSH && cantransform){
			Transform;
		}
		yield;
	}

	task Transform{
		//ObjRender_SetColor(renderPlayerObject, 0, 0, 0);
		loop(10){yield;}

		PlaySE(current~"Sound/se_cat01.wav");
		PlayerExplode(current ~ "Image/Explode2.png");
		ObjPlayer_ClearIntersection(GetPlayerObjectID());
		if(IsCat){
			IsCat = false;
			SetPlayerSpeed(3.5, 1.5);
			ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 3, 20);
			Options;
		}else{
			IsCat = true;
			SetPlayerSpeed(4.5, 1.75);
			ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 1, 20);
		}
		loop(10){yield;}
		//ObjRender_SetColor(renderPlayerObject, 255, 255, 255);
		loop(30){yield;}
	}
}

task RenderHitboxImage(plobj, offx, hitboxnum){
	if(IsHitboxActive){return;}
	while(playerdead){
		if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_FREE){return;}
		yield;
	}
	IsHitboxActive = true;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.39);
	ObjRender_SetAlpha(obj, 220);
	ObjPrim_SetTexture(obj, snowflake);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj);
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriority(obj2, 0.69);
	ObjRender_SetAlpha(obj2, 220);
	ObjPrim_SetTexture(obj2, snowflake);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj2);
	
	
	let ObjTime = 0;
	
	let AlphaSet = 0;
	let ObjAlpha = 0;
	let ObjScale = 1;
	
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
		if(!isvisible){ObjRender_SetAlpha(obj, 0); ObjRender_SetAlpha(obj2, 0);}else{ObjRender_SetAlpha(obj, 255); ObjRender_SetAlpha(obj2, 255);}

		if(IsCat){
			ObjPrim_SetTexture(obj, snowflake2);
			ObjPrim_SetTexture(obj2, snowflake2);
		}else{
			ObjPrim_SetTexture(obj, snowflake);
			ObjPrim_SetTexture(obj2, snowflake);
		}

		if(ObjScale<1.4&&!playerdead){
			ObjTime+=8;
			ObjScale += 0.125;
			PositionSet;
		}else{break;}
		yield;
	}
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
		if(!isvisible){ObjRender_SetAlpha(obj, 0); ObjRender_SetAlpha(obj2, 0);}else{ObjRender_SetAlpha(obj, 255); ObjRender_SetAlpha(obj2, 255);}

		if(IsCat){
			ObjPrim_SetTexture(obj, snowflake2);
			ObjPrim_SetTexture(obj2, snowflake2);
		}else{
			ObjPrim_SetTexture(obj, snowflake);
			ObjPrim_SetTexture(obj2, snowflake);
		}

		if(ObjScale>1&&!playerdead){
			ObjTime+=8;
			ObjScale -= 0.1;
			PositionSet;
		}else{break;}
		yield;
	}
	ObjScale = 1;
	yield;
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
		if(!isvisible){ObjRender_SetAlpha(obj, 0); ObjRender_SetAlpha(obj2, 0);}else{ObjRender_SetAlpha(obj, 255); ObjRender_SetAlpha(obj2, 255);}

		if(IsCat){
			ObjPrim_SetTexture(obj, snowflake2);
			ObjPrim_SetTexture(obj2, snowflake2);
		}else{
			ObjPrim_SetTexture(obj, snowflake);
			ObjPrim_SetTexture(obj2, snowflake);
		}

		if(!playerdead){
			ObjTime+=3;
			PositionSet;
		}else{break;}
		yield;
	}
	IsHitboxActive = false;
	while(ObjScale>0){
		if(!isvisible){ObjRender_SetAlpha(obj, 0); ObjRender_SetAlpha(obj2, 0);}else{ObjRender_SetAlpha(obj, 255); ObjRender_SetAlpha(obj2, 255);}

		if(IsCat){
			ObjPrim_SetTexture(obj, snowflake2);
			ObjPrim_SetTexture(obj2, snowflake2);
		}else{
			ObjPrim_SetTexture(obj, snowflake);
			ObjPrim_SetTexture(obj2, snowflake);
		}

		ObjTime+=3;
		ObjScale -= 0.25;
		PositionSet;
		yield;
	}

	Obj_Delete(obj);
	Obj_Delete(obj2);
	IsHitboxActive = false;
	
	task PositionSet{
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetAngleZ(obj, ObjTime);
		ObjRender_SetPosition(obj2, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetScaleXYZ(obj, ObjScale, ObjScale, ObjScale);
		ObjRender_SetScaleXYZ(obj2, ObjScale, ObjScale, ObjScale);
	}
	
}

task RenderPlayerImage(ObjectRender, offset){
	let anim_count = 0;
	let anim_type = 0;
	let Uber = 0;
	let Uberblink = 0;
	
	let x = 32;
	let y = 48;
	let ri = 6;
	let rm = 4;
	let bn = 0;
	
	ObjPrim_SetTexture(ObjectRender, imgPlayer);
	Obj_SetRenderPriority(ObjectRender, 0.45);
	
	loop{

		if(GetPlayerState!=STATE_DOWN && isvisible){
			ObjRender_SetAlpha(ObjectRender, 255);
		}
		if(GetPlayerState==STATE_DOWN || playerdead || !isvisible){ObjRender_SetAlpha(ObjectRender, 0);}
		
		
		ObjRender_SetPosition(ObjectRender, GetPlayerX, GetPlayerY, 0);
		if(!IsPlayerSpawning){
			if(GetPlayerState!=STATE_HIT){
				Blink;
				if(GetVirtualKeyState(VK_RIGHT) == KEY_FREE && GetVirtualKeyState(VK_LEFT) == KEY_FREE){
					if(anim_type != 0){
						bn = 0;
						anim_count = 0;
						anim_type = 0;
					}

					if(IsCat){
						ObjRender_SetAngleZ(renderPlayerObject, 0);
					}else{
						ObjRender_SetAngleZ(renderPlayerObject, 0);
					}
				}
				if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
					if(GetVirtualKeyState(VK_RIGHT) != KEY_HOLD && anim_type != 1){
						bn = 0.85;
						anim_count = 0;
						anim_type = 1;
					}

					if(IsCat){
						ObjRender_SetAngleZ(renderPlayerObject, -10);
					}else{
						ObjRender_SetAngleZ(renderPlayerObject, 0);
					}
				}
				if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
					if(anim_type != 2){
						bn = 0.85;
						anim_count = 0;
						anim_type = 2;
					}

					if(IsCat){
						ObjRender_SetAngleZ(renderPlayerObject, 10);
					}else{
						ObjRender_SetAngleZ(renderPlayerObject, 0);
					}
				}
				anim_count+=(1+bn);
				if(floor(anim_count) != anim_count && bn == 0){
					anim_count = floor(anim_count);
				}
			}
		}else{
			Blink;
			anim_count+=1;
			if(anim_type != 0){
				anim_count = 0;
				anim_type = 0;
			}
		}
		//0 - Idle; 1 - Left; 2 - Right
		
		if(!IsCat){
			if(anim_type==0){
				if(round(anim_count) >= 0){ObjSprite2D_SetSourceRect(ObjectRender, 0, 0+offset, x, y+offset);}
				if(round(anim_count) >= ri){ObjSprite2D_SetSourceRect(ObjectRender, x, 0+offset, x*2, y+offset);}
				if(round(anim_count) >= ri*2){ObjSprite2D_SetSourceRect(ObjectRender, x*2, 0+offset, x*3, y+offset);}
				if(round(anim_count) >= ri*3){ObjSprite2D_SetSourceRect(ObjectRender, x*3, 0+offset, x*4, y+offset);}
				if(round(anim_count) >= ri*4){ObjSprite2D_SetSourceRect(ObjectRender, x*4, 0+offset, x*5, y+offset);}
				if(round(anim_count) >= ri*5){ObjSprite2D_SetSourceRect(ObjectRender, x*5, 0+offset, x*6, y+offset);}
				if(round(anim_count) >= ri*6){ObjSprite2D_SetSourceRect(ObjectRender, x*6, 0+offset, x*7, y+offset);}
				if(round(anim_count) >= ri*7){ObjSprite2D_SetSourceRect(ObjectRender, x*7, 0+offset, x*8, y+offset);}
				if(round(anim_count) >= ri*8-1){anim_count = ri-1;}
			}
			if(anim_type==1){
				if(round(anim_count) >= 0){ObjSprite2D_SetSourceRect(ObjectRender, 0, y+offset, x, y*2+offset);}
				if(round(anim_count) >= rm){ObjSprite2D_SetSourceRect(ObjectRender, x, y+offset, x*2, y*2+offset);}
				if(round(anim_count) >= rm*2){ObjSprite2D_SetSourceRect(ObjectRender, x*2, y+offset, x*3, y*2+offset);}
				if(round(anim_count) >= rm*3){ObjSprite2D_SetSourceRect(ObjectRender, x*3, y+offset, x*4, y*2+offset);}
				if(round(anim_count) >= rm*4){ObjSprite2D_SetSourceRect(ObjectRender, x*4, y+offset, x*5, y*2+offset);bn = 0}
				if(round(anim_count) >= rm*5){ObjSprite2D_SetSourceRect(ObjectRender, x*5, y+offset, x*6, y*2+offset);}
				if(round(anim_count) >= rm*6){ObjSprite2D_SetSourceRect(ObjectRender, x*6, y+offset, x*7, y*2+offset);}
				if(round(anim_count) >= rm*7){ObjSprite2D_SetSourceRect(ObjectRender, x*7, y+offset, x*8, y*2+offset);}
				if(round(anim_count) >= rm*8-1){anim_count = rm*4-1;}
			}
			if(anim_type==2){
				if(round(anim_count) >= 0){ObjSprite2D_SetSourceRect(ObjectRender, 0, y*2+offset, x, y*3+offset);}
				if(round(anim_count) >= rm){ObjSprite2D_SetSourceRect(ObjectRender, x, y*2+offset, x*2, y*3+offset);}
				if(round(anim_count) >= rm*2){ObjSprite2D_SetSourceRect(ObjectRender, x*2, y*2+offset, x*3, y*3+offset);}
				if(round(anim_count) >= rm*3){ObjSprite2D_SetSourceRect(ObjectRender, x*3, y*2+offset, x*4, y*3+offset);}
				if(round(anim_count) >= rm*4){ObjSprite2D_SetSourceRect(ObjectRender, x*4, y*2+offset, x*5, y*3+offset);bn = 0}
				if(round(anim_count) >= rm*5){ObjSprite2D_SetSourceRect(ObjectRender, x*5, y*2+offset, x*6, y*3+offset);}
				if(round(anim_count) >= rm*6){ObjSprite2D_SetSourceRect(ObjectRender, x*6, y*2+offset, x*7, y*3+offset);}
				if(round(anim_count) >= rm*7){ObjSprite2D_SetSourceRect(ObjectRender, x*7, y*2+offset, x*8, y*3+offset);}
				if(round(anim_count) >= rm*8-1){anim_count = rm*4-1;}
			}
		}else{
			ObjSprite2D_SetSourceRect(ObjectRender, 0, 144, 32, 192);
		}

		while(kittyglompactive){
			ObjRender_SetPosition(ObjectRender, GetPlayerX, GetPlayerY, 0);
			yield;
		}
		
		
		ObjSprite2D_SetDestCenter(ObjectRender);
		yield;
	}	
	task Blink{
		if(GetPlayerInvincibilityFrame>0){
			if(Uber==0){ObjRender_SetColor(ObjectRender, 255, 255, 255);}
			if(Uber!=0){ObjRender_SetColor(ObjectRender, 32, 32, 255);}
			Uberblink+=1;
			if(Uberblink>1){Uberblink=0;Uber++;if(Uber>1){Uber=0}}
		}else{
			ObjRender_SetColor(ObjectRender, 255, 255, 255);
		}
	}
}

task Spawn{
	let obj = GetPlayerObjectID;
	let y = 0;
	ObjRender_SetX(obj, GetStgFrameWidth/2);
	ObjRender_SetY(obj, GetStgFrameHeight-32);
	SetPlayerSpeed(0,0);
	IsPlayerSpawning = true;
	while(y<82){
		SetPlayerClip(11,11,GetStgFrameWidth-11,GetStgFrameHeight+64);	
		SetPlayerSpeed(0,0);
		ObjRender_SetY(obj, GetStgFrameHeight+32-y);
		y+=2;
		yield;
	}
	IsPlayerSpawning = false;
	SetPlayerSpeed(4,1.75);
}

task PlayerExplode(path){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.60);
	ObjPrim_SetTexture(obj, path);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 63, 63);
	ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);

	let alpha = 255;
	let scale = 0;
	loop(30){
		scale += 0.2;
		alpha -= 8;
		ObjRender_SetColor(obj, alpha, alpha, alpha);
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1);
		yield;
	}
	Obj_Delete(obj);
}

task TMagicCircle{
		let countVertex = 64;
		let listRadius = [];
		loop(countVertex)
		{
			listRadius = listRadius ~ [0];
		}

		let path = current ~ "Image/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;
		}
	}

task BlazingWheel{
	SetForbidPlayerShot(true);
	SetPlayerInvincibilityFrame(420);
	cantransform = false;

	let manage = GetSpellManageObject;
	let speed = GetPlayerSpeed;

	SetPlayerSpeed(1, 1);

	let obj = ObjSpell_Create;
	ObjSpell_Regist(obj);
	ObjSpell_SetDamage(obj, 10);
	ObjSpell_SetEraseShot(obj, true);
	ObjSpell_SetIntersectionCircle(obj, GetPlayerX, GetPlayerY, 96);

	let render1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(render1, current ~ "Image/Bomb_1.png");
	ObjSprite2D_SetSourceRect(render1, 0, 0, 256, 256);
	ObjSprite2D_SetDestCenter(render1);
	ObjRender_SetScaleXYZ(render1, 0, 0, 0);
	ObjRender_SetPosition(render1, GetPlayerX, GetPlayerY, 0);
	Obj_SetRenderPriority(render1, 0.31);

	let render2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(render2, current ~ "Image/Bomb_2.png");
	ObjSprite2D_SetSourceRect(render2, 0, 0, 256, 256);
	ObjSprite2D_SetDestCenter(render2);
	ObjRender_SetScaleXYZ(render2, 0, 0, 0);
	ObjRender_SetPosition(render2, GetPlayerX, GetPlayerY, 0);
	ObjRender_SetAlpha(render2, 160);
	Obj_SetRenderPriority(render2, 0.3);

	let s = 0;
	let s2 = 0;

	let a = 0;
	let a2 = 0;

	PlaySE(current ~ "Sound/se_nep00.wav");
	while(s < 1){
		ObjMove_SetPosition(obj, GetPlayerX, GetPlayerY);
		ObjSpell_SetIntersectionCircle(obj, GetPlayerX, GetPlayerY, 96);

		ObjRender_SetPosition(render1, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetPosition(render2, GetPlayerX, GetPlayerY, 0);

		ObjRender_SetScaleXYZ(render1, s, s, s);
		ObjRender_SetScaleXYZ(render2, s2, s2, s2);
		ObjRender_SetAngleZ(render1, a);
		ObjRender_SetAngleZ(render2, a2);
		s += 0.01;
		s2 += 0.015;
		a += 8;
		a2 -= 8;
		yield;
	}
	loop(180){
		ObjMove_SetPosition(obj, GetPlayerX, GetPlayerY);
		ObjSpell_SetIntersectionCircle(obj, GetPlayerX, GetPlayerY, 96);
		
		ObjRender_SetPosition(render1, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetPosition(render2, GetPlayerX, GetPlayerY, 0);

		ObjRender_SetAngleZ(render1, a);
		ObjRender_SetAngleZ(render2, a2);

		a += 8;
		a2 -= 8;
		yield;
	}
	while(s > 0){
		ObjMove_SetPosition(obj, GetPlayerX, GetPlayerY);

		ObjRender_SetPosition(render1, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetPosition(render2, GetPlayerX, GetPlayerY, 0);

		ObjRender_SetScaleXYZ(render1, s, s, s);
		ObjRender_SetScaleXYZ(render2, s2, s2, s2);
		ObjRender_SetAngleZ(render1, a);
		ObjRender_SetAngleZ(render2, a2);
		s -= 0.01;
		s2 -= 0.015;
		a += 8;
		a2 -= 8;
		yield;
	}
	Obj_Delete(obj);
	Obj_Delete(render1);
	Obj_Delete(render2);
	Obj_Delete(manage);


	SetForbidPlayerShot(false);
	SetPlayerSpeed(speed[0], speed[1]);
	cantransform = true;
}

task KittyGlomp{
	SetPlayerInvincibilityFrame(300);
	cantransform = false;

	let manage = GetSpellManageObject;
	let speed = GetPlayerSpeed;
	let pos = [GetPlayerX, GetPlayerY];

	kittyglompactive = true;

	let obj = ObjShot_Create(OBJ_SHOT);
	ObjShot_Regist(obj);
	ObjShot_SetDamage(obj, 25);
	ObjShot_SetPenetration(obj, 100000);
	ObjShot_SetEraseShot(obj, true);
	ObjShot_SetSpellFactor(obj, true);
	ObjShot_SetIntersectionCircleA1(obj, 32);
	ObjMove_SetPosition(obj, GetPlayerX, GetPlayerY);

	SetForbidPlayerShot(true);

	SetPlayerSpeed(0, 0);
	loop(60){yield;}
	ObjSprite2D_SetSourceRect(renderPlayerObject, 32, 144, 64, 192);
	

	let angle = -90;
	let enem = [GetStgFrameWidth/2, 96, 1000];
	let ar = GetIntersectionRegistedEnemyID;
	ascent(i in 0..(length(ar))){
		let dis = ((GetPlayerX - ObjMove_GetX(ar[i]))^2 + (GetPlayerY - ObjMove_GetY(ar[i]))^2)^0.5;
		if(dis<enem[2]){
			enem[2] = dis;
			enem[0] = ObjMove_GetX(ar[i]);
			enem[1] = ObjMove_GetY(ar[i]);
		}
	}
	enem[2] = 1000;

	let dir = atan2(enem[1] - GetPlayerY, enem[0] - GetPlayerX);
	let diff = dir - angle;
	while(diff >= 180) { diff -= 360; }
	while(diff < -180) { diff += 360; }
	let diffAbs = (|diff|);
	if(diffAbs <= 4){
		angle = dir;
	}else{
		angle += 4 * diff / diffAbs;
	}
	
	PlaySE(current ~ "Sound/se_boing.wav");
	ObjMove_SetDestAtWeight(obj, enem[0], enem[1], 5, 15);
	ObjRender_SetAngleZ(renderPlayerObject, angle+90);
	while(ObjMove_GetSpeed(obj) > 0){
		ObjShot_SetIntersectionCircleA1(obj, 32);
		ObjMove_SetPosition(GetPlayerObjectID, ObjMove_GetX(obj), ObjMove_GetY(obj));
		yield;
	}
	loop(70){PlaySE(current ~ "Sound/se_scratch.wav"); ObjShot_SetIntersectionCircleA1(obj, 32); yield;}
	PlaySE(current ~ "Sound/se_boingback.wav");
	ObjMove_SetDestAtWeight(obj, pos[0], pos[1], 5, 15);
	while(ObjMove_GetSpeed(obj) > 0){
		ObjShot_SetIntersectionCircleA1(obj, 32);
		ObjMove_SetPosition(GetPlayerObjectID, ObjMove_GetX(obj), ObjMove_GetY(obj));
		yield;
	}
	ObjRender_SetAngleZ(renderPlayerObject, 0);
	ObjSprite2D_SetSourceRect(renderPlayerObject, 0, 144, 32, 192);
	loop(30){yield;}

	Obj_Delete(manage);

	SetForbidPlayerShot(false);
	SetPlayerSpeed(speed[0], speed[1]);
	cantransform = true;

	kittyglompactive = false;
}

function GetDist(x1,y1,x2,y2){
	return ((x2-x1)^2+(y2-y1)^2)^0.5;
}

function GetAngle(x1,y1,x2,y2){
	return atan2(y2-y1,x2-x1);
}