Jump to content

simple multiplayer example


GooodWin67
 Share

Recommended Posts

Hi all)

I decided to try to learn a multiplayer with phaser, node, socket.io

found a few examples. But so almost nothing did not understand :)

This example works. but the player moves when the mouse is pressed, at the point where they clicked. How to make the player move by pressing the arrows of the keyboard. Without multiplayer I can do it. And how do I do not understand in multiplayer ((

 

server.js

var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);

app.use('/js',express.static(__dirname + '/js'));
app.use('/assets',express.static(__dirname + '/assets'));

app.get('/',function(req,res){
    res.sendFile(__dirname+'/index.html');
});

server.listen(process.env.PORT || 8081,function(){
    console.log('Listening on '+server.address().port);
});

server.lastPlayerID = 0;

io.on('connection', function(socket) {
    socket.on('newplayer', function(){
        socket.player = {
            id : server.lastPlayerID++,
            x : 200,
            y : 200
        };
        socket.emit('allplayers', getAllPlayers());
        socket.broadcast.emit('newplayer', socket.player);

        socket.on('disconnect', function() {
            io.emit('remove', socket.player.id);
        });

        socket.on('click', function(data) {
            socket.player.x = data.x;
            socket.player.y = data.y;
            io.emit('move', socket.player);
        });
    });
});

function getAllPlayers(){
    var players = [];
    Object.keys(io.sockets.connected).forEach(function(socketID){
        var player = io.sockets.connected[socketID].player;
        if(player) players.push(player);
    });
    return players;
}

client.js

var Client = {};
Client.socket = io.connect();

Client.askNewPlayer = function() {
    Client.socket.emit('newplayer');
}

Client.socket.on('newplayer', function(data) {
    Game.addNewPlayer(data.id,data.x,data.y)
});

Client.socket.on('allplayers', function(data) {
    console.log(data);
    for(var i = 0; i < data.length; i++) {
        Game.addNewPlayer(data[i].id, data[i].x, data[i].y);
    }
});

Client.socket.on('remove', function(id) {
    Game.removePlayer(id);
});

Client.sendClick = function(x,y) {
    Client.socket.emit('click', {x:x,y:y});
};

Client.socket.on('move', function(data) {
    Game.movePlayer(data.id, data.x, data.y);
});

game.js


var Game = {};

Game.init = function(){
    game.stage.disableVisibilityChange = true;
};

Game.preload = function() {
    game.load.tilemap('map', 'assets/map/example_map.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.spritesheet('tileset', 'assets/map/tilesheet.png',32,32);
    game.load.image('sprite','assets/sprites/sprite.png');
};

Game.create = function(){
    
    Game.playerMap = {};
    var map = game.add.tilemap('map');
    map.addTilesetImage('tilesheet', 'tileset'); // tilesheet is the key of the tileset in map's JSON file
    var layer;
    for(var i = 0; i < map.layers.length; i++) {
        layer = map.createLayer(i);
    }
    layer.inputEnabled = true; // Allows clicking on the map ; it's enough to do it on the last layer
    Client.askNewPlayer();

    cursors = game.input.keyboard.createCursorKeys();
    game.physics.startSystem(Phaser.Physics.ARCADE);

    layer.events.onInputUp.add(Game.getCoordinates, this);
};
Game.addNewPlayer = function(id,x,y) {
    Game.playerMap[id] = game.add.sprite(x,y,'sprite');
    
    game.physics.arcade.enable(Game.playerMap[id]);
}
Game.removePlayer = function(id) {
    Game.playerMap[id].destroy();
    delete Game.playerMap[id];
};

Game.getCoordinates = function(layer, pointer) {
    Client.sendClick(pointer.worldX, pointer.worldY);
}

Game.movePlayer = function(id,x,y) {
    var player = Game.playerMap[id];
    var distance = Phaser.Math.distance(player.x, player.y,x,y);
    var duration = distance*10;
    
    var tween = game.add.tween(player);
    tween.to({x:x, y:y}, duration);
    tween.start();
};

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...