var Tile = require("@akashic-extension/akashic-tile").Tile;
var game = g.game;

module.exports = function() {
    var scene = new g.Scene({game: game, assetIds:["map"]});
    scene.loaded.add(function() {
        var tile = new Tile({
            scene: scene,
            src: scene.assets["map"],
            tileWidth: 32,
            tileHeight: 32,
            //
            // マップデータ
            //
            tileData: [
                [ -1, -1, 84, 85, -1, -1, -1, -1,122,123,164, -1, -1, -1, -1,125, 88, 89, -1, -1, -1, -1, -1, -1, -1, -1, -1,182,180, -1, -1, 76],
                [151,189,100,101,170,190,191,168,138,139, 77, -1,157,158,159,141,104,105,169,204,172,205,112,113,114,206, 10, 11, 12, 60, -1,201],
                [ 49, 49, 49, 49, 49, 49, 49, 49, 50, 83, 78,115, 55, 56, 56, 56, 56, 56, 56, 56, 56, 57,128,129,130, 58, 42, 43, 44,145, -1,202],
                [ 33, 34,165, 32, 34, -1, -1, -1,173, 99, 78,131,121, 90, 91, -1, -1, -1, 72,192,126,166, 58, 59, 59, 60,167,  5,  6,161, -1,202],
                [198, -1, 86, 87, -1,122,123, -1, 48, 50, 78,131,137,106,107,175,150, -1, 72,193,142, -1,201, 37, 38,201, -1, 21, 22, 74,184,202],
                [199,149,102,103,152,154,155,153,156,179, 78, 61, 62, 62, 62, 62, 63,115, 72,194, 71,207,202, 53, 54,203, -1, 69, 70, 75, 74,202],
                [199, 48, 49, 49, 49, 49, 49, 49, 49, 50, 79,195, -1, 45, 46, 47, -1,131, 72,  7,  8,  9,203, -1,183, -1, -1, -1, -1, 76, 75,202],
                [199, -1, -1, 66, -1,124,146,119, -1, 94, 95,196,127, 92, 93,147,144,131, 72, 23, 24, 25, 19, 20, 58, 60,115, 58, 60, -1, 76,202],
                [199,148, 80, 81, 82,140,162,135,171,110,111,197,143,108,109,163,160,131, 72, 39, 40, 41, 67, 68,120, -1,131, -1, -1, 58, 60,202],
                [199, 64, 96, 97, 98,  0,  1,  2, 97, 13, 14, 15,115, 61, 62, 62, 62, 63, 73,176,177, -1, 58, 60,136,178,131,  3,  4,174, -1,203],
                [200, 65,116,116,116, 16, 17, 18,116, 29, 30, 31,131,185,187,186,188,181, -1, 10, 12,118,118,118, 10, 11, 12, 35, 36, 77,117,117],
                [116, 65,132,132,132, 16, 17, 18,132, 29, 30, 31, 14, 14, 14, 14, 14, 15,118, 26, 28,134,134,134, 26, 27, 28, 51, 52, 78,133,133],
            ],
            touchable: true
        });

        var dw = tile.width - game.width;
        if (dw > 0) {
            //
            // スクロール速度
            //
            var vx = -1;

            var scroll = false;
            tile.pointDown.add(function() {
                scroll = !scroll;
            });
            tile.update.add(function() {
                if (!scroll) {
                    return;
                }
                tile.x += vx;
                if (tile.x < -dw) {
                    tile.x = -dw;
                    vx *= -1;
                } else if (tile.x > 0) {
                    tile.x = 0;
                    vx *= -1;
                }
                tile.modified();
            });
        }
        scene.append(tile);
    });

    return scene;
}
