/******************************************************************************
This file is part of PIRATES.

PIRATES is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PIRATES is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PIRATES.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/

game.PlayScreen = me.ScreenObject.extend({
  /**
  *  action to perform on state change
  */
  onResetEvent: function(players, idPlayer) {
    //Map init
    me.levelDirector.loadLevel("map_base");

    // reset the score
    game.data.score = 0;

    var t = this;
    this.players = players;
    this.playerId = idPlayer;

    //Add the players
    for(var id in players){
      me.game.world.addChild(players[id]);
    }


    //Emit first player data
    var data =
    {
      x:t.players[t.playerId].pos._x,
      y:t.players[t.playerId].pos._y,
      velX:t.players[t.playerId].body.vel.x,
      velY:t.players[t.playerId].body.vel.y
    };
    socket.emit('updatePlayer', {id: t.playerId, player: data});

    //On each update event, update all the other players
    socket.on('update', function(param){
      var now = Date.now();

      for(var i in param) {
        var p = param[i];

        if(p.id !== t.playerId){
          var dt = now - p.lastMaj;

          t.players[p.id].body.vel.set(p.vel.x, p.vel.y);
          t.players[p.id].pos.x = p.pos.x;
          t.players[p.id].pos.y = p.pos.y;

          if(!isNaN(dt)){
            t.players[p.id].update(dt);
          }
          //TODO update other params
        }
      }
      //To force the drawing of each object by the engine
      me.game.repaint();

      var data =
      {
        x:t.players[t.playerId].pos._x,
        y:t.players[t.playerId].pos._y,
        velX:t.players[t.playerId].body.vel.x,
        velY:t.players[t.playerId].body.vel.y,
        lastMaj: Date.now(),
        currentAnimation:t.players[t.playerId].characterRenderable.current.name
      };
      socket.emit('updatePlayer', {id: t.playerId, player: data});
    });

    // add our HUD to the game world
    this.HUD = new game.HUD.Container();
    me.game.world.addChild(this.HUD);

    // set the display to follow our position on both axis
    me.game.viewport.follow(this.players[this.playerId], me.game.viewport.AXIS.BOTH);
  },

  /**
  *  action to perform when leaving this screen (state change)
  */
  onDestroyEvent: function() {
    // remove the HUD from the game world
    me.game.world.removeChild(this.HUD);
  }
});

@@ -826,32 +826,33 @@
 xtend(%7B%0A  /**%0A  
+ 
 *  action to per
@@ -872,16 +872,17 @@
  change%0A
+ 
   */%0A  o
@@ -984,59 +984,708 @@
 ase%22
-);%0A%0A    // reset the score%0A    game.data.score = 0;
+, %7B%0A      flatten: false,%0A      container: me.game.world%0A    %7D);%0A    %0A    /**%0A     * The container of all characters in the game, creatd in Tiled%0A     * @type %7Bme.Container%7D%0A     */%0A    this.playerLayer = me.game.world.getChildByName(%22entities%22)%5B0%5D;%0A    %0A    if (me.game.HASH.debug === true) %7B%0A      console.assert(this.playerLayer !== undefined, 'No children named '%0A        + '%22entities%22 found. The Tiled map must include such a layer to add '%0A        + 'the players to the game and the layer must not be empty.', this);%0A      console.assert(this.playerLayer instanceof me.Container, 'The %22entities%22 '%0A        + 'Tiled layer must be an object layer to allow adding players inside it.', this);%0A    %7D
 %0A%0A  
@@ -1811,37 +1811,40 @@
 ers)%7B%0A      
-me.game.world
+this.playerLayer
 .addChild(pl

Fix z-position of players to draw objects above them The map must now have a "entities" object layer, containing at least one element. The layer's reference is saves and players are now added to it at the beginning, so we can order the layer as we want in Tiled.