io.write("StopableTropfLava-Maker included!\n");

--
-- required files:
--  * tropflava%%%%.png (0-29)
--
-- use with:
--  StopableTropfLava:new (x_pos, y_pos, startframe, framerate, x_pos_switch, y_pos_switch);
--    where "startframe" is a number from 0..29
--
-- ToDo:
--  * Add Sound
--  * This include file uses the bs.player.callback.livelost wich may conflict if it's used elsewhere.
--

bs.player.callback.livelost("StopableTropfLava_HookLiveLost");


StopableTropfLava = {
	count_objects = 0,
	GfxSwitchOn = bs.gfx.load("fg_switch_green.bmp"),
	GfxSwitchOff = bs.gfx.load("fg_switch_red.bmp"),
	TimerTurnOffAtEndOfCycle = bs.timer.new("StopableTropfLava_TimerTurnOffAtEndOfCycle", 0.01),
}
function StopableTropfLava:new (x_pos, y_pos, startframe, framerate, x_pos_switch, y_pos_switch)
	self.count_objects = self.count_objects + 1;
	local i = self.count_objects;
	self[i] = {};
	
	self[i].switchOn = 0;
	self[i].fgObjSwitch = bs.obj.new(x_pos_switch, y_pos_switch, self.GfxSwitchOff, "visible");
	self[i].fgObjSwitch.callback.playeraction("StopableTropfLava_ToggleSwitch");
	
	self[i].flagTurnOffAtEndOfCycle = 0;
	
	self[i].framerate = framerate;
	self[i].fgObjTropfLava = TropfLava:new(x_pos, y_pos, startframe, framerate);
end

function StopableTropfLava_ToggleSwitch(fgObjSwitch)
	for i = 1,StopableTropfLava.count_objects do
		--TODO fix workaround, warum geht "StopableTropfLava[i].fgObjSwitch == fgObjSwitch" nicht?
		if StopableTropfLava[i].fgObjSwitch.x() == fgObjSwitch.x() and StopableTropfLava[i].fgObjSwitch.y() == fgObjSwitch.y() then
			if StopableTropfLava[i].switchOn == 0 then
				StopableTropfLava[i].switchOn = 1;
				StopableTropfLava[i].fgObjSwitch.gfx(StopableTropfLava.GfxSwitchOn);
				StopableTropfLava[i].flagTurnOffAtEndOfCycle = 1;
				StopableTropfLava.TimerTurnOffAtEndOfCycle.active(bs.yes);
			else
				StopableTropfLava[i].switchOn = 0;
				StopableTropfLava[i].fgObjSwitch.gfx(StopableTropfLava.GfxSwitchOff);
				StopableTropfLava[i].flagTurnOffAtEndOfCycle = 0;
				StopableTropfLava[i].fgObjTropfLava.gfx().currentframe(0);
				StopableTropfLava[i].fgObjTropfLava.killing(bs.yes);
				StopableTropfLava[i].fgObjTropfLava.visible(bs.yes);
			end
		end
	end
end

function StopableTropfLava_TimerTurnOffAtEndOfCycle(fgObjSwitch)
	local timer_is_still_needed = 0; -- assume we're not needed anymore
	for i = 1,StopableTropfLava.count_objects do
		if StopableTropfLava[i].flagTurnOffAtEndOfCycle == 1 then
			if StopableTropfLava[i].fgObjTropfLava.gfx().currentframe() == 29 then
				StopableTropfLava[i].fgObjTropfLava.visible(bs.no);
				StopableTropfLava[i].fgObjTropfLava.killing(bs.no);
				StopableTropfLava[i].flagTurnOffAtEndOfCycle = 0;
			else
				timer_is_still_needed = 1;
			end
		end
	end
	if timer_is_still_needed == 0 then
		StopableTropfLava.TimerTurnOffAtEndOfCycle.active(bs.no);
	end
end

function StopableTropfLava_HookLiveLost ()
	for i = 1,StopableTropfLava.count_objects do
		StopableTropfLava[i].switchOn = 0;
		StopableTropfLava[i].fgObjSwitch.gfx(StopableTropfLava.GfxSwitchOff);
		StopableTropfLava[i].flagTurnOffAtEndOfCycle = 0;
		StopableTropfLava[i].fgObjTropfLava.gfx().currentframe(0);
		StopableTropfLava[i].fgObjTropfLava.killing(bs.yes);
		StopableTropfLava[i].fgObjTropfLava.visible(bs.yes);
	end
end
