var fps30 = 1;
var fpsmax = 1;

Number.prototype.to_delta_time = function()
{
    var deltatime = fpsmax/fps30;
    return this * deltatime;
}

setInterval
(
    function()
    {
        fps30 = Date.now();

        var body = document.querySelector('body');
        var is_update = body.getAttribute('is_update');

        if(is_update == 'false')
        {
            return;
        }
        else
        {

        }

        var u = document.querySelectorAll("*[update]");

        u.forEach
        (
            function(c)
            {
                var w = c.getAttribute('update');
                var id = c.getAttribute('id');
                eval(w.PROPS({this:id}));
            }
        );

    }
    ,
    1000/30
);
setInterval
(
    function()
    {
        fpsmax = Date.now();
    }
    ,
    0
);
setInterval
(
    function()
    {
        var u = document.querySelectorAll("*[seconds]");
        u.forEach
        (
            function(c)
            {
                var w = c.getAttribute('seconds');
                var id = c.getAttribute('id');
                eval(w.PROPS({this:id}));
            }
        );
    }
    ,
    1000/3
);
setInterval
(
    function()
    {
        //matas vs bugs

        var matas = document.querySelectorAll('tower[is_prefab=false]');
        var bugs = document.querySelectorAll('bug[is_prefab=false]');

        console.log(matas);
        console.log(bugs);

        bugs.forEach
        (
            function(bug)
            {
                matas.forEach
                (
                    function(mata)
                    {
                        var b = is_colliding(bug, mata);
                        if(b)
                        {
                            bug.remove();
                            mata.remove();
                        }
                    }
                )

                if(is_colliding(bug, endgame))
                {
                    //bugs vs endgame
                    alert('P E R D I S T E / Y O U  L O S E !');
                    location.reload();
                }
            }
        );

        matas.forEach
        (
            function(n)
            {
                if(is_colliding(n, wingame))
                {
                    //matas vs wingame
                    alert('G A N A S T E / Y O U  W I N !');
                    location.reload();
                }
            }
        )

        
    }
    ,
    6000
);