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

Lag reduce, running in worker thread #309

Open
tungbuivn opened this issue Jun 19, 2016 · 0 comments
Open

Lag reduce, running in worker thread #309

tungbuivn opened this issue Jun 19, 2016 · 0 comments

Comments

@tungbuivn
Copy link

tungbuivn commented Jun 19, 2016

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;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants