function InitBugs()
	BUGS_AMOUNT = 3;
	BUGS_FPS = 25
	
	current_bug = 0;
	state = 'stop'
	
	Bugs = {}
	
	----------------------------------------
	Bugs[1] = {}
	InitBug1(Bugs[1]);
	Bugs[1].Entity:AttachTo(miniGamePlayField);
	Bugs[1].Entity:SetZ(201);
	Bugs[1].Entity:SetHotSpot(-50, -70);
	Bugs[1].Entity:SetXY(-50, -70);
	Bugs[1].Entity:SetDestroysOnEnd(false);
	Bugs[1].Entity:Stop();
	Bugs[1].Time = Bugs[1].Entity:GetDuration();
	Bugs[1].CurTime = 0.0;
	Bugs[1].Run = false;
	----------------------------------------
	
	----------------------------------------
	Bugs[2] = {}
	InitBug2(Bugs[2]);
	Bugs[2].Entity:AttachTo(miniGamePlayField);
	Bugs[2].Entity:SetZ(202);
	Bugs[2].Entity:SetHotSpot(-50, -70);
	Bugs[2].Entity:SetXY(-50, -70);
	Bugs[2].Entity:SetDestroysOnEnd(false);
	Bugs[2].Entity:Stop();
	Bugs[2].Time = Bugs[1].Entity:GetDuration();
	Bugs[2].CurTime = 0.0;
	Bugs[2].Run = false;
	----------------------------------------
	
	----------------------------------------
	Bugs[3] = {}
	InitBug3(Bugs[3]);
	Bugs[3].Entity:AttachTo(miniGamePlayField);
	Bugs[3].Entity:SetZ(203);
	Bugs[3].Entity:SetHotSpot(-50, -70);
	Bugs[3].Entity:SetXY(-50, -70);
	Bugs[3].Entity:SetDestroysOnEnd(false);
	Bugs[3].Entity:Stop();
	Bugs[3].Time = Bugs[1].Entity:GetDuration();
	Bugs[3].CurTime = 0.0;
	Bugs[3].Run = false;
	----------------------------------------
end

function UpdateBugs()
		
	if(state == 'stop') then --если надо создать нового жука
		if(Bugs[current_bug]) then
			UnRunBug();
		end
		
		local rand = 0;
		repeat
			rand = math.random(BUGS_AMOUNT);
		until current_bug ~= rand
	
		current_bug = rand;
		miniGamePlayField:GetObject('actZoneBug'):SetXY(50, 70);
		miniGamePlayField:GetObject('actZoneBug'):AttachTo(Bugs[current_bug].Element);
		Bugs[current_bug].Entity:Play();
		Bugs[current_bug].Run = true;
		state = 'move';
	end
	
	if(Bugs[current_bug] ~= nil and Bugs[current_bug].Entity:GetCurrentTime() >= Bugs[current_bug].Time and state  ~= 'wait')then
		state = 'wait';
		TimerDoString('state = "stop";', math.random(15), miniGamePlayField:GetTimer());
	elseif(state == 'move' and math.random(10000) < 10) then --если надо остановить жука
		Bugs[current_bug].Run = false;
		Bugs[current_bug].Entity:Pause();
		state = 'pause';
	elseif(state == 'pause' and math.random(10000) < 10) then--если надо пустить жука
	    Bugs[current_bug].Run = true;
		Bugs[current_bug].Entity:Resume();
		state = 'move';
	end
end

function RunBug()
	if(Bugs[current_bug].Run == false and state == 'pause') then
		speed = 1.5;
		Bugs[current_bug].Run = true;
		Bugs[current_bug].Entity:Pause();
		Bugs[current_bug].Element:GetTexture():SetSpeed(BUGS_FPS * 1.2);
		Bugs[current_bug].Entity:SetSpeed(2.0);
		Bugs[current_bug].Entity:Resume();
	end
end


function UnRunBug()
	if(Bugs[current_bug].Run == true) then
		speed = 1.0;
		Bugs[current_bug].Run = false;
		Bugs[current_bug].Element:GetTexture():SetSpeed(BUGS_FPS * speed);
		Bugs[current_bug].Entity:SetSpeed(speed);
	end
