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

--
-- required files:
--  * explo1.bmp - explo9.bmp
--  * bomb.png
--
-- todo: ExplodeAnimation(), fuse() and explode() don't use any kind of "namespacing"

-- use with:
--  Bomb:new(x, y);
--

Bomb = {
	gfx_explosion = {
			[1] = bs.gfx.load("explo1.png"),
			[2] = bs.gfx.load("explo2.png"),
			[3] = bs.gfx.load("explo3.png"),
			[4] = bs.gfx.load("explo4.png"),
			[5] = bs.gfx.load("explo5.png"),
			[6] = bs.gfx.load("explo6.png"),
			[7] = bs.gfx.load("explo7.png"),
			[8] = bs.gfx.load("explo8.png"),
			[9] = bs.gfx.load("explo9.png")
	},
	gfx_bomb=bs.gfx.load("bomb.png");
	obj_cnt = 0,
	exploding=0,
	explosiontimer=bs.timer.new("explode",0.9)
};
function Bomb:new(x,y)
    local obj = {};
    setmetatable(obj, self);
    self.__index = self;
    obj.fgobject=bs.obj.new((x-1)*32, (y-1)*32, obj.gfx_bomb, "visible,ontop");
    self.obj_cnt = self.obj_cnt + 1;
    self[self.obj_cnt] = obj;
end


function ExplodeAnimation ()
	for i = 1, Bomb.obj_cnt do
		    Bomb[i].animationFrame = math.mod(Bomb[i].animationFrame, 9) + 1;
		    Bomb[i].fgobject.gfx(Bomb.gfx_explosion[Bomb[i].animationFrame]);
	end
end
function fuse()
    if Bomb.exploding==0 then 
        --play explosion sound WHILE playing explosion animation
        Bomb.exploding=1;
	Bomb.explosiontimer.active(bs.yes);
    end
end
function explode()
    if Bomb.exploding==2 then
        Bomb.explosiontimer.active(bs.no);
	Bomb.exploding=0;
	-- Here the object should erase itself, so the bomb doesn't appear anymore
        bs.player.kill();
    end
    if Bomb.exploding==1 then
        bs.timer.new("ExplodeAnimation", 0.1);
        Bomb.exploding=2;
    end
end
