#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["LoLK Marisa (Lushie)"]
#Title["LoLK Marisa"]
#Text["Replicates LoLK Marisa but with lasers that have penetration[r]Spell:Disabled[r]By: Lushie/Lusushime"]
#Image["./Marisa.png"]
#ReplayName["LoLK Marisa Lushie"]

let objPlayer = GetPlayerObjectID();
let current = GetCurrentScriptDirectory();
let anisp = 5;
let aniwait = 0;
let Sound = [];
let SoundPath = [];
@Initialize{
    SetUpSound;
    SetPlayerSpeed(5,2);
    let path = current ~ "ShotData.dnh";
    LoadPlayerShotData(path);

    SetPlayerAutoItemCollectLine(100);
    ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 1, 5);
    Animate;
    Focus;
    NormalShot;
    SetPlayerClip(13,15,384-13,448-15);
    ObjMove_SetX(objPlayer,192);
    ObjMove_SetY(objPlayer,420);
    CallOptions;
}
task SetUpSound{

    SoundPath = [
    GetCurrentScriptDirectory~"./se/se_plst00.wav",
    GetCurrentScriptDirectory~"./se/se_graze.wav",
    GetCurrentScriptDirectory~"./se/se_tan00.wav",
    GetCurrentScriptDirectory~"./se/se_pldead00.wav",
    GetCurrentScriptDirectory~"./se/se_pldead01.wav",
    GetCurrentScriptDirectory~"./se/se_nep00.wav",
    GetCurrentScriptDirectory~"./se/se_slash.wav",
    GetCurrentScriptDirectory~"./se/se_laser.wav",
    GetCurrentScriptDirectory~"./se/se_msl.wav",
    GetCurrentScriptDirectory~"./se/se_msl2.wav"
    ];
    ascent(i in 0..length(SoundPath)){
    Sound = Sound~[ObjSound_Create];
    }
    ascent(i in 0..length(Sound)){
    ObjSound_Load(Sound[i],SoundPath[i]);
    ObjSound_SetSoundDivision(Sound[i],SOUND_SE);
    }
    ObjSound_SetVolumeRate(Sound[0],40);
    ObjSound_SetVolumeRate(Sound[2],80);
    ObjSound_SetVolumeRate(Sound[3],80);
    ObjSound_SetVolumeRate(Sound[5],80);
    ObjSound_SetVolumeRate(Sound[6],80);
    ObjSound_SetVolumeRate(Sound[7],15);
    ObjSound_SetVolumeRate(Sound[8],45);
    ObjSound_SetVolumeRate(Sound[9], 45);
    yield;
    tx = GetCameraFocusX();
    ty = GetCameraFocusY();
}
@MainLoop{yield;}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_SPELL)
	{
        
		let spell = GetPlayerSpell();
		if(spell >= 1)
		{

			SetScriptResult(true);
			//SetPlayerSpell(spell-1);
			//TSpell();

		}
		else
		{
			SetScriptResult(false);
		}
	}
	case(EV_HIT)
	{
        ObjSound_Play(Sound[3]);
        DestructionA1(objPlayer,GetPlayerX,GetPlayerY,60);

	}
	case(EV_GRAZE)
	{
        ObjSound_Play(Sound[1]);

	}
	case(EV_PLAYER_REBIRTH)
	{
 aniwait = 50;
         Respawn;
	}
	case(EV_PLAYER_SHOOTDOWN)
	{

	PlayerDie(GetPlayerX,GetPlayerY);
	SetPlayerSpell(3);

	}
	case(EV_GET_ITEM)
	{

		let itemType = GetEventArgument(0);
		alternative(itemType)
		case(ITEM_SPELL)
		{
			//WriteLog("ITEM_SPELL");
		}
		case(ITEM_SPELL_S)
		{
			//WriteLog("ITEM_SPELL_S");
		}
		case(ITEM_POWER)
		{
			//WriteLog("ITEM_POWER");
		}
		case(ITEM_POWER_S)
		{
			//WriteLog("ITEM_POWER_S");
		}
	}

}

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

task Respawn{
    SetPlayerClip(13,15,384-13,448-30);
    ObjMove_SetX(objPlayer,192);
    ObjMove_SetY(objPlayer,448+30);
    ObjRender_SetDestAtFrame(objPlayer,GetPlayerX,GetPlayerY-63,50);
    SetPlayerInvincibilityFrame(180);
    Blink;
    loop(50){yield;}
    SetPlayerClip(13,15,384-13,448-15);
}

