Advertisement
Lubomir_Iliev

flags

Jun 16th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | None | 0 0
  1.  
  2.  
  3. // Creating variables
  4. var pX=[],pY=[],pT=[],shoottimer=0
  5. var socket = io();
  6. var patronX=[],patronY=[],patronT=[],patronDX=[],patronDY=[];
  7. var walls=0,oldX=0,oldY=0;
  8. var playersrn,myId,myteam,myX,myY;
  9. var stenaX=[],stenaY=[],stenaGX=[],stenaGY=[];
  10. socket.on("new player",function (players){
  11.     playersrn=players
  12.     //console.log(players)
  13. })
  14. socket.on("walls",function(X,Y,XG,YG,id)
  15. {
  16.     stenaX[id]=X;
  17.     stenaY[id]=Y;
  18.     stenaGX[id]=XG;
  19.     stenaGY[id]=YG;
  20.     console.log(YG)
  21. })
  22. socket.on("playercordinatesmoved",function(id,X,Y,team){
  23.     pX[id]=X;
  24.     pY[id]=Y;
  25.     pT[id]=team;
  26.  
  27. })
  28. socket.on("all",function(X,Y,T,I)
  29. {
  30. pX[I]=X;
  31. pY[I]=Y;
  32. pT[I]=T;
  33.  
  34. })
  35. socket.on("deltas",function(id,dX,dY,teamm)
  36. {
  37.     patronT[id]=teamm;
  38.     patronX[id]=pX[id]
  39.     patronY[id]=pY[id]
  40.     patronDX[id]=dX;
  41.     patronDY[id]=dY;
  42. })
  43.  
  44.  
  45.  
  46. socket.on("your id",function (id,w){
  47.     walls=w;
  48.     myId=id
  49.     console.log(myId)
  50.     myteam=myId%2;
  51.     myX=Math.random()*770;
  52.     myY=Math.random()*570;
  53.     socket.emit("mycordinates",myId,myteam,myX,myY)
  54. })
  55.  
  56. function update() {
  57.     if(isKeyPressed[65])
  58.     {
  59.         for(let i=0;i<walls;i++)
  60.         {
  61.             if(areColliding(pX[myId]-3,pY[myId],20,20,stenaX[i],stenaY[i],stenaGX[i],stenaGY[i]))
  62.             {
  63.                 pX[myId]+=3;
  64.             }
  65.             else
  66.             {
  67.                 pX[myId]-=3/walls;
  68.             }
  69.         }
  70.         socket.emit("mycordinates",myId,myteam,pX[myId],pY[myId])
  71.     }
  72.     if(isKeyPressed[68])
  73.     {
  74.         for(let i=0;i<walls;i++)
  75.         {
  76.             if(areColliding(pX[myId]+3,pY[myId],20,20,stenaX[i],stenaY[i],stenaGX[i],stenaGY[i]))
  77.             {
  78.                 pX[myId]-=3
  79.             }
  80.             else
  81.             {
  82.                 pX[myId]+=3/walls;
  83.             }
  84.         }
  85.  
  86.  
  87.         socket.emit("mycordinates",myId,myteam,pX[myId],pY[myId])
  88.     }
  89.     if(isKeyPressed[83])
  90.     {
  91.         for(let i=0;i<walls;i++)
  92.         {
  93.             if(areColliding(pX[myId],pY[myId]+3,20,20,stenaX[i],stenaY[i],stenaGX[i],stenaGY[i]))
  94.             {
  95.      
  96.                 pY[myId]-=3;
  97.                 console.log("udyr")        
  98.             }
  99.             else
  100.             {
  101.                 pY[myId]+=3/walls;  
  102.             }
  103.         }
  104.  
  105.         socket.emit("mycordinates",myId,myteam,pX[myId],pY[myId])
  106.     }
  107.     if(isKeyPressed[87])
  108.     {
  109.         for(let i=0;i<walls;i++)
  110.         {
  111.             if(areColliding(pX[myId],pY[myId]+3,20,20,stenaX[i],stenaY[i],stenaGX[i],stenaGY[i]))
  112.             {
  113.                 console.log("udyr")
  114.                 pY[myId]+=3;        
  115.             }
  116.             else
  117.             {
  118.                 //oldY=pY[myId]
  119.                 pY[myId]-=3/walls;
  120.             }
  121.         }
  122.  
  123.         socket.emit("mycordinates",myId,myteam,pX[myId],pY[myId])
  124.     }  
  125.     for(let i=0;i<=playersrn;i++)
  126.     {
  127.         patronX[i]+=patronDX[i]*10;
  128.         patronY[i]+=patronDY[i]*10;
  129.     }
  130.  
  131.  
  132.          
  133. }
  134. function draw() {
  135.     for(let i=0;i<walls;i++)
  136.     {
  137.         context.fillStyle="black";
  138.         context.fillRect(stenaX[i],stenaY[i],stenaGX[i],stenaGY[i])
  139.     }
  140.     for(let i=0;i<playersrn+1;i++)
  141.     {
  142.         if(i==myId)
  143.         {
  144.             context.fillStyle="green";
  145.         }
  146.         else
  147.         {
  148.             if(pT[i]==myteam)
  149.             {
  150.                 context.fillStyle="blue";
  151.             }
  152.             else
  153.             {
  154.                 context.fillStyle="red";
  155.             }
  156.         }
  157.         context.fillRect(pX[i],pY[i],20,20)
  158.         context.fillRect(patronX[i],patronY[i],5,5)
  159.         context.fillStyle="black";
  160.         context.fillText(i,pX[i]+6,pY[i]+13)
  161.     }
  162. }
  163. function keydown(key) {
  164.     console.log(key)
  165. }
  166. function mouseup() {
  167.     if(shoottimer==0)
  168.     {
  169.         socket.emit("shooting",mouseX,mouseY,pX[myId],pY[myId],myId,myteam)
  170.        
  171.     }
  172.     console.log(mouseX,mouseY)
  173.    
  174. }
  175. server js
  176. var app = require('express')();
  177. var http = require('http').Server(app);
  178. var io = require('socket.io')(http);
  179.  
  180. app.get('/', function(req, res){
  181.     res.sendFile(__dirname + "/start.html");
  182. });
  183. app.get('/game.js', function(req, res){
  184.     res.sendFile(__dirname + "/game.js");
  185. });
  186.  
  187. var players=0;
  188. var pX=[],pY=[],pT=[],walls=18;
  189. var stenaX=[],stenaY=[],stenaGX=[],stenaGY=[];
  190. for(let i1=0;i1<walls;i1++)
  191. {
  192.     stenaX[i1]=Math.random()*760;
  193.     stenaY[i1]=Math.random()*560;
  194.     let G=Math.floor(Math.random()*11)
  195.     if(G<5)
  196.     {
  197.         stenaGX[i1]=40;
  198.         stenaGY[i1]=5;
  199.     }
  200.     else
  201.     {
  202.         stenaGX[i1]=5;
  203.         stenaGY[i1]=40;
  204.     }
  205. }
  206. io.on('connection', function(socket){
  207.  
  208. io.emit("new player",players)
  209. socket.emit("your id",players,walls)
  210. for(let i=0;i<walls;i++)
  211. {
  212.     socket.emit("walls",stenaX[i],stenaY[i],stenaGX[i],stenaGY[i],i)
  213. }
  214. for(let i=0;i<players+1;i++)
  215. {
  216.     socket.emit("all",pX[i],pY[i],pT[i],i)
  217. }
  218. socket.on("mycordinates",function(id,team,X,Y){
  219.     pX[id]=X;
  220.     pY[id]=Y;
  221.     pT[id]=team;
  222.     io.emit("playercordinatesmoved",id,X,Y,team)
  223.    
  224. })
  225. socket.on("shooting",function(pX,pY,X,Y,i,team){
  226. let X1=pX-X,Y1=pY-Y;
  227. let d=Math.sqrt(X1*X1+Y1*Y1)
  228. let shootdeltaX=X1/d;
  229. let shootdeltaY=Y1/d;
  230. io.emit("deltas",i,shootdeltaX,shootdeltaY,team)
  231. socket.emit("deltas",i,shootdeltaX,shootdeltaY,team)
  232. console.log(shootdeltaX,shootdeltaY)
  233. })
  234. players++;
  235. });
  236.  
  237. http.listen(3000, function(){
  238.     console.log("server started");
  239. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement