Advertisement
Guest User

Untitled

a guest
Dec 5th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const canvas = document.getElementById('gameCanvas');
  2. const ctx = canvas.getContext('2d');
  3.  
  4. const ws = new WebSocket('ws://127.0.0.1:12345');
  5. let playerId;
  6. let players = {};
  7. let blackout = false;
  8.  
  9. const keysPressed = {};
  10. let speedX = 0;
  11. let speedY = 0;
  12. let maxSpeed = 5;
  13. let acceleration = 0.1;
  14. let deceleration = 0.5;
  15.  
  16. let lastSentTime = 0;
  17. const sendInterval = 50; //ping
  18.  
  19. ws.onopen = () => {
  20.     console.log('Подключение установлено');
  21. };
  22.  
  23. ws.onmessage = (event) => {
  24.     const data = JSON.parse(event.data);
  25.     console.log('message:', data);
  26.  
  27.     if (data.type === 'init') {
  28.         playerId = data.id;
  29.         players = data.players;
  30.         Object.keys(players).forEach(id => {
  31.             players[id].lastX = players[id].x;
  32.             players[id].lastY = players[id].y;
  33.             players[id].renderX = players[id].x;
  34.             players[id].renderY = players[id].y;
  35.         });
  36.         drawPlayers();
  37.     } else if (data.type === 'update') {
  38.         if (!players[data.id]) {
  39.             players[data.id] = { x: data.x, y: data.y, lastX: data.x, lastY: data.y, renderX: data.x, renderY: data.y };
  40.         } else {
  41.             players[data.id].lastX = players[data.id].x;
  42.             players[data.id].lastY = players[data.id].y;
  43.             players[data.id].x = data.x;
  44.             players[data.id].y = data.y;
  45.         }
  46.         drawPlayers();
  47.     } else if (data.type === 'remove') {
  48.         delete players[data.id];
  49.         drawPlayers();
  50.     } else if (data.type === 'blackout') {
  51.         blackout = true;
  52.         drawPlayers();
  53.     }
  54. };
  55.  
  56. //RENDER
  57.  
  58. function drawPlayers() {
  59.     console.log('Рисование игроков', players);
  60.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  61.  
  62.     for (const id in players) {
  63.         if (players.hasOwnProperty(id)) {
  64.             const player = players[id];
  65.  
  66.             player.renderX += (player.x - player.renderX) * 0.2;
  67.             player.renderY += (player.y - player.renderY) * 0.2;
  68.  
  69.             console.log(`Рисование игрока ${id} на (${player.renderX}, ${player.renderY})`);
  70.             ctx.beginPath();
  71.             ctx.arc(player.renderX, player.renderY, 10, 0, 2 * Math.PI);
  72.             ctx.fillStyle = blackout ? 'black' : (id === playerId ? 'blue' : 'red');
  73.             ctx.fill();
  74.         }
  75.     }
  76. }
  77.  
  78. //CONTROL
  79.  
  80. document.addEventListener('keydown', (event) => {
  81.     keysPressed[event.code] = true;
  82.  
  83.     if (event.code === 'KeyB') {
  84.         console.log('blackout');
  85.         ws.send(JSON.stringify({ type: 'blackout' }));
  86.     }
  87. });
  88.  
  89. document.addEventListener('keyup', (event) => {
  90.     delete keysPressed[event.code];
  91. });
  92.  
  93. function handleMovement() {
  94.     const keyMap = {
  95.         'KeyW': { dx: 0, dy: -1 },
  96.         'KeyA': { dx: -1, dy: 0 },
  97.         'KeyS': { dx: 0, dy: 1 },  
  98.         'KeyD': { dx: 1, dy: 0 }  
  99.     };
  100.  
  101.     let accelerationX = 0;
  102.     let accelerationY = 0;
  103.  
  104.     for (const key in keysPressed) {
  105.         if (keyMap[key]) {
  106.             accelerationX += keyMap[key].dx;
  107.             accelerationY += keyMap[key].dy;
  108.         }
  109.     }
  110.  
  111.     speedX += accelerationX * acceleration;
  112.     speedY += accelerationY * acceleration;
  113.  
  114.     speedX = Math.min(Math.max(speedX, -maxSpeed), maxSpeed);
  115.     speedY = Math.min(Math.max(speedY, -maxSpeed), maxSpeed);
  116.  
  117.     const currentTime = Date.now();
  118.     if (currentTime - lastSentTime > sendInterval) {
  119.         if (speedX !== 0 || speedY !== 0) {
  120.             console.log(`move: ${JSON.stringify({ type: 'move', dx: speedX, dy: speedY })}`);
  121.             ws.send(JSON.stringify({ type: 'move', dx: speedX, dy: speedY }));
  122.             lastSentTime = currentTime;
  123.         }
  124.     }
  125.  
  126.     if (accelerationX === 0) {
  127.         speedX *= (1 - deceleration);
  128.     }
  129.     if (accelerationY === 0) {
  130.         speedY *= (1 - deceleration);
  131.     }
  132. }
  133.  
  134. function update() {
  135.     handleMovement();
  136.     drawPlayers();
  137.     requestAnimationFrame(update);
  138. }
  139.  
  140. update();
  141.  
  142.  
  143. window.addEventListener('blur', () => {
  144.     console.log('no focus');
  145.     Object.keys(keysPressed).forEach(key => {
  146.         delete keysPressed[key];
  147.     });
  148. });
  149.  
  150. window.addEventListener('focus', () => {
  151.     console.log('focus');
  152.     drawPlayers();
  153. });
  154.  
  155. window.addEventListener('beforeunload', () => {
  156.     ws.close();
  157. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement