"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

require("core-js/modules/web.dom-collections.iterator.js");

var THREE = _interopRequireWildcard(require("three"));

var _react = _interopRequireWildcard(require("react"));

var _fiber = require("@react-three/fiber");

var _drei = require("@react-three/drei");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function Png(_ref) {
  let {
    url,
    scale,
    name
  } = _ref;
  const imageMap = (0, _drei.useTexture)("".concat(url));
  imageMap.encoding = THREE.sRGBEncoding;
  imageMap.magFilter = THREE.NearestFilter;
  imageMap.minFilter = THREE.LinearFilter;
  imageMap.generateMipmaps = false;
  const [center, setCenter] = (0, _react.useState)([0, 0, 0]);
  const ref = (0, _react.useRef)();
  (0, _react.useEffect)(() => {
    const box = new THREE.Box3().setFromObject(ref.current);
    const sphere = new THREE.Sphere();
    box.getBoundingSphere(sphere);
    setCenter([-sphere.center.x, -sphere.center.y, 0]);
  }, []);
  return /*#__PURE__*/_react.default.createElement("group", {
    name: name,
    position: center,
    scale: [scale, scale, 1],
    ref: ref
  }, /*#__PURE__*/_react.default.createElement("mesh", null, /*#__PURE__*/_react.default.createElement("planeGeometry", {
    args: [imageMap.image.width / imageMap.image.height, 1, 1, 1]
  }), /*#__PURE__*/_react.default.createElement("meshBasicMaterial", {
    map: imageMap,
    transparent: true,
    toneMapped: false
  })));
}

function App(_ref2) {
  let {
    minimapMapScale,
    minimapPath,
    pointPlayerScale,
    pointPlayerPath
  } = _ref2;
  return /*#__PURE__*/_react.default.createElement("group", null, /*#__PURE__*/_react.default.createElement(Png, {
    name: "minimapMap",
    url: minimapPath,
    scale: minimapMapScale
  }), /*#__PURE__*/_react.default.createElement(Png, {
    name: "pointPlayer",
    url: pointPlayerPath,
    scale: pointPlayerScale
  }));
}

function Cube(props) {
  const mapScale = props.mapScale;
  const mapOffsetX = props.mapOffsetX;
  const mapOffsetY = props.mapOffsetY;
  const pointPlayerScale = props.pointPlayerScale;
  const pointPlayerPath = props.pointPlayerPath;
  const minimapPath = props.minimapPath;
  const minimapMapScale = props.minimapMapScale;
  const {
    scene
  } = (0, _fiber.useThree)();
  const [theScene, target, theCamera] = (0, _react.useMemo)(() => {
    const theScene = new THREE.Scene();
    theScene.background = new THREE.Color('#808080');
    const target = new THREE.WebGLRenderTarget(1024, 1024, {
      format: THREE.RGBAFormat,
      stencilBuffer: false
    });
    target.samples = 8;
    const theCamera = new THREE.OrthographicCamera(-400.0, 400.0, 400.0, -400.0, 0.1, 10000);
    theScene.add(theCamera);
    theCamera.position.set(0, 0, 50);
    return [theScene, target, theCamera];
  }, []);
  let thePlayer = null;
  let minimapMap = null;
  let pointPlayer = null;
  const rotationn = new THREE.Euler();
  (0, _fiber.useFrame)(_ref3 => {
    let {
      gl
    } = _ref3;

    if (thePlayer && pointPlayer && minimapMap) {
      minimapMap.position.x = -thePlayer.parent.position.x * mapScale + mapOffsetX;
      minimapMap.position.y = thePlayer.parent.position.z * mapScale + mapOffsetY;
      rotationn.setFromQuaternion(thePlayer.parent.quaternion);

      if (rotationn.x < 0) {
        pointPlayer.rotation.z = -rotationn.y;
      } else {
        pointPlayer.rotation.z = rotationn.y + Math.PI;
      }
    } else {
      if (!thePlayer) {
        thePlayer = scene.getObjectByName("Player");
      }

      if (!minimapMap) {
        minimapMap = theScene.getObjectByName("minimapMap");
      }

      if (!pointPlayer) {
        pointPlayer = theScene.getObjectByName("pointPlayer");
      }
    }

    gl.setRenderTarget(target);
    gl.render(theScene, theCamera);
    gl.setRenderTarget(null);
  });
  return /*#__PURE__*/_react.default.createElement("group", props, (0, _fiber.createPortal)( /*#__PURE__*/_react.default.createElement(App, {
    minimapMapScale: minimapMapScale,
    minimapPath: minimapPath,
    pointPlayerScale: pointPlayerScale,
    pointPlayerPath: pointPlayerPath
  }), theScene), /*#__PURE__*/_react.default.createElement(_drei.Circle, {
    args: [0.2, 30]
  }, /*#__PURE__*/_react.default.createElement("meshBasicMaterial", {
    attach: "material",
    map: target.texture,
    toneMapped: false,
    transparent: true
  })));
}

var _default = Cube;
exports.default = _default;