/* global game */
var state = {};
var DungeonGen = require('../util/dungeon_gen');
var Player = require('../entity/player');

var WORLD_WIDTH = 64;
var WORLD_HEIGHT = 64;

var players;
var currentPlayer;
var layer;

state.create = function() {
    players = game.add.group();
    var p1 = new Player(50, 50);
    var p2 = new Player(100, 50);
    var p3 = new Player(50, 100);
    players.add(p1);
    players.add(p2);
    players.add(p3);
    currentPlayer = p1;
    game.camera.follow(currentPlayer);
    game.camera.roundPx = false;
    var map = game.add.tilemap();
    map.addTilesetImage('test_tileset', 'test_tileset', 32, 32, 32, 32, 1);
    layer = map.create('walls', WORLD_WIDTH, WORLD_HEIGHT, 32, 32);
    layer.resizeWorld();
    DungeonGen.Dungeon.Generate();
    var mapArray = DungeonGen.Dungeon.map;
    var i, j;
    for (i = 0; i < WORLD_WIDTH; i++)
        for (j = 0; j < WORLD_HEIGHT; j++)
            map.putTile(mapArray[i][j], i, j);
    map.setCollision(2);
    game.physics.arcade.setBoundsToWorld();
    
    // add input mask
    var mask = game.add.sprite(0,0);
    mask.height = game.world.height;
    mask.width = game.world.width;
    mask.inputEnabled = true;
    mask.events.onInputUp.add(function() {
        console.log(game.input.activePointer.worldX);
        console.log(game.input.activePointer.worldY);
        if (currentPlayer.moveTarget) currentPlayer.moveTarget.destroy();
        currentPlayer.moveTarget = game.add.sprite(game.input.activePointer.worldX,
            game.input.activePointer.worldY, 'pix');
        currentPlayer.moveTarget.width = 5;
        currentPlayer.moveTarget.height = 5;
        currentPlayer.moveTarget.anchor.set(0.5);
        game.physics.enable(currentPlayer.moveTarget);
    });

    // add HUD
    var hud = {};
    var TEXT_STYLE = {
        fontSize: 30,
        fill: 'green'
    };
    var hudGroup = game.add.group();
    hud.p1 = game.make.text(0, 0, "P1", TEXT_STYLE);
    hud.p2 = game.make.text(300, 0, "P2", TEXT_STYLE);
    hud.p3 = game.make.text(600, 0, "P3", TEXT_STYLE);
    hudGroup.add(hud.p1);
    hudGroup.add(hud.p2);
    hudGroup.add(hud.p3);
    var activatePlayer = function(player, sprite, pointer) {
        console.log('player activated');
        currentPlayer = player;
        game.camera.follow(currentPlayer);
    };
    [hud.p1, hud.p2, hud.p3].forEach(function(e) {
        e.inputEnabled = true;
        e.fixedToCamera = true;
        e.input.useHandCursor = true;
    });
    hud.p1.events.onInputUp.add(activatePlayer.bind(null, p1));
    hud.p2.events.onInputUp.add(activatePlayer.bind(null, p2));
    hud.p3.events.onInputUp.add(activatePlayer.bind(null, p3));
};

state.update = function() {
    players.forEach(function(player) {
        if (player.moveTarget)
            game.physics.arcade.moveToXY(player, player.moveTarget.x,
                player.moveTarget.y, 300);
    });
    game.physics.arcade.collide(players, layer);
    players.callAll('isAtDestination');
};

module.exports = state;
@@ -209,16 +209,28 @@
 layer;%0A%0A
+var enemy;%0A%0A
 state.cr
@@ -1038,16 +1038,347 @@
 );%0A    %0A
+    // create enemies%0A    var ex, ey;%0A    ex = (DungeonGen.Dungeon.rooms%5B0%5D.x + 1 ) * 32;%0A    ey = (DungeonGen.Dungeon.rooms%5B0%5D.y + 1 ) * 32;%0A    enemy = game.add.sprite(ex, ey, 'pix');%0A    enemy.width = 50;%0A    enemy.height = 50;%0A    enemy.tint = 0xff0000;%0A    game.physics.arcade.enable(enemy);%0A    enemy.body.immovable = true;%0A%0A
     // a
@@ -1424,16 +1424,17 @@
 prite(0,
+ 
 0);%0A    
@@ -1578,116 +1578,8 @@
 ) %7B%0A
-        console.log(game.input.activePointer.worldX);%0A        console.log(game.input.activePointer.worldY);%0A
     
@@ -2438,49 +2438,8 @@
 ) %7B%0A
-        console.log('player activated');%0A
     
@@ -3180,16 +3180,70 @@
 tion');%0A
+    %0A    game.physics.arcade.collide(enemy, players);%0A
 %7D;%0A%0Amodu

Add an enemy sprite to a random room.