123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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 { PoolManager } from '../core/manager/PoolManager';
- import { uiMgr } from '../core/manager/UIManager';
- import { Constants } from '../data/Constants';
- import { Main } from './Main';
- 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();
- }
- /**
- * 下一关
- */
- public nextLevel(){
- userIns.data.level += 1;
- //加载新的map
- this.loadMap();
- //刷新新的任务
- uiMgr.getPageComponent(Constants.mainUIs.main).loadData();
- this.isGameOver = this.isPause = false;
- //加载敌人
- this.buildEnemys.loadLevelEnemys();
- }
- /**
- * 重玩
- */
- public restart(){
- this.isGameOver = this.isPause = false;
- //加载敌人
- this.buildEnemys.loadLevelEnemys();
- //创建玩家拥有的枪
- this.player.restart();
- }
- /**
- * 加载map
- */
- public loadMap(){
- const name: string = userIns.getCurLevelData().map;
- if(Utils.isNull(name)
- ||(this.map && this.map.name == name)){
- return;
- }
- Logger.log(`加载地图:${name}`);
- ResUtil.loadRes(`map/${name}`).then((map:Node)=>{
- //把旧的地图回收
- if(this.map){
- PoolManager.putNode(this.map.node);
- 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);
- //主角摄像机
- const cameraNode: Node = Utils.findName(map, 'mainCamera');
- this.camera = cameraNode.getComponent(Camera);
- });
- }
- /**
- * 游戏结束回收
- */
- public gameEnd(){
- this.buildEnemys.recycle();
- }
- }
|