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

--
-- required files:
--  * monster_bee_left1.bmp
--  * monster_bee_left2.bmp
--  * monster_bee_right1.bmp
--  * monster_bee_right2.bmp
--
-- use with:
--  SimpleBeeMonster:new(x, y, distance, speed_x, speed_y, randomness);
--   will create Bee that flyes within circle with radius "distance"
-- or:
--  BoxedBeeMonster:new(xleft, ytop, xright, ybottom, speed_x, speed_y, randomness);
--   will create Bee that flyes in rectangle box
-- 
-- where "randomness" is [0..1000]. 0 = no random direction-changes, 1000 = 50 changes per second
--
-- Example:
--  SimpleBeeMonster:new(250, 250, 150, 3, 3, 0);
--  BoxedBeeMonster:new(50, 50, 150, 250, 2, 3, 0);
-- 


SimpleBeeMonster = {
	gfx_left = bs.anim.load("monster_bee_left%.bmp", 1, 2, 10, bs.anim.type.forwardloop),
	gfx_right = bs.anim.load("monster_bee_right%.bmp", 1, 2, 10, bs.anim.type.forwardloop),
	timer = bs.timer.new("SimpleBeeMonster_Timer", 0.02),
	snd_kill_monster = bs.snd.load("fx_kill_monster.ogg"),
	obj_cnt = 0
};
function SimpleBeeMonster:new(x, y, distance, speed_x, speed_y, randomness)
	local obj = {};
    setmetatable(obj, self);
	self.__index = self;
	obj.isAlive = 1;
	obj.randomness = randomness;
	obj.fgobject = bs.obj.new(x, y, self.gfx_left, "visible,ontop");
	obj.fgobject.callback.playercollision("SimpleBeeMonster_PlayerCollision");
	obj.x_start = x;
	obj.y_start = y;
	obj.max_distance = distance;
	obj.dir_x = speed_x;
	obj.dir_y = speed_y;
	self.obj_cnt = self.obj_cnt + 1;
	self[self.obj_cnt] = obj;
	return obj_cnt;
end

function SimpleBeeMonster_Timer(fgObject)
	for i = 1,SimpleBeeMonster.obj_cnt do
		SimpleBeeMonster[i].fgobject.x( SimpleBeeMonster[i].fgobject.x() + SimpleBeeMonster[i].dir_x );
		SimpleBeeMonster[i].fgobject.y( SimpleBeeMonster[i].fgobject.y() + SimpleBeeMonster[i].dir_y );
		
		--local distance = math.abs( SimpleBeeMonster[i].fgobject.y() - SimpleBeeMonster[i].y_start);
		local x_now = SimpleBeeMonster[i].fgobject.x();
		local y_now = SimpleBeeMonster[i].fgobject.y();
		local x_start = SimpleBeeMonster[i].x_start;
		local y_start = SimpleBeeMonster[i].y_start;
		
		local distance = -1;
		
		if math.abs(y_start-y_now) + math.abs(x_start-x_now) < SimpleBeeMonster[i].max_distance then
			distance = 0; --abschaetzung nach oben um math.pow zu vermeiden
		else
			if math.abs(y_start-y_now) > SimpleBeeMonster[i].max_distance or math.abs(x_start-x_now) > SimpleBeeMonster[i].max_distance then
				distance = SimpleBeeMonster[i].max_distance + 1; --abschaetzung nach unten um math.pow zu vermeiden
			else
				distance = math.pow(((y_start-y_now)^2) + ((x_start-x_now)^2), 0.5);
			end
		end
		
		if distance > SimpleBeeMonster[i].max_distance then
			if x_now > x_start then
				SimpleBeeMonster[i].dir_x = - math.abs(SimpleBeeMonster[i].dir_x);
			else
				SimpleBeeMonster[i].dir_x = math.abs(SimpleBeeMonster[i].dir_x);
			end
			if y_now > y_start then
				SimpleBeeMonster[i].dir_y = - math.abs(SimpleBeeMonster[i].dir_y);
			else
				SimpleBeeMonster[i].dir_y = math.abs(SimpleBeeMonster[i].dir_y);
			end
			--SimpleBeeMonster[i].dir_x = SimpleBeeMonster[i].dir_x * -1;
			--SimpleBeeMonster[i].dir_y = SimpleBeeMonster[i].dir_y * -1;
			if SimpleBeeMonster[i].dir_x > 0 then
				SimpleBeeMonster[i].fgobject.gfx(SimpleBeeMonster.gfx_right);
			else
				SimpleBeeMonster[i].fgobject.gfx(SimpleBeeMonster.gfx_left);
			end
		else
			if SimpleBeeMonster[i].randomness > math.random(0,1000) then
				SimpleBeeMonster[i].dir_x = SimpleBeeMonster[i].dir_x * -1;
			end
			if SimpleBeeMonster[i].randomness > math.random(0,1000) then
				SimpleBeeMonster[i].dir_y = SimpleBeeMonster[i].dir_y * -1;
			end
		end
	end
end

function SimpleBeeMonster_PlayerCollision(fgObject, collisionOverlap)
	for i = 1,SimpleBeeMonster.obj_cnt do
		if fgObject.internal == SimpleBeeMonster[i].fgobject.internal and SimpleBeeMonster[i].isAlive == 1 then
			if bs.player.speed_y() > 0 and bs.player.speed_y() > SimpleBeeMonster[i].dir_y then
				io.write("BeeKill: oben:",collisionOverlap.top," unten:",collisionOverlap.bottom," right:",collisionOverlap.right," left:",collisionOverlap.left, "!\n");
				--kill monster
				SimpleBeeMonster.snd_kill_monster.play();
				SimpleBeeMonster[i].fgobject.visible(bs.no);
				SimpleBeeMonster[i].isAlive = 0;
				bs.player.score( bs.player.score() + 600 );
			else
				--kill kate
				bs.player.kill();
			end
		end
	end
end







BoxedBeeMonster = {
	gfx_left = bs.anim.load("monster_bee_left%.bmp", 1, 2, 10, bs.anim.type.forwardloop),
	gfx_right = bs.anim.load("monster_bee_right%.bmp", 1, 2, 10, bs.anim.type.forwardloop),
	timer = bs.timer.new("BoxedBeeMonster_Timer", 0.02),
	snd_kill_monster = bs.snd.load("fx_kill_monster.ogg"),
	obj_cnt = 0
};
function BoxedBeeMonster:new(xleft, ytop, xright, ybottom, speed_x, speed_y, randomness)
	local obj = {};
    setmetatable(obj, self);
	self.__index = self;
	obj.isAlive = 1;
	obj.randomness = randomness;
	obj.xleft = xleft;
	obj.ytop = ytop;
	obj.xright = xright;
	obj.ybottom = ybottom;
	obj.fgobject = bs.obj.new((xleft+xright)/2, (ytop+ybottom)/2, self.gfx_left, "visible,ontop");
	obj.fgobject.callback.playercollision("BoxedBeeMonster_PlayerCollision");
	obj.dir_x = speed_x;
	obj.dir_y = speed_y;
	self.obj_cnt = self.obj_cnt + 1;
	self[self.obj_cnt] = obj;
	return obj_cnt;
end

function BoxedBeeMonster_Timer(fgObject)
	for i = 1,BoxedBeeMonster.obj_cnt do
		BoxedBeeMonster[i].fgobject.x( BoxedBeeMonster[i].fgobject.x() + BoxedBeeMonster[i].dir_x );
		BoxedBeeMonster[i].fgobject.y( BoxedBeeMonster[i].fgobject.y() + BoxedBeeMonster[i].dir_y );

		if BoxedBeeMonster[i].fgobject.y() > BoxedBeeMonster[i].ybottom or BoxedBeeMonster[i].fgobject.y() < BoxedBeeMonster[i].ytop then
			BoxedBeeMonster[i].dir_y = BoxedBeeMonster[i].dir_y * -1;
		else
			if BoxedBeeMonster[i].randomness > math.random(0,1000) then
				BoxedBeeMonster[i].dir_y = SimpleBeeMonster[i].dir_y * -1;
			end		
		end
		local switched_x = 0;
		if BoxedBeeMonster[i].fgobject.x() > BoxedBeeMonster[i].xright or BoxedBeeMonster[i].fgobject.x() < BoxedBeeMonster[i].xleft then
			BoxedBeeMonster[i].dir_x = BoxedBeeMonster[i].dir_x * -1;
			switched_x = 1;
		else
			if BoxedBeeMonster[i].randomness > math.random(0,1000) then
				BoxedBeeMonster[i].dir_x = BoxedBeeMonster[i].dir_x * -1;
				switched_x = 1;
			end	
		end
		if switched_x == 1 then
			if BoxedBeeMonster[i].dir_x > 0 then
				BoxedBeeMonster[i].fgobject.gfx(BoxedBeeMonster.gfx_right);
			else
				BoxedBeeMonster[i].fgobject.gfx(BoxedBeeMonster.gfx_left);
			end
		end
	end
end

function BoxedBeeMonster_PlayerCollision(fgObject, collisionOverlap)
	for i = 1,BoxedBeeMonster.obj_cnt do
		if fgObject.internal == BoxedBeeMonster[i].fgobject.internal and BoxedBeeMonster[i].isAlive == 1 then
			if bs.player.speed_y() > 0 and bs.player.speed_y() > BoxedBeeMonster[i].dir_y then
				io.write("BeeKill: oben:",collisionOverlap.top," unten:",collisionOverlap.bottom," right:",collisionOverlap.right," left:",collisionOverlap.left, "!\n");
				--kill monster
				BoxedBeeMonster.snd_kill_monster.play();
				BoxedBeeMonster[i].fgobject.visible(bs.no);
				BoxedBeeMonster[i].isAlive = 0;
				bs.player.score( bs.player.score() + 600 );
			else
				--kill kate
				bs.player.kill();
			end
		end
	end
end
