Fixed some bugs and better arrow controls

This commit is contained in:
Klemek
2019-01-08 08:08:34 +01:00
parent 7b55763b89
commit 701a53b3f1
2 changed files with 40 additions and 41 deletions
+25 -27
View File
@@ -78,15 +78,14 @@ function drawGame() {
ctx.fillStyle = '#545454'; ctx.fillStyle = '#545454';
ctx.fillText('snex.io', canvas.width / 2, canvas.height / 2 + ratio * 40); ctx.fillText('snex.io', canvas.width / 2, canvas.height / 2 + ratio * 40);
ctx.lineCap = "round"; ctx.lineCap = 'round';
ctx.lineJoin = "round"; ctx.lineJoin = 'round';
ctx.lineWidth = 3; ctx.lineWidth = 3;
ctx.textAlign = 'left'; ctx.textAlign = 'left';
ctx.font = `normal ${ratio * 10}px Roboto`; ctx.font = `normal ${ratio * 10}px Roboto`;
const names = Object.keys(players); const names = Object.keys(players);
names.forEach(function (name) { names.forEach(function (name) {
const p = players[name];
drawPlayer(ratio, players[name]); drawPlayer(ratio, players[name]);
if (name === current.name) if (name === current.name)
current = players[name]; current = players[name];
@@ -119,44 +118,43 @@ socket.on('players', function (p) {
players = p; players = p;
}); });
const keys = {};
let currentAngleSpd = 0;
function changeAngle(spd) {
if (currentAngleSpd !== spd) {
current.angleSpd = spd;
currentAngleSpd = spd;
socket.emit('update', current);
}
}
$(document).on('keydown', function (e) { $(document).on('keydown', function (e) {
switch (e.keyCode) { switch (e.keyCode) {
case 32://space;
current.color = 'new';
socket.emit('update', current);
return;
case 37://left; case 37://left;
current.angleSpd = -1; changeAngle(-1);
socket.emit('update', current);
break;
case 38://up;
break; break;
case 39://right; case 39://right;
current.angleSpd = 1; changeAngle(1);
socket.emit('update', current);
break;
case 40://down;
break; break;
} }
keys[e.keyCode] = true;
}); });
$(document).on('keyup', function (e) { $(document).on('keyup', function (e) {
switch (e.keyCode) { switch (e.keyCode) {
case 32://space;
current.color = 'new';
socket.emit('update', current);
return;
case 37://left; case 37://left;
current.angleSpd = 0; if (keys[39])
socket.emit('update', current); changeAngle(1);
break; else
case 38://up; changeAngle(0);
break; break;
case 39://right; case 39://right;
current.angleSpd = 0; if (keys[37])
socket.emit('update', current); changeAngle(-1);
break; else
case 40://down; changeAngle(0);
break; break;
} }
keys[e.keyCode] = false;
}); });
+6 -5
View File
@@ -2,16 +2,19 @@ const app = require('express')();
const http = require('http').Server(app); const http = require('http').Server(app);
const io = require('socket.io')(http); const io = require('socket.io')(http);
const convert = require('color-convert'); const convert = require('color-convert');
const fs = require('fs');
app.get('*', function (req, res) { app.get('*', function (req, res) {
if(req.originalUrl === '/') if(req.originalUrl === '/')
res.sendFile(`${__dirname}/client/index.html`); res.sendFile(`${__dirname}/client/index.html`);
else else if (fs.existsSync(`${__dirname}/client${req.originalUrl}`))
res.sendFile(`${__dirname}/client${req.originalUrl}`); res.sendFile(`${__dirname}/client${req.originalUrl}`);
else
res.status(404).send('file not found');
}); });
function randomColor() { function randomColor() {
return '#' + convert.hsl.hex([randInt(0, 256), randInt(50, 200), 256]); return '#' + convert.hsl.hex([randInt(0, 256), randInt(150, 200), 256]);
} }
function randInt(min, max) { function randInt(min, max) {
@@ -126,7 +129,7 @@ setInterval(function () {
}, 20); }, 20);
setInterval(function () { setInterval(function () {
process.stdout.write(`\rtick ${20 * upc * 20 / 1000}/20 ms (${ic} intersect/s)`); process.stdout.write(`\rtick ${20 * upc * 20 / 1000}/20 ms (${Object.keys(players).length} connected) (${ic} intersect/s) `);
upc = 0; upc = 0;
ic = 0; ic = 0;
}, 1000); }, 1000);
@@ -171,8 +174,6 @@ io.on('connection', function (socket) {
} }
if (!p.alive) if (!p.alive)
return; return;
if (newp.color === 'new')
players[newp.name].color = randomColor();
if (newp.angleSpd > 0) if (newp.angleSpd > 0)
players[newp.name].angleSpd = MAX_ANGLE_SPEED; players[newp.name].angleSpd = MAX_ANGLE_SPEED;
else if (newp.angleSpd < 0) else if (newp.angleSpd < 0)