SHARE
TWEET

Untitled

a guest Mar 7th, 2016 2,339 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //app.js
  2. var express = require('express');
  3. var app = express();
  4. var serv = require('http').Server(app);
  5.  
  6. app.get('/',function(req, res) {
  7.     res.sendFile(__dirname + '/client/index.html');
  8. });
  9. app.use('/client',express.static(__dirname + '/client'));
  10.  
  11. serv.listen(2000);
  12. console.log("Server started.");
  13.  
  14. var SOCKET_LIST = {};
  15. var PLAYER_LIST = {};
  16.  
  17. var Player = function(id){
  18.     var self = {
  19.         x:250,
  20.         y:250,
  21.         id:id,
  22.         number:"" + Math.floor(10 * Math.random()),
  23.         pressingRight:false,
  24.         pressingLeft:false,
  25.         pressingUp:false,
  26.         pressingDown:false,
  27.         maxSpd:10,
  28.     }
  29.     self.updatePosition = function(){
  30.         if(self.pressingRight)
  31.             self.x += self.maxSpd;
  32.         if(self.pressingLeft)
  33.             self.x -= self.maxSpd;
  34.         if(self.pressingUp)
  35.             self.y -= self.maxSpd;
  36.         if(self.pressingDown)
  37.             self.y += self.maxSpd;
  38.     }
  39.     return self;
  40. }
  41.  
  42. var io = require('socket.io')(serv,{});
  43. io.sockets.on('connection', function(socket){
  44.     socket.id = Math.random();
  45.     SOCKET_LIST[socket.id] = socket;
  46.  
  47.     var player = Player(socket.id);
  48.     PLAYER_LIST[socket.id] = player;
  49.    
  50.     socket.on('disconnect',function(){
  51.         delete SOCKET_LIST[socket.id];
  52.         delete PLAYER_LIST[socket.id];
  53.     });
  54.    
  55.     socket.on('keyPress',function(data){
  56.         if(data.inputId === 'left')
  57.             player.pressingLeft = data.state;
  58.         else if(data.inputId === 'right')
  59.             player.pressingRight = data.state;
  60.         else if(data.inputId === 'up')
  61.             player.pressingUp = data.state;
  62.         else if(data.inputId === 'down')
  63.             player.pressingDown = data.state;
  64.     });
  65.    
  66.    
  67. });
  68.  
  69. setInterval(function(){
  70.     var pack = [];
  71.     for(var i in PLAYER_LIST){
  72.         var player = PLAYER_LIST[i];
  73.         player.updatePosition();
  74.         pack.push({
  75.             x:player.x,
  76.             y:player.y,
  77.             number:player.number
  78.         });    
  79.     }
  80.     for(var i in SOCKET_LIST){
  81.         var socket = SOCKET_LIST[i];
  82.         socket.emit('newPositions',pack);
  83.     }
  84.    
  85.    
  86.    
  87.    
  88. },1000/25);
  89.  
  90.  
  91. //index.html
  92.  
  93. <canvas id="ctx" width="500" height="500" style="border:1px solid #000000;"></canvas>
  94.  
  95. <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
  96. <script>
  97.     var ctx = document.getElementById("ctx").getContext("2d");
  98.     ctx.font = '30px Arial';
  99.    
  100.     var socket = io();
  101.        
  102.     socket.on('newPositions',function(data){
  103.         ctx.clearRect(0,0,500,500);
  104.         for(var i = 0 ; i < data.length; i++)
  105.             ctx.fillText(data[i].number,data[i].x,data[i].y);      
  106.     });
  107.  
  108.     document.onkeydown = function(event){
  109.         if(event.keyCode === 68)    //d
  110.             socket.emit('keyPress',{inputId:'right',state:true});
  111.         else if(event.keyCode === 83)   //s
  112.             socket.emit('keyPress',{inputId:'down',state:true});
  113.         else if(event.keyCode === 65) //a
  114.             socket.emit('keyPress',{inputId:'left',state:true});
  115.         else if(event.keyCode === 87) // w
  116.             socket.emit('keyPress',{inputId:'up',state:true});
  117.            
  118.     }
  119.     document.onkeyup = function(event){
  120.         if(event.keyCode === 68)    //d
  121.             socket.emit('keyPress',{inputId:'right',state:false});
  122.         else if(event.keyCode === 83)   //s
  123.             socket.emit('keyPress',{inputId:'down',state:false});
  124.         else if(event.keyCode === 65) //a
  125.             socket.emit('keyPress',{inputId:'left',state:false});
  126.         else if(event.keyCode === 87) // w
  127.             socket.emit('keyPress',{inputId:'up',state:false});
  128.     }
  129.    
  130. </script>
RAW Paste Data
Top