task Focus{
    let obj = [ObjPrim_Create(OBJ_SPRITE_2D),ObjPrim_Create(OBJ_SPRITE_2D), ObjPrim_Create(OBJ_SPRITE_2D)];
    let timer = 0;
    let z = 0;
    let alpha = 0;
    let Target = current~"./etama2.png";
	Obj_SetRenderPriorityI(obj[0], 51);
	ObjPrim_SetTexture(obj[0], Target);
	ObjRender_SetBlendType(obj[0], BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(obj[0], 0, 18, 63, 78);
	ObjSprite2D_SetDestCenter(obj[0]);
    ObjRender_SetScaleXYZ(obj[0], 1, 1, 1);
	ObjRender_SetPosition(obj[0],GetPlayerX,GetPlayerY,0);

    let scale = 3;
    ascent(i in 0..length(obj)-1){
    	i+=1;
		Obj_SetRenderPriorityI(obj[i], 39);
		ObjPrim_SetTexture(obj[i], Target);
		ObjRender_SetBlendType(obj[i], BLEND_ALPHA);
		ObjSprite2D_SetSourceRect(obj[i], 65, 18, 65+63, 78);

        ObjSprite2D_SetDestCenter(obj[i]);
        ObjRender_SetScaleXYZ(obj[i], 1, 1, 1);

        ObjRender_SetPosition(obj[i],GetPlayerX,GetPlayerY,0);
    }
    while(!Obj_IsDeleted(objPlayer)){

        if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD && GetPlayerState==STATE_NORMAL){
            alpha+=12;
            scale-=0.05;
        }
        else{
            alpha-=36;
            scale +=0.1;
        }
        if(scale < 1){
            scale = 1;
            z+=2;
        }
        if(scale > 1){
            z+=6;

        }
        if(scale > 2){
            scale = 2;
        }
        if(alpha > 255){
            alpha = 255;
        }
        if(alpha < 0){
            alpha = 0;
        }
        ascent(i in 0..length(obj)){
            ObjRender_SetAlpha(obj[i],alpha);
            ObjRender_SetAngleZ(obj[1],z);
            ObjRender_SetAngleZ(obj[2],-z);
            ObjRender_SetScaleXYZ(obj[i], scale, scale, scale);
            ObjRender_SetPosition(obj[i],GetPlayerX,GetPlayerY,0);
        }
    yield;
    }
}

task Blink{
    let frameInv = GetPlayerInvincibilityFrame();

    while(frameInv >  0){
        frameInv = GetPlayerInvincibilityFrame();
        ascent(i in 0..2){
        ObjRender_SetColor(objPlayer,255*i,255*i,255);
        loop(2){yield;}
        }
        loop(1){yield;}
    }
}
task Animate{
    let aniwait = 4;
    ObjPrim_SetTexture(GetPlayerObjectID,GetCurrentScriptDirectory~"./pl01.png");
    loop{
        ascent(i in 0..8){
            ObjSprite2D_SetSourceRect(GetPlayerObjectID,32*i,1,32*(i+1),48);
            ObjSprite2D_SetDestCenter(GetPlayerObjectID);
            if(GetVirtualKeyState(VK_LEFT)==KEY_FREE&&GetVirtualKeyState(VK_RIGHT)==KEY_FREE){
                loop(aniwait){yield;}
            }
        }
        if(GetVirtualKeyState(VK_LEFT)==KEY_HOLD||GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){
            ascent(i in 3..8){
                if(GetVirtualKeyState(VK_LEFT)==KEY_FREE&&GetVirtualKeyState(VK_RIGHT)==KEY_FREE){
                    ObjSprite2D_SetSourceRect(GetPlayerObjectID,32*i,1,32*(i+1),48);
                    ObjSprite2D_SetDestCenter(GetPlayerObjectID);
                }
                if(GetVirtualKeyState(VK_LEFT)==KEY_HOLD){
                    ObjSprite2D_SetSourceRect(GetPlayerObjectID,32*i,48,32*(i+1),48*2);
                    ObjSprite2D_SetDestCenter(GetPlayerObjectID);
                }
                if(GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){
                    ObjSprite2D_SetSourceRect(GetPlayerObjectID,32*i,48*2,32*(i+1),48*3);
                    ObjSprite2D_SetDestCenter(GetPlayerObjectID);
                }
                loop(aniwait){yield;}
            }
        }
        yield;
    }
}

task NormalShot{
    loop{
        if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD && GetPlayerState==STATE_NORMAL&&IsPermitPlayerShot){

        NormShot(10);
        NormShot(-10);
        ObjSound_Play(Sound[0]);
        }
        loop(3){yield;}
    }
}


function GetAngleToEnemy(obj,enemy){
    return atan2(ObjMove_GetY(enemy)-ObjMove_GetY(obj),ObjMove_GetX(enemy)-ObjMove_GetX(obj));
}

task NormShot(x){
    
    let obj = CreatePlayerShotA1(GetPlayerX()+x, GetPlayerY()-16, 1, 270, 3, 1, 1);
    let LastX = ObjMove_GetX(obj);
    let LastY = ObjMove_GetY(obj);
    ObjRender_SetAngleZ(obj, -90);
    ObjRender_SetAlpha(obj,200);
    yield;
    ObjMove_SetSpeed(obj,25);
    while(!Obj_IsDeleted(obj)){
        LastX = ObjMove_GetX(obj);
        LastY = ObjMove_GetY(obj);
        yield;
    }
    if(LastY > 0){
        CreateParticle2(LastX,LastY,270);
    }
}

task CreateParticle2(x,y,a){
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjPrim_SetTexture(obj,current~"./pl01.png");
    Obj_SetRenderPriorityI(obj,49);
    ObjRender_SetScaleXYZ(obj,1,1,1);
    let speed = 3;
    let count = 0;
    let angle = a;
    let mx = speed*cos(angle);
    let my = speed*sin(angle);
    animatePar(obj);
    ObjRender_SetAlpha(obj,210);

    //ObjSprite2D_SetSourceRect(obj,1,1,4,4);
    ObjSprite2D_SetDestCenter(obj);

    ObjRender_SetX(obj,x);
    ObjRender_SetY(obj,y);
    ObjRender_SetAngleZ(obj, -90);
    ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);

    while(!Obj_IsDeleted(obj)){
    count++;
    ObjRender_SetPosition(obj,ObjRender_GetX(obj)+mx,ObjRender_GetY(obj)+my,0);
    
    yield;
    }
}

task animatePar(obj){
    let x = 31;
    
    ObjSprite2D_SetSourceRect(obj,x,144,x+32,160);
    ObjSprite2D_SetDestCenter(obj);
    x += 31;
    loop(8){yield;}
    ObjSprite2D_SetSourceRect(obj,x,144,x+32,160);
    ObjSprite2D_SetDestCenter(obj);
    x += 31;
    loop(8){yield;}
    ObjSprite2D_SetSourceRect(obj,x,144,x+32,160);
    ObjSprite2D_SetDestCenter(obj);
    loop(8){yield;}
    Obj_Delete(obj);
}

task RocketShot(obj2){
    let prio = 45;
    ObjSound_Play(Sound[0]);
    let obj = CreatePlayerShotA1(ObjRender_GetX(obj2), ObjRender_GetY(obj2), 2, 270, 9, 1, 2);
    let LastX = ObjMove_GetX(obj);
    let LastY = ObjMove_GetY(obj);
    Obj_SetRenderPriorityI(obj, 45);
    ObjRender_SetAngleZ(obj, -90);
    ObjRender_SetAlpha(obj,150);
    
    yield;
    ObjMove_SetAcceleration(obj, 0.5);
    ObjMove_SetMaxSpeed(obj, 25);
    while(!Obj_IsDeleted(obj)){
        LastX = ObjMove_GetX(obj);
        LastY = ObjMove_GetY(obj);
        yield;
    }
    if(LastY > 0){
        CreateParticle3(LastX,LastY,270);
    }

    task CreateParticle3(x,y,blend){
    
        ObjSound_Play(Sound[9]);
        let xoff = 15;
        let yoff = 15;
        let n2 = 1;
        loop(2){
            spawnparticle(n2);
            wait(8);
            n2 += 1;
        }

        task spawnparticle(n2){
            let array = [];
            loop(n2){
                let obj = ObjPrim_Create(OBJ_SPRITE_2D);
                ObjPrim_SetTexture(obj,current~"./pl01.png");
                ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
                ObjRender_SetScaleXYZ(obj,1,1,1);
                Obj_SetRenderPriorityI(obj,prio);
                ObjRender_SetAlpha(obj,70);
                ObjSprite2D_SetSourceRect(obj,0,224,32,256);
                ObjSprite2D_SetDestCenter(obj);
                ObjRender_SetPosition(obj,x + rand(-xoff, xoff),y + rand(-yoff-10, yoff),1);
                array = array ~ [obj];
            }


            let n = 0;
            wait(2);
            let l = length(array);
            
            loop(7){
                n += 1;
                let x1 = 0 + n*32;
                let x2 = x1 + 32;
                ascent(i in 0..l){
                    ObjSprite2D_SetSourceRect(array[i],x1,224,x2,256);
                }
                
                wait(2);
            }
            ascent(i in 0..l){
                Obj_Delete(array[i]);
            }
        }
    }
}



task CallOptions{
    let x1 = 15;
    let y1 = 40;
    let x2 = 35;
    let y2 = -5;

    let fx1 = 7;
    let fy1 = 25;
    let fx2 = 20;
    let fy2 = 16;

    let by = [8,-5];
    let obj = [
   
    CreateOrbGraphic([[-x1,-y1+by[0]],[-fx1,-fy1+by[1]]]),//left2
    CreateOrbGraphic([[x1,-y1+by[0]],[fx1,-fy1+by[1]]]),//right2
    CreateOrbGraphic([[-x2,y2+by[0]],[-fx2,-fy2+by[1]]]), //left1
    CreateOrbGraphic([[x2,y2+by[0]],[fx2,-fy2+by[1]]]),//right1
    ];

    let trocket = 7;
    Manage;
    LaserShot(obj);
    task Manage{
        loop{
            if(GetVirtualKeyState(VK_SHOT)==KEY_HOLD&&GetVirtualKeyState(VK_SLOWMOVE)==KEY_FREE&&GetPlayerState==STATE_NORMAL&&IsPermitPlayerShot){
               // ascent(i in 0..length(obj)){
                    //HomingShot(ObjRender_GetX(obj[i]),ObjRender_GetY(obj[i]),arr[i]);
                //}
                lasertoggle = 1;
                loop(6){yield;}
            }
            else{
                lasertoggle = 0;
            }
            yield;
        }

    }
    loop{
        if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD && GetPlayerState==STATE_NORMAL&&IsPermitPlayerShot&&GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
            RocketShot(obj[0]);
            RocketShot(obj[1]);
            ObjSound_Play(Sound[8]);
        }
        loop(trocket){yield;}
        if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD && GetPlayerState==STATE_NORMAL&&IsPermitPlayerShot&&GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
            RocketShot(obj[2]);
            RocketShot(obj[3]);
            ObjSound_Play(Sound[8]);
        }
        loop(trocket){yield;}
    }

}

let lasertoggle = 0;

task LaserShot(obj){
    let laser1 = [];
    let laser2 = [];
    let optiongraphic = [];
    let ldamage = 0.4;
    let ol = 800;
    let ow = 20;
    let l = 0;
    let w = 0;
    let t = 12;
    let i = 0;

     let sc = 256*3;
    let anglearray = [270, 270, 270-5, 270+5];
    loop(4){
        let ls = CreateStraightLaserA1(0, 0, anglearray[i], ol, ow, 9999, 1,0);
        ObjLaser_SetInvalidLength(ls, 0, 0);
        ObjLaser_SetIntersectionWidth(ls, ow);
        Obj_SetVisible(ls, false);
        laser1 = laser1 ~ [ls];
        let ls2 = ObjPrim_Create(OBJ_SPRITE_2D);
        lasergraphic(ls2,anglearray[i]);
        laser2 = laser2 ~ [ls2];
        let og = ObjPrim_Create(OBJ_SPRITE_2D);
        optiongraphicdetail(og);
        optiongraphic = optiongraphic ~ [og];
        i += 1;
    }
    let n = 4;


    task lasergraphic(ls2, angle){
        ObjPrim_SetTexture(ls2,GetCurrentScriptDirectory~"./pl01.png");
        Obj_SetRenderPriorityI(ls2,GetPlayerRenderPriorityI+1);
        ObjSprite2D_SetSourceRect(ls2,0 % sc,177,256 % sc,191);
        ObjRender_SetScaleXYZ(ls2, 0, 0, 1);
        ObjSprite2D_SetDestRect(ls2, 0, -7, sc, 7);
        ObjRender_SetAngleZ(ls2, angle);
        ObjRender_SetBlendType(ls2,BLEND_ADD_ARGB);
    }

    task optiongraphicdetail(og){
        ObjPrim_SetTexture(og,GetCurrentScriptDirectory~"./pl01.png");
        Obj_SetRenderPriorityI(og,GetPlayerRenderPriorityI+1);
        ObjSprite2D_SetSourceRect(og,0, 192, 33, 223);
        ObjSprite2D_SetDestCenter(og);
        ObjRender_SetAlpha(og, 150);
        ObjRender_SetBlendType(og,BLEND_ADD_ARGB);
        Obj_SetVisible(og, false);

        let spin = 21;
        let angle = 0;
        let sc = 1.2;
        let sc2 = 0.8;
        let t = 8;
        let scm = (sc-sc2)/t;
        let scale = sc2;
        let posneg = 1;
        loop{
            ObjRender_SetScaleXYZ(og, scale, scale, 1);
            ObjRender_SetAngleZ(og, angle);
            angle += spin;
            scale += scm * posneg;
            if(scale > sc || scale < sc2){posneg *= -1;}
            yield;
        }
    }
    let olsc = 1;
    let owsc = 1;
    let lsc = 0;
    let wsc = 0;
    let frame = 0;
    let fxp = -8;

    let r = 50;
    let g = 50;
    let b = 255;
    let frame2 = 0;
    loop{
        if(lasertoggle == 0){
            ascent(i in 0..length(laser1)){
                Obj_Delete(laser1[i]);
            }
            laser1 = [];
            ascent(i in 0..n){
               
                let ls = CreateStraightLaserA1(0, 0, anglearray[i], ol, ow, 9999, 1,0);
                ObjShot_SetDamage(ls, ldamage);
                ObjLaser_SetInvalidLength(ls, 0, 0);
                ObjLaser_SetIntersectionWidth(ls, ow);
                Obj_SetVisible(ls, false);
                laser1 = laser1 ~ [ls];
            }
            ascent(i in 0..n){
                Obj_SetVisible(optiongraphic[i], false);
            }
            loop(t){
                if(wsc > 0){
                    wsc -= owsc/t;
                    ascent(i in 0..n){
                        ObjRender_SetScaleXYZ(laser2[i], lsc, wsc, 1);
                    }
                }
                ascent(i in 0..n){
                    //ObjMove_SetPosition(laser1[i], ObjRender_GetX(obj[i]), ObjRender_GetY(obj[i]));      
                    ObjRender_SetPosition(laser2[i], ObjRender_GetX(obj[i]), ObjRender_GetY(obj[i]), 1);
                    ObjRender_SetPosition(optiongraphic[i], ObjRender_GetX(obj[i])-1, ObjRender_GetY(obj[i]), 1);
                    ObjSprite2D_SetSourceRect(laser2[i], 0 + (frame*fxp) % sc, 177, sc + (frame*fxp) % sc, 191);
                    if(ObjCol_IsIntersected(laser1[i]) == true){ObjRender_SetColor(laser2[i], r, g, b);}
                    else{ObjRender_SetColor(laser2[i], 255, 255, 255);}
                }
                frame += 1;
                yield;
            }
            ascent(i in 0..length(laser1)){
                Obj_Delete(laser1[i]);
            }
            frame = 0;
            while(lasertoggle == 0){yield;}
            ascent(i in 0..n){
                Obj_SetVisible(optiongraphic[i], true);
            }
            laser1 = [];
            ascent(i in 0..n){
               
                let ls = CreateStraightLaserA1(0, 0, anglearray[i], ol, ow, 9999, 1,0);
                ObjShot_SetDamage(ls, ldamage);
                ObjLaser_SetInvalidLength(ls, 0, 0);
                ObjLaser_SetIntersectionWidth(ls, ow);
                Obj_SetVisible(ls, false);
                laser1 = laser1 ~ [ls];
            }
            wsc = 0;
            lsc = 0;
            loop(t){
                wsc += owsc/t; lsc += olsc/t;
                ascent(i in 0..n){
                    ObjRender_SetScaleXYZ(laser2[i], wsc, lsc, 1);
                    ObjMove_SetPosition(laser1[i], ObjRender_GetX(obj[i]), ObjRender_GetY(obj[i]));
                    ObjRender_SetPosition(laser2[i], ObjRender_GetX(obj[i]), ObjRender_GetY(obj[i]), 1);
                    ObjRender_SetPosition(optiongraphic[i], ObjRender_GetX(obj[i])-1, ObjRender_GetY(obj[i]), 1);
                    ObjSprite2D_SetSourceRect(laser2[i], 0 + (frame*fxp) % sc, 177, sc + (frame*fxp) % sc, 191);
                    if(ObjCol_IsIntersected(laser1[i]) == true){ObjRender_SetColor(laser2[i], r, g, b);}
                    else{ObjRender_SetColor(laser2[i], 255, 255, 255);}
                    
                }
                frame += 1;
                ObjSound_Play(Sound[7]);
                yield;
            }
            frame2 = 0;
        }
        
        ascent(i in 0..n){
            ObjMove_SetPosition(laser1[i], ObjRender_GetX(obj[i]), ObjRender_GetY(obj[i]));
            ObjRender_SetPosition(laser2[i], ObjRender_GetX(obj[i]), ObjRender_GetY(obj[i]), 1);
            ObjRender_SetPosition(optiongraphic[i], ObjRender_GetX(obj[i])-1, ObjRender_GetY(obj[i]), 1);
            ObjSprite2D_SetSourceRect(laser2[i], 0 + (frame*fxp) % sc, 177, sc + (frame*fxp) % sc, 191);
            
        }
        frame += 1;
        if(frame2 < 7200){
            ascent(i in 0..n){
                if(ObjCol_IsIntersected(laser1[i]) == true){ObjRender_SetColor(laser2[i], r, g, b);}
                else{ObjRender_SetColor(laser2[i], 255, 255, 255);}
            }
            frame2 += 1;
        }
        ObjSound_Play(Sound[7]);
        yield;
    }
}



function Distance(obj1,obj2){
    let x1 = ObjMove_GetX(obj1);
    let y1 = ObjMove_GetY(obj1);
    let x2 = ObjMove_GetX(obj2);
    let y2 = ObjMove_GetY(obj2);
    return ((x2-x1)^2 + (y2-y1)^2)^0.5;
}

