277 lines
8.8 KiB
TypeScript
Raw Normal View History

2023-11-01 19:07:15 +08:00
import { Vec2 } from "cc";
2023-11-01 02:01:35 +08:00
import GBaseMode from "../GBaseMode";
import { GRoleUtil } from "../entity/GRole";
import { GTactical } from "../entity/GTactical";
2023-11-01 19:07:15 +08:00
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";
2023-11-03 02:57:38 +08:00
import { GMapLoop } from "../base/common/map/GMapLoop";
import { Node } from "cc";
import JNFrameTime from "../../../../extensions/ngame/assets/ngame/sync/frame/game/time/JNFrameTime";
2023-11-06 02:25:02 +08:00
import { TD, app } from "../../App";
2023-11-03 02:57:38 +08:00
import { v3 } from "cc";
2023-11-03 19:01:58 +08:00
import { v2 } from "cc";
import GFSMOnHookMode from "./OnHook/GFSMOnHookMode";
2023-11-06 02:25:02 +08:00
import { TB } from "../../../resources/config/data/schema";
2023-11-01 19:07:15 +08:00
const { ccclass, property } = _decorator;
2023-11-01 02:01:35 +08:00
2023-11-03 19:01:58 +08:00
//挂机模式状态
export enum GOnHookModeState{
GoTarget,//前往目标
Attack, //攻击
AttackEnd, //攻击结束
}
2023-11-01 02:01:35 +08:00
//角色
export enum GOnHookModePlayerEnum{
PLAYER, //玩家
ENEMY, //怪物
}
//玩家信息
2023-11-01 19:07:15 +08:00
export interface GOnHookInfo{
2023-11-01 02:01:35 +08:00
//阵法
tactical: GTactical;
//宠物列表
2023-11-06 02:25:02 +08:00
roles: TB.TbGRole[];
2023-11-01 02:01:35 +08:00
}
/**
*
*/
2023-11-01 19:07:15 +08:00
@ccclass('GOnHookMode')
2023-11-01 02:01:35 +08:00
export default class GOnHookMode extends GBaseMode<{}>{
2023-11-01 19:07:15 +08:00
@property(Prefab)
rolePrefab: Prefab = null;
2023-11-03 02:57:38 +08:00
//场景地图
@property(GMapLoop)
map1:GMapLoop;
@property(GMapLoop)
map2:GMapLoop;
@property(GMapLoop)
map3:GMapLoop;
@property(Node)
objects: Node = null;
get scene():Node{
return this.objects;
}
2023-11-01 19:07:15 +08:00
2023-11-01 02:01:35 +08:00
//玩家信息
2023-11-01 19:07:15 +08:00
playerInfo:GOnHookInfo;
//宠物信息
enemyInfo:GOnHookInfo;
//玩家宠物位置
2023-11-02 01:41:11 +08:00
playerPos: Vec2 = new Vec2(-400,0);
2023-11-01 19:07:15 +08:00
//怪物位置
2023-11-02 01:41:11 +08:00
enemyPos: Vec2 = new Vec2(400,0);
2023-11-01 02:01:35 +08:00
2023-11-01 19:07:15 +08:00
//玩家宠物
playerRoles: GRoleDefault[] = [];
//敌方宠物
enemyRoles: GRoleDefault[] = [];
2023-11-03 02:57:38 +08:00
//地图信息
2023-11-06 02:25:02 +08:00
mapInfo:TB.TbGMap;
2023-11-01 02:01:35 +08:00
2023-11-03 19:01:58 +08:00
//每一波怪的距离
2023-11-04 05:56:28 +08:00
everyX:number = 600;
2023-11-03 19:01:58 +08:00
//下一波怪的对战位置X
_nextFightX:number = 0;
get nextFightX(){return this._nextFightX}
set nextFightX(value:number){
//修改玩家阵法位置
this.playerInfo.tactical.setOffset(this.playerPos.clone().add(v2(value,0)))
2023-11-04 05:56:28 +08:00
this.enemyInfo.tactical.setOffset(this.enemyPos.clone().add(v2(value,0)))
2023-11-03 19:01:58 +08:00
this._nextFightX = value;
}
fsm:GFSMOnHookMode;
2023-11-05 03:26:09 +08:00
//是否允许攻击
isAllowAttack:boolean = false;
2023-11-01 02:01:35 +08:00
onSyncInitSuccess():void{
//初始化战斗
console.log("GOnHookMode 模式初始化");
2023-11-03 02:57:38 +08:00
//调整相机
let camreaPos = this.camera.node.worldPosition;
2023-11-15 02:32:00 +08:00
this.camera.node.worldPosition = v3(0,800,camreaPos.z)
2023-11-03 02:57:38 +08:00
2023-11-03 19:01:58 +08:00
//初始化状态机
this.fsm = new GFSMOnHookMode(this);
2023-11-03 02:57:38 +08:00
//初始化地图
2023-11-06 02:25:02 +08:00
this.mapInfo = TD.TbGMap.get(60001);
2023-11-15 02:32:00 +08:00
let scale = this.mapInfo.scale;
this.map1.init(app.battleRes.maps[60001][0],1,app.battleRes.maps[60001][0].width * scale,app.battleRes.maps[60001][0].height * scale);
this.map2.init(app.battleRes.maps[60001][1],1,app.battleRes.maps[60001][1].width * scale,app.battleRes.maps[60001][1].height * scale);
this.map3.init(app.battleRes.maps[60001][2],1,app.battleRes.maps[60001][1].width * scale,1048 * scale);
2023-11-04 05:56:28 +08:00
this.onUpdateMap(0);
2023-11-03 02:57:38 +08:00
2023-11-01 19:07:15 +08:00
this.playerInfo = { tactical: GTactical.getTactical().setOffset(this.playerPos), roles: GRoleUtil.getGRoles([10004,10004,10004,10004,10003,10003]) };
2023-11-02 01:41:11 +08:00
this.enemyInfo = { tactical: GTactical.getTactical(true).setOffset(this.enemyPos), roles: GRoleUtil.getGRoles([10002]) };
2023-11-01 02:01:35 +08:00
//生成玩家
2023-11-01 19:07:15 +08:00
this.playerInfo.roles.forEach((info,index) => this.onGenRole(GOnHookModePlayerEnum.PLAYER,index + 1,info))
2023-11-03 19:01:58 +08:00
// //生成敌人
// this.onResetGenerateEnemy();
2023-11-01 02:01:35 +08:00
}
2023-11-01 19:07:15 +08:00
onSyncUpdate(dt: number,frame:JNFrameInfo, input?: {}){
2023-11-03 19:01:58 +08:00
super.onSyncUpdate(dt,frame,input);
2023-11-04 05:56:28 +08:00
this.onUpdateMap(dt);
2023-11-03 19:01:58 +08:00
this.onUpdateCamera(dt);
this.fsm.onUpdate(dt,frame);
}
2023-11-04 05:56:28 +08:00
//更新地图
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);
}
2023-11-03 19:01:58 +08:00
//更新相机逻辑
onUpdateCamera(dt:number){
//如果没有敌人相机永远锁定最前面的宠物
2023-11-05 03:26:09 +08:00
if(this.isAllowAttack && this.isHaveEnemy()){
//如果有敌人则移动到战斗位置
let cameraWorld = this.camera.node.worldPosition.clone();
this.camera.node.worldPosition = cameraWorld.lerp(v3(this.nextFightX,cameraWorld.y,cameraWorld.z),(dt / 1000));
2023-11-03 19:01:58 +08:00
2023-11-05 03:26:09 +08:00
}else{
2023-11-03 19:01:58 +08:00
//获取冲到最前面的宠物
2023-11-04 05:56:28 +08:00
let roles = this.getOnesRoleAlive(GOnHookModePlayerEnum.PLAYER);
2023-11-03 19:01:58 +08:00
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));
}
2023-11-01 19:07:15 +08:00
}
//生成宠物
2023-11-06 02:25:02 +08:00
onGenRole(type: GOnHookModePlayerEnum,index:number,info:TB.TbGRole) {
2023-11-01 19:07:15 +08:00
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;
}
2023-11-03 19:01:58 +08:00
//获取存活的宠物
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());
}
2023-11-01 19:07:15 +08:00
//获取敌人
getEnumy(player:GRoleDefault,type:GOnHookModePlayerEnum):GRoleDefault{
2023-11-05 03:26:09 +08:00
if(!this.isAllowAttack) return null;
2023-11-01 19:07:15 +08:00
let enumyOnes = GOnHookModePlayerEnum.ENEMY
//如果是ENEMY 则 它的敌人是 PLAYER
if(type == GOnHookModePlayerEnum.ENEMY) enumyOnes = GOnHookModePlayerEnum.PLAYER
//获取敌人
2023-11-03 19:01:58 +08:00
let roles = this.getOnesRoleAlive(enumyOnes);
2023-11-01 19:07:15 +08:00
//返回敌人
//获取我在第几排
let playerXY = player.tactical.getXY(player.tacticalIndex);
//通过排数获取最近的敌人
2023-11-03 19:01:58 +08:00
let sort = roles.sort((enumy1,enumy2) => {
2023-11-01 19:07:15 +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]
}
//生成敌人
onResetGenerateEnemy(){
this.enemyRoles = [];
2023-11-02 01:41:11 +08:00
this.enemyInfo.roles.forEach((info,index) => this.onGenRole(GOnHookModePlayerEnum.ENEMY,index + 1,info))
2023-11-01 19:07:15 +08:00
}
//角色死亡回调
onRoleKillBack(role:GRoleDefault){
2023-11-03 02:57:38 +08:00
//死亡销毁
JNFrameTime.getInstance().setTimeout(() => {
if(role.isValid)
role.node.destroy()
},3000)
2023-11-03 19:01:58 +08:00
// //如果没有敌人则生成敌人
// if(this.getOnesRole(GOnHookModePlayerEnum.ENEMY).filter(role => !!role.get()).length <= 0){
// //生成敌人
// this.onResetGenerateEnemy();
// }
}
//是否有怪物
isHaveEnemy(){
2023-11-05 03:26:09 +08:00
if(!this.isAllowAttack) return [];
2023-11-03 19:01:58 +08:00
return this.getOnesRoleAlive(GOnHookModePlayerEnum.ENEMY).filter(role => !!role.get()).length > 0;
}
//前往下一个目标
onNextTarget(){
2023-11-04 05:56:28 +08:00
//下一个目标点
this.nextFightX = this.nextFightX + this.everyX;
2023-11-03 19:01:58 +08:00
2023-11-01 19:07:15 +08:00
}
2023-11-01 02:01:35 +08:00
}