end


function InitBug1(Bug)
	local element = nil;
	local speed = 0.8;
	
    local subComposition = CreateCompositionSequence();
    local compositionDuration = 5.44 / speed;
    subComposition:SetDuration(compositionDuration);
	subComposition:SetCyclic(false);
	
	local zOffset = 200
	-- Layer Layer_1
	element = CreateCompositionElement("data/Texture/Minigames/Zeppelin/Bug_seq01.png");
	local anim = element:GetTexture();
	
	anim:SetSpeed(BUGS_FPS * speed); -- fps
	anim:SetFrames(7);
	anim:SetFrame(0);
	--anim:SetHotSpot(50, 70);
	anim:SetTextureRect(0, 0, 100, 140);
	anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
	anim:Play();
	
	element:SetName("bug_1");
	subComposition:AddElement(element);
	element:SetParentElement(subComposition);
	
	effector = element:GetEffector();
	effector:SetKey(_TRACK_X, 0 / speed, 573);
	effector:SetKey(_TRACK_Y, 0 / speed, 846);
	effector:SetKey(_TRACK_X, 0.36 / speed, 582);
	effector:SetKey(_TRACK_Y, 0.36 / speed, 772);
	effector:SetKey(_TRACK_X, 0.68 / speed, 590);
	effector:SetKey(_TRACK_Y, 0.68 / speed, 705);
	effector:SetKey(_TRACK_X, 0.88 / speed, 594.242721557617);
	effector:SetKey(_TRACK_Y, 0.88 / speed, 665.786407470703);
	effector:SetKey(_TRACK_X, 1.32 / speed, 623.630542559563);
	effector:SetKey(_TRACK_Y, 1.32 / speed, 586.418474906489);
	effector:SetKey(_TRACK_X, 1.64 / speed, 656.219704899255);
	effector:SetKey(_TRACK_Y, 1.64 / speed, 525.393991768329);
	effector:SetKey(_TRACK_X, 2 / speed, 679.0275101648);
	effector:SetKey(_TRACK_Y, 2 / speed, 456.45397682769);
	effector:SetKey(_TRACK_X, 2.44 / speed, 677.038169503555);
	effector:SetKey(_TRACK_Y, 2.44 / speed, 386.871998331962);
	effector:SetKey(_TRACK_X, 3 / speed, 652.109821062618);
	effector:SetKey(_TRACK_Y, 3 / speed, 307.621084792579);
	effector:SetKey(_TRACK_X, 3.44 / speed, 631.110391773975);
	effector:SetKey(_TRACK_Y, 3.44 / speed, 234.802526626693);
	effector:SetKey(_TRACK_X, 3.8 / speed, 622.811325196115);
	effector:SetKey(_TRACK_Y, 3.8 / speed, 175.090991107481);
	effector:SetKey(_TRACK_X, 4.24 / speed, 631.013949650891);
	effector:SetKey(_TRACK_Y, 4.24 / speed, 98.9953564894367);
	effector:SetKey(_TRACK_X, 4.88 / speed, 646.572814941406);
	effector:SetKey(_TRACK_Y, 4.88 / speed, 0.73786926269531);
	effector:SetKey(_TRACK_X, 5.44 / speed, 660.242721557617);
	effector:SetKey(_TRACK_Y, 5.44 / speed, -117.320388793945);
	effector:SetKey(_TRACK_SX, 0 / speed, 0.85);
	effector:SetKey(_TRACK_SY, 0 / speed, 0.85);
	effector:SetKey(_TRACK_SX, 0.52 / speed, 0.85);
	effector:SetKey(_TRACK_SY, 0.52 / speed, 0.85);
	effector:SetKey(_TRACK_SX, 0.68 / speed, 0.8738029212993);
	effector:SetKey(_TRACK_SY, 0.68 / speed, 0.7836926286674);
	effector:SetKey(_TRACK_SX, 0.88 / speed, 1);
	effector:SetKey(_TRACK_SY, 0.88 / speed, 1);
	effector:SetKey(_TRACK_SX, 4.56 / speed, 1);
	effector:SetKey(_TRACK_SY, 4.56 / speed, 1);
	effector:SetKey(_TRACK_SX, 4.68 / speed, 0.85);
	effector:SetKey(_TRACK_SY, 4.68 / speed, 0.85);
	effector:SetKey(_TRACK_A, 0 / speed, 0.08545987619481);
	effector:SetKey(_TRACK_A, 0.48 / speed, 0.16280300400564);
	effector:SetKey(_TRACK_A, 0.84 / speed, 0.10664229986529);
	effector:SetKey(_TRACK_A, 1.08 / speed, 0.41807030883119);
	effector:SetKey(_TRACK_A, 1.24 / speed, 0.47603692120432);
	effector:SetKey(_TRACK_A, 1.56 / speed, 0.397635961181);
	effector:SetKey(_TRACK_A, 1.8 / speed, 0.25279367075626);
	effector:SetKey(_TRACK_A, 1.96 / speed, 0.13082440691358);
	effector:SetKey(_TRACK_A, 2.12 / speed, 0.02998230002013);
	effector:SetKey(_TRACK_A, 2.28 / speed, -0.10684681683931);
	effector:SetKey(_TRACK_A, 2.4 / speed, -0.23242908281688);
	effector:SetKey(_TRACK_A, 2.64 / speed, -0.39292191525431);
	effector:SetKey(_TRACK_A, 3 / speed, -0.26870141264129);
	effector:SetKey(_TRACK_A, 3.28 / speed, -0.1648139128342);
	effector:SetKey(_TRACK_A, 3.44 / speed, -0.14272919846868);
	effector:SetKey(_TRACK_A, 3.6 / speed, 0.00903954217022);
	effector:SetKey(_TRACK_A, 3.76 / speed, 0.03877890862902);
	effector:SetKey(_TRACK_A, 4.64 / speed, 0.13486641120078);
	effector:Play();
	
	Bug.Entity = subComposition;
	Bug.Element = element;
end

function InitBug2(Bug)
	local element = nil;
	local speed = 0.8;
	
    local subComposition = CreateCompositionSequence();
    local compositionDuration = 6.36/ speed;
    subComposition:SetDuration(compositionDuration);
	subComposition:SetCyclic(false);
	
	local zOffset = 200
	-- Layer Layer_1
	element = CreateCompositionElement("data/Texture/Minigames/Zeppelin/Bug_seq01.png");
	local anim = element:GetTexture();
	anim:SetSpeed(BUGS_FPS * speed); -- fps
	anim:SetFrames(7);
	anim:SetFrame(0);
	--anim:SetHotSpot(50, 70);
	anim:SetTextureRect(0, 0, 100, 140);
	anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);

	anim:Play();
	
	element:SetName("bug_2");
	subComposition:AddElement(element);
	element:SetParentElement(subComposition);
	
	effector = element:GetEffector();
	effector:SetKey(_TRACK_X, 0 / speed, 943);
	effector:SetKey(_TRACK_Y, 0 / speed, 840);
	effector:SetKey(_TRACK_X, 0.4 / speed, 935);
	effector:SetKey(_TRACK_Y, 0.4 / speed, 755);
	effector:SetKey(_TRACK_X, 0.76 / speed, 927);
	effector:SetKey(_TRACK_Y, 0.76 / speed, 682);
	effector:SetKey(_TRACK_X, 1.12 / speed, 903);
	effector:SetKey(_TRACK_Y, 1.12 / speed, 586);
	effector:SetKey(_TRACK_X, 1.4 / speed, 886.409091287356);
	effector:SetKey(_TRACK_Y, 1.4 / speed, 528.500001109577);
	effector:SetKey(_TRACK_X, 1.56 / speed, 873);
	effector:SetKey(_TRACK_Y, 1.56 / speed, 498);
	effector:SetKey(_TRACK_X, 1.72 / speed, 851.299929052606);
	effector:SetKey(_TRACK_Y, 1.72 / speed, 464.199913286518);
	effector:SetKey(_TRACK_X, 1.96 / speed, 810);
	effector:SetKey(_TRACK_Y, 1.96 / speed, 421);
	effector:SetKey(_TRACK_X, 2.2 / speed, 759);
	effector:SetKey(_TRACK_Y, 2.2 / speed, 381);
	effector:SetKey(_TRACK_X, 2.56 / speed, 692);
	effector:SetKey(_TRACK_Y, 2.56 / speed, 349);
	effector:SetKey(_TRACK_X, 2.96 / speed, 606);
	effector:SetKey(_TRACK_Y, 2.96 / speed, 328);
	effector:SetKey(_TRACK_X, 3.36 / speed, 524);
	effector:SetKey(_TRACK_Y, 3.36 / speed, 321);
	effector:SetKey(_TRACK_X, 3.8 / speed, 426);
	effector:SetKey(_TRACK_Y, 3.8 / speed, 316);
	effector:SetKey(_TRACK_X, 4 / speed, 383.625097796626);
	effector:SetKey(_TRACK_Y, 4 / speed, 307.750024793511);
	effector:SetKey(_TRACK_X, 4.12 / speed, 355);
	effector:SetKey(_TRACK_Y, 4.12 / speed, 298);
	effector:SetKey(_TRACK_X, 4.28 / speed, 330.499665737859);
	effector:SetKey(_TRACK_Y, 4.28 / speed, 277.999642417245);
	effector:SetKey(_TRACK_X, 4.44 / speed, 312);
	effector:SetKey(_TRACK_Y, 4.44 / speed, 252);
	effector:SetKey(_TRACK_X, 4.72 / speed, 287);
	effector:SetKey(_TRACK_Y, 4.72 / speed, 194);
	effector:SetKey(_TRACK_X, 4.92 / speed, 274);
	effector:SetKey(_TRACK_Y, 4.92 / speed, 155);
	effector:SetKey(_TRACK_X, 5.16 / speed, 260);
	effector:SetKey(_TRACK_Y, 5.16 / speed, 104);
	effector:SetKey(_TRACK_X, 5.48 / speed, 247);
	effector:SetKey(_TRACK_Y, 5.48 / speed, 43);
	effector:SetKey(_TRACK_X, 5.8 / speed, 235);
	effector:SetKey(_TRACK_Y, 5.8 / speed, -17);
	effector:SetKey(_TRACK_X, 6.12 / speed, 222);
	effector:SetKey(_TRACK_Y, 6.12 / speed, -87);
	effector:SetKey(_TRACK_SX, 0 / speed, 0.85);
	effector:SetKey(_TRACK_SY, 0 / speed, 0.85);
	effector:SetKey(_TRACK_SX, 0.64 / speed, 0.85);
	effector:SetKey(_TRACK_SY, 0.64 / speed, 0.85);
	effector:SetKey(_TRACK_SX, 0.8 / speed, 1);
	effector:SetKey(_TRACK_SY, 0.8 / speed, 1);
	effector:SetKey(_TRACK_SX, 3.24 / speed, 1);
	effector:SetKey(_TRACK_SY, 3.24 / speed, 1);
	effector:SetKey(_TRACK_SX, 3.36 / speed, 1.05);
	effector:SetKey(_TRACK_SY, 3.36 / speed, 1.05);
	effector:SetKey(_TRACK_SX, 3.56 / speed, 1);
	effector:SetKey(_TRACK_SY, 3.56 / speed, 1);
	effector:SetKey(_TRACK_SX, 5.36 / speed, 1);
	effector:SetKey(_TRACK_SY, 5.36 / speed, 1);
	effector:SetKey(_TRACK_SX, 5.52 / speed, 0.85);
	effector:SetKey(_TRACK_SY, 5.52 / speed, 0.85);
	effector:SetKey(_TRACK_A, 0 / speed, 0.08545987619481);
	effector:SetKey(_TRACK_A, 0.4 / speed, -0.03516379224746);
	effector:SetKey(_TRACK_A, 1.12 / speed, -0.23183966865753);
	effector:SetKey(_TRACK_A, 1.56 / speed, -0.54920428590273);
	effector:SetKey(_TRACK_A, 2.04 / speed, -0.7290577854897);
	effector:SetKey(_TRACK_A, 2.2 / speed, -0.83631308055942);
	effector:SetKey(_TRACK_A, 2.56 / speed, -1.04219900219311);
	effector:SetKey(_TRACK_A, 2.96 / speed, -1.29379460733399);
	effector:SetKey(_TRACK_A, 3.36 / speed, -1.51384540375819);
	effector:SetKey(_TRACK_A, 4.12 / speed, -1.28495730612252);
	effector:SetKey(_TRACK_A, 4.44 / speed, -0.71856961928757);
	effector:SetKey(_TRACK_A, 4.72 / speed, -0.38606679147977);
	effector:SetKey(_TRACK_A, 4.92 / speed, -0.2735011758047);
	effector:SetKey(_TRACK_A, 5.16 / speed, -0.20980613288186);
	effector:Play();
	
	Bug.Entity = subComposition;
	Bug.Element = element;
