"use strict";
cc._RF.push(module, '0b9eetmSUdJRZCQS91TYCw/', 'TmoTiledMap');
// Script/TmoTiledMap.js

"use strict";

// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
var TmoTiledLayer = require("./TmoTiledLayer");

var TmoObjectGroup = require("./TmoObjectGroup");

cc.Class({
  "extends": cc.TiledMap,
  properties: {
    objectMaterial: cc.Material,
    noColor: true,
    singleTexture: true,
    _objectTextures: []
  },
  _applyFile: function _applyFile() {
    if (!this.objectMaterial) {
      cc.error("TiledmapOptimize: 未设置objectMaterial!");
      return;
    }

    this._super();
  },
  _buildLayerAndGroup: function _buildLayerAndGroup() {
    var tilesets = this._tilesets;
    var texGrids = this._texGrids;
    var animations = this._animations;
    texGrids.length = 0;

    for (var i = 0, l = tilesets.length; i < l; ++i) {
      var tilesetInfo = tilesets[i];
      if (!tilesetInfo) continue;
      cc.TiledMap.fillTextureGrids(tilesetInfo, texGrids, i);
    }

    this._fillAniGrids(texGrids, animations);

    var layers = this._layers;
    var groups = this._groups;
    var images = this._images;
    var oldNodeNames = {};

    for (var _i = 0, n = layers.length; _i < n; _i++) {
      oldNodeNames[layers[_i].node._name] = true;
    }

    for (var _i2 = 0, _n = groups.length; _i2 < _n; _i2++) {
      oldNodeNames[groups[_i2].node._name] = true;
    }

    for (var _i3 = 0, _n2 = images.length; _i3 < _n2; _i3++) {
      oldNodeNames[images[_i3]._name] = true;
    }

    layers = this._layers = [];
    groups = this._groups = [];
    images = this._images = [];
    var mapInfo = this._mapInfo;
    var node = this.node;
    var layerInfos = mapInfo.getAllChildren();
    var textures = this._textures;
    var maxWidth = 0;
    var maxHeight = 0;
    this._objectTextures = [];
    var firstTmxLayer = null;

    if (layerInfos && layerInfos.length > 0) {
      for (var _i4 = 0, len = layerInfos.length; _i4 < len; _i4++) {
        var layerInfo = layerInfos[_i4];
        var name = layerInfo.name;
        var child = this.node.getChildByName(name);
        oldNodeNames[name] = false;

        if (!child) {
          child = new cc.Node();
          child.name = name;
          node.addChild(child);
        }

        child.setSiblingIndex(_i4);
        child.active = layerInfo.visible;

        if (layerInfo instanceof cc.TMXLayerInfo) {
          // 此处修改 改为创建MyTiledLayer
          var layer = child.getComponent(TmoTiledLayer);

          if (!layer) {
            layer = child.addComponent(TmoTiledLayer);
          } // 修改结束
          // 此处修改 传递firstTmxLayer 记录firstTmxLayer


          layer._init(layerInfo, mapInfo, tilesets, textures, texGrids, this, firstTmxLayer);

          firstTmxLayer = firstTmxLayer || layer; // 修改结束
          // tell the layerinfo to release the ownership of the tiles map.

          layerInfo.ownTiles = false;
          layers.push(layer);
        } else if (layerInfo instanceof cc.TMXObjectGroupInfo) {
          var group = child.getComponent(TmoObjectGroup);

          if (!group) {
            group = child.addComponent(TmoObjectGroup);
          }

          group._init(layerInfo, mapInfo, texGrids, this);

          groups.push(group);
        } else if (layerInfo instanceof cc.TMXImageLayerInfo) {
          var texture = layerInfo.sourceImage;
          child.opacity = layerInfo.opacity;
          child.layerInfo = layerInfo;
          child._offset = cc.v2(layerInfo.offset.x, -layerInfo.offset.y);
          var image = child.getComponent(cc.Sprite);

          if (!image) {
            image = child.addComponent(cc.Sprite);
          }

          var spf = image.spriteFrame || new cc.SpriteFrame();
          spf.setTexture(texture);
          image.spriteFrame = spf;
          child.width = texture.width;
          child.height = texture.height;
          images.push(child);
        }

        maxWidth = Math.max(maxWidth, child.width);
        maxHeight = Math.max(maxHeight, child.height);
      }
    } // 设置材质的texture属性


    var objectTextures = this._objectTextures;

    for (var _i5 = 0; _i5 < objectTextures.length; _i5++) {
      var idx = _i5 === 0 ? '' : _i5;
      this.objectMaterial.setProperty("texture" + idx, objectTextures[_i5], 0);
    }

    var children = node.children;

    for (var _i6 = 0, _n3 = children.length; _i6 < _n3; _i6++) {
      var c = children[_i6];

      if (oldNodeNames[c._name]) {
        c.destroy();
      }
    }

    this.node.width = maxWidth;
    this.node.height = maxHeight;

    this._syncAnchorPoint();
  },
  getObjectMaterial: function getObjectMaterial() {
    return this.objectMaterial;
  },
  isNoColor: function isNoColor() {
    return this.noColor;
  },
  isSingleTexture: function isSingleTexture() {
    return this.singleTexture;
  },
  getObjectTextures: function getObjectTextures() {
    return this._objectTextures;
  }
});

cc._RF.pop();