let csd = GetCurrentScriptDirectory; // For the sake of laziness, to load all pics and sounds.
// Images.
let Marisprite = csd~"img/dot_marisa_kai.png";
let Maricutin =csd~"img/cutin_marisa.png";
let SpellBG1 = csd~"img/SpellBG1.png";
let SpellBG2 = csd~"img/SpellBG2.png";
let Enemy = csd~"img/enemy.png";
let ByakuFam = csd~"img/byakufam.png";
let BGM = csd~"img/BGM.png";
// Sounds.
let Shot1 = csd~"se/Shot1.wav";
let Shot2 = csd~"se/shot2.wav";
let Charge = csd~"se/Charge.wav";
let Timeout1 = csd~"se\timeout01.wav";
let Timeout2 =csd~"se\timeout02.wav";
let Alert = csd~"se/Alert.wav";
let Option = csd~"se/option.wav";
let Spark = csd~"se/Spark.wav";
let SunSummon = csd~"se/SunSummon.wav";
let Laser = csd~"se/laser.wav";
let Capture = csd~"se/capture.wav";
let BossDeath = csd~"se/BossDeath.wav";
let Familiar = csd~"se/option.wav";
let Boon = csd~"se/BOON.wav";
// Music.
let MagusNight = csd~"bgm.mp3";

let bgalpha = 0;	
let bgscroll = 0;
let BR = [0,0,64,80];
let bossflip = 0;
let ani = 0;

task LoadStuff{
	LoadGraphic(Marisprite);
	LoadGraphic(Maricutin);
	LoadGraphic(SpellBG1);
	LoadGraphic(SpellBG2);
	LoadGraphic(Enemy);
	LoadGraphic(ByakuFam);
	LoadGraphic(BGM);
	LoadSE(Shot1);
	LoadSE(Shot2);
	LoadSE(BossDeath);
	LoadSE(Charge);
	LoadSE(Timeout1);
	LoadSE(Timeout2);
	LoadSE(Alert);
	LoadSE(Option);
	LoadSE(Spark);
	LoadSE(SunSummon);
	LoadSE(Laser);
	LoadSE(Capture);
	LoadSE(Familiar);
	LoadSE(Boon);
	LoadMusic(MagusNight);
	}
task DeleteStuff{ // Delete stuff too.
	DeleteGraphic(Marisprite);
	DeleteGraphic(Maricutin);
	DeleteGraphic(SpellBG1);
	DeleteGraphic(SpellBG2);
	DeleteGraphic(Familiar);
	}
function Wait(t){loop(t){yield;}} // The classic Wait function.
task Vulnerable{ // Makes the boss vulnerable after a set time.
	Wait(1200);
	SetDamageRate(50,50);
	}
task Timer { // Plays the timeout sounds.
	loop
		{
		if (GetTimer > 5 && GetTimer <= 11){PlaySE(Timeout1);}
		if (GetTimer >= 1 && GetTimer <= 5){PlaySE(Timeout2);}
		Wait(60);
		}
	}
function GetRelativeAngleToPlayer(objX,objY){ // Useful function to make getting angles easier.
	return GetRelativeAngleToTarget(objX,objY,GetPlayerX,GetPlayerY)
	}
function GetRelativeAngleToTarget(objX,objY,tarX,tarY){ // The same function than before, but this time to get to any point on the field.
	if (objX > tarX){ return 180 - atan2((|objY-tarY|),(| objX-tarX |));}
	return atan2((|objY-tarY|),(| objX-tarX |));
	}
function DividePlayingFieldX (number, DistToBorder){  // Returns an array with the X coordinates for  an x number of points on the field, where they're separated in between them by equal amounts of pixels, and the most extern ones have a DistToBorder distance to the edge of the playing field. Complicated, yes.
	let DPFX = [];
	let PlayingSpaceX = GetClipMaxX + GetClipMinX - 2*DistToBorder;
	ascent (i in 0..number){
		DPFX = DPFX ~ [DistToBorder + GetClipMinX +PlayingSpaceX/number*i];
		}
	return DPFX;
	}
function DividePLayingFieldY (number, DistToBorder){ // The same as DividePlayingFieldX, but for the Y rect. Mostly for completionism's sake.
	let DPFY = [];
	let PlayingSpaceY = GetClipMaxY + GetClipMinY - 2*DistToBorder;
	ascent (i in 0..number){
		DPFY = DPFY ~ [DistToBorder +GetClipMinY + PlayingSpaceY/number*i];
		}
	return DPFY;
	}
task CircularMovement (radius){ // Causes the familiar to engage in a circular movement, with the radius specified. Note that the speed will be set by the radius as well, so it will take 6 seconds to make a full loop (cause I need that speed). It also assumes the familiar is spawned straight below the boss.
	loop{
		SetMovePositionHermite(GetX,GetY-radius*2, 3, 0, 3, 180, 180);
		Wait(180);
		SetMovePositionHermite(GetX,GetY+radius*2, 3, 180, 3, 0, 180);
		Wait(180);
		}
	}
task DrawName(name){ // Draws the character's name below the lifebar.
	DrawText(name,40,30,12,255);
	}
task SpellBG{ // Fancy Spellcard Background.
	if (bgalpha < 255){bgalpha += 5;}
	SetAlpha(bgalpha);
	SetGraphicScale(2.2,2.2);
	SetColor(255,255,255);
	SetGraphicRect(0,0,256,256);
	SetTexture(SpellBG1);
	DrawGraphic(GetCenterX,GetCenterY);
	SetAlpha(bgalpha/2);
	SetTexture(SpellBG2);
	SetGraphicRect(0,bgscroll,563,bgscroll+563);
	SetGraphicScale (1, 1);
	DrawGraphic(GetCenterX,GetCenterY);
	bgscroll -= 2;
	}
task MoveAnimation{loop{ //Fancy Animation.
	ani++;
	let currentani = ani;
	if (GetSpeedX > 0){
		bossflip = 0;
		loop(2){if(currentani==ani){BR = [0,80,64,160]; yield;}}
		loop(2){if(currentani==ani){BR = [64,80,128,160]; yield;}}
		loop(2){if(currentani==ani){BR = [128,80,192,160]; yield;}}
		while(GetSpeed>0 && currentani==ani){BR = [192,80,256,160]; yield;}
		}
	else if (GetSpeedX < 0){
		bossflip = 1;
		loop(2){if(currentani==ani){BR = [0,80,64,160]; yield;}}
		loop(2){if(currentani==ani){BR = [64,80,128,160]; yield;}}
		loop(2){if(currentani==ani){BR = [128,80,192,160]; yield;}}
		while(GetSpeed>0 && currentani==ani){BR = [192,80,256,160]; yield;}
		}
	else if(GetSpeedX == 0){while (GetSpeedX == 0 && currentani==ani){
		loop(8){BR = [0,0,64,80]; yield;}
		loop(8){BR = [64,0,128,80]; yield;}
		loop(8){BR = [128,0,192,80]; yield;}
		loop(8){BR = [192,0,256,80]; yield;}
		}}
	}}
task CaptureBonus{ // plays the Capture bonus when a card is captured
	if(GotSpellCardBonus){
		PlaySE(Capture);
		}
	}