
	let GCSD=GetCurrentScriptDirectory;

	let imgMeteo=GPSD~"img\meteo.png";

	let nameopacity=0;
	let MeteoFrame=0;

	let flickeralpha=1;
	let split=0;
	let splitoffsetx=0;
	let splitoffsety=0;
	let splitalpha=0;
	let numflickers=0;
	let numenemies=0;

function MeteoDrawLogic(x,y){
	framecheckmain++;

	//drawloop

	gloweyex[MeteoFrame%4]=round(x);
	gloweyey[MeteoFrame%4]=round(y+sin(MeteoFrame*3)*4)-10;

	if(MeteoFrame%3==0){
		glowbodyx[gframe%12]=x;
		glowbodyy[gframe%12]=y+sin(MeteoFrame*3)*4;
		glowbodyxs[gframe%12]=rand(-1,1);
		glowbodyys[gframe%12]=rand(-1,1);
		glowbodyxv[gframe%12]=rand(-20,20);
		glowbodyyv[gframe%12]=rand(-20,20);
		glowbodys[gframe%12]=MeteoFrame;

		sparksx[gframe%12]=x+rand(-20,20);
		sparksy[gframe%12]=y+rand(0,70);
		sparkst[gframe%12]=rand_int(0,7);
		sparksxv[gframe%12]=rand(-20,20);
		sparksyv[gframe%12]=rand(80,150);
		sparksa[gframe%12]=rand(0,360);

		gframe++;
	}
	if(MeteoFrame%7==0){
		numflickers--;
		if(rand_int(0,9)==0||numflickers>0){
			flickeralpha=rand(0.75,1);
			split=rand_int(20,75);
			splitoffsetx=rand_int(-1,3);
			splitoffsety=rand_int(-1,3);
			splitalpha=rand(0.35,1)*RandDir;
			if(numflickers<=0){
				numflickers=rand_int(1,3);
			}
		}
		halox[MeteoFrame/7%6]=GetX;
		haloxv[MeteoFrame/7%6]=rand(-30,30);
		haloy[MeteoFrame/7%6]=GetY-48+sin(MeteoFrame*3)*4;
		halof[MeteoFrame/7%6]=MeteoFrame;
	}
	if(MeteoFrame%7==4){
		flickeralpha=1;
		split=0;
		splitoffsetx=0;
		splitoffsety=0;
	}
	MeteoFrame++;
}

let gloweyex=[0,0,0,0];
let gloweyey=[0,0,0,0];
let halox=[0,0,0,0,0,0];
let haloxv=[0,0,0,0,0,0];
let haloy=[0,0,0,0,0,0];
let halof=[0,0,0,0,0,0];

let glowbodyx=[0,0,0,0,0,0,0,0,0,0,0,0];
let glowbodyy=[0,0,0,0,0,0,0,0,0,0,0,0];
let glowbodyxs=[0,0,0,0,0,0,0,0,0,0,0,0];
let glowbodyys=[0,0,0,0,0,0,0,0,0,0,0,0];
let glowbodyxv=[0,0,0,0,0,0,0,0,0,0,0,0];
let glowbodyyv=[0,0,0,0,0,0,0,0,0,0,0,0];
let glowbodys=[0,0,0,0,0,0,0,0,0,0,0,0];	//start frame

let sparksx=[0,0,0,0,0,0,0,0,0,0,0,0];
let sparksy=[0,0,0,0,0,0,0,0,0,0,0,0];
let sparksa=[0,0,0,0,0,0,0,0,0,0,0,0];
let sparksxv=[0,0,0,0,0,0,0,0,0,0,0,0];
let sparksyv=[0,0,0,0,0,0,0,0,0,0,0,0];
let sparkst=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1];

let gframe=0;
let meteoalpha=0;

function MeteoDrawLoop(x,y){
	SetRenderState(ALPHA);
	SetColor(255,255,255);

        SetTexture(imgMeteo);
	SetAlpha(255*meteoalpha);
	SetGraphicScale(1,1);
	SetGraphicRect(1,0,85,96);
	SetGraphicAngle(0,0,0);

	SetColor(150,0,255);
	let percentage=0;
	let i=gframe;
	loop(12){
		if(glowbodys[i%12]>0){
			percentage=(MeteoFrame-glowbodys[i%12])/(12*3);
			//SetAlpha((200-percentage*200)*flickeralpha);
			SetAlpha((200-percentage*200)*flickeralpha*meteoalpha);
			SetGraphicScale(1+glowbodyxs[i%12]*percentage,1+glowbodyys[i%12]*percentage);
			DrawGraphic(glowbodyx[i%12]+glowbodyxv[i%12]*percentage-5,glowbodyy[i%12]+glowbodyyv[i%12]*percentage);
		}
		i++;
	}

	SetColor(255,255,255);
	SetGraphicScale(1,1);

	SetAlpha((145+sin(MeteoFrame*4)*45)*flickeralpha*meteoalpha);
	if(split==0){
		SetGraphicRect(1,0,85,95);
        	DrawGraphic(round(x)-5, round(y+sin(MeteoFrame*3)*4));
	}else{
		if(splitalpha>0){
			SetAlpha((145+sin(MeteoFrame*4)*45)*flickeralpha*splitalpha*meteoalpha);
		}
		SetGraphicRect(1,0,85,split-1);
        	DrawGraphic(round(x)+splitoffsetx-5, round(y+sin(MeteoFrame*3)*4)-((95/2)-(split-1)/2)+splitoffsety);
		if(splitalpha<0){
			SetAlpha(((145+sin(MeteoFrame*4)*45)*flickeralpha*splitalpha*-1)*meteoalpha);
		}
		SetGraphicRect(1,split+1,85,95);
        	DrawGraphic(round(x)+splitoffsetx-5, round(y+sin(MeteoFrame*3)*4)+((split+1)/2)+splitoffsety);
	}

	SetRenderState(ADD);

	SetColor(100*meteoalpha,100*meteoalpha,255*meteoalpha);
	SetGraphicRect(97,25,127,50);
	i=MeteoFrame;
	loop(4){
	        DrawGraphic(gloweyex[i%4],gloweyey[i%4]);	//glow eye
		i++;
	}

	SetColor(140,140,255);
	SetGraphicRect(97,0,127,25);
        DrawGraphic(round(x)+splitoffsetx, round(y+sin(MeteoFrame*3)*4)-10+splitoffsety);	//solid eye

	SetColor(255,255,255);

	let color=[155,0,255];
	i=0;
	loop(12){
		if(sparkst[i]>-1){
			percentage=(MeteoFrame-glowbodys[i])/(12*3);
			SetColor(color[0]*(1-absolute(percentage*2-1)),color[1]*(1-absolute(percentage*2-1)),color[2]*(1-absolute(percentage*2-1)));
			SetGraphicAngle(0,0,sparksa[i]);
			SetSparkGraphicRect(sparkst[i]);
			DrawGraphic(sparksx[i]+percentage*sparksxv[i],sparksy[i]-percentage*sparksyv[i]);
		}
		i++;
	}

	SetColor(140,140,255);
	SetGraphicRect(89,50,128,71);
	SetGraphicScale(1,1);
	SetGraphicAngle(0,0,0);
	DrawGraphic(GetX+2,GetY-48+sin(MeteoFrame*3)*4);
	i=0;
	loop(6){
		percentage=1-(((MeteoFrame-1)-halof[i])/(7*6));
		SetColor(40*percentage,40*percentage,70*percentage);
		SetGraphicScale(percentage,8-percentage*7);
		SetGraphicAngle(0,0,-90+90*percentage);
		DrawGraphic(halox[i]+haloxv[i]*(1-percentage)+2,haloy[i]);
		i++;
	}
}

function SetSparkGraphicRect(num){
	let y=1;
	if(num>=4){
		num-=4;
		y+=20;
	}
	SetGraphicRect(129+40+num*20,1+y,128+40+num*20+19,19+y);
}

task MeteoInit(time){
	SetEffectForZeroLife(2,0,0);
	HandleLifebar;
	practicing=0;
	loop(time){
		meteoalpha+=1/time;
		yield;
	}
	meteoalpha=1;
}
task MeteoAlpha(start,end,time){
	let diff=(end-start)/time;
	loop(time){
		meteoalpha+=diff;
		yield;
	}
}
function MeteoFinalize(x,y,explosion){
	ClearText(250);
	let color=0;
	SetCommonData("EnemyX2",x);
	SetCommonData("EnemyY2",y);
	loop(20){
		color=rand(0,360);
		CreateExplosionEX(1,x,y,rand(0.01,1),[lower(sin(color)*150+255,255),100,lower(-sin(color)*150+255,255)],rand(0.01,0.5),rand(80,120));
	}
	SetCommonData("LastMeteoMissCount",GetMissCount-GetCommonDataDefault("TotalMeteoMissCount",0));
	SetCommonData("TotalMeteoMissCount",GetMissCount)
}


function SpawnMeteoEnemy(type,x,y,color1,color2){
	CreateEnemyFromFile(GPSD~"enemy\00.txt", x,y+1,color1,color2,type);
	numenemies++;
}

task EnemyDamageMeteo(num){
//kills Meteo after x enemies are killed
	let maxlife=GetLife;
	let kills=0;
	while(kills<num){
		if( (numenemies-GetEnemyNum)+1>kills){
			kills++;
			AddLife(-maxlife/num);
		}
		yield;
	}
	SetLife(-1);
}

task SetMovePositionNewM(x,y,time){
	let oldx;
	let oldy;
	if(DrawArea[0]==NULL){
		oldx=GetX;
		oldy=GetY;
	}else{
		oldx=DrawArea[0];
		oldy=DrawArea[1];
	}
	x-=oldx;
	y-=oldy;
	let frame=0;
	loop(time){
		DrawArea[0]=sin(frame)*x+oldx;
		DrawArea[1]=sin(frame)*y+oldy;
		frame+=90/time;
		yield;
	}
	DrawArea=[x+oldx,y+oldy];
}