#TouhouDanmakufu[Player]
#ID["YukaMarisa"]
#Title["Marisa and Yuka Team"]
#Text[" Marisa Kirisame [r] Shot - \"Loving Star Barrage\" [r] Yuka Kazami [r] Shot - \"The Flower's Inner Fury\" [r] Marisa Bomb - \"Stardust Reverie\" [r] Yuka Bomb - \"Master Spark\" [r] By Spectral Nexus"]
#Image["./YukaMarisaCutin.png"]
#ReplayName["YukaMarisa"]
#ScriptVersion[3]

let objPlayer = GetPlayerObjectID;
let CSD = GetCurrentScriptDirectory;
let shot = CSD ~ "./Shot.wav";
let spark = CSD ~ "./Spark.wav";
let wave = CSD ~ "./Wave.wav";
let graze = CSD ~ "./Graze.wav";
let death = CSD ~ "./Death.wav";
let spell = CSD ~ "./Spell.wav";
let life = CSD ~ "./Life.wav";
let item = CSD ~ "./Item.wav";
let ForceYuka = false;
let ForceMarisa = false;
@Initialize{

	let path = CSD ~ "./YukaMarisaShot.dnh";
	LoadPlayerShotData(path);
	LoadSound(shot);
	LoadSound(spark);
	LoadSound(graze);
	LoadSound(death);
	LoadSound(spell);
	LoadSound(life);
	LoadSound(item);
	
	ObjPlayer_AddIntersectionCircleA1(objPlayer,0,0,2,20);
	SetPlayerClip(11,18,GetStgFrameWidth-11,GetStgFrameHeight-18);
	SetPlayerSpeed(4,2);
	SetPlayerRebirthFrame(20);
	SetPlayerRebirthLossFrame(2.125);
	SetPlayerDownStateFrame(60);
	
	Spawn;
	Shot;
	Draw;
	Focus;
	
	Orb(50,5,25,10,8,false);
	Orb(-50,5,-25,10,8,true);
	Orb(30,-5,40,-10,8,true);
	Orb(-30,-5,-40,-10,8,false);
}

@Event{

	alternative(GetEventType)
	
	case(EV_REQUEST_SPELL){
		
		let spell = GetPlayerSpell;
		
		if(spell >= 1){
			if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD){
				YukaBomb;
			}
			else if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PULL || GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE){
				MarisaBomb;
			}
			SetScriptResult(true);
			SetPlayerSpell(spell-1);
		}
		
		else{
			SetScriptResult(false);
		}
		
	}
	
	case(EV_HIT){
		Death;
		PlaySE(death);
	}
	
	case(EV_GRAZE){
	
		PlaySE(graze);
	
	}
	
	case(EV_PLAYER_REBIRTH){
	
		SetPlayerInvincibilityFrame(180);
		Spawn;
		InvincibilityFlash;
		SetPlayerSpell(3);
	
	}
	
	case(EV_PLAYER_SHOOTDOWN){
	
	}
	
	case(EV_GET_ITEM){
	
		alternative(GetEventArgument(0))
		
		case(ITEM_SPELL){
			WriteLog("ITEM_SPELL");
			PlaySE(item);
			PlaySE(life);
			SetPlayerSpell( GetPlayerSpell + 1 );
		}
		case(ITEM_SPELL_S){
			WriteLog("ITEM_SPELL_S");
			PlaySE(item);
			PlaySE(life);
			SetPlayerSpell( GetPlayerSpell + 1 );
		}
		case(ITEM_1UP){
			WriteLog("ITEM_1UP");
			PlaySE(life);
			PlaySE(item);
			SetPlayerSpell( GetPlayerLife + 1 );
		}
		case(ITEM_1UP){
			WriteLog("ITEM_1UP_S");
			PlaySE(life);
			PlaySE(item);
			SetPlayerSpell( GetPlayerLife + 1 );
		}
		others{
			PlaySE(item);
		}
	}
}

@MainLoop{
	yield;
}

@Finalize{
}

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

task Spawn{
	let y = 0;
	SetPlayerSpeed(0,0);
	SetPlayerClip(11,18,GetStgFrameWidth-11,GetStgFrameHeight+32);
	ObjRender_SetY(objPlayer,GetStgFrameHeight+32);
	
	while(y<82){
	ObjRender_SetY(objPlayer,GetStgFrameHeight+32-y);
	y+=2;
	yield;
	}
	SetPlayerClip(11,18,GetStgFrameWidth-11,GetStgFrameHeight-18);
	SetPlayerSpeed(4,2);
}

task Orb(x2,y2,x1,y1,time,dir){	
	let path = CSD ~ "./Familiars.png";
	let f = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	
	ObjPrim_SetTexture(obj,path);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 32, 32);
	ObjSprite2D_SetDestRect(obj, 0, 0, 16, 16);
    ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
	
	ObjPrim_SetTexture(obj1,path);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 32, 32);
	ObjSprite2D_SetDestRect(obj1, 0, 0, 16, 16);
    ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1,GetPlayerX,GetPlayerY,0);
	
	OrbShot;
	while(!Obj_IsDeleted(objPlayer)){
	
		while(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD  || ForceYuka == true){
	
			ObjRender_SetDestAtFrame(obj,GetPlayerX+x1,GetPlayerY+y1,time);
			ObjRender_SetDestAtFrame(obj1,GetPlayerX+x1,GetPlayerY+y1,time);
			ObjRender_SetAlpha(obj1,0);
			ObjRender_SetScaleXYZ(obj,0.8,0.8,0.8);
			
			ObjSprite2D_SetSourceRect(obj, 32, 0, 64, 32);
			if(ObjRender_GetX(obj)<=GetPlayerX && ObjRender_GetX(obj)<=GetPlayerX){
			ObjRender_SetAngleZ(obj,f);
			}
			if(ObjRender_GetX(obj)>=GetPlayerX && ObjRender_GetX(obj)>=GetPlayerX){
			ObjRender_SetAngleZ(obj,-f);
			}
			
			f+=7;
			yield;
		}
		
		while(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PULL || GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE  || ForceMarisa == true){
	
			ObjRender_SetDestAtFrame(obj,GetPlayerX+x2,GetPlayerY+y2,time);
			ObjRender_SetDestAtFrame(obj1,GetPlayerX+x2,GetPlayerY+y2,time);
			ObjRender_SetAlpha(obj1,255);
			ObjRender_SetScaleXYZ(obj,0.6,0.6,0.6);
			ObjRender_SetScaleXYZ(obj1,0.4,0.4,0.4);
			
			if(ObjRender_GetX(obj)<=GetPlayerX && ObjRender_GetX(obj)<=GetPlayerX){
				ObjSprite2D_SetSourceRect(obj, 0, 0, 32, 32);
				ObjSprite2D_SetSourceRect(obj1, 0, 0, 32, 32);
				ObjRender_SetAngleZ(obj,-f);
				ObjRender_SetAngleZ(obj1,f);
				f+=7;
			}
			
			if(ObjRender_GetX(obj)>=GetPlayerX && ObjRender_GetX(obj)>=GetPlayerX){
				ObjSprite2D_SetSourceRect(obj, 0, 0, 32, 32);
				ObjSprite2D_SetSourceRect(obj1, 0, 0, 32, 32);
				ObjRender_SetAngleZ(obj,f);
				ObjRender_SetAngleZ(obj1,-f);
				f+=7;
			}
			
			yield;
		}
		yield;
		
	}
	
	task OrbShot{	
			let spin = 0;
			let x;
			let y;
			while(!Obj_IsDeleted(objPlayer)){
					
					if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD && (GetVirtualKeyState(VK_SLOWMOVE)==KEY_FREE || GetVirtualKeyState(VK_SLOWMOVE)==KEY_PULL) && GetPlayerState==STATE_NORMAL && ObjRender_GetX(obj)<=GetPlayerX && ObjRender_GetX(obj)<=GetPlayerX || ForceMarisa == true && IsPermitPlayerShot == true){
						
						PlaySE(shot);
						CreatePlayerShot(ObjRender_GetX(obj),ObjRender_GetY(obj)-16,15,270,1,2,3);
						CreatePlayerShot(ObjRender_GetX(obj),ObjRender_GetY(obj)-16,25,260,1,2,1);
						CreatePlayerShot(ObjRender_GetX(obj),ObjRender_GetY(obj)-16,25,250,1,2,1);
					
					}	
					
					if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD && (GetVirtualKeyState(VK_SLOWMOVE)==KEY_FREE || GetVirtualKeyState(VK_SLOWMOVE)==KEY_PULL) && GetPlayerState==STATE_NORMAL && ObjRender_GetX(obj)>=GetPlayerX && ObjRender_GetX(obj)>=GetPlayerX  || ForceMarisa == true && IsPermitPlayerShot == true){
						
						PlaySE(shot);
						CreatePlayerShot(ObjRender_GetX(obj),ObjRender_GetY(obj)-16,15,270,1,2,3);
						CreatePlayerShot(ObjRender_GetX(obj),ObjRender_GetY(obj)-16,25,280,1,2,1);
						CreatePlayerShot(ObjRender_GetX(obj),ObjRender_GetY(obj)-16,25,290,1,2,1);
					
					}
					
					if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD && (GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD || GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH) && GetPlayerState==STATE_NORMAL && ObjRender_GetX(obj)<=GetPlayerX && ObjRender_GetX(obj)<=GetPlayerX  || ForceYuka == true && IsPermitPlayerShot == true){
						
						PlaySE(shot);
						CreatePlayerShot(ObjRender_GetX(obj),ObjRender_GetY(obj)-16,20,265,1,2,2);
						CreatePlayerShot(ObjRender_GetX(obj),ObjRender_GetY(obj)-16,20,260,1,2,2);
					
					}
					
					if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD && (GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD || GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH) && GetPlayerState==STATE_NORMAL && ObjRender_GetX(obj)>=GetPlayerX && ObjRender_GetX(obj)>=GetPlayerX  || ForceYuka == true && IsPermitPlayerShot == true){
						
						PlaySE(shot);
						CreatePlayerShot(ObjRender_GetX(obj),ObjRender_GetY(obj)-16,20,275,1,2,2);
						CreatePlayerShot(ObjRender_GetX(obj),ObjRender_GetY(obj)-16,20,280,1,2,2);
		
					}
						
				task CreatePlayerShot(x,y,speed,angle,pene,damage,graphic){
					let shotObj = ObjShot_Create(OBJ_SHOT);
					ObjMove_SetPosition(shotObj,x,y);
					ObjMove_SetSpeed(shotObj,speed);
					ObjMove_SetAngle(shotObj,angle);
					ObjShot_SetGraphic(shotObj,graphic);
					ObjShot_SetDamage(shotObj,damage*0.625,);
					ObjShot_SetPenetration(shotObj,pene*1.33);
					ObjShot_Regist(shotObj);
					while(!Obj_IsDeleted(shotObj)){
						if(ObjMove_GetX(shotObj)<-1 || ObjMove_GetX(shotObj)>GetStgFrameWidth+1 || ObjMove_GetY(shotObj)<-1 || ObjMove_GetY(shotObj)>GetStgFrameHeight+1){return;}
						x = ObjMove_GetX(shotObj);
						y = ObjMove_GetY(shotObj);
						yield;
					}
					BulletFade(obj,x,y,angle,graphic*10);
				}
							
				wait(5);
				spin+=10;
			}
		}
		Obj_Delete(obj);
		Obj_Delete(obj1);
}

task BulletFade(id,x,y,angle,graphic){
	if(ObjMove_GetX(id)<-1 || ObjMove_GetX(id)>GetStgFrameWidth+1 || ObjMove_GetY(id)<-1 || ObjMove_GetY(id)>GetStgFrameHeight+1){return;}
	let alpha = 255;
	let fadeObj = ObjShot_Create(OBJ_SHOT);
	ObjMove_SetPosition(fadeObj,x,y);
	ObjMove_SetSpeed(fadeObj,2);
	ObjMove_SetAngle(fadeObj,angle);
	ObjShot_SetGraphic(fadeObj,graphic);
	ObjRender_SetBlendType(fadeObj, BLEND_ADD_ARGB);
	ObjShot_SetDamage(fadeObj,0);
	ObjShot_SetPenetration(fadeObj,9999);
	ObjShot_Regist(fadeObj);
	loop(10){
		ObjRender_SetColor(fadeObj,alpha,alpha,alpha);
		alpha -= 25.5;
		yield;
	}
	Obj_Delete(fadeObj);
}

task Shot{
	while(!Obj_IsDeleted(objPlayer)){
		if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD && (GetVirtualKeyState(VK_SLOWMOVE)==KEY_FREE || GetVirtualKeyState(VK_SLOWMOVE)==KEY_PULL) && GetPlayerState==STATE_NORMAL && IsPermitPlayerShot == true){
		
		PlaySE(shot);
		CreatePlayerShot(GetPlayerX-8,GetPlayerY-32,10,270,1,1.5,3);
		CreatePlayerShot(GetPlayerX+8,GetPlayerY-32,10,270,1,1.5,3);
	
		}
		
		if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD && (GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD || GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH) && GetPlayerState==STATE_NORMAL && IsPermitPlayerShot == true){
		
		PlaySE(shot);
		CreatePlayerShot(GetPlayerX-28,GetPlayerY-24,15,270,1,0.5,2);
		CreatePlayerShot(GetPlayerX-10,GetPlayerY-24,15,270,1,1,2);
		CreatePlayerShot(GetPlayerX+10,GetPlayerY-24,15,270,1,1,2);
		CreatePlayerShot(GetPlayerX+28,GetPlayerY-24,15,270,1,0.5,2);
		CreatePlayerShot(GetPlayerX-28,GetPlayerY-36,15,270,1,0.5,2);
		CreatePlayerShot(GetPlayerX-10,GetPlayerY-36,15,270,1,1,2);
		CreatePlayerShot(GetPlayerX+10,GetPlayerY-36,15,270,1,1,2);
		CreatePlayerShot(GetPlayerX+28,GetPlayerY-36,15,270,1,0.5,2);
	
		
		}
		
				task CreatePlayerShot(x,y,speed,angle,pene,damage,graphic){
					let shotObj = ObjShot_Create(OBJ_SHOT);
					ObjMove_SetPosition(shotObj,x,y);
					ObjMove_SetSpeed(shotObj,speed*2);
					ObjMove_SetAngle(shotObj,angle);
					ObjShot_SetGraphic(shotObj,graphic);
					ObjShot_SetDamage(shotObj,damage*0.5);
					ObjShot_SetPenetration(shotObj,pene*2);
					ObjShot_Regist(shotObj);
					while(!Obj_IsDeleted(shotObj)){
						if(ObjMove_GetX(shotObj)<-1 || ObjMove_GetX(shotObj)>GetStgFrameWidth+1 || ObjMove_GetY(shotObj)<-1 || ObjMove_GetY(shotObj)>GetStgFrameHeight+1){return;}
						x = ObjMove_GetX(shotObj);
						y = ObjMove_GetY(shotObj);
						yield;
					}
					BulletFade(	1,x,y,angle,graphic*10);
				}
		wait(2);
	}
	
	task BulletFade(id,x,y,angle,graphic){
	if(ObjMove_GetX(id)<-1 || ObjMove_GetX(id)>GetStgFrameWidth+1 || ObjMove_GetY(id)<-1 || ObjMove_GetY(id)>GetStgFrameHeight+1){return;}
	
	let alpha = 255;
	let fadeObj = ObjShot_Create(OBJ_SHOT);
	ObjMove_SetPosition(fadeObj,x,y);
	ObjMove_SetSpeed(fadeObj,2);
	ObjMove_SetAngle(fadeObj,angle);
	ObjRender_SetBlendType(fadeObj, BLEND_ADD_ARGB);
	ObjShot_SetGraphic(fadeObj,graphic);
	ObjShot_SetDamage(fadeObj,0);
	ObjShot_SetPenetration(fadeObj,9999);
	ObjShot_Regist(fadeObj);
	loop(10){
		ObjRender_SetColor(fadeObj,alpha,alpha,alpha);
		alpha -= 25.5;
		yield;
	}
	Obj_Delete(fadeObj);
}
	
}

//Task Made By Ultima. Thanks!
task ObjRender_SetDestAtFrame(obj, x, y, frames){
    let ox = ObjRender_GetX(obj);
    let oy = ObjRender_GetY(obj);
    let xc = (x-ox)/frames;
    let yc = (y-oy)/frames;
    loop(frames){
        ox += xc;
        oy += yc;
        ObjRender_SetX(obj, ox);
        ObjRender_SetY(obj, oy);
        yield;
    }
}

task Draw{
	let f = 0;
	let f2 = 0;
	let f3 = 0;
	let moveframe = 2;
	let idleframe = 3.75;
	let moveframe2 = 2.75;
	let idleframe2 = 3.25;
	let path = CSD ~ "./YukaSprite.png";
	let path2 = CSD ~ "./MariSprite.png";
	
	ObjRender_SetPosition(objPlayer,GetPlayerX,GetPlayerY,0);
		InvincibilityFlash;
	while(!Obj_IsDeleted(objPlayer) && (GetPlayerState != STATE_HIT || GetPlayerState != STATE_DOWN || GetPlayerState != STATE_END)){
		while(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD || ForceYuka == true){
	
		ObjPrim_SetTexture(objPlayer,path);
		
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
			if(f2>=0 && f2<moveframe){ObjSprite2D_SetSourceRect(objPlayer,0,48,32,96);}
			if(f2>=moveframe && f2<moveframe*2){ObjSprite2D_SetSourceRect(objPlayer,32,48,64,96);}
			if(f2>=moveframe*2 && f2<moveframe*3){ObjSprite2D_SetSourceRect(objPlayer,64,48,96,96);}
			if(f2>=moveframe*3 && f2<moveframe*4){ObjSprite2D_SetSourceRect(objPlayer,96,48,128,96);}
			if(f2>=moveframe*4 && f2<moveframe*5){ObjSprite2D_SetSourceRect(objPlayer,128,48,160,96);}
			if(f2>=moveframe*5 && f2<moveframe*6){ObjSprite2D_SetSourceRect(objPlayer,160,48,192,96);}
			if(f2>=moveframe*6 && f2<moveframe*7){ObjSprite2D_SetSourceRect(objPlayer,192,48,224,96);}
			if(f2>=moveframe*7 && f2<moveframe*8){ObjSprite2D_SetSourceRect(objPlayer,224,48,256,96);}
			if(f2>=moveframe*8){f2 = moveframe*4;}
			ObjRender_SetScaleXYZ(objPlayer,1,1,0);
			
			f2++;
			f = 0;
			f3 = 0;
		}
		
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
			if(f3>=0 && f3<moveframe){ObjSprite2D_SetSourceRect(objPlayer,0,96,32,144);}
			if(f3>=moveframe && f3<moveframe*2){ObjSprite2D_SetSourceRect(objPlayer,32,96,64,144);}
			if(f3>=moveframe*2 && f3<moveframe*3){ObjSprite2D_SetSourceRect(objPlayer,64,96,96,144);}
			if(f3>=moveframe*3 && f3<moveframe*4){ObjSprite2D_SetSourceRect(objPlayer,96,96,128,144);}
			if(f3>=moveframe*4 && f3<moveframe*5){ObjSprite2D_SetSourceRect(objPlayer,128,96,160,144);}
			if(f3>=moveframe*5 && f3<moveframe*6){ObjSprite2D_SetSourceRect(objPlayer,160,96,192,144);}
			if(f3>=moveframe*6 && f3<moveframe*7){ObjSprite2D_SetSourceRect(objPlayer,192,96,224,144);}
			if(f3>=moveframe*7 && f3<moveframe*8){ObjSprite2D_SetSourceRect(objPlayer,224,96,256,144);}
			if(f3>=moveframe*8){f3 = moveframe*4;}
			ObjRender_SetScaleXYZ(objPlayer,1,1,0);
			
			f3++;
			f = 0;
			f2 = 0;
		}
		
		else{
			if(f>=0 && f<idleframe){ObjSprite2D_SetSourceRect(objPlayer,0,0,32,48);}
			if(f>=idleframe && f<idleframe*2){ObjSprite2D_SetSourceRect(objPlayer,32,0,64,48);}
			if(f>=idleframe*2 && f<idleframe*3){ObjSprite2D_SetSourceRect(objPlayer,64,0,96,48);}
			if(f>=idleframe*3 && f<idleframe*4){ObjSprite2D_SetSourceRect(objPlayer,96,0,128,48);}
			if(f>=idleframe*4 && f<idleframe*5){ObjSprite2D_SetSourceRect(objPlayer,128,0,160,48);}
			if(f>=idleframe*5 && f<idleframe*6){ObjSprite2D_SetSourceRect(objPlayer,160,0,192,48);}
			if(f>=idleframe*6 && f<idleframe*7){ObjSprite2D_SetSourceRect(objPlayer,192,0,224,48);}
			if(f>=idleframe*7 && f<idleframe*8){ObjSprite2D_SetSourceRect(objPlayer,224,0,256,48);}
			if(f>=idleframe*8){f = 0;}
			ObjRender_SetScaleXYZ(objPlayer,1,1,0);
			
			f++;
			f2 = 0;
			f3 = 0;
		}
		
		ObjSprite2D_SetDestRect(objPlayer, -16,-24,16,24);
		ObjSprite2D_SetDestCenter(objPlayer);
		yield;
	}
	
	while(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PULL || GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE || ForceMarisa == true){
	
		ObjPrim_SetTexture(objPlayer,path2);
		
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
			if(f2>=0 && f2<moveframe2){ObjSprite2D_SetSourceRect(objPlayer,0,48,32,96);}
			if(f2>=moveframe2 && f2<moveframe2*2){ObjSprite2D_SetSourceRect(objPlayer,32,48,64,96);}
			if(f2>=moveframe2*2 && f2<moveframe2*3){ObjSprite2D_SetSourceRect(objPlayer,64,48,96,96);}
			if(f2>=moveframe2*3 && f2<moveframe2*4){ObjSprite2D_SetSourceRect(objPlayer,96,48,128,96);}
			if(f2>=moveframe2*4 && f2<moveframe2*5){ObjSprite2D_SetSourceRect(objPlayer,128,48,160,96);}
			if(f2>=moveframe2*5 && f2<moveframe2*6){ObjSprite2D_SetSourceRect(objPlayer,160,48,192,96);}
			if(f2>=moveframe2*6 && f2<moveframe2*7){ObjSprite2D_SetSourceRect(objPlayer,192,48,224,96);}
			if(f2>=moveframe2*7 && f2<moveframe2*8){ObjSprite2D_SetSourceRect(objPlayer,224,48,256,96);}
			if(f2>=moveframe2*8){f2 = moveframe2*4;}
			ObjRender_SetScaleXYZ(objPlayer,1,1,0);
			
			f2++;
			f = 0;
			f3 = 0;
		}
		
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
			if(f3>=0 && f3<moveframe2){ObjSprite2D_SetSourceRect(objPlayer,0,96,32,143);}
			if(f3>=moveframe2 && f3<moveframe2*2){ObjSprite2D_SetSourceRect(objPlayer,32,96,64,143);}
			if(f3>=moveframe2*2 && f3<moveframe2*3){ObjSprite2D_SetSourceRect(objPlayer,64,96,96,143);}
			if(f3>=moveframe2*3 && f3<moveframe2*4){ObjSprite2D_SetSourceRect(objPlayer,96,96,128,143);}
			if(f3>=moveframe2*4 && f3<moveframe2*5){ObjSprite2D_SetSourceRect(objPlayer,128,96,160,144);}
			if(f3>=moveframe2*5 && f3<moveframe2*6){ObjSprite2D_SetSourceRect(objPlayer,160,96,192,144);}
			if(f3>=moveframe2*6 && f3<moveframe2*7){ObjSprite2D_SetSourceRect(objPlayer,192,96,224,144);}
			if(f3>=moveframe2*7 && f3<moveframe2*8){ObjSprite2D_SetSourceRect(objPlayer,224,96,256,144);}
			if(f3>=moveframe2*8){f3 = moveframe2*4;}
			ObjRender_SetScaleXYZ(objPlayer,1,1,0);
			
			f3++;
			f = 0;
			f2 = 0;
		}
		
		else{
			if(f>=0 && f<idleframe2){ObjSprite2D_SetSourceRect(objPlayer,0,0,32,48);}
			if(f>=idleframe2 && f<idleframe2*2){ObjSprite2D_SetSourceRect(objPlayer,32,0,64,48);}
			if(f>=idleframe2*2 && f<idleframe2*3){ObjSprite2D_SetSourceRect(objPlayer,64,0,96,48);}
			if(f>=idleframe2*3 && f<idleframe2*4){ObjSprite2D_SetSourceRect(objPlayer,96,0,128,48);}
			if(f>=idleframe2*4 && f<idleframe2*5){ObjSprite2D_SetSourceRect(objPlayer,128,0,160,48);}
			if(f>=idleframe2*5 && f<idleframe2*6){ObjSprite2D_SetSourceRect(objPlayer,160,0,192,48);}
			if(f>=idleframe2*6 && f<idleframe2*7){ObjSprite2D_SetSourceRect(objPlayer,192,0,224,48);}
			if(f>=idleframe2*7 && f<idleframe2*8){ObjSprite2D_SetSourceRect(objPlayer,224,0,256,48);}
			if(f>=idleframe2*8){f = 0;}
			ObjRender_SetScaleXYZ(objPlayer,1,1,0);
			
			f++;
			f2 = 0;
			f3 = 0;
		}
		
		ObjSprite2D_SetDestRect(objPlayer, -16,-24,16,24);
		ObjSprite2D_SetDestCenter(objPlayer);
		yield;
	}
	yield;
	}
}

