#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["gamejamspell3"]
#Text["le gamejam"]
#include "script/sample/gamejam/my_system/Default_ShotConst2.txt"
#System["./my_system/My_system.txt"]

let bossObj;
let bossX = 0;
let bossY = 0;
let objScene = GetEnemyBossSceneObjectID();
let imgBoss = GetCurrentScriptDirectory ~ "sprite/boss.png";
let animFrame = 0; 
let animFrame2 = 0;
let spellcardSFX = GetCurrentScriptDirectory ~ "SFX/SPELLCARD.wav";
let bossdeathSFX = GetCurrentScriptDirectory ~ "SFX/ATTACK3.wav";
let objText = ObjText_Create();
let shootSFX = GetCurrentScriptDirectory ~ "SFX/ATTACK4.wav";
let sky = GetCurrentScriptDirectory ~ "sprite/luigibg.png";
let overlay = GetCurrentScriptDirectory ~ "sprite/luigioverlay.png";
let textoverlay = GetCurrentScriptDirectory ~ "sprite/spelltext.png";
let circleObj = ObjPrim_Create(OBJ_SPRITE_2D);
let smallSFX = GetCurrentScriptDirectory ~ "SFX/TWINKLE3.wav";





@Initialize {
    TMagicCircle;
	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, "Mustache pondering ~ "Or, is it the bagel?");
    ObjText_SetFontSize(objText, 12);
    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, 2, 225, 2);
    ObjText_SetFontBorderWidth(objText, 1);
    ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
    ObjText_SetMaxWidth(objText, 300);
   ObjRender_SetX(objText, 100 - 50);
	ObjRender_SetY(objText, 20);
   
              LoadSound (spellcardSFX);
        LoadSound(bossdeathSFX);
LoadSound(smallSFX);
     PlaySE(spellcardSFX);
	 LoadSound(shootSFX);

     mainTask;

}




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

    yield;
}


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


} 

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

}

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

     ObjPrim_SetTexture(circleObj,path);
     ObjRender_SetBlendType(circleObj,BLEND_ADD_ARGB);
     Obj_SetRenderPriorityI(circleObj,70);
     ObjRender_SetAlpha(circleObj,120);
     ObjRender_SetScaleXYZ(circleObj,0.8,0.8,0);
     ObjSprite2D_SetSourceRect(circleObj,0,0,300,300);
     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.8;
		yield;
        }
    
       yield;
  
 }

@Finalize {

}


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


task renderBoss {
	let dir;
        let speed;

        //texture the boss and set the center
        ObjPrim_SetTexture(bossObj,imgBoss);
        ObjSprite2D_SetSourceRect(bossObj,0,0,30,45);
        ObjSprite2D_SetDestCenter(bossObj);
        ObjRender_SetScaleXYZ(bossObj,1,1,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);
			
			
			
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			
		}
		else if(cos(dir) > 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
		
		}
		animFrame++;		// count animFrame. (++ is +1)
		animFrame2+=2;		// count animFrame2.
		if(animFrame > 20) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
		
		
	}
}

 task SpellImage{
        let obj = ObjPrim_Create(OBJ_SPRITE_2D);
        let imgpath = GetCurrentScriptDirectory() ~ "sprite/luigi.png";
        ObjPrim_SetTexture(obj, imgpath);
		Obj_SetRenderPriorityI(obj,60);
        ObjSprite2D_SetSourceRect(obj, 0, 1, 51, 68); 
        ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetScaleXYZ(obj,6,6,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,2-scrollTex,0-scrollTex);
		
          scrollTex += -1; //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 TMagicCircle
{  
      let spin = 0;
	  let path = GetCurrentScriptDirectory ~ "greencircle.png";

     ObjPrim_SetTexture(circleObj,path);
     ObjRender_SetBlendType(circleObj,BLEND_ADD_ARGB);
     Obj_SetRenderPriorityI(circleObj,0.50);
     ObjRender_SetScaleXYZ(circleObj,0.8,0.8,0);
     ObjSprite2D_SetSourceRect(circleObj,0,0,182,182);
     ObjSprite2D_SetDestCenter(circleObj);
     
       ObjRender_SetPosition(circleObj,bossX,bossY-5,0);
       ObjRender_SetAngleZ(circleObj,spin);
       ObjRender_SetAlpha(circleObj,120);

		 spin+=2;
		 
    
       yield;
  
 }
 

task movement {
	wait(100);
	loop(50) { fire;
	wait(60);
	fire;
	wait(60);
	fire;
	wait(60);
	fire;
	wait(60);

	}
}


task movement2 {
	wait(100);

	loop(50) { fire2;
	wait(60);
	fire2;
	wait(60);
	fire2;
	wait(60);
	fire2;
	wait(60);

	}
}
function angleToPlayer {
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}
task fire {



 let angleT = angleToPlayer;
 
loop(10){
 ascent(i in 0..26){
 PlaySE(shootSFX);
 let obj = CreateShotA2(bossX, bossY, 5, angleT, 0.01, 4, DS_BALL_M_GREEN	, 5);
		
			
			angleT += 360/26;
}
}
}



task fire2{
wait(100);
 let angleT = angleToPlayer;
 

 ascent(i in 0..20){

 let obj = CreateShotA2(bossX, bossY, 2, angleT, 0.01, 3, DS_STAR_M_R_WHITE	, 2);
		ObjRender_SetBlendType(obj,BLEND_ALPHA);
			
			angleT += 360/20;
		
		}
		}

 
 
task SnakePattern(obj, angleT){
	let wvel = 1.2;
	ascent(i in 0..12){
		ObjMove_AddPatternA2(obj, i * 80, ObjMove_GetSpeed(obj), angleT, 0.01, wvel, 2.5);
		wvel *= -0.5;
	}
}

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

 
        let scrollTex = 0;

        //background
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,20); 
	ObjPrim_SetTexture(obj,sky); 
	ObjSprite2D_SetSourceRect(obj,0,0,452,640);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);

        //spell card overlay
        let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
        Obj_SetRenderPriorityI(overlayObj,21); 
	ObjPrim_SetTexture(overlayObj,overlay); 
        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 TEnd 
{    
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
        PlaySE(bossdeathSFX);
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(20);	
	CloseScript(GetOwnScriptID());

}