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

function CreateSprite(x, y, img, source, priority){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img);
	Obj_SetRenderPriorityI(obj, priority);
	ObjSprite2D_SetSourceRect(obj, source[0], source[1], source[2], source[3]);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, x, y, 0);

	return obj;
}

task Leaf(x, y, dir, color){ //Based on Ozzy's functions
	let objStar = ObjPrim_Create(OBJ_SPRITE_2D);

	let alpha = 200;
	let spinA = rand(0, 180);
	let spinB = rand(0, 180);
	let scale = 0;
	let speed = rand(1, 4);

	Obj_SetRenderPriorityI(objStar, 21);
	ObjPrim_SetTexture(objStar, GetCurrentScriptDirectory ~ "../IMG/Effect/Cherry.png");
	ObjSprite2D_SetSourceRect(objStar, 0, 0, 256, 256);
	ObjSprite2D_SetDestCenter(objStar);
	ObjRender_SetBlendType(objStar, BLEND_ADD_RGB);
		
	while(!Obj_IsDeleted(objStar)){
		ObjRender_SetPosition(objStar, x, y, 0);
		ObjRender_SetScaleXYZ(objStar, scale, scale, scale);
		ObjRender_SetAngleXYZ(objStar, spinA, spinB, spinA);
		ObjRender_SetAlpha(objStar, alpha);
		ObjRender_SetColor(objStar, color[0]*(alpha/255), color[1]*(alpha/255), color[2]*(alpha/255));

		if(scale < 0.15 && speed > 0){scale += 0.03;}
		if(speed > 1.0){speed -= 0.01;}
		if(speed < 1 && alpha > 0){alpha -= rand(0.5, 1.5); scale -= rand(0.01, 0.025);}
		if(alpha <= 0){Obj_Delete(objStar);}

		spinA += rand(1,2);
		spinB += rand(-1,-2);
		x += speed*cos(dir);
		y += speed*sin(dir);
		yield;
	}
}

function InitializeSounds{
	let soundsGame = [
		CreateSound(GetCurrentScriptDirectory ~ "../SE/1up.wav"),	 				//Life extend; 				index = 0
		CreateSound(GetCurrentScriptDirectory ~ "../SE/barrier_diff.wav"),	 		//BPoHC barrier erase; 		index = 1
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom00.wav"),	 				//Player bomb declare; 		index = 2
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom01.wav"),	 				//Yabusame's bomb; 			index = 3
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom02.wav"),	 				//Tsubakura's bomb; 		index = 4
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom03.wav"),	 				//Tsubakura's bomb; 		index = 5
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom04.wav"),	 				//Tsubakura's bomb; 		index = 6
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom05.wav"),	 				//Shion's bomb; 			index = 7
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom06.wav"),	 				//Yabusame's bomb (short);	index = 8
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom07.wav"),	 				//Kuroji's bomb; 			index = 9
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom08.wav"),	 				//Iyozane's bomb; 			index = 10
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom09.wav"),	 				//Tsugumi's bomb; 			index = 11
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom10.wav"),	 				//Some other bomb; 			index = 12
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom_flash_00.wav"),	 		//Weird flashbomb; 			index = 13
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom_flash_01.wav"),	 		//Flashbomb; 				index = 14
		CreateSound(GetCurrentScriptDirectory ~ "../SE/bom_get.wav"),	 			//Bomb extend; 				index = 15
		CreateSound(GetCurrentScriptDirectory ~ "../SE/boss_d.wav"),	 			//Boss explosion prepare; 	index = 16
		CreateSound(GetCurrentScriptDirectory ~ "../SE/boss_end.wav"),	 			//Boss explosion; 			index = 17
		CreateSound(GetCurrentScriptDirectory ~ "../SE/burst_00.wav"),	 			//Burst 1; 					index = 18
		CreateSound(GetCurrentScriptDirectory ~ "../SE/burst_01.wav"),	 			//Burst 2; 					index = 19
		CreateSound(GetCurrentScriptDirectory ~ "../SE/burst_02.wav"),	 			//Strange burst; 			index = 20
		CreateSound(GetCurrentScriptDirectory ~ "../SE/burst_03.wav"),	 			//Strange burst (short); 	index = 21
		CreateSound(GetCurrentScriptDirectory ~ "../SE/buy_upgrade.wav"),	 		//Upgrade purchase; 		index = 22
		CreateSound(GetCurrentScriptDirectory ~ "../SE/cell_move_00.wav"),	 		//Move left; 				index = 23
		CreateSound(GetCurrentScriptDirectory ~ "../SE/cell_move_01.wav"),	 		//Move right; 				index = 24
		CreateSound(GetCurrentScriptDirectory ~ "../SE/charge_0.wav"),	 			//Charge 1; 				index = 25
		CreateSound(GetCurrentScriptDirectory ~ "../SE/charge_1.wav"),	 			//Charge 2; 				index = 26
		CreateSound(GetCurrentScriptDirectory ~ "../SE/charge_2.wav"),	 			//Charge 3; 				index = 27
		CreateSound(GetCurrentScriptDirectory ~ "../SE/charge_3.wav"),	 			//Charge 4; 				index = 28
		CreateSound(GetCurrentScriptDirectory ~ "../SE/charge_up.wav"),	 			//Flashbomb ready; 			index = 29
		CreateSound(GetCurrentScriptDirectory ~ "../SE/cursor_00.wav"),	 			//Select; 					index = 30
		CreateSound(GetCurrentScriptDirectory ~ "../SE/cursor_01.wav"),	 			//Confirm; 					index = 31
		CreateSound(GetCurrentScriptDirectory ~ "../SE/cursor_03.wav"),	 			//Cancel;		 			index = 32
		CreateSound(GetCurrentScriptDirectory ~ "../SE/cursor_04.wav"),	 			//High pitch confirm; 		index = 33
		CreateSound(GetCurrentScriptDirectory ~ "../SE/cursor_05.wav"),	 			//High pitch cancel; 		index = 34
		CreateSound(GetCurrentScriptDirectory ~ "../SE/damage_00.wav"),	 			//Boss damage 1; 			index = 35
		CreateSound(GetCurrentScriptDirectory ~ "../SE/damage_01.wav"),	 			//Boss damage 2; 			index = 36
		CreateSound(GetCurrentScriptDirectory ~ "../SE/e_shot_00.wav"),	 			//Bullet 1; 				index = 37
		CreateSound(GetCurrentScriptDirectory ~ "../SE/e_shot_01.wav"),	 			//Bullet 2; 				index = 38
		CreateSound(GetCurrentScriptDirectory ~ "../SE/endtime.wav"),	 			//Countdown; 				index = 39
		CreateSound(GetCurrentScriptDirectory ~ "../SE/enemy_death.wav"),	 		//Enemy explosion; 			index = 40
		CreateSound(GetCurrentScriptDirectory ~ "../SE/freeze_00.wav"),	 			//Freeze; 					index = 41
		CreateSound(GetCurrentScriptDirectory ~ "../SE/full_power.wav"),	 		//Full power; 				index = 42
		CreateSound(GetCurrentScriptDirectory ~ "../SE/haniwa_00.wav"),	 			//Haniwa summon; 			index = 43
		CreateSound(GetCurrentScriptDirectory ~ "../SE/item_00.wav"),	 			//Collect item; 			index = 44
		CreateSound(GetCurrentScriptDirectory ~ "../SE/j_barrier.wav"),	 			//Player barrier shatter; 	index = 45
		CreateSound(GetCurrentScriptDirectory ~ "../SE/j_barrier_on.wav"),	 		//Player invincibility end;	index = 46
		CreateSound(GetCurrentScriptDirectory ~ "../SE/j_end.wav"),	 				//Player kill; 				index = 47
		CreateSound(GetCurrentScriptDirectory ~ "../SE/j_hit.wav"),	 				//Player hit;	 			index = 48
		CreateSound(GetCurrentScriptDirectory ~ "../SE/j_shot_00.wav"),	 			//Player shot 1; 			index = 49
		CreateSound(GetCurrentScriptDirectory ~ "../SE/j_shot_01.wav"),	 			//Player shot 2; 			index = 50
		CreateSound(GetCurrentScriptDirectory ~ "../SE/jigen_break.wav"),	 		//Barrier destruction; 		index = 51
		CreateSound(GetCurrentScriptDirectory ~ "../SE/laser_0.wav"),	 			//Laser spawn (long); 		index = 52
		CreateSound(GetCurrentScriptDirectory ~ "../SE/laser_1.wav"),	 			//Laser expand (long);		index = 53
		CreateSound(GetCurrentScriptDirectory ~ "../SE/laser_2.wav"),	 			//Laser spawn (shot); 		index = 54
		CreateSound(GetCurrentScriptDirectory ~ "../SE/laser_3.wav"),	 			//Laser expand (short); 	index = 55
		CreateSound(GetCurrentScriptDirectory ~ "../SE/power_up.wav"),	 			//Power level increase;		index = 56
		CreateSound(GetCurrentScriptDirectory ~ "../SE/rei_drain_00.wav"),	 		//Shion's spirit absorb; 	index = 57
		CreateSound(GetCurrentScriptDirectory ~ "../SE/rei_en_00.wav"),	 			//Spirit something; 		index = 58
		CreateSound(GetCurrentScriptDirectory ~ "../SE/rei_en_01.wav"),	 			//Spirit something;			index = 59
		CreateSound(GetCurrentScriptDirectory ~ "../SE/seal_00.wav"),	 			//Life/Bomb seal alert;		index = 60
		CreateSound(GetCurrentScriptDirectory ~ "../SE/soukou_no.wav"),	 			//No armor mode for you;	index = 61
		CreateSound(GetCurrentScriptDirectory ~ "../SE/soukou_on.wav"),	 			//Armor mode enable;		index = 62
		CreateSound(GetCurrentScriptDirectory ~ "../SE/spell_0.wav"),	 			//Spell declare;			index = 63
		CreateSound(GetCurrentScriptDirectory ~ "../SE/stop_00.wav"),	 			//Pause (unused);			index = 64
		CreateSound(GetCurrentScriptDirectory ~ "../SE/stop_01.wav"),	 			//Unpause (unused);			index = 65
		CreateSound(GetCurrentScriptDirectory ~ "../SE/stop_02.wav"),	 			//Pause;					index = 66
		CreateSound(GetCurrentScriptDirectory ~ "../SE/stop_03.wav"),	 			//Unpause;					index = 67
		CreateSound(GetCurrentScriptDirectory ~ "../SE/sub_event_00.wav"),	 		//Event 1;					index = 68
		CreateSound(GetCurrentScriptDirectory ~ "../SE/sub_event_01.wav"),	 		//Event 2;					index = 69
		CreateSound(GetCurrentScriptDirectory ~ "../SE/sub_event_02.wav"),	 		//Event 3;					index = 70
		CreateSound(GetCurrentScriptDirectory ~ "../SE/sub_event_03.wav"),	 		//Event 4;					index = 71
		CreateSound(GetCurrentScriptDirectory ~ "../SE/talk_00.wav"),	 			//Dialogue advance;			index = 72
		CreateSound(GetCurrentScriptDirectory ~ "../SE/thrill.wav"),	 			//Graze;					index = 73
		CreateSound(GetCurrentScriptDirectory ~ "../SE/warning.wav"),	 			//Warning/Bling;			index = 74
	];

	return soundsGame;
}

function CreateSound(path){
	let obj = ObjSound_Create();
	ObjSound_Load(obj, path);
	ObjSound_SetVolumeRate(obj, GetAreaCommonData("Settings", "SE_VOL", 100));
	ObjSound_SetSoundDivision(obj, SOUND_SE);
	return obj;
}

task PlaySoundA1(index, volume, delay){
	wait(delay);
	NotifyEventAll(EV_USER_PACKAGE, [index, volume]);
}

function IsValueInArray(array, value){ //all values in the array must be of the same type for this to work
	ascent(i in 0 .. length(array)){
		if(value == array[i]){return true;}
	}
	return false;
}