Fixed invisible tail bug + tail refresh from server every second

This commit is contained in:
Klemek
2019-01-08 10:15:15 +01:00
parent 4cdabd0b6e
commit ae6ec885c3
2 changed files with 43 additions and 20 deletions
+34 -18
View File
@@ -22,29 +22,50 @@ function ellipse(cx, cy, rx, ry) {
ctx.fill(); ctx.fill();
} }
function drawPlayer(ratio, p) { function clone(a) {
ctx.fillStyle = p.alive ? p.color : '#757575'; return {x: parseFloat(a.x), y: parseFloat(a.y)};
ctx.fillText(p.name, p.pos.x, p.pos.y - ratio * 10); }
if (p.alive) {
ellipse(p.pos.x, p.pos.y, ratio * 3, ratio * 3);
if (p.starting > 0)
return;
if (!lock) { setInterval(function () {
if (!history[p.name]) //main loop
Object.keys(players).forEach(function (name) {
const p = players[name];
if (p.alive) {
if (p.starting <= 0 && !lock) {
if (!history[p.name])
history[p.name] = {
i0: 0,
list: new Array(hsize)
};
else
history[p.name].i0 = (history[p.name].i0 + 1) % hsize;
history[p.name].list[history[p.name].i0] = clone(p.pos);
} else {
history[p.name] = { history[p.name] = {
i0: 0, i0: 0,
list: new Array(hsize) list: new Array(hsize)
}; };
else history[p.name].list[0] = clone(p.pos);
history[p.name].i0 = (history[p.name].i0 + 1) % hsize; }
history[p.name].list[history[p.name].i0] = p.pos;
} }
} });
}, 20);
function drawPlayer(ratio, p) {
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.lineWidth = 3;
ctx.fillStyle = p.alive ? p.color : '#757575';
ctx.fillText(p.name, p.pos.x, p.pos.y - ratio * 10);
if (p.alive)
ellipse(p.pos.x, p.pos.y, ratio * 3, ratio * 3);
if (history[p.name]) { if (history[p.name]) {
const h = history[p.name]; const h = history[p.name];
ctx.strokeStyle = p.alive ? p.color : '#757575'; ctx.strokeStyle = p.alive ? p.color : '#757575';
console.log('h', h.list.length);
ctx.beginPath(); ctx.beginPath();
if (!h.list[h.i0]) if (!h.list[h.i0])
return; return;
@@ -93,10 +114,6 @@ function drawGame() {
ctx.textAlign = 'left'; ctx.textAlign = 'left';
writeText(leaderboard, 30, canvas.width / 30, canvas.height / 15, 'normal'); writeText(leaderboard, 30, canvas.width / 30, canvas.height / 15, 'normal');
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.lineWidth = 3;
ctx.textAlign = 'left'; ctx.textAlign = 'left';
ctx.font = `normal ${ratio * 15}px Roboto`; ctx.font = `normal ${ratio * 15}px Roboto`;
const names = Object.keys(players); const names = Object.keys(players);
@@ -139,7 +156,6 @@ socket.on('disconnect', function () {
socket.on('info', function (res) { socket.on('info', function (res) {
current = res.self; current = res.self;
history = res.history; history = res.history;
hsize = res.hsize;
players = res.players; players = res.players;
room = res.room; room = res.room;
window.location.hash = '#' + room; window.location.hash = '#' + room;
+9 -2
View File
@@ -135,7 +135,7 @@ setInterval(function () {
let lastpoint = h2.list[h2.i0]; let lastpoint = h2.list[h2.i0];
if (!lastpoint) if (!lastpoint)
return; return;
for (let di = 1; di < HSIZE; di++) { for (let di = 1; di < HSIZE - 1; di++) {
const point = h2.list[(h2.i0 - di + HSIZE) % HSIZE]; const point = h2.list[(h2.i0 - di + HSIZE) % HSIZE];
if (point !== undefined) { if (point !== undefined) {
if (Math.abs(lastpoint.x - point.x) < 1600 * .9 && Math.abs(lastpoint.y - point.y) < 900 * .9) { if (Math.abs(lastpoint.x - point.x) < 1600 * .9 && Math.abs(lastpoint.y - point.y) < 900 * .9) {
@@ -169,6 +169,12 @@ setInterval(function () {
io.to(rname).emit('lock', true); io.to(rname).emit('lock', true);
} }
io.to(rname).emit('players', room.players); io.to(rname).emit('players', room.players);
room.historyTimer -= 20;
if (room.historyTimer <= 0) {
io.to(rname).emit('history', room.history);
room.historyTimer += 1000;
}
}); });
updateCount++; updateCount++;
}, 20); }, 20);
@@ -199,7 +205,8 @@ io.on('connection', function (socket) {
rooms[socket.room] = { rooms[socket.room] = {
waiting: 0, waiting: 0,
players: {}, players: {},
history: {} history: {},
historyTimer: 0
} }
} }