// First we define some variables for us to use. ///////////////

let BossDraw_frame = 0;			// counter for setting graphic rects
let BossDraw_next = 5;			// time to wait until setting the next set of rects
let BossFrame_IDLE = 0;			// counter used to set the graphic rects of boss's idle animation
let BossFrame_SPELL = 0;		// counter used to set the graphic rects of boss's spellcard animation
let BossFrame_SHOOT = 0;		// counter used to set the graphic rects of boss's bullet shooting animation
let BossFrame_MOVE = 0;			// counter used to set the graphic rects of boss's moving animation
let BossDraw_mode = 0;			// action that the boss is doing at the moment
	let BossMode_IDLE = 0;		// 0 = idle
	let BossMode_SPELL = 1;		// 1 = spell casting
	let BossMode_SHOOT = 2;		// 2 = regular shot
	let BossMode_MOVE = 3;		// 3 = moving
let BossDraw_rectpair = [0,0];		// graphic rects to set
let BossDraw_count = 0;			// countdown until end of animation

let ACT_IDLE_0 = 0;		// Idle animation 0: no movement
					// < 1 >
let ACT_IDLE_1 = 1;		// Idle animation 1: loop frames 1 to 3
					// < 1, 2, 3 >
let ACT_IDLE_2 = 2;		// Idle animation 2: loop frames 1 to 4
					// < 1, 2, 3, 4 >
let ACT_IDLE_3 = 3;		// Idle animation 3: loop frames 1-3 3 times, then use frame 4 as a blinking animation frame
					// < [(x3) 1, 2, 3] 4 >
let ACT_IDLE_4 = 4;		// Idle animation 4: loop through the first three frames 3 times, then use frame 4 as a blinking animation frame
					// < [(x3) 1, 2, 1, 3] 4 >
let ACT_SPELL_0 = 0;		// Spell casting animation 0: play through frames until end of animation, freeze at frame 4, then finish with playing frame 2
					// < 1, 2, 3, [ 4 ] 2 >
let ACT_SPELL_1 = 1;		// Spell casting animation 1: play through, loop frames 3/4, end with frame 2
					// < 1, 2, [ 3, 4 ] 2 >
let ACT_SHOOT_0 = 0;		// Bullet shooting animation 0: play through frames until end of animation, freeze at frame 4, then finish with playing frame 2
					// < 1, 2, 3, [ 4 ] 2 >
let ACT_SHOOT_1 = 1;		// Bullet shooting animation 1: play through, loop frames 3/4, end with frame 2
					// < 1, 2, [ 3, 4 ] 2 >
let ACT_MOVE_0 = 0;		// Moving animation 0: play through frames 1/2, freeze at frame 3, finish by playing backwards
					// < 1, 2, [ 3 ] 2, 1 >
let ACT_MOVE_1 = 1;		// Moving animation 1: play through frames 1/2, switch between frames 3/4, finish by playing backwards
					// < 1, 2, [ 3, 4 ] 3, 2, 1 >
let ACT_IDLE = 0;		// Default idle animation ID is 0.
let ACT_SPELL = 0;		// Default spell casting animation ID is 0.
let ACT_SHOOT = 0;		// Default bullet shooting animation ID is 0.
let ACT_MOVE = 0;		// Default moving animation ID is 0.
let BossDraw_anilimit = [	// Alright, so we're going to have to set the frames when the boss animations reset. Ready, set, go!
	[1, 3, 4, 10, 12],		// Idle frames
	[5, 5],				// Spell casting frames
	[5, 5],				// Bullet shooting frames
	[4, 5],				// Moving frames
];

function SetBossAnimation(BossDraw_modeNew, BossModeUsageTime) { BossDraw_count = 0; BossDraw_mode = BossDraw_modeNew; BossDraw_count = BossModeUsageTime; }
				// Wahahaha. Here's a function for easily making the boss use different sprite animations.

function SetBossAnimeStyle(idle,spell,shoot,move) { ACT_IDLE = idle; ACT_SPELL = spell; ACT_SHOOT = shoot; ACT_MOVE = move; }
				// And this thing lets you choose how the boss animates.


