import { _decorator,Camera,Color,DirectionalLight,director,Node, ParticleSystem2D, Scene, TextureCube} from 'cc'; import { GMap } from './GMap'; import { Player } from './Player'; import { Utils } from '../utils/Utils'; import { Logger } from '../extend/Logger'; import { ResUtil } from '../utils/ResUtil'; import { userIns } from '../data/UserData'; import { BuildEnemys } from './BuildEnemys'; import { autoBind } from '../extend/AutoBind'; import { BaseExp } from '../core/base/BaseExp'; import { uiMgr } from '../core/manager/UIManager'; import { Constants } from '../data/Constants'; const { ccclass, property } = _decorator; @ccclass('Game') export class Game extends BaseExp { public static I: Game; @property(TextureCube) public dayCube!: TextureCube; protected dayColor: Color = Color.WHITE; @property(TextureCube) public nightCube!: TextureCube; protected nightColor: Color = new Color(0, 0, 0, 255); @autoBind({type: Node,tooltip: "map节点的位置"}) public map_pos: Node = null!; /*是否暂停游戏*/ public isPause: boolean = false; /**是否游戏结束*/ public _isGameOver: boolean = false; public set isGameOver(value: boolean) { if(value){this.gameEnd();} this._isGameOver = value; } public get isGameOver(){ return this._isGameOver; } //当前地图场景 public map: GMap = null!; //创建敌人的组建 public buildEnemys: BuildEnemys = null!; //玩家组建 public player: Player = null!; //主角摄像机 public camera: Camera = null!; //Canvas2D节点 public canvas: Node = null!; start() { Game.I = this; this.canvas = this.node.parent.getChildByName('Canvas') this.canvas.active = true; this.map_pos.active = true; //加载map this.loadMap(); } /** * 下一关 */ public nextLevel(){ userIns.data.level += 1; this.loadMap(()=>{ this.isGameOver = this.isPause = false; //刷新新的任务 uiMgr.getPageComponent(Constants.mainUIs.main).loadData(); this.player.restart(); //加载敌人 this.buildEnemys.loadLevelEnemys(); }); } /** * 重玩 */ public restart(){ this.loadMap(()=>{ //创建玩家拥有的枪 this.player.restart(); //加载敌人 this.buildEnemys.loadLevelEnemys(); }); } /** * 加载map * @param cb 回调 */ public loadMap(cb: Function = null){ let levelData:any = userIns.getCurLevelData(); //是否是夜晚(0白天,1是晚上) let scene = director.getScene() as Scene; scene.globals.skybox.envmap = levelData.night == 0 ? this.dayCube : this.nightCube; const name: string = levelData.map; if(Utils.isNull(name))return Logger.log(`加载地图:${name}`); ResUtil.loadRes(`map/${name}`).then((map:Node)=>{ //把旧的地图回收 这儿不回收了 懒得处理初始状态 全部重新加载一次 PoolManager.putNode(this.map.node); if(this.map){ this.map.node.removeFromParent(); this.map.node.destroy(); this.map = null!; } let light:DirectionalLight = map.getChildByName("Main Light").getComponent(DirectionalLight); light.color = levelData.night == 0 ? this.dayColor : this.nightColor; map.parent = this.map_pos.parent; map.worldPosition = this.map_pos.worldPosition; this.map = map.getComponent(GMap); this.map.resetData(); //创建敌人的组建 this.buildEnemys = this.map.getComponent(BuildEnemys); //玩家组建 this.player = this.map.getComponentInChildren(Player); //设置玩家位置 this.player.setPlayerPos(); //主角摄像机 const cameraNode: Node = Utils.findName(map, 'mainCamera'); this.camera = cameraNode.getComponent(Camera); cb?.(); }); //去加载下一关map所需要的地图 这儿只要加载过 就会缓存 意思就这一次加载的 //把下一次要用的到的地图提前预加载一遍 const nextLevelData = userIns.levelsTable.find(e=>e.id == (levelData.id + 1)); if(nextLevelData && name != nextLevelData.map){ ResUtil.loadRes(`map/${nextLevelData.map}`); } } /** * 游戏结束回收 */ public gameEnd(){ this.buildEnemys.recycle(); } }