dungeonManager.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import GameLogic from "../gameLogic/gameLogic";
  2. import gameData from "../gameLogic/utrl/gameData";
  3. import bundleManager from "./bundleManager";
  4. class dungeonManager {
  5. private _dungeonPrefab: cc.Node = null;
  6. private _curOpenID: string;
  7. remove() {
  8. if (this._dungeonPrefab) {
  9. this._dungeonPrefab.active = false;
  10. }
  11. }
  12. /**
  13. *
  14. * @param str 房间名
  15. * @param type 房间类型
  16. * @param callback 回调
  17. * @returns
  18. */
  19. open(str: string, type: number, callback?: Function) {
  20. let self = this;
  21. if (this._dungeonPrefab && str == this._curOpenID && type == gameData.curInDungeonType) {
  22. this._dungeonPrefab.active = true;
  23. if (callback) {
  24. callback();
  25. }
  26. return;
  27. }
  28. gameData.curInDungeonType = type;
  29. self.saveCurOpenID(str);
  30. setTimeout(() => {
  31. bundleManager.getBundleByName('dungeon').load('dungeon_' + str, cc.Prefab, function (err, prefab: cc.Prefab) {
  32. if (prefab) {
  33. let node = cc.instantiate(prefab);
  34. var cavans = cc.director.getScene().getChildByName('world')
  35. node.parent = cavans;
  36. // node.zIndex = 200
  37. self.remove();
  38. self._dungeonPrefab = node;
  39. cavans.getComponent(GameLogic).dungeonInit(node);
  40. if (callback) {
  41. callback(node);
  42. }
  43. }
  44. })
  45. }, 50);
  46. }
  47. preLoadLevel(str: string) {
  48. bundleManager.getBundleByName('dungeon').preload('dungeon_' + str, cc.Prefab, () => {
  49. console.log('预加载副本' + str + '完成');
  50. });
  51. }
  52. /**
  53. * 保存当前打开的副本ID
  54. * @param id 副本ID
  55. */
  56. saveCurOpenID(str: string) {
  57. this._curOpenID = str;
  58. }
  59. /**
  60. *
  61. * @returns 获取当前打开的副本ID
  62. */
  63. getCurOpenId() {
  64. return this._curOpenID;
  65. }
  66. resetDungen() {
  67. this._dungeonPrefab = null;
  68. }
  69. }
  70. export default new dungeonManager();