System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2", "__unresolved_3", "__unresolved_4", "__unresolved_5", "__unresolved_6", "__unresolved_7"], function (_export, _context) {
  "use strict";

  var _reporterNs, _cclegacy, _decorator, Component, Node, Prefab, instantiate, Camera, Label, ServerListItem, FrameSyncExecutor, PlayerData, PlayerComponent, InputType, JoystickComponent, _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9, _descriptor10, _temp, _crd, ccclass, property, Demo1SceneManager;

  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

  function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }

  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

  function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }

  function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); }

  function _reportPossibleCrUseOfGateClient(extras) {
    _reporterNs.report("GateClient", "../../shared/gateClient/gateClient", _context.meta, extras);
  }

  function _reportPossibleCrUseOfServerListItem(extras) {
    _reporterNs.report("ServerListItem", "../ui/ServerListItem", _context.meta, extras);
  }

  function _reportPossibleCrUseOfIGameServerInfo(extras) {
    _reporterNs.report("IGameServerInfo", "../../shared/gateClient/GameServer", _context.meta, extras);
  }

  function _reportPossibleCrUseOfGameClient(extras) {
    _reporterNs.report("GameClient", "../../shared/gameClient/gameClient", _context.meta, extras);
  }

  function _reportPossibleCrUseOfFrameSyncExecutor(extras) {
    _reporterNs.report("FrameSyncExecutor", "../common/FrameSyncExecutor", _context.meta, extras);
  }

  function _reportPossibleCrUseOfPlayerData(extras) {
    _reporterNs.report("PlayerData", "./PlayerData", _context.meta, extras);
  }

  function _reportPossibleCrUseOfPlayerComponent(extras) {
    _reporterNs.report("PlayerComponent", "./PlayerComponent", _context.meta, extras);
  }

  function _reportPossibleCrUseOfConnectionInputOperate(extras) {
    _reporterNs.report("ConnectionInputOperate", "../../shared/gameClient/GameSyncFrame", _context.meta, extras);
  }

  function _reportPossibleCrUseOfInputType(extras) {
    _reporterNs.report("InputType", "../../shared/gameClient/games/DemoSimpleFrameSyncModels", _context.meta, extras);
  }

  function _reportPossibleCrUseOfJoystickComponent(extras) {
    _reporterNs.report("JoystickComponent", "../common/ui/JoystickComponent", _context.meta, extras);
  }

  function _reportPossibleCrUseOfIMoveDirection(extras) {
    _reporterNs.report("IMoveDirection", "../common/ui/BaseJoystick", _context.meta, extras);
  }

  return {
    setters: [function (_unresolved_) {
      _reporterNs = _unresolved_;
    }, function (_cc) {
      _cclegacy = _cc.cclegacy;
      _decorator = _cc._decorator;
      Component = _cc.Component;
      Node = _cc.Node;
      Prefab = _cc.Prefab;
      instantiate = _cc.instantiate;
      Camera = _cc.Camera;
      Label = _cc.Label;
    }, function (_unresolved_2) {}, function (_unresolved_3) {
      ServerListItem = _unresolved_3.ServerListItem;
    }, function (_unresolved_4) {
      FrameSyncExecutor = _unresolved_4.FrameSyncExecutor;
    }, function (_unresolved_5) {
      PlayerData = _unresolved_5.PlayerData;
    }, function (_unresolved_6) {
      PlayerComponent = _unresolved_6.PlayerComponent;
    }, function (_unresolved_7) {
      InputType = _unresolved_7.InputType;
    }, function (_unresolved_8) {
      JoystickComponent = _unresolved_8.JoystickComponent;
    }],
    execute: function () {
      _crd = true;

      _cclegacy._RF.push({}, "39e024xEA5DiLy4WDP32Xgc", "Demo1SceneManager", undefined);

      ({
        ccclass,
        property
      } = _decorator);

      _export("Demo1SceneManager", Demo1SceneManager = (_dec = ccclass('Demo1SceneManager'), _dec2 = property(Label), _dec3 = property(Prefab), _dec4 = property(Node), _dec5 = property(Node), _dec6 = property(Node), _dec7 = property(Node), _dec8 = property(Node), _dec9 = property(Prefab), _dec10 = property(Camera), _dec11 = property(_crd && JoystickComponent === void 0 ? (_reportPossibleCrUseOfJoystickComponent({
        error: Error()
      }), JoystickComponent) : JoystickComponent), _dec(_class = (_class2 = (_temp = class Demo1SceneManager extends Component {
        constructor() {
          super(...arguments);

          _defineProperty(this, "gateClient", void 0);

          _defineProperty(this, "gameClient", void 0);

          _initializerDefineProperty(this, "DebugInfo", _descriptor, this);

          _initializerDefineProperty(this, "ServerInfoListItemTpl", _descriptor2, this);

          _initializerDefineProperty(this, "ServerList", _descriptor3, this);

          _initializerDefineProperty(this, "ViewServerList", _descriptor4, this);

          _initializerDefineProperty(this, "ViewGameUI", _descriptor5, this);

          _initializerDefineProperty(this, "ViewGame3D", _descriptor6, this);

          _initializerDefineProperty(this, "PlayersContainer", _descriptor7, this);

          _initializerDefineProperty(this, "PlayerPrefab", _descriptor8, this);

          _initializerDefineProperty(this, "MainCamera", _descriptor9, this);

          _initializerDefineProperty(this, "Joystick", _descriptor10, this);

          _defineProperty(this, "frameIndex", 0);

          _defineProperty(this, "frameSyncExecutor", void 0);

          _defineProperty(this, "allPlayers", {});

          _defineProperty(this, "myPlayer", void 0);
        }

        start() {
          this.gateClient = globalThis.getGateClient("http://127.0.0.1:7100");
          this.refreshList();
          this.stopGame();

          this.Joystick.onmove = dir => {
            this.onJoysickMoveStart(dir);
          };

          this.Joystick.onmoveend = () => {
            this.onJoysickMoveEnd();
          };

          setInterval(() => {
            this.DebugInfo.string = "frameIndex:" + this.frameIndex;
          }, 100);
        }

        refreshList() {
          var _this = this;

          return _asyncToGenerator(function* () {
            if (!_this.ServerInfoListItemTpl || !_this.ServerList) return;
            var serverListRet = yield _this.gateClient.getGameServers();

            if (!serverListRet.isSucc) {
              var _client$logger;

              (_client$logger = _this.gateClient.client.logger) === null || _client$logger === void 0 ? void 0 : _client$logger.error("获取游戏服务器列表失败:" + serverListRet.err);
              return;
            }

            _this.ServerList.removeAllChildren();

            serverListRet.res.GameServerList.forEach(serverInfo => {
              var itemNode = instantiate(_this.ServerInfoListItemTpl);
              var siComp = itemNode.getComponent(_crd && ServerListItem === void 0 ? (_reportPossibleCrUseOfServerListItem({
                error: Error()
              }), ServerListItem) : ServerListItem);

              if (siComp) {
                var _this$ServerList;

                siComp.serverInfo = serverInfo;

                siComp.onClick = sv => _this.onServerItemClick(sv);

                (_this$ServerList = _this.ServerList) === null || _this$ServerList === void 0 ? void 0 : _this$ServerList.addChild(itemNode);
              }
            });
          })();
        }

        onServerItemClick(sv) {
          var _this2 = this;

          return _asyncToGenerator(function* () {
            if (!_this2.gateClient) return; //从入口服务器获取登录token,这是模拟登录的实现(正常需要输入用户名或密码,或者第三方授权,在入口服务器获得登录token)

            var ret = yield _this2.gateClient.loginByCustomUserName(sv, "testUser");

            if (!ret.succ) {
              var _this2$gateClient$cli;

              return (_this2$gateClient$cli = _this2.gateClient.client.logger) === null || _this2$gateClient$cli === void 0 ? void 0 : _this2$gateClient$cli.error(ret.err);
            } //登录到游戏服务器


            _this2.gameClient = globalThis.getGameClient(sv.serverWSUrl);

            _this2.gameClient.disconnectHandler = () => {
              return true;
            };

            _this2.gameClient.onReconnectResult = (succ, err) => {
              if (!succ) {
                _this2.stopGame();
              }

              return;
            };

            var err = yield _this2.gameClient.login(ret.data);

            if (err) {
              var _this2$gateClient$cli2;

              return (_this2$gateClient$cli2 = _this2.gateClient.client.logger) === null || _this2$gateClient$cli2 === void 0 ? void 0 : _this2$gateClient$cli2.error(err);
            }

            if (_this2.ViewServerList) _this2.ViewServerList.active = false;
            if (_this2.ViewGameUI) _this2.ViewGameUI.active = true;
            if (_this2.ViewGame3D) _this2.ViewGame3D.active = true;

            _this2.startGame();
          })();
        }

        onExitGameClick() {
          this.stopGame();
        }

        stopGame() {
          var _this3 = this;

          return _asyncToGenerator(function* () {
            var _this3$frameSyncExecu;

            yield (_this3$frameSyncExecu = _this3.frameSyncExecutor) === null || _this3$frameSyncExecu === void 0 ? void 0 : _this3$frameSyncExecu.dispose();
            if (_this3.ViewServerList) _this3.ViewServerList.active = true;
            if (_this3.ViewGameUI) _this3.ViewGameUI.active = false;
            if (_this3.ViewGame3D) _this3.ViewGame3D.active = false;
          })();
        }

        startGame() {
          this.frameSyncExecutor = new (_crd && FrameSyncExecutor === void 0 ? (_reportPossibleCrUseOfFrameSyncExecutor({
            error: Error()
          }), FrameSyncExecutor) : FrameSyncExecutor)(this.gameClient, this, stateData => this.onAfterFrames(stateData), (dt, frameIndex) => this.executeOneFrame(dt, frameIndex), () => this.allPlayers);
        }

        update(dt) {}

        lateUpdate() {
          if (this.myPlayer) {
            var _this$MainCamera, _this$MainCamera2;

            (_this$MainCamera = this.MainCamera) === null || _this$MainCamera === void 0 ? void 0 : _this$MainCamera.node.setPosition(this.myPlayer.node.position.x, this.myPlayer.node.position.y + 10, this.myPlayer.node.position.z + 20);
            (_this$MainCamera2 = this.MainCamera) === null || _this$MainCamera2 === void 0 ? void 0 : _this$MainCamera2.node.lookAt(this.myPlayer.node.position);
          }
        }

        onAfterFrames(stateData) {
          var _this$PlayersContaine;

          this.allPlayers = stateData; //重新创建所有节点

          (_this$PlayersContaine = this.PlayersContainer) === null || _this$PlayersContaine === void 0 ? void 0 : _this$PlayersContaine.removeAllChildren();

          for (var connId in this.allPlayers) {
            var _this$PlayersContaine2, _this$gameClient;

            var p = this.allPlayers[connId];
            var newPlayer = instantiate(this.PlayerPrefab);
            newPlayer.name = "PlayerId_" + connId;
            var playerComp = newPlayer.getComponent(_crd && PlayerComponent === void 0 ? (_reportPossibleCrUseOfPlayerComponent({
              error: Error()
            }), PlayerComponent) : PlayerComponent);
            playerComp.init(p);
            (_this$PlayersContaine2 = this.PlayersContainer) === null || _this$PlayersContaine2 === void 0 ? void 0 : _this$PlayersContaine2.addChild(newPlayer);

            if (connId == ((_this$gameClient = this.gameClient) === null || _this$gameClient === void 0 ? void 0 : _this$gameClient.connectionId)) {
              this.myPlayer = playerComp;
            }
          }
        }

        execInput_NewPlayer(connId, inputFrame, dt) {
          var _this$PlayersContaine3, _this$gameClient2;

          var p = new (_crd && PlayerData === void 0 ? (_reportPossibleCrUseOfPlayerData({
            error: Error()
          }), PlayerData) : PlayerData)();
          p.connId = connId;
          p.userName = inputFrame.userName;
          this.allPlayers[connId] = p;
          var newPlayer = instantiate(this.PlayerPrefab);
          newPlayer.name = "PlayerId_" + connId;
          var playerComp = newPlayer.getComponent(_crd && PlayerComponent === void 0 ? (_reportPossibleCrUseOfPlayerComponent({
            error: Error()
          }), PlayerComponent) : PlayerComponent);
          playerComp.init(p);
          (_this$PlayersContaine3 = this.PlayersContainer) === null || _this$PlayersContaine3 === void 0 ? void 0 : _this$PlayersContaine3.addChild(newPlayer);

          if (connId == ((_this$gameClient2 = this.gameClient) === null || _this$gameClient2 === void 0 ? void 0 : _this$gameClient2.connectionId)) {
            this.myPlayer = playerComp;
          }
        }

        execInput_RemovePlayer(connId, inputFrame, dt) {
          var _this$PlayersContaine4, _this$PlayersContaine5;

          var p = this.allPlayers[connId];
          delete this.allPlayers[connId];
          var playerNode = (_this$PlayersContaine4 = this.PlayersContainer) === null || _this$PlayersContaine4 === void 0 ? void 0 : _this$PlayersContaine4.getChildByName("PlayerId_" + p.connId);
          if (playerNode) (_this$PlayersContaine5 = this.PlayersContainer) === null || _this$PlayersContaine5 === void 0 ? void 0 : _this$PlayersContaine5.removeChild(playerNode);
        }

        execInput_MoveDirStart(connId, inputFrame, dt) {
          var player = this.allPlayers[connId];
          if (!player) return;
          player.moveDirX = inputFrame.dirX;
          player.moveDirZ = inputFrame.dirZ;
          player.moveAcuteRad = inputFrame.acuteRad;
        }

        execInput_MoveDirEnd(connId, inputFrame, dt) {
          var player = this.allPlayers[connId];
          if (!player) return;
          player.moveDirX = 0;
          player.moveDirZ = 0;
          player.moveAcuteRad = 0;
        }

        executeOneFrame(dt, frameIndex) {
          this.frameIndex = frameIndex;

          for (var connId in this.allPlayers) {
            var p = this.allPlayers[connId];
            this.playerUpdate(p, dt);
          }
        }

        playerUpdate(player, dt) {
          if (player.moveDirX != 0 || player.moveDirZ != 0) {
            //有移动,转向直接生效,小方块,懒得转忽略
            var distance = player.speed * dt; //本帧移动的距离

            var oldX = player.x;
            var oldZ = player.z;
            var cosA = Math.cos(player.moveAcuteRad);
            var sinA = Math.sin(player.moveAcuteRad);
            var stepReC = distance;
            var stepReX = player.moveDirZ == 0 ? player.moveDirX * distance : cosA * stepReC * player.moveDirX; //如果Z方向没动,直接用X方向的距离

            var stepReZ = player.moveDirX == 0 ? player.moveDirZ * distance : sinA * stepReC * player.moveDirZ; //如果X方向没动,直接用Z方向的距离

            player.x = oldX + stepReX;
            player.z = oldZ + stepReZ;
          }
        }

        onJoysickMoveStart(move) {
          var _this$frameSyncExecut;

          (_this$frameSyncExecut = this.frameSyncExecutor) === null || _this$frameSyncExecut === void 0 ? void 0 : _this$frameSyncExecut.sendInputFrame({
            inputType: (_crd && InputType === void 0 ? (_reportPossibleCrUseOfInputType({
              error: Error()
            }), InputType) : InputType).MoveDirStart,
            dirX: move.dirX,
            dirZ: move.dirZ,
            acuteRad: move.acuteRad
          });
        }

        onJoysickMoveEnd() {
          var _this$frameSyncExecut2;

          (_this$frameSyncExecut2 = this.frameSyncExecutor) === null || _this$frameSyncExecut2 === void 0 ? void 0 : _this$frameSyncExecut2.sendInputFrame({
            inputType: (_crd && InputType === void 0 ? (_reportPossibleCrUseOfInputType({
              error: Error()
            }), InputType) : InputType).MoveDirEnd
          });
        }

      }, _temp), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "DebugInfo", [_dec2], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: null
      }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "ServerInfoListItemTpl", [_dec3], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: null
      }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "ServerList", [_dec4], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: null
      }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "ViewServerList", [_dec5], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: null
      }), _descriptor5 = _applyDecoratedDescriptor(_class2.prototype, "ViewGameUI", [_dec6], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: null
      }), _descriptor6 = _applyDecoratedDescriptor(_class2.prototype, "ViewGame3D", [_dec7], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: null
      }), _descriptor7 = _applyDecoratedDescriptor(_class2.prototype, "PlayersContainer", [_dec8], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: null
      }), _descriptor8 = _applyDecoratedDescriptor(_class2.prototype, "PlayerPrefab", [_dec9], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: null
      }), _descriptor9 = _applyDecoratedDescriptor(_class2.prototype, "MainCamera", [_dec10], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: null
      }), _descriptor10 = _applyDecoratedDescriptor(_class2.prototype, "Joystick", [_dec11], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: null
      })), _class2)) || _class));

      _cclegacy._RF.pop();

      _crd = false;
    }
  };
});
//# sourceMappingURL=Demo1SceneManager.js.map