Skip to content
This repository was archived by the owner on Oct 2, 2021. It is now read-only.
This repository was archived by the owner on Oct 2, 2021. It is now read-only.

Lag reduce, running in worker thread #309

@tungbuivn

Description

@tungbuivn

hello, first of all, i'm really like your superior script :)
the lag happen when there are many snake on screen, and the loop handle too muck for ..loop, this cause client lag while running script, i'think you could put it into a worker thread, and your script only handle event from worker, i saw some where this script
var blob = new Blob([
"onmessage = function(e) { postMessage('msg from worker'); }"]);

// Obtain a blob URL reference to our worker 'file'.
var blobURL = window.URL.createObjectURL(blob);

var worker = new Worker(blobURL);
worker.onmessage = function(e) {
// e.data == 'msg from worker'
};
worker.postMessage(); // Start the worker.

Doest is possible to make your script run in thread ?

and another issue is setTimeout,setInterval function, i think window.requestAnimationFrame is better, what do you think ?
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / bot.opt.targetFps);
};
})();

and a small piece of code check colision between snakes:

from line 617:
canvasUtil.getDistance2FromSnake(collisionPoint);
bot.addCollisionAngle(collisionPoint);

                        if (scPoint === undefined ||
                            scPoint.distance > collisionPoint.distance) {
                            scPoint = collisionPoint;
                        }

this is the colision of bone object, a safe colision will be side of bot snake face to, do i wrong ?
if (scPoint === undefined ||
scPoint.distance > collisionPoint.distance-collisionPoint.radius-bot.snakeRadius) {
scPoint = collisionPoint;
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions