Advertisement
Guest User

Untitled

a guest
Apr 1st, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function init()
  2. {
  3.     websocket = new WebSocket('ws://192.168.1.246:8000');
  4.     websocket.onopen = function(evt) { onOpen(evt) };
  5.     websocket.onclose = function(evt) { onClose(evt) };
  6.     websocket.onmessage = function(evt) { handle_message(evt.data) };
  7.     websocket.onerror = function(evt) { onError(evt) };
  8.     canvas = document.getElementById('game');
  9.     R = canvas.width;
  10.     document.onkeyup = function(evt) {
  11.     var key = evt.key;
  12.     if (
  13.         ['a','d','w'].includes(key)    
  14.     ) {
  15.         if (keydown == key) {
  16.         websocket.send(key + ' ' + (Date.now()/1000.-keystart).toString());
  17.         }      
  18.     }
  19.     }
  20.     document.onkeydown = function(evt) {
  21.     var key = evt.key;
  22.     if (['a','d','w'].includes(key)) {
  23.         keydown = key;
  24.         keystart = Date.now()/1000.;
  25.     }
  26.     }
  27.     context = canvas.getContext('2d');    
  28.     setTimeout(update_game,0);
  29. }
  30.  
  31. function update_game() {
  32.     setTimeout(update_game,0);
  33.     context.clearRect(0,0,500,500);
  34.     if (!playing) {return;}
  35.     var time = Date.now()/1000.0;
  36.     var dt = time-start_time-ref_time;
  37.     var coords = coordinates(dt);
  38.     var x = coords[0];
  39.     var y = coords[1];
  40.     context.beginPath();
  41.     context.arc(x,y,r,0,2*Math.PI);
  42.     context.stroke();
  43.     var adt = time-start_time-a_ref_time;
  44.     var a_coords = a_coordinates(adt);
  45.     var ax = a_coords[0];
  46.     var ay = a_coords[1];
  47.     context.beginPath();
  48.     context.arc(ax,ay,ar,0,2*Math.PI);
  49.     context.stroke();
  50.     if (detector==player) {
  51.     var vels = velocities(dt);
  52.     var vx = vels[0];
  53.     var vy = vels[1];
  54.     var a_vels = a_velocities(adt);
  55.     var avx = a_vels[0];
  56.     var avy = a_vels[1];
  57.     if (x < r && vx < 0) {
  58.         websocket.send('j');       
  59.     }
  60.     else if (x > R-r && vx > 0) {
  61.         websocket.send('l');       
  62.     }
  63.     else if (y < r && vy < 0) {
  64.         websocket.send('o');
  65.     }
  66.     else if (y > R-r && vy > 0) {
  67.         websocket.send('i');
  68.     }
  69.     else if (ay > R-ar && avy > 0) {
  70.         websocket.send('k');
  71.     }
  72.     else {
  73.         var dx = x - ax;
  74.         var dy = y - ay;
  75.         if (dx**2 +dy**2 < (r+ar)**2) {
  76.         var a_vels = a_velocities(adt);
  77.         var avx = a_vels[0];
  78.         var avy = a_vels[1];
  79.         var dvx = vx - avx;
  80.         var dvy = vy - avy;
  81.         var v_proj = dvx * dx + dvy * dy;
  82.         if (v_proj < 0) {
  83.             websocket.send('x');
  84.         }
  85.         }
  86.     }
  87.     }
  88. }
  89.  
  90. function onOpen(evt)
  91. {
  92.     console.log('connected');
  93. }
  94.  
  95. function onClose(evt)
  96. {
  97.     console.log('disconnected');
  98. }
  99.  
  100. function handle_message(message)
  101. {
  102.     var fragments = message.split(' ');
  103.     var command = fragments.shift();
  104.     var data = fragments.pop();
  105.     if (fragments.length) {
  106.     var payload = fragments.pop()
  107.     }
  108.     if (command=='s') {
  109.     start_time = Date.now()/1000.0;
  110.     ref_time = 0;
  111.     a_ref_time = 0;
  112.     playing = true;
  113.     player = data;
  114.     xo = R/2;
  115.     yo = 0;
  116.     vxo = 0;
  117.     vyo = 0;
  118.     axo = R/2;
  119.     ayo = R-ar;
  120.     avxo = 0;
  121.     avyo = 0;
  122.     a_stuck = true;
  123.     var color = {
  124.         'a':'red','b':'blue'
  125.     }[player];
  126.     var player_font_div = document.querySelector('#player font');
  127.     player_font_div.setAttribute('color',color);
  128.     player_font_div.innerText=player;
  129.     canvas.focus();
  130.     }
  131.     else if (['a','d','w','k'].includes(command)) {
  132.     new_time = parseFloat(data);
  133.     var dt = new_time - a_ref_time;
  134.     a_ref_time = new_time;
  135.     var a_coords = a_coordinates(dt);
  136.     var ax = a_coords[0];
  137.     var ay = a_coords[1];
  138.     axo = ax;
  139.     ayo = ay;
  140.     if (command == 'k') {
  141.         avx = 0;
  142.         avy = 0;
  143.         a_stuck = true;
  144.     }
  145.     else {
  146.         var a_vels = a_velocities(dt);
  147.         var avx = a_vels[0];
  148.         var avy = a_vels[1];
  149.         var impulse_map = {
  150.         'a':[-leap_fraction,-leap_fraction],
  151.         'd':[+leap_fraction,-leap_fraction],
  152.         'w':[0,-1]
  153.         }[command];
  154.         var impulse = a_impulse_coeff * parseFloat(payload);
  155.         var davx = impulse * impulse_map[0];
  156.         var davy = impulse * impulse_map[1];
  157.         avxo = avx + davx;
  158.         avyo = avy + davy;
  159.         a_stuck = false;
  160.     }
  161.     }
  162.     else if (['j','l','i','o','x'].includes(command)) {
  163.     var new_time = parseFloat(data)
  164.     var dt = new_time-ref_time;
  165.     ref_time = new_time;
  166.     var coords = coordinates(dt);
  167.     var x = coords[0];
  168.     var y = coords[1];
  169.     xo = x;
  170.     yo = y;
  171.     var vels = velocities(dt);
  172.     var vx = vels[0];
  173.     var vy = vels[1];
  174.     if (['j','l','i','o'].includes(command)) {
  175.         var vel_map = {
  176.         'j':[-1,1],
  177.         'l':[-1,1],
  178.         'i':[1,-1],
  179.         'o':[1,-1]
  180.         }[command];
  181.         vxo = vels[0]*vel_map[0];
  182.         vyo = vels[1]*vel_map[1];
  183.     }
  184.     else if (command == 'x') {
  185.         var adt = new_time - a_ref_time;
  186.         var a_coords = a_coordinates(adt);
  187.         var ax = a_coords[0];
  188.         var ay = a_coords[1];
  189.         var dx = x - ax;
  190.         var dy = y - ay;
  191.         var d2 = dx**2+dy**2;
  192.         var a_vels = a_velocities(adt);
  193.         var avx = a_vels[0];
  194.         var avy = a_vels[1];
  195.         var dvx = vx - avx;
  196.         var dvy = vy - avy;
  197.         var v_proj = dvx*dx + dvy*dy;
  198.         var sat_factor = 1+1/(1+(v_proj/v_sat)**2/d2);
  199.         console.log(sat_factor);
  200.         var dvx = dvx - sat_factor * v_proj * dx / d2;
  201.         var dvy = dvy - sat_factor * v_proj * dy / d2;
  202.         vxo = avx + dvx;
  203.         vyo = avy + dvy;
  204.     }
  205.     }
  206. }
  207.  
  208. function coordinates(dt) {
  209.     var exp_factor = Math.exp(-dt/tau)-1;
  210.     var x = xo - tau * vxo * exp_factor
  211.     var y = yo + v_term*dt + tau * (v_term-vyo) * exp_factor;
  212.     return [x,y];
  213. }
  214.  
  215. function velocities(dt) {
  216.     var exp_term = Math.exp(-dt/tau);
  217.     var vx = vxo * exp_term;
  218.     var vy = vyo + (v_term-vyo)*(1-exp_term);
  219.     return [vx,vy];
  220. }
  221.  
  222. function a_coordinates(dt) {
  223.     if (a_stuck) {
  224.     var ax = axo;
  225.     var ay = ayo;  
  226.     }
  227.     else {
  228.     var ax = axo + avxo * dt;
  229.     var ay = ayo + avyo * dt + .5 * g * dt**2;
  230.     }
  231.     return [ax,ay];
  232. }
  233.  
  234. function a_velocities(dt) {
  235.     if (a_stuck) {
  236.     var avx = 0;
  237.     var avy = 0;
  238.     }
  239.     else {
  240.     var avx = avxo;
  241.     var avy = avyo + g*dt;
  242.     }
  243.     return [avx,avy];
  244. }
  245.  
  246. function onError(evt)
  247. {
  248.     websocket.close();
  249. }
  250.  
  251. var start_time;
  252. var ref_time;
  253. var a_ref_time;
  254. var playing = false;
  255. var player;
  256. var axo;
  257. var ayo;
  258. var avxo;
  259. var avyo;
  260. var a_stuck;
  261. var xo;
  262. var yo;
  263. var vxo;
  264. var vyo;
  265. var R;
  266. var r = 20;
  267. var ar = 50;
  268. var g = 500;
  269. var v_term = 2000;
  270. var tau = 4.;
  271. var impulse = 100;
  272. var canvas;
  273. var context;
  274. var websocket;
  275. var detector='a';
  276. var keydown;
  277. var keystart;
  278. var leap_fraction = .3;
  279. var a_impulse_coeff = 2000;
  280. var v_sat = 1000;
  281. window.onload = init;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement