////////////////////////////

task ShotBaseAReimu(x){
	CreatePlayerShotA1(GetPlayerX+x,GetPlayerY+8, 35, 270, 120/60*3/2, 1, 1);
}

task ShotBaseBReimu(x,y,ang){
	CreatePlayerShotA1(x,y, 45, 270+ang, 180/60*3/8, 1, 2);
}

task SpellReimu{
	let manage = GetSpellManageObject;
	let dam = 3000/180;
	SetPlayerSpeed(speed[1],speed[1]); 
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	CutName("&quot;Slow Red Sphere&quot;",90);
	_PlaySE(4);
	ObjPrim_SetTexture(playerObj, CSD ~ "img/player_sprite_00.png");

	Effect;
	
	ObjPrim_SetTexture(playerObj, CSD ~ "img/player_sprite_0"~IntToString(GetCommonData("Shottype",0))~".png");
	SetPlayerSpeed(speed[0],speed[1]); 
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	Obj_Delete(manage);
	
	function Effect{
		SetPlayerInvincibilityFrame(120);
		Orbu(GetPlayerX,GetPlayerY);
		wait(120);
	}

	task Orbu(xa,let ya){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, CSD ~ "img/shot.png");
		ObjSprite2D_SetSourceRect(obj, 385, 385, 512, 512);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetPosition(obj,xa,ya,0);
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(obj,44);
		
		let d = ObjSpell_Create;
		ObjSpell_SetDamage(d,dam/3);
		ObjSpell_SetEraseShot(d,true);
		ObjSpell_Regist(d);

		let t = 1;

		ascent(a in 1..41){
			ObjRender_SetScaleXYZ(obj,a/10,a/10,0);
			ObjRender_SetPosition(obj,xa,ya-t,0);
			ObjRender_SetAngleZ(obj,ObjRender_GetAngleZ(obj)+15);
			ObjSpell_SetIntersectionCircle(d,xa,ya-t,100/2*a/10);
			t+=t/10;
			yield;
		}
		ObjSpell_SetDamage(d,dam);
		while(ya-t > -400){
			ObjRender_SetPosition(obj,xa,ya-t,0);
			ObjRender_SetAngleZ(obj,ObjRender_GetAngleZ(obj)+15);
			ObjSpell_SetIntersectionCircle(d,xa,ya-t,100/2*3);
			t+=t/70;
			yield;
		}
		Obj_Delete(obj);
	}
}

////////////////////////////

task ShotBaseAMarisa(x){
	CreatePlayerShotA1(GetPlayerX+x,GetPlayerY+8, 35, 270, 120/60*3/2, 1, 3);
}

task ShotBaseBMarisa(x,y,dir){
	let obj = CreatePlayerShotA1(x,y, 0, 270, 160/60*8/8, 1, 4);
	let t = 0;
	let x2 = x;
	let y2 = y;

	while(ObjMove_GetY(obj) > -40 && !Obj_IsDeleted(obj)){

		ObjMove_SetPosition(obj,x+30*dir*sin(t*180/6),y-30/2*t);
		ObjRender_SetAngleZ(obj,30*dir*sin(-t*180/6+90));

		x2 = x+30*dir*sin(t*180/6);
		y2 = y-30/2*t;

		t++;

		yield;
	}

	TExplosionA1(x2+rand(-15,15), y2+rand(-15,15), 32);
}

task SpellMarisa{
	let manage = GetSpellManageObject;
	let dam = 4000/150;
	SetPlayerSpeed(1,1); 
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	CutName("&quot;Laser Filled with Love I Guess&quot;",90);
	_PlaySE(4);
	ObjPrim_SetTexture(playerObj, CSD ~ "img/player_sprite_01.png");

	Effect;
	
	ObjPrim_SetTexture(playerObj, CSD ~ "img/player_sprite_0"~IntToString(GetCommonData("Shottype",0))~".png");
	SetPlayerSpeed(speed[0],speed[1]); 
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	Obj_Delete(manage);
	
	function Effect{
		SetPlayerInvincibilityFrame(180);
		MastaSpark();
		wait(180);
	}

	task MastaSpark(){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, CSD ~ "img/shot.png");
		ObjSprite2D_SetSourceRect(obj, 1,384,255,512);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(obj,60);
		ObjRender_SetScaleXYZ(obj,2.5,2,0);

		let obja = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obja, CSD ~ "img/b001.png");
		ObjSprite2D_SetSourceRect(obja, -40,-40,680,520);
		ObjSprite2D_SetDestRect(obja, -40,-40,680,520);
		ObjRender_SetBlendType(obja,BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(obja,62);
		
		let d = ObjSpell_Create;
		ObjSpell_SetDamage(d,dam);
		ObjSpell_SetEraseShot(d,true);
		ObjSpell_Regist(d);

		let t = 0;
		let angl = -90;

		ScreenShakeA1(15, 5);

		ascent(a in 1..16){
			ObjRender_SetScaleY(obj,2*a/15);
			ObjRender_SetAngleZ(obj,angl);
			ObjRender_SetPosition(obj,GetPlayerX+256*cos(angl),GetPlayerY+256*sin(angl),0);

			ObjRender_SetColorHSV(obj,t*15,255/2,255);
			ObjRender_SetAlpha(obja,a*50/15);

			t++;

			yield;
		}

		ScreenShakeA1(150, 15);

		loop(150){
			ObjRender_SetAngleZ(obj,angl);
			ObjRender_SetPosition(obj,GetPlayerX+256*cos(angl),GetPlayerY+256*sin(angl),0);
			ObjSpell_SetIntersectionLine(d,GetPlayerX,GetPlayerY,GetPlayerX+1024*cos(angl),GetPlayerY+1024*sin(angl),256/2);

			if(t%10 == 0){LETHING(t);}

			ObjRender_SetColorHSV(obj,t*15,255/2,255);
			ObjRender_SetAlpha(obja,50+100*sin(t*360*3/150));

			t++;

			yield;
			if(GetVirtualKeyState(VK_RIGHT) != KEY_FREE){
				angl+=0.05;
			}
			else if(GetVirtualKeyState(VK_LEFT) != KEY_FREE){
				angl-=0.05;
			}

		}

		ScreenShakeA1(15, 5);

		descent(a in 1..16){
			ObjRender_SetScaleY(obj,2*a/15);
			ObjRender_SetAngleZ(obj,angl);
			ObjRender_SetPosition(obj,GetPlayerX+256*cos(angl),GetPlayerY+256*sin(angl),0);

			ObjRender_SetColorHSV(obj,t*15,255/2,255);
			ObjRender_SetAlpha(obja,a*150/15);

			t++;

			yield;
		}

		Obj_Delete(obj);
		Obj_Delete(obja);

		task LETHING(let ta){
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obj, CSD ~ "img/shot.png");
			ObjSprite2D_SetSourceRect(obj, 257,255,353,511);
			ObjSprite2D_SetDestCenter(obj);
			ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
			Obj_SetRenderPriorityI(obj,61);
			ObjRender_SetScaleXYZ(obj,1.5,1.5,0);

			let r = 50;

			while(r < 800 && ta <= 160){

				ObjRender_SetScaleXYZ(obj,1.2+r/800*0.5,1.2+r/800*0.5,0);

				ObjRender_SetAngleZ(obj,angl+180);
				ObjRender_SetPosition(obj,GetPlayerX+r*cos(angl),GetPlayerY+r*sin(angl),0);

				ta++;

				r+= 40;

				yield;
			}

			Obj_Delete(obj);
		}
	}
}

////////////////////////////////////////

task TExplosionA1(x, y, r){
	let path = CSD ~ "img/charge_wave.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let dScale = 0;
		
	let xa = [rand(-15,15),180+rand(-15,15)][truncate(rand(0,1.99))];
	let ya = [rand(-15,15),180+rand(-15,15)][truncate(rand(0,1.99))];
		
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 0.65);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 128, 128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAngleXYZ(obj,xa,ya,0);
	ObjRender_SetPosition(obj,x,y,0);
	ObjRender_SetColorHSV(obj, rand(0,399), 128, 128);

	let scale = 0;
	let alpha = 255;
	while(alpha > 0)
	{
		ObjRender_SetAlpha(obj, alpha);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1);

		scale = r/66*sin(dScale);
		dScale+= 90/20;
		alpha -= 255/20;
		yield;
	}
	Obj_Delete(obj);
}

function FindTarget(obj){
	let enemy_id = GetIntersectionRegistedEnemyID;
	let mindis = 999;
	let h_obj;
	if(length(enemy_id)>0){
	h_obj = enemy_id[0];
	ascent(i in 0..length(enemy_id)){
	let dis = GetDistance(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetX(enemy_id[i]),ObjMove_GetY(enemy_id[i]));
		if(mindis>dis){
		mindis = dis;
		h_obj = enemy_id[i];
		}
	}
	return h_obj;
	}
	else
	{
	return -1;
	}
	
	function GetDistance(x1,y1,x2,y2){return((|((x2-x1)^2+(y2-y1)^2)^(1/2)|));}
}

task Homing(obj,h_obj,maxTraverse){
	let angle = ObjMove_GetAngle(obj);
	let dir = 0;
	let boss = GetIntersectionRegistedEnemyID;
    while(! Obj_IsDeleted(obj) && Obj_GetValueD(h_obj,"HomingOrNot",1)==1 && FindTarget(obj)!=-1) {
        ObjMove_SetAngle(obj, angle);
		maxTraverse += 0.5;
		if(length(boss)>=1){
		dir = atan2(ObjMove_GetY(h_obj) - ObjMove_GetY(obj),
                        ObjMove_GetX(h_obj) - ObjMove_GetX(obj));
						if(ObjRender_GetX(h_obj)==0 && ObjRender_GetY(h_obj)==0){
							if(FindTarget(obj)!=-1 && ObjMove_GetX(h_obj) > GetStgFrameWidth+40 && ObjMove_GetX(h_obj) < -40 && ObjMove_GetY(h_obj) > GetStgFrameHeight+40 && ObjMove_GetY(h_obj) < -40)
							{
								h_obj = FindTarget(obj);
							}
							else
							{
								break;
							}
						}
						}
        let diff = dir - angle;
        while(diff >= 180) { diff -= 360; }
        while(diff < -180) { diff += 360; }

        let diffAbs = (|diff|);
        if(diffAbs < maxTraverse) {
            angle = dir;
        } else {
            angle += maxTraverse * diff / diffAbs;
        }
		yield;
    }
}	

	task ScreenShakeA1(shaketime, intensity){//Python?
		let x = Get2DCameraX;
		let y = Get2DCameraY;
		while(shaketime>0){
			Set2DCameraFocusX(GetStgFrameWidth/2 + rand(-intensity, intensity));
			Set2DCameraFocusY(GetStgFrameHeight/2 + rand(-intensity, intensity));
			shaketime--;
			yield;
		}
		Set2DCameraFocusX(GetStgFrameWidth/2);
		Set2DCameraFocusY(GetStgFrameHeight/2);
		yield;
	}

///////////////////////////////////

task hitboxRender{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj, 0.54);
	ObjPrim_SetTexture(obj, CSD ~ "img/shot.png");
	ObjSprite2D_SetSourceRect(obj, 406, 6, 469, 69);
	ObjSprite2D_SetDestCenter(obj);
	
	let sc = 0;
	let z = 0;
	hitboxActive = true;
	
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
		
		if(sc < 1.4 && GetPlayerState!=STATE_DOWN){
			sc += 0.125;
			z  += 3;
			Pos;
		}else{break;}
		
		yield;
	}
	
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
		
		if(sc > 1 && GetPlayerState!=STATE_DOWN){
			sc -= 0.1;
			z  += 3;
			Pos;
		}
		else{break;}
		
		yield;
	}
	
	sc = 1;
	yield;
	
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
		
		if(GetPlayerState!=STATE_DOWN){
			z  += 3;
			Pos;
		}else{break;}
		yield;
	}
	
	hitboxActive = false;
	
	while(sc>0){
		sc -= 0.25;
		z  += 3;
		Pos;
		yield;
	}
	
	Obj_Delete(obj);
	
	task Pos{
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetScaleXYZ(obj, sc, sc, sc);
		ObjRender_SetAngleZ(obj,z);
	}
}

task Spawn{
	ObjMove_SetPosition(playerID,playerPosDeath[0],playerPosDeath[1]);
	SetPlayerInvincibilityFrame(120);
	SetForbidPlayerSpell(true);
	wait(120);
	SetForbidPlayerSpell(false);
}

task CutName(name,t){
	let obj = ObjText_Create;
		
	ObjText_SetText(obj,name);
	ObjText_SetFontType(obj, "MonoSpatial");
	Obj_SetRenderPriority(obj, 0.93);
	ObjRender_SetX(obj, 128);
	ObjRender_SetY(obj,480-64);
	ObjText_SetFontSize(obj,15);
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, 255, 120, 120);
	ObjText_SetFontBorderWidth(obj, 2);
	ObjText_SetFontBorderColor(obj, 0,0,0);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
		
	ascent(a in 1..61){
		ObjRender_SetAlpha(obj,255*sin(a*90/60));
		ObjText_SetFontSize(obj,25-min(20,a)/2);
		yield;
	}
		
	loop(t){
		yield;
	}
		
	ascent(a in 1..31){
		ObjRender_SetY(obj,480+(64)*sin(a*90/30-90));//50
		yield;
	}

	Obj_Delete(obj);
}