123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import { _decorator,Camera,Node} 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';
- import { audioMgr } from '../core/manager/AudioManager';
- const { ccclass, property } = _decorator;
- @ccclass('Game')
- export class Game extends BaseExp {
- public static I: Game;
- @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(()=>{
- audioMgr.play(Constants.audios.BGM,true);
- });
- }
- /**
- * 下一关
- */
- public nextLevel(){
- userIns.data.level += 1;
- //加载新的map
- this.loadMap(()=>{
- this.isGameOver = this.isPause = false;
- //刷新新的任务
- uiMgr.getPageComponent(Constants.mainUIs.main).loadData();
- //加载敌人
- this.buildEnemys.loadLevelEnemys();
- });
- }
- /**
- * 重玩
- */
- public restart(){
- //重新加载map
- this.loadMap(()=>{
- //创建玩家拥有的枪
- this.player.restart();
- //加载敌人
- this.buildEnemys.loadLevelEnemys();
- });
- }
- /**
- * 加载map
- * @param cb 回调
- */
- public loadMap(cb: Function = null){
- let levelData:any = userIns.getCurLevelData();
- 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!;
- }
- map.parent = this.map_pos.parent;
- map.worldPosition = this.map_pos.worldPosition;
- this.map = map.getComponent(GMap);
- //创建敌人的组建
- 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();
- }
- }
|