"use strict";
cc._RF.push(module, '30e2eSQWDNKI69FbaYfN62j', 'TmoObjectGroup');
// Script/TmoObjectGroup.js

"use strict";

exports.__esModule = true;
exports["default"] = void 0;

// 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 TmoSprite = require("./TmoSprite");

var _default = cc.Class({
  "extends": cc.TiledObjectGroup,
  _init: function _init(groupInfo, mapInfo, texGrids, tiledMap) {
    var TiledMap = cc.TiledMap;
    var TMXObjectType = TiledMap.TMXObjectType;
    var Orientation = TiledMap.Orientation;
    var StaggerAxis = TiledMap.StaggerAxis;
    var TileFlag = TiledMap.TileFlag;
    var FLIPPED_MASK = TileFlag.FLIPPED_MASK;
    var FLAG_HORIZONTAL = TileFlag.HORIZONTAL;
    var FLAG_VERTICAL = TileFlag.VERTICAL;
    this._groupName = groupInfo.name;
    this._positionOffset = groupInfo.offset;
    this._mapInfo = mapInfo;
    this._properties = groupInfo.getProperties();
    this._offset = cc.v2(groupInfo.offset.x, -groupInfo.offset.y);
    this._opacity = groupInfo._opacity;
    var mapSize = mapInfo._mapSize;
    var tileSize = mapInfo._tileSize;
    var width = 0,
        height = 0;

    if (mapInfo.orientation === Orientation.HEX) {
      if (mapInfo.getStaggerAxis() === StaggerAxis.STAGGERAXIS_X) {
        height = tileSize.height * (mapSize.height + 0.5);
        width = (tileSize.width + mapInfo.getHexSideLength()) * Math.floor(mapSize.width / 2) + tileSize.width * (mapSize.width % 2);
      } else {
        width = tileSize.width * (mapSize.width + 0.5);
        height = (tileSize.height + mapInfo.getHexSideLength()) * Math.floor(mapSize.height / 2) + tileSize.height * (mapSize.height % 2);
      }
    } else if (mapInfo.orientation === Orientation.ISO) {
      var wh = mapSize.width + mapSize.height;
      width = tileSize.width * 0.5 * wh;
      height = tileSize.height * 0.5 * wh;
    } else {
      width = mapSize.width * tileSize.width;
      height = mapSize.height * tileSize.height;
    }

    this.node.setContentSize(width, height);
    var leftTopX = width * this.node.anchorX;
    var leftTopY = height * (1 - this.node.anchorY);
    var objects = groupInfo._objects;
    var aliveNodes = {}; // 生成映射Map   { Texture2D: textureIndex }

    var textureIndexMap = new Map();
    var objectTextures = tiledMap.getObjectTextures();
    objectTextures.forEach(function (texture, idx) {
      return textureIndexMap.set(texture, idx);
    });

    for (var i = 0, l = objects.length; i < l; i++) {
      var object = objects[i];
      var objType = object.type;
      object.offset = cc.v2(object.x, object.y);
      var points = object.points || object.polylinePoints;

      if (points) {
        for (var pi = 0; pi < points.length; pi++) {
          points[pi].y *= -1;
        }
      }

      if (Orientation.ISO !== mapInfo.orientation) {
        object.y = height - object.y;
      } else {
        var posIdxX = object.x / tileSize.height;
        var posIdxY = object.y / tileSize.height;
        object.x = tileSize.width * 0.5 * (mapSize.height + posIdxX - posIdxY);
        object.y = tileSize.height * 0.5 * (mapSize.width + mapSize.height - posIdxX - posIdxY);
      }

      if (objType === TMXObjectType.TEXT) {
        var textName = "text" + object.id;
        aliveNodes[textName] = true;
        var textNode = this.node.getChildByName(textName);

        if (!textNode) {
          textNode = new cc.Node();
        }

        textNode.active = object.visible;
        textNode.anchorX = 0;
        textNode.anchorY = 1;
        textNode.angle = -object.rotation;
        textNode.x = object.x - leftTopX;
        textNode.y = object.y - leftTopY;
        textNode.name = textName;
        textNode.parent = this.node;
        textNode.color = object.color;
        textNode.opacity = this._opacity;
        textNode.setSiblingIndex(i);
        var label = textNode.getComponent(cc.Label);

        if (!label) {
          label = textNode.addComponent(cc.Label);
        }

        label.overflow = cc.Label.Overflow.SHRINK;
        label.lineHeight = object.height;
        label.string = object.text;
        label.horizontalAlign = object.halign;
        label.verticalAlign = object.valign;
        label.fontSize = object.pixelsize;
        textNode.width = object.width;
        textNode.height = object.height;
      }

      if (objType === TMXObjectType.IMAGE) {
        var gid = object.gid;
        var grid = texGrids[(gid & FLIPPED_MASK) >>> 0];
        if (!grid) continue;
        var tileset = grid.tileset;
        var imgName = "img" + object.id;
        aliveNodes[imgName] = true;
        var imgNode = this.node.getChildByName(imgName);
        var imgWidth = object.width || grid.width;
        var imgHeight = object.height || grid.height;
        var tileOffsetX = tileset.tileOffset.x;
        var tileOffsetY = tileset.tileOffset.y; // Delete image nodes implemented as private nodes
        // Use cc.Node to implement node-level requirements

        if (imgNode instanceof cc.PrivateNode) {
          imgNode.removeFromParent();
          imgNode.destroy();
          imgNode = null;
        }

        if (!imgNode) {
          imgNode = new cc.Node();
        }

        if (Orientation.ISO == mapInfo.orientation) {
          imgNode.anchorX = 0.5 + tileOffsetX / imgWidth;
          imgNode.anchorY = tileOffsetY / imgHeight;
        } else {
          imgNode.anchorX = tileOffsetX / imgWidth;
          imgNode.anchorY = tileOffsetY / imgHeight;
        }

        imgNode.active = object.visible;
        imgNode.angle = -object.rotation;
        imgNode.x = object.x - leftTopX;
        imgNode.y = object.y - leftTopY;
        imgNode.name = imgName;
        imgNode.parent = this.node;
        imgNode.opacity = this._opacity;
        imgNode.setSiblingIndex(i);
        imgNode._tiledMap = tiledMap;
        var sp = imgNode.getComponent(TmoSprite);

        if (!sp) {
          sp = imgNode.addComponent(TmoSprite);
        }

        var spf = sp.spriteFrame;

        if (!spf) {
          spf = new cc.SpriteFrame();
        }

        if ((gid & FLAG_HORIZONTAL) >>> 0) {
          spf.setFlipX(true);
        } else {
          spf.setFlipX(false);
        }

        if ((gid & FLAG_VERTICAL) >>> 0) {
          spf.setFlipY(true);
        } else {
          spf.setFlipY(false);
        }

        var sourceImage = grid.tileset.sourceImage;
        spf.setTexture(sourceImage, cc.rect(grid));
        sp.spriteFrame = spf;
        sp.setVertsDirty(); // object group may has no width or height info

        imgNode.width = imgWidth;
        imgNode.height = imgHeight; // 收集图集 更新textureIndexMap

        if (!textureIndexMap.has(sourceImage)) {
          textureIndexMap.set(sourceImage, objectTextures.length);
          objectTextures.push(sourceImage);
        } // 设置textureIndex


        var textureIndex = textureIndexMap.get(sourceImage);
        sp.setTextureIdx(textureIndex + 1);
      }
    }

    this._objects = objects; // destroy useless node

    var children = this.node.children;
    var uselessExp = /^(?:img|text)\d+$/;

    for (var _i = 0, n = children.length; _i < n; _i++) {
      var c = children[_i];
      var cName = c._name;
      var isUseless = uselessExp.test(cName);
      if (isUseless && !aliveNodes[cName]) c.destroy();
    }
  }
});

exports["default"] = _default;
module.exports = exports["default"];

cc._RF.pop();