Game.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { _decorator,Camera,Color,DirectionalLight,director,Node, ParticleSystem2D, Scene, TextureCube} from 'cc';
  2. import { GMap } from './GMap';
  3. import { Player } from './Player';
  4. import { Utils } from '../utils/Utils';
  5. import { Logger } from '../extend/Logger';
  6. import { ResUtil } from '../utils/ResUtil';
  7. import { userIns } from '../data/UserData';
  8. import { BuildEnemys } from './BuildEnemys';
  9. import { autoBind } from '../extend/AutoBind';
  10. import { BaseExp } from '../core/base/BaseExp';
  11. import { uiMgr } from '../core/manager/UIManager';
  12. import { Constants } from '../data/Constants';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('Game')
  15. export class Game extends BaseExp {
  16. public static I: Game;
  17. @property(TextureCube)
  18. public dayCube!: TextureCube;
  19. protected dayColor: Color = Color.WHITE;
  20. @property(TextureCube)
  21. public nightCube!: TextureCube;
  22. protected nightColor: Color = new Color(0, 0, 0, 255);
  23. @autoBind({type: Node,tooltip: "map节点的位置"})
  24. public map_pos: Node = null!;
  25. /*是否暂停游戏*/
  26. public isPause: boolean = false;
  27. /**是否游戏结束*/
  28. public _isGameOver: boolean = false;
  29. public set isGameOver(value: boolean) {
  30. if(value){this.gameEnd();}
  31. this._isGameOver = value;
  32. }
  33. public get isGameOver(){
  34. return this._isGameOver;
  35. }
  36. //当前地图场景
  37. public map: GMap = null!;
  38. //创建敌人的组建
  39. public buildEnemys: BuildEnemys = null!;
  40. //玩家组建
  41. public player: Player = null!;
  42. //主角摄像机
  43. public camera: Camera = null!;
  44. //Canvas2D节点
  45. public canvas: Node = null!;
  46. start() {
  47. Game.I = this;
  48. this.canvas = this.node.parent.getChildByName('Canvas')
  49. this.canvas.active = true;
  50. this.map_pos.active = true;
  51. //加载map
  52. this.loadMap();
  53. }
  54. /**
  55. * 下一关
  56. */
  57. public nextLevel(){
  58. userIns.data.level += 1;
  59. this.loadMap(()=>{
  60. this.isGameOver = this.isPause = false;
  61. //刷新新的任务
  62. uiMgr.getPageComponent(Constants.mainUIs.main).loadData();
  63. this.player.restart();
  64. //加载敌人
  65. this.buildEnemys.loadLevelEnemys();
  66. });
  67. }
  68. /**
  69. * 重玩
  70. */
  71. public restart(){
  72. this.loadMap(()=>{
  73. //创建玩家拥有的枪
  74. this.player.restart();
  75. //加载敌人
  76. this.buildEnemys.loadLevelEnemys();
  77. });
  78. }
  79. /**
  80. * 加载map
  81. * @param cb 回调
  82. */
  83. public loadMap(cb: Function = null){
  84. let levelData:any = userIns.getCurLevelData();
  85. //是否是夜晚(0白天,1是晚上)
  86. let scene = director.getScene() as Scene;
  87. scene.globals.skybox.envmap = levelData.night == 0 ? this.dayCube : this.nightCube;
  88. const name: string = levelData.map;
  89. if(Utils.isNull(name))return
  90. Logger.log(`加载地图:${name}`);
  91. ResUtil.loadRes(`map/${name}`).then((map:Node)=>{
  92. //把旧的地图回收 这儿不回收了 懒得处理初始状态 全部重新加载一次 PoolManager.putNode(this.map.node);
  93. if(this.map){
  94. this.map.node.removeFromParent();
  95. this.map.node.destroy();
  96. this.map = null!;
  97. }
  98. let light:DirectionalLight = map.getChildByName("Main Light").getComponent(DirectionalLight);
  99. light.color = levelData.night == 0 ? this.dayColor : this.nightColor;
  100. map.parent = this.map_pos.parent;
  101. map.worldPosition = this.map_pos.worldPosition;
  102. this.map = map.getComponent(GMap);
  103. this.map.resetData();
  104. //创建敌人的组建
  105. this.buildEnemys = this.map.getComponent(BuildEnemys);
  106. //玩家组建
  107. this.player = this.map.getComponentInChildren(Player);
  108. //设置玩家位置
  109. this.player.setPlayerPos();
  110. //主角摄像机
  111. const cameraNode: Node = Utils.findName(map, 'mainCamera');
  112. this.camera = cameraNode.getComponent(Camera);
  113. cb?.();
  114. });
  115. //去加载下一关map所需要的地图 这儿只要加载过 就会缓存 意思就这一次加载的
  116. //把下一次要用的到的地图提前预加载一遍
  117. const nextLevelData = userIns.levelsTable.find(e=>e.id == (levelData.id + 1));
  118. if(nextLevelData && name != nextLevelData.map){
  119. ResUtil.loadRes(`map/${nextLevelData.map}`);
  120. }
  121. }
  122. /**
  123. * 游戏结束回收
  124. */
  125. public gameEnd(){
  126. this.buildEnemys.recycle();
  127. }
  128. }