Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Game(io) {
- var self = this;
- this.FRAME_LENGTH = 1000.0 / constants.UPS;
- this.io = io;
- this.lastTick = null;
- this.connections = [ ];
- /**
- * Will be called every time we need to update world
- */
- this.update = function(delta) {
- this.io.emit('test');
- }
- /**
- * The function calls every tick and when richs delta between frames
- * calls {@link update} function
- */
- this.loop = function() {
- var now = Date.now();
- var delta = now - this.lastTick;
- if (delta >= this.FRAME_LENGTH) {
- this.update(delta / 1000);
- this.lastTick = Date.now();
- }
- // Why `self`?
- // Because JavaScrit, that's why!
- if (now + delta < this.FRAME_LENGTH) {
- setTimeout(self.loop); // Will be called in next tick of Node.JS loop
- } else {
- setImmediate(self.loop); // Will be calle immediate
- }
- }
- /**
- * Statrs game loop
- */
- this.startLoop = function() {
- this.lastTick = Date.now();
- setImmediate(this.loop);
- }
- /**
- * Handles connection from socket.io
- *
- * @param {socket} socket Socket we get from socket.io
- */
- this.onConnection = function(socket) {
- logger.info('New connection from %s.', socket.id);
- this.connections.push(socket);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement