#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["animtest"]
#Text["3rdboss animation test"]
#include "script/default_system/Default_ShotConst.txt"

let bossObj;
let bossX = 0;
let bossY = 0;
let objScene = GetEnemyBossSceneObjectID();
let imgBoss = GetCurrentScriptDirectory ~ "sprite/teachersheet.png";
let animFrame = 0; 
let animFrame2 = 0;
let spellcardSFX = GetCurrentScriptDirectory ~ "SFX/SPELLCARD.wav";
let bossdeathSFX = GetCurrentScriptDirectory ~ "SFX/DEFEATED.wav";
let objText = ObjText_Create();
let shootSFX = GetCurrentScriptDirectory ~ "SFX/LASER.wav";
let shootSFX2 = GetCurrentScriptDirectory ~ "SFX/ATTACK3.wav";
let BG = GetCurrentScriptDirectory ~ "sprite/opsinBG.png";
let overlay1 = GetCurrentScriptDirectory ~ "sprite/school.png";
let count = 0;
let circleObj = ObjPrim_Create(OBJ_SPRITE_2D);

let textoverlay = GetCurrentScriptDirectory ~ "sprite/spelltext.png";

let CSD = GetCurrentScriptDirectory;



@Initialize {
  
SpellImage;
	SpellText;

  SetAutoDeleteObject(true); //automatically deletes object when THIS script ends

        bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
        ObjEnemy_Regist(bossObj); 

        ObjMove_SetPosition(bossObj,182,200); //warps the boss

        ObjMove_SetDestAtSpeed(bossObj,180,120,10); //movement function

//------------
 ObjText_SetText(objText, "teacher magic ~ test pressure");
    ObjText_SetFontSize(objText, 14);
    ObjText_SetFontType(objText, "Russell Square Regular");
    ObjText_SetFontBold(objText, true);
    ObjText_SetFontColorTop(objText, 255, 255, 255);
    ObjText_SetFontColorBottom(objText, 255, 255, 255);
    ObjText_SetFontBorderType(objText, BORDER_FULL);
    ObjText_SetFontBorderColor(objText, 224, 64, 192);
    ObjText_SetFontBorderWidth(objText, 1);
   ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
    ObjText_SetMaxWidth(objText, 450);
    ObjRender_SetX(objText, 5 - 80);
	ObjRender_SetY(objText, 20);
	//--------
   
              LoadSound (spellcardSFX);
        LoadSound(bossdeathSFX);
     PlaySE(spellcardSFX);
	 LoadSound(shootSFX);
	 LoadSound(shootSFX2);

     mainTask;

}


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

@MainLoop {
     bossX = ObjMove_GetX(bossObj);
     bossY = ObjMove_GetY(bossObj);
     
//enemy collision
  
   
        ObjEnemy_SetIntersectionCircleToShot(bossObj, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 32); 
        ObjEnemy_SetIntersectionCircleToPlayer(bossObj, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 24);
      
  
        count++;
        yield;
    }
    
    
 

 
task SpellImage{
        let obj = ObjPrim_Create(OBJ_SPRITE_2D);
        let imgpath = GetCurrentScriptDirectory() ~ "sprite/spellteacher1.png";
        ObjPrim_SetTexture(obj, imgpath);
		Obj_SetRenderPriorityI(obj,60);
        ObjSprite2D_SetSourceRect(obj, 0, 1, 500,684); 
        ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetScaleXYZ(obj,0.8,0.8,0);
	
		// Moves the object's coordinates 
	        	let a = 0;
             ascent(i in 0..20){
             ObjRender_SetPosition(obj, a, a, 0);
              a+= 10;
			  
yield;
           
		}
		wait(30);
		ObjRender_SetAlpha(obj,200);
		wait(5);
		ObjRender_SetAlpha(obj,100);
		wait(5);
		ObjRender_SetAlpha(obj,50);
		wait(5);
		Obj_Delete(obj);
		
		

    
	}
	task SpellText{
 let scrollTex = 0;
      
	  let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
        Obj_SetRenderPriorityI(overlayObj,21); 
	ObjPrim_SetTexture(overlayObj,textoverlay); 
        ObjRender_SetBlendType(overlayObj,BLEND_ADD_ARGB); //blend type (effect)
        ObjRender_SetAlpha(overlayObj,100); //set opacity, 200 is maximum
	ObjSprite2D_SetSourceRect(overlayObj,0,0,400,504);
	ObjRender_SetScaleXYZ(overlayObj,1,1,0);
	ObjSprite2D_SetDestRect(overlayObj,0,0,GetStgFrameWidth,GetStgFrameHeight);
 
        loop(70) {
          ObjSprite2D_SetSourceRect(overlayObj,400-scrollTex,504-scrollTex,0-scrollTex,0-scrollTex);
		
          scrollTex += 5; //controll how fast its scrolling
          yield;
		 
		 
      }
	  
		ObjRender_SetAlpha(overlayObj,200);
		wait(5);
		ObjRender_SetAlpha(overlayObj,100);
		wait(5);
		ObjRender_SetAlpha(overlayObj,50);
		wait(5);
		Obj_Delete(overlayObj);
        
	
}

task renderBG {

 
        let scrollTex = 0;

        //background
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,20); 
	ObjPrim_SetTexture(obj,BG); 
	ObjSprite2D_SetSourceRect(obj,0,0,1068,1426);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);

        //spell card overlay
        let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
        Obj_SetRenderPriorityI(overlayObj,21); 
	ObjPrim_SetTexture(overlayObj,overlay1); 
        ObjRender_SetBlendType(overlayObj,BLEND_ADD_ARGB); //blend type (effect)
        ObjRender_SetAlpha(overlayObj,100); //set opacity, 200 is maximum
	ObjSprite2D_SetSourceRect(overlayObj,0,0,1024,768);
	ObjSprite2D_SetDestRect(overlayObj,0,0,GetStgFrameWidth,GetStgFrameHeight);
 
        while(!Obj_IsDeleted(bossObj)) {
          ObjSprite2D_SetSourceRect(overlayObj,0-scrollTex,0-scrollTex,1024-scrollTex,768-scrollTex);
		
          scrollTex += 2; //controll how fast its scrolling
          yield;
      }
        Obj_Delete(overlayObj);
} 


task mainTask {
    MagicCircle; 
	renderBoss;
    renderBG;
	movement;
        TEnd;


} 

task MagicCircle
{    let alpha = 0;
      let spin = 0;
	  let path = GetCurrentScriptDirectory ~ "sprite/teachercircle.png";
	  

     ObjPrim_SetTexture(circleObj,path);
     ObjRender_SetBlendType(circleObj,BLEND_ADD_ARGB);
     Obj_SetRenderPriorityI(circleObj,40);
     ObjRender_SetAlpha(circleObj,120);
     ObjRender_SetScaleXYZ(circleObj,0.6,0.6,0);
     ObjSprite2D_SetSourceRect(circleObj,0,0,490,490);
     ObjSprite2D_SetDestCenter(circleObj);
     
   while(!Obj_IsDeleted(bossObj)){

       ObjRender_SetPosition(circleObj,bossX,bossY,0);
       ObjRender_SetAngleZ(circleObj,spin);
       ObjRender_SetAlpha(circleObj,alpha);

         alpha = 120;
		 spin+=-1;
		yield;
        }
    
       yield;
  
 }

task movement {
	wait(90);
		
	
	loop { 
	 while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){
            ObjMove_SetDestAtFrame(bossObj, rand(bossX + 90, bossX - 90), rand(bossY + 90, bossY - 90), 60);
            wait(360);
	
	}
}
}

@Event {
//boss health and timer
       alternative(GetEventType())
	   
	   
       case(EV_REQUEST_LIFE) {
          SetScriptResult(3000);
       }
	   
       case(EV_REQUEST_TIMER) {
          SetScriptResult(70);
     }
	 


}

@Finalize {

}





task renderBoss {
	let dir;
        let speed;

        //texture the boss and set the center
        ObjPrim_SetTexture(bossObj,imgBoss);
        ObjSprite2D_SetSourceRect(bossObj,0,0,131,284);
        ObjSprite2D_SetDestCenter(bossObj);
        ObjRender_SetScaleXYZ(bossObj,0.5,0.5,0);
    
	while(!Obj_IsDeleted(bossObj)) {
		//when the boss is NOT deleted

		
		dir = ObjMove_GetAngle(bossObj);
		speed = ObjMove_GetSpeed(bossObj);
		
 //animation handling
		if(speed == 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,0,0);
			if(animFrame < 5) { ObjSprite2D_SetSourceRect(bossObj,0,0,131,288); }
			if(animFrame >= 10 && animFrame < 20) { ObjSprite2D_SetSourceRect(bossObj,150,3,287,290); }
			if(animFrame >= 20) { ObjSprite2D_SetSourceRect(bossObj,300,3,433,290); }
			
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,0,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,744,0,881,290); }
			if(animFrame2 >= 15 && animFrame2 < 25) { ObjSprite2D_SetSourceRect(bossObj,885,0,1032,290); }
			if(animFrame2 >= 25 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,1032,0,1178,296); }
			if(animFrame2 >= 30 && animFrame2 < 35) { ObjSprite2D_SetSourceRect(bossObj,1182,0,1315,290); }
			
		}
		else if(cos(dir) > 0){ 
		ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,744,0,881,290); }
			if(animFrame2 >= 15 && animFrame2 < 25) { ObjSprite2D_SetSourceRect(bossObj,885,0,1032,290); }
			if(animFrame2 >= 25 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,1032,0,1178,296); }
			if(animFrame2 >= 30 && animFrame2 < 35) { ObjSprite2D_SetSourceRect(bossObj,1182,0,1315,290); }
		}
		animFrame++;		// count animFrame. (++ is +1)
		animFrame2+=2;		// count animFrame2.
		if(animFrame > 25) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
	}
}


function angleToPlayer {
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}
   task fireLaser(dir){
   PlaySE(shootSFX);
        let obj = CreateStraightLaserA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 
                90, 512, 20, 250, DS_BEAM_GREEN, 30);
        ascent(i in 0..175){
            ObjStLaser_SetAngle(obj, 90 + 2*i * dir);
            yield;
        }
    }
	

task TDrawLoop {
    let imgBoss = GetCurrentScriptDirectory() ~ "sprite/testsheet.png";
    ObjPrim_SetTexture(bossObj, imgBoss);
    ObjSprite2D_SetSourceRect(bossObj, 64, 1, 127, 64);
    ObjSprite2D_SetDestCenter(bossObj);
}


task TEnd 
{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
        
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(20);	
	CloseScript(GetOwnScriptID());

}