235 lines
6.5 KiB
TypeScript
Raw Normal View History

2023-11-17 18:29:39 +08:00
import { GOnHookPet } from "../../../../../extensions/ngame/assets/ngame/message/proto";
2023-12-28 02:56:34 +08:00
import NGameUtil from "../../../../../extensions/ngame/assets/ngame/util/NGameUtil";
import { NSystemEvent } from "../../../../../extensions/ngame/assets/ngame/util/NSystem";
2023-11-17 18:29:39 +08:00
import Singleton from "../../../../../extensions/ngame/assets/ngame/util/Singleton";
2023-12-28 02:56:34 +08:00
import { TD, app } from "../../../App";
2023-12-05 01:43:43 +08:00
import GBattleModeManager, { BattleMode } from "../../../battle/GBattleModeManager";
2023-12-28 02:56:34 +08:00
import { TB } from "../../../config/data/schema";
import { API, PlayerPetOV } from "../../../consts/API";
2023-11-17 18:29:39 +08:00
import { GAPI } from "../../../consts/GAPI";
2023-12-03 03:31:19 +08:00
import GOnHookData from "../../../data/GOnHookData";
2023-11-17 18:29:39 +08:00
import PlayerPetData from "../../../data/PlayerPetData";
import { GUI } from "../../../ui/UIConfig";
export enum GOnHookManagerEvent{
//添加死亡野怪
ADD_KILL_SREEP = "GOnHookManagerEvent_ADD_KILL_SREEP",
//删除死亡野怪
2023-12-03 03:31:19 +08:00
DEL_KILL_SREEP = "GOnHookManagerEvent_DEL_KILL_SREEP",
2023-12-05 01:43:43 +08:00
//重置数据
RESET_DATA = "GOnHookManagerEvent_RESET_DATA",
2023-12-03 03:31:19 +08:00
//删除死亡野怪
2023-12-28 02:56:34 +08:00
UPDATE_MAP = "GOnHookManagerEvent_UPDATE_MAP",
//更新挂机状态
UPDATE_ON_HOOK_STATE = "GOnHookManagerEvent_UPDATE_ON_HOOK_STATE"
2023-11-17 18:29:39 +08:00
}
//游戏模式 OnHook 管理器
export default class GOnHookManager extends Singleton{
2023-12-26 03:55:58 +08:00
isSpawn:boolean = false;
2023-11-17 18:29:39 +08:00
//野怪列表
_sreeps:Map<string,GOnHookPet> = new Map();
get sreeps(){
//如果没有野怪了 则 向服务器生成
if(!this._sreeps || !(this._sreeps.size)){
//生成
this.onSpawnSreeps();
2023-12-05 01:43:43 +08:00
return new Map();
2023-11-17 18:29:39 +08:00
}
return this._sreeps;
}
set sreeps(data:Map<string,GOnHookPet>){
this._sreeps = data;
}
//已经死亡的野怪列表
killSreeps:GOnHookPet[] = [];
2023-12-28 02:56:34 +08:00
//-------------------------- 挂机 ---------------------------------
//需要捕捉的宠物
onHookCatchPets:TB.TbGRole[] = [];
//需要主动吞噬的宠物 (主动吞噬其他 0星 宠物 升星)
onHookEngulfPets:PlayerPetOV[] = [];
//是否挂机
_isOnHook:boolean = false;
get isOnHook(){return this._isOnHook}
set isOnHook(value:boolean){
this._isOnHook = value;
//通知更新挂机状态
app.event.emit(GOnHookManagerEvent.UPDATE_ON_HOOK_STATE);
}
init(){
app.event.on(NSystemEvent.UPDATE,this.onUpdate,this);
}
destroy(): void {
app.event.off(NSystemEvent.UPDATE,this.onUpdate,this);
}
onUpdate(){
this.onUpdateOnHook();
}
//更新挂机
onUpdateOnHook = NGameUtil.ThrottleASync((async () => {
//如果是挂机 则 出售不需要捕捉的宠物
if(!this.isOnHook) return;
//捕捉 和 出售
let pet = this.killSreeps[0];
if(pet){
if(this.onHookCatchPets.indexOf(TD.TbGRole.get(pet.petTbId)) >= 0){
//捕捉
await this.onCatchCreeps(pet)
}else{
//出售
await this.onSellCreeps(pet)
}
}
//自动吞噬
for (const item of this.onHookEngulfPets) {
//获取可吞噬的宠物
let engulfs = PlayerPetData.getIns().getData()
.filter(value => (value.petStar || 0) == 0 && item.petTbId == value.petTbId && item.petId != value.petId)
.map(value => value.petId);
//吞噬
if(engulfs.length){
await API.PetUpStar(item.petId,engulfs);
}
}
}).bind(this))
2023-11-17 18:29:39 +08:00
//生成野怪
2024-01-03 15:51:16 +08:00
onSpawnSreeps = NGameUtil.ThrottleASync((async () => {
2023-11-17 18:29:39 +08:00
GAPI.GOnHookSpawnCreeps().then(data => {
data.pets.forEach(pet => {
this._sreeps.set(pet.key,pet as GOnHookPet);
})
});
2024-01-03 15:51:16 +08:00
}).bind(this))
2023-11-17 18:29:39 +08:00
//获取下一只野怪
getNextCreeps(){
if(!this.sreeps) return;
2023-12-25 02:06:56 +08:00
let creeps = this.sreeps.get(this.sreeps.keys().next()?.value);
this.sreeps.delete(creeps.key);
2023-11-17 18:29:39 +08:00
return creeps;
}
//野怪死亡
onKillSreeps(creeps:GOnHookPet){
this.sreeps.delete(creeps.key);
//记入死亡
//判断是否在死亡中如果在则跳出
if(this.killSreeps.indexOf(creeps) > -1){
return;
}
this.killSreeps.push(creeps);
//通知添加死亡野怪
app.event.emit(GOnHookManagerEvent.ADD_KILL_SREEP,creeps);
console.log(`GOnHookManager : ${creeps.key} 宠物死亡 可进行捕捉 出售`);
}
//捕捉野怪
async onCatchCreeps(creeps:GOnHookPet):Promise<boolean>{
let index;
if((index = this.killSreeps.indexOf(creeps)) < 0){
2023-11-20 03:47:00 +08:00
app.layer.Open(GUI.Tips,{text:"不可捕捉该野怪"});
2023-11-17 18:29:39 +08:00
return false;
}
//删除
this.killSreeps.splice(index,1);
//通知添加野怪被删除
app.event.emit(GOnHookManagerEvent.DEL_KILL_SREEP,creeps);
let pet:PlayerPetOV = await GAPI.GOnHookCatchCreeps(creeps.key);
//保存宠物
if(pet){
PlayerPetData.getIns().addPet(pet);
}
return !!pet;
}
2023-11-20 03:47:00 +08:00
//出售野怪
async onSellCreeps(creeps:GOnHookPet):Promise<boolean>{
let index;
if((index = this.killSreeps.indexOf(creeps)) < 0){
app.layer.Open(GUI.Tips,{text:"不可出售当前野怪"});
return false;
}
//删除
this.killSreeps.splice(index,1);
app.event.emit(GOnHookManagerEvent.DEL_KILL_SREEP,creeps);
2023-11-28 02:38:46 +08:00
// //通知添加野怪被删除
// if(await GAPI.GOnHookSellCreeps(creeps.key)){
// return true;
// }else{
// }
2023-11-20 03:47:00 +08:00
return await GAPI.GOnHookSellCreeps(creeps.key);
}
2023-12-05 01:43:43 +08:00
//重置数据
onResetData(){
this._sreeps = new Map();
this.killSreeps = [];
app.event.emit(GOnHookManagerEvent.RESET_DATA);
}
2023-12-25 02:06:56 +08:00
//下一关
async onNextLevel(){
2024-01-03 15:51:16 +08:00
await GOnHookData.getIns().onNextLevel();
2023-12-25 02:06:56 +08:00
this.onResetMap();
}
2024-01-03 15:51:16 +08:00
//战胜当前关卡
async onWinLevel(){
await GOnHookData.getIns().onWinLevel();
}
2023-12-03 03:31:19 +08:00
//切换场景
async setMap(mapId:number){
await GOnHookData.getIns().setMap(mapId);
2023-12-25 02:06:56 +08:00
this.onResetMap();
2023-12-03 03:31:19 +08:00
2023-12-25 02:06:56 +08:00
}
//重置场景
onResetMap(){
2023-12-03 03:31:19 +08:00
//通知地图已切换
app.event.emit(GOnHookManagerEvent.UPDATE_MAP);
2023-12-05 01:43:43 +08:00
//重置数据
this.onResetData();
//重置场景
GBattleModeManager.getIns().Open(BattleMode.OnHook,true);
2023-12-03 03:31:19 +08:00
}
2023-11-17 18:29:39 +08:00
}