Skip to content

Instantly share code, notes, and snippets.

@vampaynani
Last active January 29, 2018 16:42
Show Gist options
  • Save vampaynani/d05a31c624381cd7da1d3440c94dad33 to your computer and use it in GitHub Desktop.
Save vampaynani/d05a31c624381cd7da1d3440c94dad33 to your computer and use it in GitHub Desktop.
Socket.io multiplayer basis
const Client = {
socket: io(),
zombieManager: false,
newPlayer: function(){
this.socket.emit('newplayer');
},
playerMove: function(x, y){
this.socket.emit('move', {x, y});
},
shootBeam: function(x, y, angle, velocity){
this.socket.emit('shot', {x, y, angle, velocity});
},
hitBeam: function(bulletID){
this.socket.emit('hitshot', bulletID);
},
moveZombie: function(id, x, y){
if(this.zombieManager){
this.socket.emit('moveZombie', {id, x, y});
}
},
initListeners: function(){
this.socket.on('newplayer', function(player){
console.log('add', player);
});
this.socket.on('allplayers', function(players){
console.log('create game players with', players);
});
this.socket.on('allbullets', function(bullets){
console.log('create game bullets with', bullets);
});
this.socket.on('allzombies', function(zombies){
console.log('create game zombies with', zombies);
});
this.socket.on('zombiemanager', () => {
this.zombieManager = true;
console.log('This socket will be the zombie manager');
})
this.socket.on('move', function(coords){
console.log('tween to', coords);
});
this.socket.on('zombiesUpdate', function(zombies){
console.log('zombies update positions', zombies);
});
this.socket.on('newbullet', function(bullet){
console.log('create new bullet', bullet)
});
this.socket.on('destroybullet', function(bulletID){
console.log('destroy bullet', bulletID);
})
this.socket.on('remove', function(id){
console.log('remove', id);
});
}
};
Client.initListeners();
Client.newPlayer();
//Client.playerMove(100, 200);
//Client.shootBeam(100, 200, 90, {x: 10, y: 10});
const express = require('express'); // Express contains some boilerplate to for routing and such
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http); // Here's where we include socket.io as a node module
// Serve the index page
// app.get("/", function (request, response) {
// response.sendFile(__dirname + '/index.html');
// });
// Serve the assets directory
app.use(express.static(__dirname + '/public'));
app.use('/assets', express.static('assets'));
// Listen on port 5000
const port = process.env.PORT || 5000;
http.listen(port, function() {
console.log('listening on port', port);
});
let nextPlayerID = 0;
let gameBullets = [];
let gameZombies;
function getAllPlayers(){
return Object.keys(io.sockets.connected).map(socketID => io.sockets.connected[socketID].player);
}
// Tell Socket.io to start accepting connections
io.on('connection', function(socket) {
if(Object.keys(io.sockets.connected).length <= 1){
gameZombies = [];
gameZombies.push({x: 10, y: 0, velocity: {x: 5, y: 0}});
gameZombies.push({x: 20, y: 50, velocity: {x: 0, y: 5}});
gameZombies.push({x: 30, y: 100, velocity: {x: 5, y: 0}});
socket.emit('zombiemanager');
}
socket.on('newplayer', function(){
socket.player = {
id: nextPlayerID++,
x: 0,
y: 0
}
console.log(socket.player);
socket.emit('allplayers', getAllPlayers());
socket.emit('allbullets', gameBullets);
socket.emit('allzombies', gameZombies);
socket.broadcast.emit('newplayer', socket.player);
socket.on('moveZombie', function(zombie){
gameZombies[zombie.id].velocity.x = zombie.x;
gameZombies[zombie.id].velocity.y = zombie.y;
socket.broadcast.emit('zombiesUpdate', gameZombies);
})
socket.on('move', function(coords){
console.log(coords);
socket.player.x = coords.x;
socket.player.y = coords.y;
io.emit('move', socket.player);
});
socket.on('shot', function(coords){
var bullet = Object.assign(coords, {
id: new Date().getTime()
});
gameBullets.push(bullet);
io.emit('newbullet', bullet);
});
socket.on('hitshot', function(id){
gameBullets = gameBullets.filter(bullet => bullet.id != id);
io.emit('destroybullet', id);
});
socket.on('disconnect', function(){
io.emit('remove', socket.player.id);
});
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment