Hi there, I’m making a shooter multi-player top down game with phaser (arcade physics)and socket.io.
I managed to make an authoritative server to manage all the physics there (with phaser headless).
I am currently implementing the bullets and fire system. The collisions between bullets and players work, even those with the objects of the tiledMap and worldBounds but I cannot make the collision with the static layer of the map walls. (The collision between players and walls works)
This is how I’ve implemented the map:
function create() {
const self = this;
this.physics.world.setBounds(0, 0, 2560, 1920);
this.players = this.physics.add.group();
this.bullets= this.physics.add.group({ classType: Bullet, runChildUpdate: true })
const map = this.make.tilemap({key: 'map', tilewidth : 64, tileHeight: 64 });
const tileset = map.addTilesetImage('tilesheet', 'tiles');
this.wall = map.createStaticLayer('wall', tileset, 0, 0);
this.wall.setCollisionByExclusion(-1, true);
this.objects = map.createFromObjects('objects', 'object1');
this.objects.forEach(object => {
this.physics.world.enable(object);
object.body.collideWorldBounds = true;
object.body.immovable = true;
})
I’m pasting here the server shot management part (in create() function):
socket.on('shot', function(reticle){
// Get bullet from bullets group
const bullet = self.bullets.get().setActive(true).setVisible(true);
//adding collision with world bounds
bullet.body.collideWorldBounds=true;
// Turning this on will allow you to listen to the 'worldbounds' event
bullet.body.onWorldBounds = true;
bullet.body.world.on('worldbounds', function(body) {
// Check if the body's game object is the sprite you are listening for
if (body.gameObject === this) {
// Stop physics and render updates for this object
this.setActive(false);
this.setVisible(false);
io.emit('destroyBullet', bullet.id)
}
}, bullet);
if(bullet){
bullet.fire(players[socket.id], reticle);
self.physics.add.collider(self.objects,bullet, function(){
if(bullet.id === socket.id){
if (bullet.active === true)
{
bullet.setActive(false);
bullet.setVisible(false);
io.emit('destroyBullet', bullet.id)}
}
})
self.physics.add.collider(self.wall,bullet.body, function(){
if(bullet.id === socket.id){
console.log('wall hit')
bullet.setActive(false);
bullet.setVisible(false);
io.emit('destroyBullet', bullet.id)}
})
io.emit('fire', bullet)
self.players.getChildren().forEach((player) => {
if(socket.id !== player.playerId){
self.physics.add.collider(player, bullet, playerHitCallback);
}
});
}
});
});