function CreateOrbGraphic(pos){
    let obj = [ObjPrim_Create(OBJ_SPRITE_2D),ObjPrim_Create(OBJ_SPRITE_2D)];
    ascent(i in 0..length(obj)){
        ObjPrim_SetTexture(obj[i],GetCurrentScriptDirectory~"./pl01.png");
        Obj_SetRenderPriorityI(obj[i],GetPlayerRenderPriorityI);
        ObjSprite2D_SetSourceRect(obj[i],81,161,95,175);
        ObjSprite2D_SetDestCenter(obj[i]);
        
        ObjRender_SetPosition(obj[i],GetPlayerX,GetPlayerY,0);
        
    }
    ObjRender_SetScaleXYZ(obj[0],1.5,1.5,1);
    ObjRender_SetBlendType(obj[0],BLEND_ADD_ARGB);
    ObjRender_SetAlpha(obj[0], 100);
    Obj_SetRenderPriorityI(obj[0],GetPlayerRenderPriorityI-1);
    Manage;
    task Manage{
        let frames = 4;
        let x = pos[0][0];
        let y = pos[0][1];
        let oscale = 1.1;
        let scale = oscale;
        let st = 15;
        let sm = 0.2/st;
        let posneg = -1;
        loop{
            if(GetPlayerState==STATE_DOWN||GetPlayerState==STATE_END){
                ascent(i in 0..length(obj)){
                    Obj_SetVisible(obj[i],false);
                }
            }
            else{
                ascent(i in 0..length(obj)){
                    Obj_SetVisible(obj[i],true);
                }
            }
            ObjRender_SetScaleXYZ(obj[1], scale, scale, 1);
            scale += sm * posneg;
            if(scale == oscale || scale < oscale-(sm*st)){posneg *= -1;}
            //ObjRender_SetAngleZ(obj[0],ObjRender_GetAngleZ(obj[0])-6);
            //ObjRender_SetAngleZ(obj[1],ObjRender_GetAngleZ(obj[1])+6);
            let ox = ObjRender_GetX(obj[1]);
            let oy = ObjRender_GetY(obj[1]);
            if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
                x = pos[1][0];
                y = pos[1][1];
            }
            else{
                x = pos[0][0];
                y = pos[0][1];
            }
            let xc = (GetPlayerX+x-ox)/frames;
            let yc = (GetPlayerY+y-oy)/frames;
            if(GetPlayerState==STATE_DOWN){
                xc = (192+x-ox)/frames;
                yc = (478+y-oy)/frames;
            }
            ox += xc;
            oy += yc;
            ObjRender_SetPosition(obj[1],ox,oy,0);
            ObjRender_SetPosition(obj[0],ObjRender_GetX(obj[1]),ObjRender_GetY(obj[1]),0);

            yield;
        }
    }
    return obj[1];
}



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 PlayerDie(x,y){
    let dtime = 60;
    ExCircle(x,y,0.05,dtime);
    ExCircle(x-30,y,0.05,dtime);
    ExCircle(x+30,y,0.05,dtime);
    ExCircle(x,y-30,0.05,dtime);
    ExCircle(x,y+30,0.05,dtime);
    ObjSound_Play(Sound[4]);
    loop(10){yield;}
    ExCircle(x,y,0.05,dtime-10);

}

let rez = GetScreenHeight()/480;

task ExCircle(x,y,sc,dtime){
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    let timer = 0;
    let s = 0;
    let Target = current~"./Effect03.png";
    let mx = GetPlayerX();
    let my = GetPlayerY();
	Obj_SetRenderPriorityI(obj, 59);
	ObjPrim_SetTexture(obj, Target);
	ObjRender_SetBlendType(obj, BLEND_INV_DESTRGB);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 512, 512);
    ObjSprite2D_SetDestCenter(obj);
    ObjRender_SetScaleXYZ(obj, s, s, s);
    ObjRender_SetPosition(obj,x,y,0);
    loop{
        timer++;
        s+=sc;
        ObjRender_SetScaleXYZ(obj, s, s, s);

        if(timer == dtime){
            Obj_Delete(obj);
            break;
        }
        yield;
    }

}








task TSpell{
	let current = GetCurrentScriptDirectory();
	let simage = current ~ "pl00b.png";
    SetPlayerInvincibilityFrame(9000);
    Blink;
    let ManageObj = GetSpellManageObject;
    Obj_SetValue(ManageObj,"OrbCount",8);
   	ObjSound_Play(Sound[5]);
   	ObjSound_Play(Sound[6]);
    ascent(i in 0..8){
        FantasySealOrb(360/8*i);
    }
    while(Obj_GetValueD(ManageObj,"OrbCount",8)>0){yield;}
    Obj_Delete(ManageObj);
    SetPlayerInvincibilityFrame(30);
}


