137 lines
4.0 KiB
TypeScript
Raw Normal View History

2023-10-31 02:34:12 +08:00
import { _decorator,Node } from "cc";
2023-10-23 18:56:01 +08:00
import GBaseMode from "../GBaseMode";
import { GTactical } from "../entity/GTactical";
import { Prefab } from "cc";
import { instantiate } from "cc";
import { Vec2 } from "cc";
2023-10-30 02:34:11 +08:00
import { GRoleUtil } from "../entity/GRole";
import { TableGRole } from "../../../resources/config/ts/TableGRole";
2023-11-01 02:01:35 +08:00
import GRoleDefault from "../base/role/GRoleDefault";
2023-10-23 18:56:01 +08:00
const { ccclass, property } = _decorator;
//PVP 角色
export enum GPVPModePlayerEnum{
PLAYER, //玩家
ENEMY, //敌人
}
//PVP 玩家信息
export interface GPVPModePlayerInfo{
//阵法
tactical: GTactical;
//宠物列表
2023-10-30 02:34:11 +08:00
roles: TableGRole[];
2023-10-23 18:56:01 +08:00
}
/**
* PVP
*/
@ccclass('GPVPMode')
2023-10-26 03:06:44 +08:00
export default class GPVPMode extends GBaseMode<{}>{
2023-10-23 18:56:01 +08:00
@property(Prefab)
rolePrefab: Prefab = null;
2023-10-31 02:34:12 +08:00
@property(Node)
objects: Node = null;
2023-10-23 18:56:01 +08:00
//玩家信息
2023-10-26 03:06:44 +08:00
playerInfo: GPVPModePlayerInfo;
2023-10-23 18:56:01 +08:00
//敌方信息
2023-10-26 03:06:44 +08:00
enemyInfo: GPVPModePlayerInfo;
2023-10-23 18:56:01 +08:00
//玩家宠物
2023-11-01 02:01:35 +08:00
playerRoles: GRoleDefault[] = [];
2023-10-23 18:56:01 +08:00
//敌方宠物
2023-11-01 02:01:35 +08:00
enemyRoles: GRoleDefault[] = [];
2023-10-23 18:56:01 +08:00
//玩家位置
playerPos: Vec2 = new Vec2(-400,0);
//敌方位置
enemyPos: Vec2 = new Vec2(400,0);
2023-10-31 02:34:12 +08:00
get scene():Node{
return this.objects;
}
2023-10-26 03:06:44 +08:00
onLoad(){
super.onLoad();
}
2023-10-23 18:56:01 +08:00
onSyncInitSuccess(): void {
2023-10-30 02:34:11 +08:00
2023-10-23 18:56:01 +08:00
//初始化战斗
console.log("GPVPMode 模式初始化");
2023-10-26 03:06:44 +08:00
2023-11-01 02:01:35 +08:00
this.playerInfo = { tactical: GTactical.getTactical().setOffset(this.playerPos), roles: GRoleUtil.getGRoles([10001,10001,10001,10001,10003,10003]) };
this.enemyInfo = { tactical: GTactical.getTactical(true).setOffset(this.enemyPos), roles: GRoleUtil.getGRoles([10002,10002,10002,10001,10004,10003]) };
2023-10-23 18:56:01 +08:00
//生成玩家
2023-10-26 03:06:44 +08:00
this.playerInfo.roles.forEach((info,index) => this.onGenRole(GPVPModePlayerEnum.PLAYER,index+1,info))
this.enemyInfo.roles.forEach((info,index) => this.onGenRole(GPVPModePlayerEnum.ENEMY,index+1,info))
2023-10-23 18:56:01 +08:00
}
//生成角色
2023-10-30 02:34:11 +08:00
onGenRole(type: GPVPModePlayerEnum,index:number,info:TableGRole) {
2023-10-23 18:56:01 +08:00
2023-11-01 02:01:35 +08:00
let tactical = this.getInfo(type).tactical;
2023-10-23 18:56:01 +08:00
let pos:Vec2 = this.getInfo(type).tactical.getPosition(index);
if(!pos) return;
let role = instantiate(this.rolePrefab);
2023-11-01 02:01:35 +08:00
let entity = role.getComponent(GRoleDefault);
//初始化
entity.onInit(type,info,tactical,index);
//绑定寻敌
entity.onQueryEunmy = () => {
return this.getEnumy(entity,type);
}
this.addGObject(entity,tactical.getPosition(index));
2023-10-24 02:32:06 +08:00
this.getOnesRole(type).push(entity);
2023-10-23 18:56:01 +08:00
}
//获取配置
getInfo(type: GPVPModePlayerEnum): GPVPModePlayerInfo {
if(type == GPVPModePlayerEnum.PLAYER) return this.playerInfo;
if(type == GPVPModePlayerEnum.ENEMY) return this.enemyInfo;
}
2023-10-24 02:32:06 +08:00
//获取阵营角色
2023-11-01 02:01:35 +08:00
getOnesRole(type: GPVPModePlayerEnum):GRoleDefault[]{
2023-10-24 02:32:06 +08:00
if(type == GPVPModePlayerEnum.PLAYER) return this.playerRoles;
if(type == GPVPModePlayerEnum.ENEMY) return this.enemyRoles;
}
//获取敌人
2023-11-01 02:01:35 +08:00
getEnumy(player:GRoleDefault,type:GPVPModePlayerEnum):GRoleDefault{
2023-10-24 02:32:06 +08:00
let enumyOnes = GPVPModePlayerEnum.ENEMY
//如果是ENEMY 则 它的敌人是 PLAYER
2023-11-01 02:01:35 +08:00
if(type == GPVPModePlayerEnum.ENEMY) enumyOnes = GPVPModePlayerEnum.PLAYER
2023-10-24 02:32:06 +08:00
//获取敌人
let roles = this.getOnesRole(enumyOnes);
//返回敌人
//获取我在第几排
let playerXY = player.tactical.getXY(player.tacticalIndex);
//通过排数获取最近的敌人
2023-11-01 02:01:35 +08:00
let sort = roles.filter(role => !!role.get()).sort((enumy1,enumy2) => {
2023-10-24 02:32:06 +08:00
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]
2023-10-25 19:19:52 +08:00
}
2023-10-23 18:56:01 +08:00
}