task InvincibilityFlash{

	let inv = GetPlayerInvincibilityFrame;
	
	while(inv > 0){
	
		inv = GetPlayerInvincibilityFrame;
		
		ascent(i in 0..2){
			ObjRender_SetColor(objPlayer,255*i,255*i,255);
			loop(3){yield;}
		}
		
		loop(2){yield;}
	}
}

task Focus{
		
	let focusObj = ObjPrim_Create(OBJ_SPRITE_2D);
	let focusObj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let path = CSD ~ "./Focus.png";
	let alpha = 0;
	let spin = 0;
	let scale = 0;
		
	ObjPrim_SetTexture(focusObj,path);
	ObjSprite2D_SetSourceRect(focusObj,0,0,63,63);
	ObjSprite2D_SetDestCenter(focusObj);
	ObjSprite2D_SetDestRect(focusObj,-32,-32,32,32);
	ObjPrim_SetTexture(focusObj2,path);
	ObjSprite2D_SetSourceRect(focusObj2,0,0,63,63);
	ObjSprite2D_SetDestCenter(focusObj2);
	ObjSprite2D_SetDestRect(focusObj2,-32,-32,32,32);
	
	while(!Obj_IsDeleted(objPlayer)){
	
		spin+=3;
	
		if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD && GetPlayerState==STATE_NORMAL){
			if( alpha <= 255 ){
				alpha+=18;
			}
			if( scale <= 1 ){
				scale+=0.14;
			}
			
			ObjRender_SetScaleXYZ(focusObj,scale,scale,1);
			ObjRender_SetAngleXYZ(focusObj,0,0,spin);
			ObjRender_SetPosition(focusObj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetAlpha(focusObj,alpha);
			ObjRender_SetAngleXYZ(focusObj2,0,0,-spin);
			ObjRender_SetScaleXYZ(focusObj2,scale,scale,1);
			ObjRender_SetPosition(focusObj2,GetPlayerX,GetPlayerY,0);
			ObjRender_SetAlpha(focusObj2,alpha);
			
		}
		
		if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_FREE && GetPlayerState==STATE_NORMAL){
			if( alpha >= 0 ){
				alpha-=18;
			}
			if( scale >= 0 ){
				scale-=0.14;
			}
			
			ObjRender_SetScaleXYZ(focusObj,scale,scale,1);
			ObjRender_SetAngleXYZ(focusObj,0,0,spin);
			ObjRender_SetPosition(focusObj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetAlpha(focusObj,alpha);
			ObjRender_SetScaleXYZ(focusObj2,scale,scale,1);
			ObjRender_SetAngleXYZ(focusObj2,0,0,-spin);
			ObjRender_SetPosition(focusObj2,GetPlayerX,GetPlayerY,0);
			ObjRender_SetAlpha(focusObj,alpha);
			
		}
		
		yield;
		while(GetPlayerState == STATE_HIT || GetPlayerState == STATE_DOWN || GetPlayerState == STATE_END){
			yield;
			ObjRender_SetScaleXYZ(focusObj,0,0,0);
			ObjRender_SetAlpha(focusObj,0);
			ObjRender_SetScaleXYZ(focusObj2,0,0,0);
			ObjRender_SetAlpha(focusObj2,0);
		}
	}
		
}

task Death{
	DestructionA1(objPlayer,GetPlayerX,GetPlayerY,32);
}

//From Ultima. Thanks!
  task DestructionA1(objEnemy,x,y,r){
        let angle = 0;
        loop(1){
        angle+=5;
        loop(r){
        Cherry(x,y,angle);
              angle+=360/r;
                 }
        yield;
        }
        }
        task Cherry(x,y,a){
        let path2 = GetCurrentScriptDirectory ~ "Death.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);

        let angle = a;
        let speed = rand(0.5,1.5);
        let sa = round(rand(0,359));
        let xm = speed*cos(angle);
        let ym = speed*sin(angle);
        let al = rand(1.5,2);
        let alpha = 0;
        let ora = 255;
        ObjRender_SetColor(obj,255,0,0);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(obj, 62);
	ObjPrim_SetTexture(obj, path2);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 32,24);
        let s = rand(0.5,1);
        ObjSprite2D_SetDestCenter(obj);
        ObjRender_SetScaleXYZ(obj,s,s,s);
        ObjRender_SetAlpha(obj, ora);
        ObjRender_SetPosition(obj,x,y,0);

        loop(ora/al){
        alpha+=3;
        ora -=al*2;

        ObjRender_SetAlpha(obj, ora);
        ObjRender_SetPosition(obj,ObjRender_GetX(obj)+xm,ObjRender_GetY(obj)+ym,0);
        ObjRender_SetAngleXYZ(obj,sa+alpha,sa-alpha,sa+alpha);
        yield;
        }
		Obj_Delete(obj);

}
	
task YukaBomb{
	SetPlayerInvincibilityFrame(330);
	InvincibilityFlash;
	let manageObj = GetSpellManageObject;
	SetPlayerSpeed(1,1);
	PlaySE(spell);
	PlaySE(spark);
	Spark;
	AC;
	Shake;
	wait(45);
	Spark2;
	wait(275);
	SetPlayerSpeed(4,2);
	Obj_Delete(manageObj);
	
	task Spark{
		let sparkObj = ObjPrim_Create(OBJ_SPRITE_2D);
		let hitObj = ObjSpell_Create;
		let hitObj2 = ObjSpell_Create;
		ObjSpell_Regist(hitObj);
		ObjSpell_Regist(hitObj2);
		ObjSpell_SetDamage(hitObj,1.625);
		ObjSpell_SetDamage(hitObj2,1.625);
		let spin = 0;
		
		ObjPrim_SetTexture(sparkObj, CSD ~ "pl01b.png");
		ObjSprite2D_SetSourceRect(sparkObj,0,0,256,128);
		ObjSprite2D_SetDestRect(sparkObj,4,-64,256,64);
		ObjRender_SetScaleXYZ(sparkObj,2,2.6,0);
		ObjRender_SetBlendType(sparkObj,BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(sparkObj,50);
		
		loop(90){
			ObjSpell_SetIntersectionCircle(hitObj,ObjRender_GetX(sparkObj),ObjRender_GetY(sparkObj)-100,150);
			ObjSpell_SetIntersectionCircle(hitObj2,ObjRender_GetX(sparkObj),ObjRender_GetY(sparkObj)-356,150);
			ObjRender_SetPosition(sparkObj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetAngleXYZ(sparkObj,0,90-90*sin(spin),270);
			spin+=1;
			yield;
		}
		loop(120){
			ObjSpell_SetIntersectionCircle(hitObj,ObjRender_GetX(sparkObj),ObjRender_GetY(sparkObj)-100,150);
			ObjSpell_SetIntersectionCircle(hitObj2,ObjRender_GetX(sparkObj),ObjRender_GetY(sparkObj)-356,150);
			ObjRender_SetPosition(sparkObj,GetPlayerX,GetPlayerY,0);
			yield;
		}
		loop(90){
			ObjSpell_SetIntersectionCircle(hitObj,ObjRender_GetX(sparkObj),ObjRender_GetY(sparkObj)-100,150);
			ObjSpell_SetIntersectionCircle(hitObj2,ObjRender_GetX(sparkObj),ObjRender_GetY(sparkObj)-356,150);
			ObjRender_SetPosition(sparkObj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetAngleXYZ(sparkObj,0,90*sin(spin)-93,270);
			spin+=1;
			yield;
		}
		Obj_Delete(sparkObj);
		Obj_Delete(hitObj);
		Obj_Delete(hitObj2);
	}

	task Spark2{
		loop(25){
			sparkpart;
			
			wait(7.5);
		}
		task sparkpart{
			let sparkObj = ObjPrim_Create(OBJ_SPRITE_2D);
			let move = 0;
			let move2 = 0;
			ObjPrim_SetTexture(sparkObj,GetCurrentScriptDirectory ~ "pl01b2.png");
			ObjSprite2D_SetDestRect(sparkObj,0,0,96,128);
			ObjSprite2D_SetSourceRect(sparkObj,0,0,96,256);
			ObjSprite2D_SetDestCenter(sparkObj);
			ObjRender_SetBlendType(sparkObj,BLEND_ADD_ARGB);
			ObjRender_SetAlpha(sparkObj,155);
			ObjRender_SetPosition(sparkObj,GetPlayerX-8,GetPlayerY-64,0);
			ObjRender_SetAngleXYZ(sparkObj,0,0,90);
			ObjRender_SetScaleXYZ(sparkObj,1.5,0,1);
			movetask;
			
			task movetask{
				loop(127){
					if(ObjRender_GetScaleY(sparkObj) < 1.2375){
						ObjRender_SetScaleXYZ(sparkObj,1.5,0+4*sin(move2),1);
						move2+=6;
						yield;
					}
					ObjRender_SetPosition(sparkObj,GetPlayerX-8,GetPlayerY-64-move,0);
					move+=25;
					yield;
				}
			wait(50);
			Obj_Delete(sparkObj);
			}
			
			wait(200);
			Obj_Delete(sparkObj);
			}
	}

	task Shake{
		let spin = 0;
		let r = GetCameraFocusX;
		loop(280){
			Set2DCameraFocusX(GetStgFrameWidth/2 + rand(-spin/100, spin/100));
			Set2DCameraFocusY(GetStgFrameHeight/2 + rand(-spin/100, spin/100));
			SetCameraFocusX(GetCameraFocusX + rand(-spin/100, spin/100));
			yield;
			spin+=10;
		}
		yield;
		Reset2DCamera();
		SetCameraFocusX(r);
	}
	
	task AC{
			ForceYuka = true;
			SetForbidPlayerShot(true);
			SetForbidPlayerSpell(true);
		loop(320){
			CollectAllItems;
			yield;
		}
		ForceYuka = false;
		SetForbidPlayerShot(false);
		SetForbidPlayerSpell(false);
	}
	
}

task MarisaBomb{
	SetPlayerInvincibilityFrame(240);
	InvincibilityFlash;
	let manageObj = GetSpellManageObject;
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	PlaySE(spell);
	ascent(i in 0..5){
		PlaySE(wave);
		ascent(k in 0..8){
		Star(k*(360/8),i,i);
		}
	}
	ForceMarisa = true;
	loop(120){
		CollectAllItems;	
		yield;
	}
	ForceMarisa = false;
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	Obj_Delete(manageObj);
	
	task Star(angle,side,color){
		let starObj = ObjPrim_Create(OBJ_SPRITE_2D);
		let hitObj = ObjSpell_Create;
		let spin = 0;
		let spin2 = 255;
		ObjSpell_Regist(hitObj);
		ObjSpell_SetDamage(hitObj,1.5);
		
		ObjPrim_SetTexture(starObj, CSD ~ "spellstar.png");
		ObjSprite2D_SetSourceRect(starObj,0,0,256,256);
		ObjSprite2D_SetDestRect(starObj,-64,-64,64,64);
		ObjRender_SetBlendType(starObj,BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(starObj,50);
		ObjRender_SetPosition(starObj,GetPlayerX,GetPlayerY,0);
		let px = GetPlayerX;
		let py = GetPlayerY;
		  alternative(color)
		  case(0){
		   ObjRender_SetColor(starObj,255,0,0);
		  }
		  case(1){
		   ObjRender_SetColor(starObj,255,128,0);
		  }
		  case(2){
		   ObjRender_SetColor(starObj,128,255,0);
		  }
		  case(3){
		   ObjRender_SetColor(starObj,0,255,128);
		  }
		  case(4){
		   ObjRender_SetColor(starObj,255,0,255);
		  }
		  let odd=1;
		  if(side%2==0){odd=-1;}
		  loop(280){
		   let ang0=angle-spin*odd;
		   let dist=spin*(2+side);
		   let sx=px+dist*cos(ang0);
		   let sy=py+dist*sin(ang0);
			ObjSpell_SetIntersectionCircle(hitObj,sx,sy,50);
		   ObjRender_SetPosition(starObj,sx,sy,0);
		   ObjRender_SetAngleZ(starObj,angle+spin*4);
		   spin+=1;
		   yield;
		   
		  }
		ascent(i in 0..127){
		   let ang0=angle-spin*odd;
		   let dist=spin*(2+side);
		   let sx=px+dist*cos(ang0);
		   let sy=py+dist*sin(ang0);
			ObjSpell_SetIntersectionCircle(hitObj,sx,sy,50);
		   ObjRender_SetPosition(starObj,sx,sy,0);
		   ObjRender_SetAngleZ(starObj,angle+spin*4);
		   spin+=1;
		  ObjRender_SetAlpha(starObj,255-i*2);
		  yield;
		}
	}
}