1 decade ago by MatthieuB
Hello, I try to create a multiplayer RPG type or another. By changing "php comet chat" I have come to establish a client-server communication. When one player is connected, the game is 70 fps, but when a second player connects, the game falls to 5-7 fps for each players ...
I don't understand what creates the slowdown.
Attached the code of my file 'main.js'
Thank you in advance!
I don't understand what creates the slowdown.
Attached the code of my file 'main.js'
Thank you in advance!
ig.module(
'game.main'
)
.requires(
'impact.game',
'impact.font',
'plugins.gamelobby',
'game.levels.beginning',
'impact.debug.debug'
)
.defines(function(){
EntityFigther = ig.Entity.extend({
size: {x:32, y:48},
// Load an animation sheet
animSheet: new ig.AnimationSheet( 'media/figther.png', 32, 48 ),
init: function( x, y, settings ) {
// Add animations for the animation sheet
this.addAnim( 'idle', 0.1, [0] );
this.addAnim( 'bas', 0.2, [0,1,2,3] );
this.addAnim( 'gauche', 0.2, [4,5,6,7] );
this.addAnim( 'droite', 0.2, [8,9,10,11] );
this.addAnim( 'haut', 0.2, [12,13,14,15] );
// Call the parent constructor
this.parent( x, y, settings );
},
update: function() {
// User Input
if( ig.input.state('left') ) {
this.currentAnim = this.anims.gauche;
}else if( ig.input.state('right') ) {
this.currentAnim = this.anims.droite;
}else if( ig.input.state('up') ) {
this.currentAnim = this.anims.haut;
}else if( ig.input.state('down') ) {
this.currentAnim = this.anims.bas;
}else{
this.currentAnim = this.anims.idle;
}
this.parent();
}
});
EntityFigther2 = ig.Entity.extend({
size: {x:32, y:48},
// Load an animation sheet
animSheet: new ig.AnimationSheet( 'media/figther.png', 32, 48 ),
init: function( x, y, settings ) {
this.parent( x, y, settings );
this.addAnim( 'idle', 1, [0] );
},
update: function() {
this.currentAnim = this.anims.idle;
//this.parent();
}
});
MyGame = ig.Game.extend({
// Load a font
font: new ig.Font( 'media/04b03.font.png' ),
fontback: new ig.Font( 'media/04b03b.font.png' ),
clearColor: '#000',
timer: 0.24,
state: 'ingame',
message: "",
messageState: false,
lobby: new GameLobby(this),
name: Math.random(),
mmoplayer: new Array(),
init: function() {
this.loadLevel( LevelBeginning );
ig.input.bind(ig.KEY.LEFT_ARROW, 'left');
ig.input.bind(ig.KEY.RIGHT_ARROW, 'right');
ig.input.bind(ig.KEY.UP_ARROW, 'up');
ig.input.bind(ig.KEY.DOWN_ARROW, 'down');
// Initialize your game here; bind keys etc.
this.player = this.spawnEntity( EntityFigther, 200, 200 );
this.timer = new ig.Timer(0.24);
this.lobby.setPlayer( {name: this.name,id: "0"} );
this.lobby.sendMessage(200+';'+200);
this.timer2 = new ig.Timer(0.25);
},
update: function() {
var player = this.getEntitiesByType( EntityFigther )[0];
if( player ) {
this.screen.x = player.pos.x - ig.system.width/2;
this.screen.y = player.pos.y - ig.system.height/2;
if( ig.input.state('left') ) {
player.pos.x -= 2;
}else if( ig.input.state('right') ) {
player.pos.x += 2;
}else if( ig.input.state('up') ) {
player.pos.y -= 2;
}else if( ig.input.state('down') ) {
player.pos.y += 2;
}
}
if (this.timer.delta() > 0) {
this.timer.reset();
var player = this.getEntitiesByType( EntityFigther )[0];
if( player ) {
if( ig.input.state('left') || ig.input.state('right') || ig.input.state('up') || ig.input.state('down')){
this.lobby.sendMessage(player.pos.x+';'+player.pos.y);
//$.post('entry.php', {id:this.name,pox:player.pos.x,poy:player.pos.y});
}
}
}
// Update all entities and backgroundMaps
if (this.timer2.delta() > 0) {
this.timer2.reset();
var syncData = this.lobby.messages;
if ( syncData != null && syncData != ""){
syncData = syncData.split(';');
if(syncData.length > 0){
for(i=0;i<(syncData.length-1);i+=3){
if(typeof(this.mmoplayer[syncData[i]]) == "undefined"){
this.mmoplayer[syncData[i]] = "check";
this.entity = this.spawnEntity( EntityFigther2, syncData[i+1], syncData[i+2], {name:syncData[i]});
}else{
var entity = this.getEntityByName(syncData[i]);
if(entity){
entity.pos.x = syncData[i+1];
entity.pos.y = syncData[i+2];
}
}
}
}
}
}
this.parent();
// Add your own, additional update code here
},
draw: function() {
// Draw all entities and backgroundMaps
this.parent();
}
});
// Start the Game with 60fps, a resolution of 320x240, scaled
// up by a factor of 2
ig.main( '#canvas', MyGame, 60, 320, 240, 1 );
});
