#TouhouDanmakufu
#Title["Ultimate Fist"]
#Text[]
#Image[]
#BackGround[Default]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main
{
	let frame=0;
    let BossCutIn=GetCurrentScriptDirectory~"FIST.png";
	#include_function ".\spellcard.txt";
	#include_function ".\fist_replace.txt";
    let stage=0;
    let stageb=0;
    let alpha=255;
task bossgraphic{
    yield;
    let obj=Obj_Create(OBJ_EFFECT);
    Obj_SetPosition(obj, GetX, GetY);
    ObjEffect_SetTexture(obj, BossCutIn);
    ObjEffect_SetLayer(obj, 4);
    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
    ObjEffect_CreateVertex(obj, 4);
    ObjEffect_SetVertexUV(obj, 0, 0, 0);
    ObjEffect_SetVertexUV(obj, 1, 256, 0);
    ObjEffect_SetVertexUV(obj, 2, 0, 416);
    ObjEffect_SetVertexUV(obj, 3, 256, 416);
    ObjEffect_SetVertexXY(obj, 0, -128, -208);
    ObjEffect_SetVertexXY(obj, 1, 128, -208);
    ObjEffect_SetVertexXY(obj, 2, -128, 208);
    ObjEffect_SetVertexXY(obj, 3, 128, 208);
    while(Obj_BeDeleted(obj)==false){
        Obj_SetPosition(obj, GetX-121*cos(GetAngle), GetY-121*sin(GetAngle));
        ObjEffect_SetAngle(obj, 0, 0, GetAngle+90);
        ascent(i in 0..4){ObjEffect_SetVertexColor(obj, i, alpha, 255, 255, 255);}
        if(alpha==0){Obj_Delete(obj);}
        yield;
    }
}
task attack{
    SetMovePosition02(GetCenterX, GetClipMinY-150, 10);
    PlaySE(GetCurrentScriptDirectory~"sound\Charge.wav");
    Concentration01(60);
    shots;
    while(stage==0){
        loop(40){yield;}
        PlaySE(GetCurrentScriptDirectory~"sound\Laser1.wav");
        SetAngle(GetAngleToPlayer);
        loop(10){yield;}
        PlaySE(GetCurrentScriptDirectory~"sound\se_nep00.wav");
        loop(30){yield;}
        SetSpeed(12);
        while(GetX<GetClipMaxX+150 && GetX>GetClipMinX-150 && GetY<GetClipMaxY+150){yield;}
        SetSpeed(0);
        loop(30){yield;}
        SetMovePosition02(GetPlayerX, GetClipMinY-100, 1);
        yield;
        SetAngle(90);
        SetSpeed(0);
    }
}
task shots{
    let counter=0;
    let counterb=0;
    while(stage==0 || stage==2){
        counter++;
            if(counterb==20){hitbox2; counterb=31;}
            if(counterb>=1 && GetY<GetClipMinY){counterb=0;}
            counterb++;
            if(GetY>GetClipMinY && GetY<GetClipMaxY){
                if(counter%20==0){
                    ascent(i in 0..15){
                        CreateShot01(GetX, GetY, 2, GetAngleToPlayer+i*24, RED03, 20);
                    }
                }
            }
        yield;
    }
}
task hitbox2{
    let counter=0;
    let counterb=-1;
    let x=GetX;
    let y=GetY;
    while(stage==0 && counterb<70){
        if(GetY<GetClipMinY){counterb++;}
        if(counterb>=0){counterb++;}
		SetCollisionA(x,y,20);
        counter++;
        if(counter%3==0){
            Explosion01(x, y, 0, 0.2, 5);
        }
        yield;
    }
}
task shot(x, y, angle){
    let obj=Obj_Create(OBJ_SHOT);
    let counter=0;
    Obj_SetPosition(obj, x, y);
    Obj_SetSpeed(obj, 12);
    Obj_SetAngle(obj, angle);
    ObjShot_SetGraphic(obj, RED15);
    while(Obj_BeDeleted(obj)==false){
        counter++;
        if(counter%30==0){
            ascent(i in 0..10){
                CreateShot01(Obj_GetX(obj), Obj_GetY(obj), 2, Obj_GetAngle(obj)+i*36, RED03, 20);
            }
        }
        yield;
    }
}
task attack2{
    loop(200){yield;}
    let x=0;
    let y=0;
    let angle=0;
    let counter=0;
    SetMovePosition02(GetCenterX, GetClipMinY-150, 1);
    yield;
    SetAngle(90);
    SetSpeed(0);
    PlaySE(GetCurrentScriptDirectory~"sound\Charge.wav");
    Concentration01(60);
    while(stage==1){
        counter++;
        yield;
        if(counter%10==0){
            PlaySE(GetCurrentScriptDirectory~"sound\Shot1.wav");
            x=rand(GetClipMinX, GetClipMaxX);
            y=rand(GetClipMinY+25, GetClipMinY-50);
            if(x>GetCenterX){angle=rand(165, 180);}else{angle=rand(0, 15);}
            shot(x, y, angle);
            x=rand_int(0, 1)*GetClipMaxX;
            if(x==GetClipMaxX){x+=rand(50, 100);}else{x-=rand(50, 100);}
            y=rand(0, 150);
            if(x>GetCenterX){angle=rand(165, 180);}else{angle=rand(0, 15);}
            shot(x, y, angle);   
        }
        if(counter%60==0){
            x=rand_int(0, 1)*GetClipMaxX;
            if(x==GetClipMaxX){x+=rand(50, 100);}else{x-=rand(50, 100);}
            y=rand(0, 150);
            if(x>GetCenterX){angle=rand(165, 180);}else{angle=rand(0, 15);}
            SetX(x);
            SetY(y);
            SetSpeed(12);
            SetAngle(angle);
        }
        if((counter-20)%60==0){
            hitbox2;
        }
        if((counter-50)%60==0){
            SetMovePosition02(GetCenterX, GetClipMinY-200, 1);
        }
        yield;
    }
}
task attack3{
    loop(200){yield;}
    let x=0;
    let y=0;
    let angle=0;
    let counter=0;
    SetMovePosition02(GetCenterX, GetClipMinY-150, 10);
    PlaySE(GetCurrentScriptDirectory~"sound\Charge.wav");
    Concentration01(60);
    shots;
    while(stage==2){
        loop(40){yield;}
        PlaySE(GetCurrentScriptDirectory~"sound\Laser1.wav");
        SetAngle(GetAngleToPlayer);
        loop(10){yield;}
        shot(GetX, GetY, GetAngle-30);
        shot(GetX, GetY, GetAngle+30);
        PlaySE(GetCurrentScriptDirectory~"sound\se_nep00.wav");
        loop(30){yield;}
        SetSpeed(12);
        while(GetX<GetClipMaxX+150 && GetX>GetClipMinX-150 && GetY<GetClipMaxY+150){yield;}
        SetSpeed(0);
        loop(30){yield;}
        SetMovePosition02(GetCenterX, GetClipMinY-100, 1);
        yield;
        SetAngle(90);
        SetSpeed(0);
    }
}
task attack4{
    loop(200){yield;}
    let x=0;
    let y=0;
    let angle=0;
    let counter=0;
    SetMovePosition02(GetCenterX, GetClipMinY-150, 10);
    PlaySE(GetCurrentScriptDirectory~"sound\Charge.wav");
    Concentration01(60);
    shots;
    while(stage==3){
        loop(40){yield;}
        PlaySE(GetCurrentScriptDirectory~"sound\Laser1.wav");
        SetAngle(90);
        loop(10){yield;}
        ascent(i in -3..4){
            if(i!=0){shot(GetX+i*100, GetY, 90);}
        }
        PlaySE(GetCurrentScriptDirectory~"sound\se_nep00.wav");
        loop(60){yield;}
        SetSpeed(12);
        while(GetX<GetClipMaxX+150 && GetX>GetClipMinX-150 && GetY<GetClipMaxY+150){yield;}
        SetSpeed(0);
        loop(30){yield;}
        SetMovePosition02(GetPlayerX, GetClipMinY-100, 1);
        yield;
        SetAngle(90);
        SetSpeed(0);
    }
}
	@Initialize
	{
		SetInvincibility(200);
		SetLife(1000);
		LoadGraphic(BossCutIn);
		SetMovePosition02(GetCenterX,120,20);
		CutIn(YOUMU, "''Ultimate Fist''",BossCutIn,0,0,256,416);
		spellbonus(10000000);
		timer;
		SetCommonData("timercount", 9000);
        bossgraphic;
        SetAngle(90);
        SetCommonData("ONSPELL", "TRUE");
        if(GetCommonDataDefault("NOMISSCOUNT", -1)==-1){
            initmeh;
        }
	}
	
	@MainLoop
	{
        SetShotAutoDeleteClip(200, 200, 200, 200);
        if(OnBomb==true){SetDamageRate(0, 0);}else{SetDamageRate(10,5);}
		SetCollisionA(GetX,GetY,50);
		SetCollisionB(GetX,GetY,75);
		frame++;
		yield;
        if(frame==90){attack;}
        if(GetCommonData("timercount")>2400 && stage%1==0){
            if(GetLife>750){stage=0;}else if(GetLife>500){stage=1;}else if(GetLife>250){stage=2;}else{stage=3;}
        }else{
            stage=3;
        }
        if(stageb!=stage){
            PlaySE(GetCurrentScriptDirectory~"sound\Shot2.wav");
            DeleteEnemyShotToItem(ALL);
            if(stage==1){attack2;}
            if(stage==2){attack3;}
            if(stage==3){attack4;}
            stageb=stage;
        }
}
	
	@Finalize
	{
        SetCommonData("ONSPELL", "FALSE");
		AddScore(spellbonustotal);
		if(spellbonustotal>0)
		{
		PlaySE(GetCurrentScriptDirectory~"sound\se_cardget.wav");
		loop(50)
		{
			CreateItem(ITEM_SCORE,GetX+rand(-100,100),GetY+rand(20,100));
		}
		}else{
		loop(15)
		{
			CreateItem(ITEM_SCORE,GetX+rand(-100,100),GetY+rand(20,100));
		}
		}
		 
        if(GetMissCountInThisSpell==0){
            SetCommonData("NOMISSCOUNT", GetCommonData("NOMISSCOUNT")+1);
        }
	}
	
	@DrawLoop
	{
	}
}