end

function InitBug3(Bug)
	local element = nil;
	local speed = 0.8;
	
    local subComposition = CreateCompositionSequence();
    local compositionDuration = 6.24/ speed;
    subComposition:SetDuration(compositionDuration);
	subComposition:SetCyclic(false);
	
	local zOffset = 200
	-- Layer Layer_1
	element = CreateCompositionElement("data/Texture/Minigames/Zeppelin/Bug_seq01.png");
	local anim = element:GetTexture();
	anim:SetSpeed(BUGS_FPS * speed); -- fps
	anim:SetFrames(7);
	anim:SetFrame(0);
	--anim:SetHotSpot(50, 70);
	anim:SetTextureRect(0, 0, 100, 140);
	anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);

	anim:Play();
	
	element:SetName("bug_3");
	subComposition:AddElement(element);
	element:SetParentElement(subComposition);
	
	effector = element:GetEffector();
	
	effector:SetKey(_TRACK_X, 0 / speed, 938);
	effector:SetKey(_TRACK_Y, 0 / speed, -91);
	effector:SetKey(_TRACK_X, 0.28 / speed, 911);
	effector:SetKey(_TRACK_Y, 0.28 / speed, -38);
	effector:SetKey(_TRACK_X, 0.56 / speed, 878);
	effector:SetKey(_TRACK_Y, 0.56 / speed, 14);
	effector:SetKey(_TRACK_X, 0.88 / speed, 838);
	effector:SetKey(_TRACK_Y, 0.88 / speed, 72);
	effector:SetKey(_TRACK_X, 1.16 / speed, 793);
	effector:SetKey(_TRACK_Y, 1.16 / speed, 118);
	effector:SetKey(_TRACK_X, 1.44 / speed, 738);
	effector:SetKey(_TRACK_Y, 1.44 / speed, 158);
	effector:SetKey(_TRACK_X, 1.76 / speed, 683);
	effector:SetKey(_TRACK_Y, 1.76 / speed, 196);
	effector:SetKey(_TRACK_X, 2.04 / speed, 640);
	effector:SetKey(_TRACK_Y, 2.04 / speed, 241);
	effector:SetKey(_TRACK_X, 2.4 / speed, 601);
	effector:SetKey(_TRACK_Y, 2.4 / speed, 307);
	effector:SetKey(_TRACK_X, 2.72 / speed, 557);
	effector:SetKey(_TRACK_Y, 2.72 / speed, 368);
	effector:SetKey(_TRACK_X, 3.08 / speed, 495);
	effector:SetKey(_TRACK_Y, 3.08 / speed, 408);
	effector:SetKey(_TRACK_X, 3.36 / speed, 414);
	effector:SetKey(_TRACK_Y, 3.36 / speed, 419);
	effector:SetKey(_TRACK_X, 3.64 / speed, 334);
	effector:SetKey(_TRACK_Y, 3.64 / speed, 404);
	effector:SetKey(_TRACK_X, 3.96 / speed, 264);
	effector:SetKey(_TRACK_Y, 3.96 / speed, 352);
	effector:SetKey(_TRACK_X, 4.32 / speed, 218);
	effector:SetKey(_TRACK_Y, 4.32 / speed, 282);
	effector:SetKey(_TRACK_X, 4.68 / speed, 190);
	effector:SetKey(_TRACK_Y, 4.68 / speed, 205);
	effector:SetKey(_TRACK_X, 5.16 / speed, 174);
	effector:SetKey(_TRACK_Y, 5.16 / speed, 100);
	effector:SetKey(_TRACK_X, 5.56 / speed, 171);
	effector:SetKey(_TRACK_Y, 5.56 / speed, 13);
	effector:SetKey(_TRACK_X, 6.04 / speed, 172);
	effector:SetKey(_TRACK_Y, 6.04 / speed, -99);
	effector:SetKey(_TRACK_SX, 0 / speed, 0.85);
	effector:SetKey(_TRACK_SY, 0 / speed, 0.85);
	effector:SetKey(_TRACK_SX, 0.56 / speed, 0.85);
	effector:SetKey(_TRACK_SY, 0.56 / speed, 0.85);
	effector:SetKey(_TRACK_SX, 0.72 / speed, 1);
	effector:SetKey(_TRACK_SY, 0.72 / speed, 1);
	effector:SetKey(_TRACK_SX, 2.8 / speed, 1);
	effector:SetKey(_TRACK_SY, 2.8 / speed, 1);
	effector:SetKey(_TRACK_SX, 2.96 / speed, 1.04);
	effector:SetKey(_TRACK_SY, 2.96 / speed, 1.04);
	effector:SetKey(_TRACK_SX, 3.2 / speed, 1);
	effector:SetKey(_TRACK_SY, 3.2 / speed, 1);
	effector:SetKey(_TRACK_SX, 5.24 / speed, 1);
	effector:SetKey(_TRACK_SY, 5.24 / speed, 1);
	effector:SetKey(_TRACK_SX, 5.44 / speed, 0.85);
	effector:SetKey(_TRACK_SY, 5.44 / speed, 0.85);
	effector:SetKey(_TRACK_SX, 6.04 / speed, 0.85);
	effector:SetKey(_TRACK_SY, 6.04 / speed, 0.85);
	effector:SetKey(_TRACK_A, 0 / speed, -2.66449945729926);
	effector:SetKey(_TRACK_A, 0.64 / speed, -2.52768138602714);
	effector:SetKey(_TRACK_A, 0.84 / speed, -2.43236484357604);
	effector:SetKey(_TRACK_A, 1.16 / speed, -2.22095565463569);
	effector:SetKey(_TRACK_A, 1.52 / speed, -2.16465706012867);
	effector:SetKey(_TRACK_A, 1.76 / speed, -2.36368527136171);
	effector:SetKey(_TRACK_A, 1.96 / speed, -2.54357019488367);
	effector:SetKey(_TRACK_A, 2.2 / speed, -2.65431370990756);
	effector:SetKey(_TRACK_A, 2.48 / speed, -2.51493413984871);
	effector:SetKey(_TRACK_A, 2.68 / speed, -2.23942863741712);
	effector:SetKey(_TRACK_A, 2.88 / speed, -2.06511509798265);
	effector:SetKey(_TRACK_A, 3.04 / speed, -1.86216392804426);
	effector:SetKey(_TRACK_A, 3.28 / speed, -1.56596915803636);
	effector:SetKey(_TRACK_A, 3.52 / speed, -1.26715297010792);
	effector:SetKey(_TRACK_A, 3.76 / speed, -0.91698708617909);
	effector:SetKey(_TRACK_A, 4.04 / speed, -0.59927993865866);
	effector:SetKey(_TRACK_A, 4.24 / speed, -0.4400673460397);
	effector:SetKey(_TRACK_A, 4.52 / speed, -0.21789310553711);
	effector:SetKey(_TRACK_A, 4.84 / speed, -0.15594088976894);
	effector:SetKey(_TRACK_A, 5.2 / speed, 0.00646583933173);
	effector:SetKey(_TRACK_A, 6.04 / speed, 0.00646583933173);
	effector:Play();
	
	Bug.Entity = subComposition;
	Bug.Element = element;
end