sub CreateBossAnimation {
	let tempBossDrawVar = 0;								// Here we go!
	SetTexture(spr);									// First we set the boss texture so Danmakufu has an image to work with.
													// Ok, so now we have the boss texture set.
													// We need to make sure this stuff happens every frame while the boss exists, so we put this here.
	let angle = GetSpeedX<0;								// This makes it so that when the boss moves, the image we're using should be flipped to match the boss's movement.
	if(BossDraw_mode != 3) {								// Of course, when the boss ISN'T moving, we're setting the boss's image to be facing the right way.
		tempBossDrawVar = 0
	} else {										// But when the boss IS moving, we're going to have to do some special stuff to do make the boss look like it's moving the right way.
		if(angle) { tempBossDrawVar = 0 }							// the boss is leaning to the left
		else { tempBossDrawVar = -1 }								// the boss is leaning to the right
	}
	if(BossDraw_frame == 0){								// Ok, so this is what we're going to be using to make the boss look like it's doing something.
		alternative(BossDraw_mode)							// Your mission starts now! Are you ready?
			case(BossMode_IDLE) {							// Ok, first we set what happens when the boss is idle.
				alternative(ACT_IDLE)						// Alright, since we have different types of idle animation, we make different animation codes for each.
				case(ACT_IDLE_0, ACT_IDLE_1, ACT_IDLE_2) {				// Idle animations 0, 1, and 2 have similar coding, so we'll group them all into one like this.
					alternative(BossFrame_IDLE)				// Here we make the boss use different sprites at different times.
					case(0) { BossDraw_rectpair=[0,0]; }				// frame 0
					case(1) { BossDraw_rectpair=[1,0]; }				// frame 1
					case(2) { BossDraw_rectpair=[2,0]; }				// frame 2
					case(3) { BossDraw_rectpair=[3,0]; }				// frame 3
				}
				case(ACT_IDLE_3) {						// Idle animations 3 and 4 are sorta different, so I'll just code in animation 3 for now.
					alternative(BossFrame_IDLE)				// Same stuff from just above applies to here, nothing different, sorta.
					case(0,3,6) { BossDraw_rectpair=[0,0]; }			// frame 0
					case(1,4,7) { BossDraw_rectpair=[1,0]; }			// frame 1
					case(2,5,8) { BossDraw_rectpair=[2,0]; }			// frame 2
					case(9) { BossDraw_rectpair=[3,0]; }				// frame 3
				}
				case(ACT_IDLE_4) {						// Finally, we code idle animation 4. That wasn't so bad, was it?
					alternative(BossFrame_IDLE)				// Same thing as above, just even more different.
					case(0,2,4,6,8) { BossDraw_rectpair=[0,0]; }			// frame 0
					case(1,5,9) { BossDraw_rectpair=[1,0]; }			// frame 1
					case(3,7,11) { BossDraw_rectpair=[2,0]; }			// frame 2
					case(10) { BossDraw_rectpair=[3,0]; }				// frame 3
				}
				BossFrame_IDLE++;						// We put this here so that the animation will actually work.
				BossFrame_IDLE%=BossDraw_anilimit[BossMode_IDLE][ACT_IDLE];		// This makes it so that the boss's animation loops once it's over.
			}									// And we're done the idle animation process.
			case(BossMode_SPELL, BossMode_SHOOT) {					// Onto the spell casting animation part!
				alternative(ACT_SPELL)						// We shouldn't have any problems with the coding now that we've done all that from above, right?
				case(ACT_SPELL_0, ACT_SPELL_1) {				// Idle animations 0, 1, and 2 have similar coding, so we'll group them all into one like this.
					alternative(BossFrame_SPELL)				// I'll stop putting these comments so you can just read it properly.
					case(0) { BossDraw_rectpair=[0,1]; }
					case(1) { BossDraw_rectpair=[1,1]; }
					case(2) { BossDraw_rectpair=[2,1]; }
					case(3) { BossDraw_rectpair=[3,1]; }
				}
				if(BossDraw_count>1){
					BossFrame_SPELL++;
					if(BossFrame_SPELL>4){BossFrame_SPELL-=2+ACT_SPELL;}		// I'm adding this here so that it makes the boss sprite loop between the two frames.
					BossFrame_SPELL%=BossDraw_anilimit[BossMode_SPELL][ACT_SPELL];
				} else {
					alternative(BossDraw_count)
					case(0) { BossDraw_rectpair=[0,1];			// Here we override the selection of graphic rects.
						BossDraw_mode=BossMode_IDLE; }			// Of course, once we finish the ending animation, we go back to being idle.
					case(1) { BossDraw_rectpair=[1,1]; }			// But since it takes time to finish the animation, we do this other stuff.
					case(2) { BossDraw_rectpair=[2,1]; }
				}
			}									// I won't be commenting so much for the next section as we already know how to do all this.
			case(BossMode_MOVE) {
				alternative(ACT_MOVE)
				case(ACT_MOVE_0, ACT_MOVE_1) {
					alternative(BossFrame_MOVE)
					case(0) { BossDraw_rectpair=[0,3]; }
					case(1) { BossDraw_rectpair=[1,3]; }
					case(2) { BossDraw_rectpair=[2,3]; }
					case(3) { BossDraw_rectpair=[3,3]; }
				}
				if(BossDraw_count>(2+ACT_MOVE)){
					BossFrame_MOVE++;
					if(BossFrame_MOVE>2+ACT_MOVE){BossFrame_MOVE-=1+ACT_MOVE;}
					BossFrame_MOVE%=BossDraw_anilimit[BossMode_MOVE][ACT_MOVE];
				} else {
					alternative(BossDraw_count)
					case(0) { BossDraw_rectpair=[0,3]; BossDraw_mode=BossMode_IDLE; }
					case(1) { BossDraw_rectpair=[1,3]; }
					case(2) { BossDraw_rectpair=[2,3]; }
					case(3) { BossDraw_rectpair=[3,3]; }
				}
			}
		if(BossDraw_count>0){BossDraw_count-=BossDraw_next;}				// We're going to make the boss use certain sprites at the end of some animations so we'll use this to make stuff happen.
		if(BossDraw_count<0){BossDraw_count=0;}
	}											// Part 1 complete.
	SetGraphicRect(										// Now we need to actually make the boss's sprites appear.
		BossDraw_rectpair[0]*128, (BossDraw_rectpair[1]+tempBossDrawVar)*128,		// First the top left corner...
		BossDraw_rectpair[0]*128+128, (BossDraw_rectpair[1]+tempBossDrawVar)*128+128);	// And then the bottom right corner. Wuzzah!
	DrawGraphic(GetX,GetY);									// And we're really done now!
	BossDraw_frame++; BossDraw_frame%=BossDraw_next;					// Finally, we can't have the animation function actually start working without this, can we?
}												// Mission complete! Thank you for your reading!