
#Title[魔理沙スペル2]
#Text[魔理沙スペル2]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
   #include_script".\th4_1595\星蓮船素材\素材\lib_Houtou.txt"
     #include_script".\function_effect.txt"
    let name        = "光魔　「スターメイルシュトロム」";
     let csd = GetCurrentScriptDirectory;
        let gx = GetX;
        let gy = GetY;
        let gspeed = GetSpeed;
        let gangle = GetAngle;     
        let arg = GetArgument;
   let imgBOSS = csd~"img\character\dot_marisa.png";
   let wIni  = 60;
   let csd = GetCurrentScriptDirectory;
   let shotData = csd ~ "data_usershot_add.txt";
   let se1 = csd~ "bgm\bell01.wav";
   let se2 = csd~ "bgm\eco00.wav";
   let se3 = csd~ "bgm\bomb1.wav";
   let se4 = csd~ "bgm\countdown.wav";
    let effect = csd ~ "img\magic_circle\powercircle.png";
  let imgBeam = csd ~ "th4_1595\星蓮船素材\素材\img\宝塔\houtou.png";
    let imgFire = csd ~ "th4_1595\星蓮船素材\素材\img\宝塔\Fire.png";
   let arrayL = [];
  let dif = GetCommonData("Dif");
 if(dif == 2){name    = "魔宝　「ブリリアントトレジャー」";}
   @Initialize{
       CutIn(KOUMA, name, "", 0, 0, 0, 0);
           if(GetPlayerType == REIMU_A){SetLife(1000);}
            else{SetLife(1000);}
           SetTimer(90);
     SetScore(15000000);
       SetDamageRate(20,4);
           SetInvincibility(120);
           SetGraphicRect(0*128,0,1*128,128);   
           SetMovePosition03(GetCenterX,GetClipMinY+60,20,5);
   if(dif == 2){SetMovePosition01(GetCenterX,GetClipMinY+120,5);}
        SetAlpha(255);
      MagicCircle(false);
           LoadGraphic(imgBOSS);
          LoadGraphic(effect);
          LoadUserShotData(shotData);
         LoadSE(se1);LoadSE(se2);LoadSE(se3);LoadSE(se4);
           TMain;
    LoadGraphic(imgBeam); LoadGraphic(imgFire);
    }

   @MainLoop{
           SetCollisionA(GetX, GetY, 24);
           SetCollisionB(GetX, GetY, 24);

         yield;
       }

   @DrawLoop{
         SetTexture(imgBOSS);  
        DrawGraphic(GetX,GetY);
     }

    @Finalize{
  SetShotAutoDeleteClip(64,64,64,64);
        DeleteGraphic(imgBOSS);
        DeleteGraphic(effect);
        DeleteSE(se1);DeleteSE(se2);DeleteSE(se3);DeleteSE(se4);  
       if(!IsTimeOut){
                loop(125){
              CreateItem(ITEM_SCORE,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));
              }
           }         
     }
 
     task TMain{
          yield;
  SetShotAutoDeleteClip(32,32,32,32);
      Circle(40);
       wait(120);
    SetGraphicRect(0*128,2*128,1*128,3*128); 
     if(dif == 1){ Shot; ShotA; Move;}
   else if(dif == 2){Shot2;}
        Count;
    }

      task Shot{
       let v = 4;
       let length = 48;
       let width = 10;
       let color = 11;
       let spin = 0.21*v;
    Shot1;
    task Shot1{
   let base = -90;
    loop{
       let x = GetClipMinX-30;
       let angle = -30;
     ascent(a in 0..6){
     let y =base+ GetCenterY + (GetClipMaxY-GetClipMinY)/4*(a-2.5);
       L(x,y,v,angle,length,width,color,spin);
     wait(5);
         }
     base += 15;
     if(base > 90){base = -90;}
       wait(30);
      }
      }
       Shot3;
    task Shot3{
    let base = -90;
    loop{
       let x = GetClipMaxX+30;
       let angle = 150;
     descent(a in 0..6){
     let y = base+GetCenterY + (GetClipMaxY-GetClipMinY)/4*(a-2.5);
       L(x,y,v,angle,length,width,color,spin);
      wait(5);
         }
     base += 15;
     if(base > 90){base = -90;}
       wait(30);
      }
      }    
    }

   task ShotA{
   wait(60);
   let v = 4;
   let accel = -0.1;
   let color = 160; 
   let delay = 15;
   let v2 = 3;
    loop{
   let x = GetX; let y = GetY;
   ascent(a in 0..8){
    let angle = 45*a;
     P(x,y,v,v2,angle,color-a,delay,accel);
      }
   PlaySE(se1);
    wait(GetTimer*2+1);
     }
      }

  task Shot2{
    Laser;
  task Laser{
  let hhh = 0;
  let base = 15-30*6;
  let delay = 60;
  let kill = 120;
  let color = [3,2];
   let i = 0;
  loop{
   let x = GetX;
   let y = GetY;
  ascent(a in 0..12){ 
   let angle = base + 30*a;
    Houtou(x,y,angle,color[i],delay,kill);
    wait(3);
       }
   wait(delay-3*12);
   PlaySE(se3);
 ascent(a in 0..2){
  let length = 60; let width = 10;
 let delay = 0; let ref = 0;
   ascent(b in 0..12){
   let angle = base + 30*b;
 SLaser(GetX-30+60*a,GetY,5,angle,length,width,color[i]+41,delay,ref);
    }
  }
  loop(120){
     loop(3){
     CreateShot01(GetX,GetY,rand(0.5,6),rand(-180,180),color[i]+41,0);
     }
    wait(1); 
  }
  ascent(b in 0..12){
  let angle = base + 30*b;
  let accel = 0.02;
   let vmax = 1.5; let delay2 = 30;
   let radius = 0;
  let n = 0;
  loop(5){
    loop(8){
  let x2 = GetX+offsetX(radius,angle);
   let y2 = GetY+offsetY(radius,angle);
  CreateShot02(x2,y2,0,angle-n-220,accel,vmax,color[i]+153,delay2);
  CreateShot02(x2,y2,0,angle+2*n,accel,vmax,color[i]+153,delay2);
     radius += 12;
    n += 20;
       }
    wait(1);
     }
   }
  ascent(a in 0..2){
  let length = 90; let width = 10;
 let delay = 0; let ref = 2;
   ascent(b in 0..12){
   let angle = base + 30*b;
 SLaser(GetX-30+60*a,GetY,2,angle,length,width,color[i]+41,delay,ref);
    }
  }
    wait(180);
   i += 1; if(i > 1){i = 0;}
   base += 15;
   if(hhh == 0 && GetPlayerX < GetCenterX){SetMovePosition02(GetCenterX-130,GetY,20); hhh += 1;}
   else if(hhh == 0 && GetPlayerX >= GetCenterX){SetMovePosition02(GetCenterX+130,GetY,20); hhh += 1;}
   else{SetMovePosition02(GetCenterX,GetY,20); hhh = 0;}
    wait(30);
      }
     }
   }

    task L(x,y,v,angle,length,width,color,spin){
              let l = Obj_Create(OBJ_SINUATE_LASER);
            /*  arrayL = arrayL ~ [l]; */
              Obj_SetX(l,x);
              Obj_SetY(l,y);
              Obj_SetSpeed(l,v);
              Obj_SetAngle(l,angle);
              ObjShot_SetGraphic(l,color);
              ObjShot_SetDelay(l,0);
         ObjShot_SetBombResist(l,false);
              Obj_SetCollisionToObject(l,true);
              Obj_SetCollisionToPlayer(l,true);
         ObjSinuateLaser_SetLength(l,length);
         ObjSinuateLaser_SetWidth(l,width);
      
         while(!Obj_BeDeleted(l)){
           Obj_SetAngle(l,Obj_GetAngle(l)+spin);
           wait(1);
           }
        }

  task SLaser(x,y,v,angle,length,width,color,delay,ref){
         let s = Obj_Create(OBJ_SINUATE_LASER);
         Obj_SetX(s,x);
         Obj_SetY(s,y);
         Obj_SetSpeed(s,v);
         Obj_SetAngle(s,angle);
         Obj_SetAlpha(s,255);
         ObjShot_SetBombResist(s,true);
         ObjShot_SetDelay(s,delay);
         ObjShot_SetGraphic(s,color);
         ObjShot_SetBombResist(s,false);
         ObjSinuateLaser_SetLength(s,length);
         ObjSinuateLaser_SetWidth(s,width);
     Reflect;
     task Reflect{
       while(!Obj_BeDeleted(s) && ref > 0){
         if(Obj_GetX(s)>=GetClipMaxX || 
            Obj_GetX(s)<=GetClipMinX){
          Obj_SetAngle(s,180-Obj_GetAngle(s));
      ref -= 1;
            }
        else if(Obj_GetY(s)<=GetClipMinY){
           Obj_SetAngle(s,-Obj_GetAngle(s));
     ref -= 1;
           }
       wait(1);
         }
       }
    }

     task Move{
     loop{
        wait(90);
        let x = rand(32,48);
        let y = rand(0,32);
        let v = 3;
        let xmin = GetClipMinX+64;
        let ymin = GetClipMinY+60;
        let xmax = GetClipMaxX-64;
        let ymax = GetCenterY-60;
        if(x>=GetX){SetGraphicRect(2*128,3*128,3*128,4*128);}
       else if(x<=GetX){SetGraphicRect(2*128,1*128,3*128,2*128);}
         SetMovePositionRandom01(x,y,v,xmin,ymin,xmax,ymax);
         wait(60);
         SetGraphicRect(0*128,2*128,1*128,3*128); 
         wait(180);
      }
    }
   
   
 task P(x,y,v,v2,angle,color,delay,accel){
        let p = Obj_Create(OBJ_SHOT);
         Obj_SetX(p,x);
         Obj_SetY(p,y);
         Obj_SetSpeed(p,v);
         Obj_SetAngle(p,angle);
         ObjShot_SetGraphic(p,color);
         ObjShot_SetDelay(p,delay);
         ObjShot_SetBombResist(p,false);
         Obj_SetCollisionToObject(p,true);
         Obj_SetCollisionToPlayer(p,true);
         Obj_SetAutoDelete(p,true);
      wait(delay);
    while(!Obj_BeDeleted(p) && Obj_GetSpeed(p)>0){
      Obj_SetSpeed(p,Obj_GetSpeed(p)+accel);
       wait(1);
       }
      Obj_SetSpeed(p,0);
     wait(30);
      Obj_SetSpeed(p,v2);
     Obj_SetAngle(p,atan2(GetPlayerY-Obj_GetY(p),GetPlayerX-Obj_GetX(p)));
   }

      task Count{
        loop{
          if(GetTimer <= 10){PlaySE(se4);}
         wait(60);
        }
    }

   task Circle(size){
           let c= Obj_Create(OBJ_EFFECT);
             Obj_SetX(c,GetX);
             Obj_SetY(c,GetY);
             Obj_SetAngle(c,0);
             ObjEffect_SetAngle(c,0,0,0);
             ObjEffect_SetTexture(c,effect);
             ObjEffect_SetRenderState(c,ADD);
             ObjEffect_SetPrimitiveType(c,PRIMITIVE_TRIANGLEFAN);
             ObjEffect_SetLayer(c,0);
             ObjEffect_CreateVertex(c,62);
             ObjEffect_SetVertexXY(c,0,0,0);
             ObjEffect_SetVertexUV(c,0,128,128);
             ObjEffect_SetVertexColor(c,0,255,255,255,255);
             ObjEffect_SetScale(c,size/10,size/10);

            ascent(let i in 0..61){
                ObjEffect_SetVertexXY(c,i+1,cos(i*6)*20,sin(i*6)*20);
                ObjEffect_SetVertexUV(c,i+1,cos(i*6)*130+130,sin(i*6)*130+130);
                ObjEffect_SetVertexColor(c,i+1,255,255,255,255);
            }
       Spin;
       Up;
     task Spin{
      let h = 0;
         loop{
            Obj_SetX(c,GetX); Obj_SetY(c,GetY);
            ObjEffect_SetAngle(c,0,0,h);
            h += 8;
            wait(1);
           }
         }
       task Up{
            loop{
             ascent(a in 0..30){
            Obj_SetX(c,GetX); Obj_SetY(c,GetY);
            ObjEffect_SetScale(c,(size+a/5)/10,(size+a/5)/10);
            wait(1);
             }
            wait(10);
             descent(a in 0..30){
            Obj_SetX(c,GetX); Obj_SetY(c,GetY);
            ObjEffect_SetScale(c,(size+a/5)/10,(size+a/5)/10);
            wait(1);
             }
            wait(10);
           }
        }
    }

    function offsetX(radius,angle){
           return radius * cos(angle);
     }

        function offsetY(radius,angle){
            return radius * sin(angle);
     }           

       function move(f){
          SetMovePosition02(rand(GetClipMinX+60,GetClipMaxX-60),GetY+rand(-20,20),f);
     }
            
        function sc(r,g,b){
             SetShotColor(r,g,b);
     }

       sub setGraphicStop  { SetGraphicRect( 1, 1, 64, 64); }

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

     function enemy(text,x,y,speed,angle,arg){
               CreateEnemyFromFile(text,x,y,speed,angle,arg);
            }
}