import { Vec2 } from "cc"; import { TableGRole } from "../../../resources/config/ts/TableGRole"; import GBaseMode from "../GBaseMode"; import { GRoleUtil } from "../entity/GRole"; import { GTactical } from "../entity/GTactical"; import GRoleDefault from "../base/role/GRoleDefault"; import { Prefab } from "cc"; import { _decorator } from "cc"; import { instantiate } from "cc"; import { GPVPModePlayerEnum } from "./GPVPMode"; import { JNFrameInfo } from "../../../../extensions/ngame/assets/ngame/sync/frame/JNSyncFrame"; import { GMapLoop } from "../base/common/map/GMapLoop"; import { Node } from "cc"; import JNFrameTime from "../../../../extensions/ngame/assets/ngame/sync/frame/game/time/JNFrameTime"; import { TableGMap } from "../../../resources/config/ts/TableGMap"; import { app } from "../../App"; import { v3 } from "cc"; import { v2 } from "cc"; import GFSMOnHookMode from "./OnHook/GFSMOnHookMode"; const { ccclass, property } = _decorator; //挂机模式状态 export enum GOnHookModeState{ GoTarget,//前往目标 Attack, //攻击 AttackEnd, //攻击结束 } //角色 export enum GOnHookModePlayerEnum{ PLAYER, //玩家 ENEMY, //怪物 } //玩家信息 export interface GOnHookInfo{ //阵法 tactical: GTactical; //宠物列表 roles: TableGRole[]; } /** * 挂机模式 无限出现小怪 */ @ccclass('GOnHookMode') export default class GOnHookMode extends GBaseMode<{}>{ @property(Prefab) rolePrefab: Prefab = null; //场景地图 @property(GMapLoop) map1:GMapLoop; @property(GMapLoop) map2:GMapLoop; @property(GMapLoop) map3:GMapLoop; @property(Node) objects: Node = null; get scene():Node{ return this.objects; } //玩家信息 playerInfo:GOnHookInfo; //宠物信息 enemyInfo:GOnHookInfo; //玩家宠物位置 playerPos: Vec2 = new Vec2(-400,0); //怪物位置 enemyPos: Vec2 = new Vec2(400,0); //玩家宠物 playerRoles: GRoleDefault[] = []; //敌方宠物 enemyRoles: GRoleDefault[] = []; //地图信息 mapInfo:TableGMap; //每一波怪的距离 everyX:number = 600; //下一波怪的对战位置X _nextFightX:number = 0; get nextFightX(){return this._nextFightX} set nextFightX(value:number){ //修改玩家阵法位置 this.playerInfo.tactical.setOffset(this.playerPos.clone().add(v2(value,0))) this.enemyInfo.tactical.setOffset(this.enemyPos.clone().add(v2(value,0))) this._nextFightX = value; } fsm:GFSMOnHookMode; onSyncInitSuccess():void{ //初始化战斗 console.log("GOnHookMode 模式初始化"); //调整相机 let camreaPos = this.camera.node.worldPosition; this.camera.node.worldPosition = v3(0,100,camreaPos.z) //初始化状态机 this.fsm = new GFSMOnHookMode(this); //初始化地图 this.mapInfo = TableGMap.getConfig(60001); this.map1.init(app.battleRes.maps[60001][0],1); this.map2.init(app.battleRes.maps[60001][1],1); this.map3.init(app.battleRes.maps[60001][2],1,app.battleRes.maps[60001][1].width,app.battleRes.maps[60001][1].height); this.onUpdateMap(0); this.playerInfo = { tactical: GTactical.getTactical().setOffset(this.playerPos), roles: GRoleUtil.getGRoles([10004,10004,10004,10004,10003,10003]) }; this.enemyInfo = { tactical: GTactical.getTactical(true).setOffset(this.enemyPos), roles: GRoleUtil.getGRoles([10002]) }; //生成玩家 this.playerInfo.roles.forEach((info,index) => this.onGenRole(GOnHookModePlayerEnum.PLAYER,index + 1,info)) // //生成敌人 // this.onResetGenerateEnemy(); } onSyncUpdate(dt: number,frame:JNFrameInfo, input?: {}){ super.onSyncUpdate(dt,frame,input); this.onUpdateMap(dt); this.onUpdateCamera(dt); this.fsm.onUpdate(dt,frame); } //更新地图 onUpdateMap(dt){ let cameraX = this.camera.node.worldPosition.x; this.map1.UpdateMap(cameraX,0,this.mapInfo.map1OffsetY); this.map2.UpdateMap(cameraX,cameraX / 10,this.mapInfo.map2OffsetY); this.map3.UpdateMap(cameraX,0,this.mapInfo.map3OffsetY); } //更新相机逻辑 onUpdateCamera(dt:number){ //如果没有敌人相机永远锁定最前面的宠物 if(!this.isHaveEnemy()){ //获取冲到最前面的宠物 let roles = this.getOnesRoleAlive(GOnHookModePlayerEnum.PLAYER); let frontRole = roles.sort((role1,role2) => role2.v2World.x - role1.v2World.x)[0]; if(!frontRole) return; //设置相机 位置 let cameraWorld = this.camera.node.worldPosition.clone(); this.camera.node.worldPosition = cameraWorld.lerp(v3(frontRole.v2World.x,cameraWorld.y,cameraWorld.z),(dt / 1000)); }else{ //如果有敌人则移动到战斗位置 let cameraWorld = this.camera.node.worldPosition.clone(); this.camera.node.worldPosition = cameraWorld.lerp(v3(this.nextFightX,cameraWorld.y,cameraWorld.z),(dt / 1000)); } } //生成宠物 onGenRole(type: GOnHookModePlayerEnum,index:number,info:TableGRole) { let tactical = this.getInfo(type).tactical; let pos:Vec2 = this.getInfo(type).tactical.getPosition(index); if(!pos) return; let role = instantiate(this.rolePrefab); let entity = role.getComponent(GRoleDefault); //初始化 entity.onInit(type,info,tactical,index); //绑定寻敌 entity.onQueryEunmy = () => { return this.getEnumy(entity,type); } //绑定死亡回调 entity.addKillBackEvent(this.onRoleKillBack.bind(this)) this.addGObject(entity,tactical.getPosition(index)); this.getOnesRole(type).push(entity); } //获取配置 getInfo(type: GOnHookModePlayerEnum): GOnHookInfo { if(type == GOnHookModePlayerEnum.PLAYER) return this.playerInfo; if(type == GOnHookModePlayerEnum.ENEMY) return this.enemyInfo; } //获取阵营宠物 getOnesRole(type: GOnHookModePlayerEnum):GRoleDefault[]{ if(type == GOnHookModePlayerEnum.PLAYER) return this.playerRoles; if(type == GOnHookModePlayerEnum.ENEMY) return this.enemyRoles; } //获取存活的宠物 getOnesRoleAlive(type: GOnHookModePlayerEnum):GRoleDefault[]{ if(type == GOnHookModePlayerEnum.PLAYER) return this.playerRoles.filter(role => !!role.get()); if(type == GOnHookModePlayerEnum.ENEMY) return this.enemyRoles.filter(role => !!role.get()); } //获取敌人 getEnumy(player:GRoleDefault,type:GOnHookModePlayerEnum):GRoleDefault{ let enumyOnes = GOnHookModePlayerEnum.ENEMY //如果是ENEMY 则 它的敌人是 PLAYER if(type == GOnHookModePlayerEnum.ENEMY) enumyOnes = GOnHookModePlayerEnum.PLAYER //获取敌人 let roles = this.getOnesRoleAlive(enumyOnes); //返回敌人 //获取我在第几排 let playerXY = player.tactical.getXY(player.tacticalIndex); //通过排数获取最近的敌人 let sort = roles.sort((enumy1,enumy2) => { let enumy1XY = enumy1.tactical.getXY(enumy1.tacticalIndex); let enumy2XY = enumy2.tactical.getXY(enumy2.tacticalIndex); return Math.abs((playerXY.y * 1000) - (enumy1XY.y * 1000)) + Math.abs((playerXY.x - enumy1XY.x)) - Math.abs((playerXY.y * 1000) - (enumy2XY.y * 1000)) + Math.abs((playerXY.x - enumy2XY.x)) }); return sort[0] } //生成敌人 onResetGenerateEnemy(){ this.enemyRoles = []; this.enemyInfo.roles.forEach((info,index) => this.onGenRole(GOnHookModePlayerEnum.ENEMY,index + 1,info)) } //角色死亡回调 onRoleKillBack(role:GRoleDefault){ //死亡销毁 JNFrameTime.getInstance().setTimeout(() => { if(role.isValid) role.node.destroy() },3000) // //如果没有敌人则生成敌人 // if(this.getOnesRole(GOnHookModePlayerEnum.ENEMY).filter(role => !!role.get()).length <= 0){ // //生成敌人 // this.onResetGenerateEnemy(); // } } //是否有怪物 isHaveEnemy(){ return this.getOnesRoleAlive(GOnHookModePlayerEnum.ENEMY).filter(role => !!role.get()).length > 0; } //前往下一个目标 onNextTarget(){ //下一个目标点 this.nextFightX = this.nextFightX + this.everyX; } }