function FantasySealOrb(a){


    let obj = [
    ObjPrim_Create(OBJ_SPRITE_2D),
    ObjPrim_Create(OBJ_SPRITE_2D),
    ObjPrim_Create(OBJ_SPRITE_2D),
    ObjPrim_Create(OBJ_SPRITE_2D)
    ];
    let SpellObj = ObjSpell_Create;
    ObjSpell_SetDamage(SpellObj,1);
    ObjSpell_SetEraseShot(SpellObj,true);
    ObjSpell_Regist(SpellObj);
    ascent(i in 0..length(obj)){
    Obj_SetRenderPriorityI(obj[i],GetShotRenderPriorityI);
    ObjPrim_SetTexture(obj[i],GetCurrentScriptDirectory~"./pl00b.png");
    ObjSprite2D_SetSourceRect(obj[i],0,0,128,128);
    ObjSprite2D_SetDestCenter(obj[i]);
    ObjRender_SetBlendType(obj[i],BLEND_ADD_ARGB);
    }
    ObjRender_SetColor(obj[1],255,0,0);
    ObjRender_SetColor(obj[2],0,255,0);
    ObjRender_SetColor(obj[3],0,0,255);
    Manage;
    task Manage{
        let sw = 0;
        let range = 0.05;
        let speed;
        let angle;
        let mx;
        let my;
        let enemy;
        let radius = 0;
        let sw2 = 0;
        loop(120){
            sw+=6;
            a+=4;
            sw2+=6;
            if(radius<120){radius+=2;}
            ObjRender_SetAngleZ(obj[0],ObjRender_GetAngleZ(obj[0])+26);
            ObjRender_SetAngleZ(obj[1],ObjRender_GetAngleZ(obj[1])-26);
            ObjRender_SetAngleZ(obj[2],ObjRender_GetAngleZ(obj[2])+26);
            ObjRender_SetAngleZ(obj[3],ObjRender_GetAngleZ(obj[3])-26);
            ObjRender_SetPosition(obj[0],GetPlayerX+radius*cos(a),GetPlayerY+radius*sin(a),0);
            ObjSpell_SetIntersectionCircle(SpellObj,ObjRender_GetX(obj[0]),ObjRender_GetY(obj[0]),64);
            ascent(i in 1..length(obj)){
                ObjRender_SetScaleXYZ(obj[i],1+range*sin(sw+45*i),1+range*sin(sw+45*i),NULL);
                ObjRender_SetPosition(obj[i],ObjRender_GetX(obj[0])+8*cos(360/3*i+sw2),ObjRender_GetY(obj[0])+4*sin(360/3*i+sw2),0);
            }
            yield;
        }
        enemy = GetClosestEnemyID(ObjRender_GetX(obj[0]), ObjRender_GetY(obj[0]));
        angle = atan2(ObjMove_GetY(enemy)-ObjRender_GetY(obj[0]),ObjMove_GetX(enemy)-ObjRender_GetX(obj[0]));
        speed = 3;
        let frames = 60;
        let f = 0;
        let ox = ObjRender_GetX(obj[0]);
        let oy = ObjRender_GetY(obj[0]);
        let xc = (ObjMove_GetX(enemy)-ox)/(frames);
        let yc = (ObjMove_GetY(enemy)-oy)/(frames);
        let f2 = 0;
        loop(frames){
                          
            f2++;
            if(enemy!=ID_INVALID&&f<20){
                enemy = GetClosestEnemyID(ObjRender_GetX(obj[0]), ObjRender_GetY(obj[0]));
                ox = ObjRender_GetX(obj[0]);
                oy = ObjRender_GetY(obj[0]);
                xc = (ObjMove_GetX(enemy)-ox)/(frames/2);
                yc = (ObjMove_GetY(enemy)-oy)/(frames/2);
            }
            else if(enemy==ID_INVALID&&f<20){
                enemy = GetClosestEnemyID(ObjRender_GetX(obj[0]), ObjRender_GetY(obj[0]));
                f = 0;
                ox = ObjRender_GetX(obj[0]);
                oy = ObjRender_GetY(obj[0]);
                xc = (ox-ox)/(frames/2);
                yc = (oy-oy)/(frames/2);
            }

            sw+=6;
            sw2+=6;
            ox += xc;
            oy += yc;  
            if(f2<=frames-10){
                ObjRender_SetPosition(obj[0],ox,oy,0);
            }
            ObjRender_SetAngleZ(obj[0],ObjRender_GetAngleZ(obj[0])+26);
            ObjRender_SetAngleZ(obj[1],ObjRender_GetAngleZ(obj[1])-26);
            ObjRender_SetAngleZ(obj[2],ObjRender_GetAngleZ(obj[2])+26);
            ObjRender_SetAngleZ(obj[3],ObjRender_GetAngleZ(obj[3])-26);
            ObjSpell_SetIntersectionCircle(SpellObj,ObjRender_GetX(obj[0]),ObjRender_GetY(obj[0]),64);
            ascent(i in 1..length(obj)){
                ObjRender_SetScaleXYZ(obj[i],1+range*sin(sw+45*i),1+range*sin(sw+45*i),NULL);
                ObjRender_SetPosition(obj[i],ObjRender_GetX(obj[0])+8*cos(360/3*i+sw2),ObjRender_GetY(obj[0])+4*sin(360/3*i+sw2),0);
            }
            yield;
        }

        loop(20){
            sw+=6;
            sw2+=6;
            ObjRender_SetAngleZ(obj[0],ObjRender_GetAngleZ(obj[0])+26);
            ObjRender_SetAngleZ(obj[1],ObjRender_GetAngleZ(obj[1])-26);
            ObjRender_SetAngleZ(obj[2],ObjRender_GetAngleZ(obj[2])+26);
            ObjRender_SetAngleZ(obj[3],ObjRender_GetAngleZ(obj[3])-26);
            ObjSpell_SetIntersectionCircle(SpellObj,ObjRender_GetX(obj[0]),ObjRender_GetY(obj[0]),64);
            ascent(i in 1..length(obj)){
            ObjRender_SetScaleXYZ(obj[i],1+range*sin(sw+45*i),1+range*sin(sw+45*i),NULL);
            ObjRender_SetPosition(obj[i],ObjRender_GetX(obj[0])+8*cos(360/3*i+sw2),ObjRender_GetY(obj[0])+4*sin(360/3*i+sw2),0);
            }
            yield;
        }
        let scale = 1;
        speed = 3;
        SShake(12,30);
        ObjSound_Play(Sound[2]);
        while(scale>0){
            scale-=0.05;
            sw+=6;
            sw2+=6;
            ObjRender_SetAngleZ(obj[0],ObjRender_GetAngleZ(obj[0])+26);
            ObjRender_SetAngleZ(obj[1],ObjRender_GetAngleZ(obj[1])-26);
            ObjRender_SetAngleZ(obj[2],ObjRender_GetAngleZ(obj[2])+26);
            ObjRender_SetAngleZ(obj[3],ObjRender_GetAngleZ(obj[3])-26);
            ascent(i in 1..length(obj)){
                mx = speed*cos(360/3*i);
                my = speed*sin(360/3*i);
                ObjRender_SetScaleXYZ(obj[0],scale,scale,NULL);
                ObjRender_SetScaleXYZ(obj[i],scale,scale,NULL);
                ObjRender_SetPosition(obj[i],ObjRender_GetX(obj[i])+mx,ObjRender_GetY(obj[i])+my,0);
            }
            yield;
        }
        let ManageObj = GetSpellManageObject;
        Obj_SetValue(ManageObj,"OrbCount",0);
        Obj_Delete(SpellObj);
        ascent(i in 0..length(obj)){
            Obj_Delete(obj[i]);
        }
    }
    return obj;
}

function GetDistance(x1,y1,x2,y2){
	return(((x2-x1)^2+(y2-y1)^2)^(1/2));
}


function GetClosestEnemyID(x1, y1){
	let earray = GetAllEnemyID;
	if(GetEnemyBossSceneObjectID != ID_INVALID){earray = GetIntersectionRegistedEnemyID;}
	let objid = ID_INVALID;
	let dist = max(384, 448)*1.25;
	let size = max(-1, length(earray));
	if(size>=0){
		ascent(b in 0..size){
			let eID = earray[b];
			let edist = GetDistance(x1,y1,ObjMove_GetX(eID),ObjMove_GetY(eID));
			if(edist<dist){
				dist = edist;
				objid = eID;
			}
		}
	}
	return objid;
}

let tx = GetCameraFocusX();
let ty = GetCameraFocusY();

task SShake(dist,time){
    let cenx = 384/2;
    let ceny = 448/2;

    loop(time){

    Set2DCameraFocusX(cenx+rand(-dist,dist));
    Set2DCameraFocusY(ceny+rand(-dist,dist));
    SetCameraFocusX(tx+rand(dist,dist*2));
    SetCameraFocusY(ty+rand(dist,dist*2));
    yield;
    }

    Set2DCameraFocusY(448/2);
    Set2DCameraFocusX(384/2);;
    SetCameraFocusX(tx);
    SetCameraFocusY(ty);
}

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 = current ~ "etama2.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);

    let angle = a;
    let speed = rand(1.5,3);
    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, 192, 224, 229,256);
    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);

    while(!Obj_IsDeleted(obj)){
        alpha+=3;
        ora -=al;
        if(ora<=0){
        Obj_Delete(obj);
        }

        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;
    }

}



function GetAngleToBoss(obj){
    let eid = GetClosestEnemyID(ObjMove_GetX(obj), ObjMove_GetY(obj));
    return atan2(ObjMove_GetY(eid)-ObjMove_GetY(obj),ObjMove_GetX(eid)-ObjMove_GetX(obj));
}

task ImageFadeOut(obj){
    let alpha = 100;

    while(alpha > 0){
    alpha-=6;
    ObjRender_SetAlpha(obj,alpha);
    yield;
    }
    Obj_Delete(obj);
}