shrani
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
const app = require('express')();
|
||||
const server = require('http').createServer(app);
|
||||
const io = require('socket.io')(server, {
|
||||
cors: {
|
||||
origin: "*", // Allow all origins for dev
|
||||
methods: ["GET", "POST"]
|
||||
}
|
||||
});
|
||||
|
||||
const players = {};
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log('Player connected:', socket.id);
|
||||
|
||||
// Initialize player data
|
||||
players[socket.id] = {
|
||||
id: socket.id,
|
||||
x: 0,
|
||||
y: 0,
|
||||
anim: 'idle'
|
||||
};
|
||||
|
||||
// Send the current list of players to the new player
|
||||
socket.emit('currentPlayers', players);
|
||||
|
||||
// Notify other players about the new player
|
||||
socket.broadcast.emit('newPlayer', players[socket.id]);
|
||||
|
||||
// Handle Disconnect
|
||||
socket.on('disconnect', () => {
|
||||
console.log('Player disconnected:', socket.id);
|
||||
delete players[socket.id];
|
||||
io.emit('playerDisconnected', socket.id);
|
||||
});
|
||||
|
||||
// Handle Movement
|
||||
socket.on('playerMovement', (data) => {
|
||||
if (players[socket.id]) {
|
||||
players[socket.id].x = data.x;
|
||||
players[socket.id].y = data.y;
|
||||
players[socket.id].anim = data.anim;
|
||||
players[socket.id].flipX = data.flipX;
|
||||
|
||||
// Broadcast to others (excluding self)
|
||||
socket.broadcast.emit('playerMoved', players[socket.id]);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle World State (Simple Block Placement sync)
|
||||
// Warning: No validation/persistence in this MVP
|
||||
socket.on('worldAction', (action) => {
|
||||
// action: { type: 'build', x: 10, y: 10, building: 'fence' }
|
||||
socket.broadcast.emit('worldAction', action);
|
||||
});
|
||||
});
|
||||
|
||||
const PORT = 3000;
|
||||
server.listen(PORT, () => {
|
||||
console.log(`SERVER RUNNING on port ${PORT}`);
|
||||
console.log(`To play multiplayer:`);
|
||||
console.log(`1. Run 'node server.js' in a terminal`);
|
||||
console.log(`2. Start the game clients`);
|
||||
});
|
||||
Reference in New Issue
Block a user