Skip to content

Commit

Permalink
game loop changes. setTimeouts replaced with process.nextTick and set…
Browse files Browse the repository at this point in the history
…Interval
  • Loading branch information
ehsanul committed Nov 1, 2011
1 parent 4c27981 commit 66c5856
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
10 changes: 4 additions & 6 deletions lib/joop/joop.coffee
@@ -1,14 +1,12 @@
module.exports = joop = (t, funcs...)->
if typeof t == 'function'
funcs.push(t)
funcs.unshift t
t = 0
for func, i in funcs
unless i == funcs.length-1
unless i == funcs.length - 1
funcs[i] = ((func,i)->
return ->
func()
setTimeout(funcs[i+1], 0)
process.nextTick funcs[i + 1]
)(func,i)
return ->
setTimeout(arguments.callee, t)
funcs[0]()
setInterval funcs[0], t
9 changes: 3 additions & 6 deletions lib/joop/joop.js
Expand Up @@ -4,7 +4,7 @@ module.exports = joop = function() {
var func, funcs, i, t, _len;
t = arguments[0], funcs = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if (typeof t === 'function') {
funcs.push(t);
funcs.unshift(t);
t = 0;
}
for (i = 0, _len = funcs.length; i < _len; i++) {
Expand All @@ -13,13 +13,10 @@ module.exports = joop = function() {
funcs[i] = (function(func, i) {
return function() {
func();
return setTimeout(funcs[i + 1], 0);
return process.nextTick(funcs[i + 1]);
};
})(func, i);
}
}
return function() {
setTimeout(arguments.callee, t);
return funcs[0]();
};
return setInterval(funcs[0], t);
};
3 changes: 1 addition & 2 deletions xarkon-server.coffee
Expand Up @@ -320,12 +320,11 @@ broadcastPositions = ->


#TODO take a closer look at joop's behaviour
GameLoop = joop 30,
joop 30,
processCommands
propelEngines
physicsStep
broadcastPositions
GameLoop()


WEBROOT = path.dirname __filename
Expand Down
5 changes: 2 additions & 3 deletions xarkon-server.js
@@ -1,4 +1,4 @@
var CHARCODE_OFFSET, Command, Engine, Game, GameLoop, GameObjects, GravityControl, Grid, Physics, Player, Players, Pos, SerializeCreate, ShipCommand, SocketIoClient, Vel, WEBROOT, broadcastPositions, fs, gcomponent, grid, gridCols, gridH, gridRows, gridW, hasCommand, hasEngine, hasPhysics, hasPos, http, io, joop, log, paperboy, path, physicsStep, processCommands, propelEngines, server, socket, sys, url, v, _;
var CHARCODE_OFFSET, Command, Engine, Game, GameObjects, GravityControl, Grid, Physics, Player, Players, Pos, SerializeCreate, ShipCommand, SocketIoClient, Vel, WEBROOT, broadcastPositions, fs, gcomponent, grid, gridCols, gridH, gridRows, gridW, hasCommand, hasEngine, hasPhysics, hasPos, http, io, joop, log, paperboy, path, physicsStep, processCommands, propelEngines, server, socket, sys, url, v, _;
http = require('http');
url = require('url');
fs = require('fs');
Expand Down Expand Up @@ -403,8 +403,7 @@ broadcastPositions = function() {
}
return _results;
};
GameLoop = joop(30, processCommands, propelEngines, physicsStep, broadcastPositions);
GameLoop();
joop(30, processCommands, propelEngines, physicsStep, broadcastPositions);
WEBROOT = path.dirname(__filename);
server = http.createServer(function(req, res) {
return paperboy.deliver(WEBROOT, req, res).error(function(statCode, msg) {
Expand Down

0 comments on commit 66c5856

Please sign in to comment.