Update server/src/index.js
This commit is contained in:
@@ -193,15 +193,24 @@ io.on('connection', socket => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// very basic server-side movement (trusting client target)
|
// Optional: keep click target for future pathing/validation
|
||||||
socket.on('move:click', target => {
|
socket.on('move:click', target => {
|
||||||
const p = socketsToPlayers.get(socket.id);
|
const p = socketsToPlayers.get(socket.id);
|
||||||
if (!p) return;
|
if (!p) return;
|
||||||
// You can store target on the server if you want later:
|
|
||||||
p.targetX = Math.max(0, Math.min(WORLD.width, target.x));
|
p.targetX = Math.max(0, Math.min(WORLD.width, target.x));
|
||||||
p.targetY = Math.max(0, Math.min(WORLD.height, target.y));
|
p.targetY = Math.max(0, Math.min(WORLD.height, target.y));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Authoritative position updates from the client
|
||||||
|
socket.on('pos:update', ({ x, y }) => {
|
||||||
|
const p = socketsToPlayers.get(socket.id);
|
||||||
|
if (!p) return;
|
||||||
|
const clampedX = Math.max(0, Math.min(WORLD.width, x));
|
||||||
|
const clampedY = Math.max(0, Math.min(WORLD.height, y));
|
||||||
|
p.x = clampedX;
|
||||||
|
p.y = clampedY;
|
||||||
|
});
|
||||||
|
|
||||||
socket.on('gather', async ({ nodeId }, ack) => {
|
socket.on('gather', async ({ nodeId }, ack) => {
|
||||||
const p = socketsToPlayers.get(socket.id);
|
const p = socketsToPlayers.get(socket.id);
|
||||||
if (!p) return ack?.({ error: 'not authed' });
|
if (!p) return ack?.({ error: 'not authed' });
|
||||||
@@ -240,6 +249,7 @@ io.on('connection', socket => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// game loop: respawn nodes + broadcast positions
|
// game loop: respawn nodes + broadcast positions
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|||||||
Reference in New Issue
Block a user