Advertisement
Guest User

Untitled

a guest
Jan 7th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Game(io) {
  2.     var self = this;
  3.  
  4.     this.FRAME_LENGTH = 1000.0 / constants.UPS;
  5.  
  6.     this.io = io;
  7.  
  8.     this.lastTick = null;
  9.     this.connections = [ ];
  10.  
  11.     /**
  12.      * Will be called every time we need to update world
  13.      */
  14.     this.update = function(delta) {
  15.         this.io.emit('test');
  16.     }
  17.  
  18.     /**
  19.      * The function calls every tick and when richs delta between frames
  20.      * calls {@link update} function
  21.      */
  22.     this.loop = function() {
  23.         var now = Date.now();
  24.         var delta = now - this.lastTick;
  25.  
  26.         if (delta >= this.FRAME_LENGTH) {
  27.             this.update(delta / 1000);
  28.            
  29.             this.lastTick = Date.now();
  30.         }
  31.  
  32.         // Why `self`?
  33.         // Because JavaScrit, that's why!
  34.         if (now + delta < this.FRAME_LENGTH) {
  35.             setTimeout(self.loop); // Will be called in next tick of Node.JS loop
  36.         } else {
  37.             setImmediate(self.loop); // Will be calle immediate
  38.         }
  39.     }
  40.  
  41.     /**
  42.      * Statrs game loop
  43.      */
  44.     this.startLoop = function() {
  45.         this.lastTick = Date.now();
  46.  
  47.         setImmediate(this.loop);
  48.     }
  49.  
  50.     /**
  51.      * Handles connection from socket.io
  52.      *
  53.      * @param {socket} socket Socket we get from socket.io
  54.      */
  55.     this.onConnection = function(socket) {
  56.         logger.info('New connection from %s.', socket.id);
  57.         this.connections.push(socket);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement