mirror of
https://github.com/richardharrington/robotwar.git
synced 2024-06-09 08:29:32 +00:00
got enqueuing of downloaded json worlds going
(fair amount of hacky debugging stuff in there now)
This commit is contained in:
parent
5750481e02
commit
03ce17eb9a
|
@ -11,7 +11,71 @@
|
|||
Hello World, Welcome to Robotwar.
|
||||
</p>
|
||||
<script>
|
||||
var debugCountdown = 10;
|
||||
var debugAnimationCounter = 0;
|
||||
var debugSimulationCounter = 0;
|
||||
|
||||
var tickQueue = new Queue();
|
||||
var fastForward = 5;
|
||||
var GAME_SECONDS_PER_TICK = 0.03;
|
||||
var tickDuration = parseInt (GAME_SECONDS_PER_TICK / fastForward * 1000);
|
||||
|
||||
var fps = 60;
|
||||
var frameDuration = parseInt (1000 / fps);
|
||||
|
||||
var animationLoopId;
|
||||
|
||||
function display(world) {
|
||||
console.log(world);
|
||||
}
|
||||
|
||||
function simulationLoop(tick) {
|
||||
console.log("simulationCounter " + debugSimulationCounter);
|
||||
debugSimulationCounter++;
|
||||
if (tickQueue.isEmpty() || debugCountdown === 0) {
|
||||
clearTimeout(animationLoopId);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
debugCountdown -= 1;
|
||||
}
|
||||
if (tickQueue.getLength() < 500) {
|
||||
fetchWorlds();
|
||||
}
|
||||
tickQueue.dequeue();
|
||||
var nextTick = tick + tickDuration;
|
||||
setTimeout(function() {
|
||||
simulationLoop(nextTick);
|
||||
}, nextTick - Date.now());
|
||||
}
|
||||
|
||||
function animationLoop(frame) {
|
||||
console.log("animationCounter " + debugAnimationCounter);
|
||||
debugAnimationCounter++;
|
||||
if (tickQueue.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
display(tickQueue.peek());
|
||||
var nextFrame = frame + frameDuration;
|
||||
animationLoopId = setTimeout(function() {
|
||||
animationLoop(nextFrame);
|
||||
}, nextFrame - Date.now());
|
||||
}
|
||||
|
||||
function fetchWorlds(callback) {
|
||||
$.getJSON('worlds/500', function(worlds) {
|
||||
tickQueue.enqueueArray(worlds);
|
||||
if (callback) callback();
|
||||
});
|
||||
}
|
||||
|
||||
fetchWorlds(function() {
|
||||
console.log(tickQueue.getLength());
|
||||
var now = Date.now();
|
||||
simulationLoop(now);
|
||||
animationLoop(now);
|
||||
});
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue
Block a user