394 lines
14 KiB
TypeScript
394 lines
14 KiB
TypeScript
|
|
||
|
import { UserMgr } from "db://assets/module_basic/scripts/UserMgr";
|
||
|
import { IHeroCfg } from "db://assets/module_basic/shared/configs/interface/IHeroCfg";
|
||
|
import { ISkinCfg } from "db://assets/module_basic/shared/configs/interface/ISkinCfg";
|
||
|
import { areanMgr } from "db://assets/module_arean/scripts/AreanManager";
|
||
|
import { tgxUIMgr } from "db://assets/core_tgx/tgx";
|
||
|
import {
|
||
|
EAreanBuyType,
|
||
|
EAreanItemType,
|
||
|
IAreanUserCard, IRewardData,
|
||
|
ItemAttributes,
|
||
|
SectType
|
||
|
} from "db://assets/module_basic/shared/protocols/public/arean/AreanTypeDef";
|
||
|
import { UICommonReward } from "db://assets/module_arean/ui_common_reward/UICommonReward";
|
||
|
import { ISectCfg } from "../../module_basic/shared/configs/interface/ISectCfg";
|
||
|
import { IPropCfg } from "../../module_basic/shared/configs/interface/IPropCfg";
|
||
|
import { _decorator, Component, Node, Label, Input, UITransform, view, v3, Prefab } from 'cc';
|
||
|
import {resLoader} from "db://assets/core_tgx/base/utils/ResLoader";
|
||
|
import {ModuleDef} from "db://assets/scripts/ModuleDef";
|
||
|
import {UICommonItemInfo} from "db://assets/module_arean/ui_common_item_info/UICommonItemInfo";
|
||
|
/************************************************************************************
|
||
|
|
||
|
* FileName : CommonFun.ts
|
||
|
* Description :
|
||
|
* Version : v1.0.0
|
||
|
* CreateTime : 2024-11-01 10:16:42
|
||
|
* Author :
|
||
|
* Copyright (c) since 2024
|
||
|
* ==============================================================
|
||
|
* Method Description:
|
||
|
*
|
||
|
* ==============================================================
|
||
|
************************************************************************************/
|
||
|
export class CommonFun {
|
||
|
private static _instance: CommonFun;
|
||
|
public static getInstance(): CommonFun {
|
||
|
|
||
|
return this._instance || (this._instance = new this());
|
||
|
}
|
||
|
|
||
|
// 检查皮肤是否属于哪个英雄英雄
|
||
|
public checkSkinBelongToHero(skinData: ISkinCfg, _heroLocalData: IHeroCfg[]): IHeroCfg {
|
||
|
let heroLocalData: IHeroCfg[] = areanMgr.cfgMgr.HeroDatas.getAllData();
|
||
|
let len = heroLocalData.length;
|
||
|
for (let i = 0; i < len; i++) {
|
||
|
let element = heroLocalData[i];
|
||
|
if (skinData.id === element.skinRes[0]) {
|
||
|
return element;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
// 检查英雄默认皮肤
|
||
|
public checkHeroDefaultSkin(heroData: IHeroCfg, skinLocalData: ISkinCfg[]): ISkinCfg {
|
||
|
// let skinLocalData : ISkinCfg[] = areanMgr.cfgMgr.SkinData.getAllData();
|
||
|
let len = skinLocalData.length;
|
||
|
for (let i = 0; i < len; i++) {
|
||
|
let element = skinLocalData[i];
|
||
|
if (element.id === heroData.skinRes[0]) {
|
||
|
return element;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
// 获取英雄所有皮肤
|
||
|
public getChooseHeroAllSkin(heroId: number, skinLocalData: ISkinCfg[]): ISkinCfg[] {
|
||
|
let tmp: ISkinCfg[] = [];
|
||
|
let localHeroData = areanMgr.cfgMgr.HeroDatas.getAllDataMap();
|
||
|
// let skinLocalData : ISkinCfg[] = areanMgr.cfgMgr.SkinData.getAllData();
|
||
|
let skinArr = localHeroData.get(heroId).skinRes
|
||
|
let len = skinArr.length;
|
||
|
for (let i = 0; i < len; i++) {
|
||
|
// tmp.push(skinLocalData[skinArr[i]]);
|
||
|
tmp.push(skinLocalData.find((v) => v.id === skinArr[i]));
|
||
|
}
|
||
|
return tmp;
|
||
|
}
|
||
|
|
||
|
// 是否有此英雄
|
||
|
public async checkIsHaveHero(heroId: number): Promise<boolean> {
|
||
|
let userInfo = await UserMgr.inst.rpc_GetUserInfo(UserMgr.inst.userId)
|
||
|
// @ts-ignore
|
||
|
return userInfo.hero.includes(heroId);
|
||
|
}
|
||
|
|
||
|
// 是否有此皮肤
|
||
|
public async checkIsHaveSkin(skinId: number): Promise<boolean> {
|
||
|
let userInfo = await UserMgr.inst.rpc_GetUserInfo(UserMgr.inst.userId)
|
||
|
// @ts-ignore
|
||
|
return userInfo.skin.includes(heroId);
|
||
|
}
|
||
|
|
||
|
// 计算卡牌数量
|
||
|
public CalCardCount(quality: number, cardList: IAreanUserCard[]): number {
|
||
|
let count = 0;
|
||
|
let len = cardList.length;
|
||
|
let data = areanMgr.cfgMgr.CardDatas;
|
||
|
for (let i = 0; i < len; i++) {
|
||
|
const rare = data.getData(cardList[i].id).cardRare;
|
||
|
if (rare == quality) {
|
||
|
count++;
|
||
|
}
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
|
|
||
|
// 获取羁绊的数量
|
||
|
public getSectCount(type: SectType, data: any): number {
|
||
|
let count = 0;
|
||
|
for (const typeKey in data) {
|
||
|
if (type.toString() == typeKey) {
|
||
|
count = data[typeKey];
|
||
|
}
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
|
|
||
|
// 获取羁绊的数据
|
||
|
public getSectData(type: SectType): ISectCfg[] {
|
||
|
let arr: ISectCfg[] = [];
|
||
|
let localData = areanMgr.cfgMgr.SectData.getAllData();
|
||
|
for (let i = 0; i < localData.length; i++) {
|
||
|
if (Math.floor(localData[i].id / 100) == type) {
|
||
|
arr.push(localData[i]);
|
||
|
}
|
||
|
}
|
||
|
return arr;
|
||
|
}
|
||
|
// 计算段位信息
|
||
|
public calDanData(star: number): { curDan: number, mainDan: number, needStar: number, curDanStar: number } {
|
||
|
let data = areanMgr.cfgMgr.DanData.getAllData();
|
||
|
let len = data.length;
|
||
|
let danInfo: any = {
|
||
|
curDan: -1,
|
||
|
mainDan: -1,
|
||
|
needStar: -1,
|
||
|
curDanStar: star
|
||
|
};
|
||
|
if (star > data[len - 2].allStar) {
|
||
|
// 越界
|
||
|
} else {
|
||
|
if (star === 0) {
|
||
|
danInfo.curDan = 1;
|
||
|
danInfo.mainDan = 1;
|
||
|
danInfo.needStar = data[0].allStar;
|
||
|
} else {
|
||
|
for (let i = 0; i < len; i++) {
|
||
|
if (star <= data[i].allStar) {
|
||
|
danInfo.curDan = i + 1;
|
||
|
danInfo.mainDan = data[i].mainDan;
|
||
|
danInfo.needStar = data[i].allStar;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return danInfo;
|
||
|
}
|
||
|
|
||
|
// 展示通用奖励
|
||
|
public showCommonReward(reward : IRewardData[]) : void {
|
||
|
tgxUIMgr.inst.showUI(UICommonReward,null,null,reward);
|
||
|
}
|
||
|
|
||
|
// 根据道具id获取道具图标
|
||
|
public getIcon(itemId: number): string {
|
||
|
let propData: IPropCfg = areanMgr.cfgMgr.PropData.getData(itemId);
|
||
|
let propType: EAreanItemType = propData.type
|
||
|
if (!propData) {
|
||
|
console.warn("没有找到相应的数据 === ", itemId);
|
||
|
return "";
|
||
|
}
|
||
|
switch (propType) {
|
||
|
case EAreanItemType.None: {
|
||
|
return "";
|
||
|
}
|
||
|
case EAreanItemType.Currency: {
|
||
|
return `res/Currency/${propData.icon}`;
|
||
|
}
|
||
|
case EAreanItemType.Hero: {
|
||
|
return "";
|
||
|
}
|
||
|
case EAreanItemType.Skin: {
|
||
|
return "";
|
||
|
}
|
||
|
case EAreanItemType.PlayerDecoration: {
|
||
|
return `icons/${propData.icon}`
|
||
|
}
|
||
|
case EAreanItemType.HeroDecoration: {
|
||
|
return "";
|
||
|
}
|
||
|
case EAreanItemType.Token: {
|
||
|
return "";
|
||
|
}
|
||
|
case EAreanItemType.Box: {
|
||
|
return `res/Iamge/Item/${propData.icon}`;
|
||
|
}
|
||
|
case EAreanItemType.Item: {
|
||
|
return "";
|
||
|
}
|
||
|
default:
|
||
|
return ""
|
||
|
}
|
||
|
}
|
||
|
|
||
|
HighlightNumbers(desc: string) {
|
||
|
// 处理 [t=数字] 格式
|
||
|
const regexT = /\[t=(\d+)\]/g;
|
||
|
const matchesT = desc.match(regexT);
|
||
|
|
||
|
if (matchesT) {
|
||
|
const skillSpeed = areanMgr.BattleView.getLocalPlayerData().attr.skillSpeed;
|
||
|
|
||
|
matchesT.forEach(match => {
|
||
|
const number = parseInt(match.slice(3, -1), 10); // 提取数字
|
||
|
let newNumber = number;
|
||
|
|
||
|
if (skillSpeed !== 0) {
|
||
|
newNumber = parseInt(Math.max(1, number / (1 + skillSpeed * 0.1)).toFixed(0)); // 减去技能速度值
|
||
|
}
|
||
|
|
||
|
// 高亮数字
|
||
|
if (newNumber != number) {
|
||
|
const highlightedNumber = `<color=green>${newNumber}</color>`;
|
||
|
desc = desc.replace(match, highlightedNumber); // 替换原数字
|
||
|
}
|
||
|
else {
|
||
|
const highlightedNumber = `${newNumber}`;
|
||
|
desc = desc.replace(match, highlightedNumber); // 替换原数字
|
||
|
}
|
||
|
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 处理 [s=数字] 格式
|
||
|
const regexS = /\[s=(\d+)\]/g;
|
||
|
const matchesS = desc.match(regexS);
|
||
|
|
||
|
if (matchesS) {
|
||
|
matchesS.forEach(match => {
|
||
|
const spAttack = areanMgr.BattleView.getLocalPlayerData().attr.spAttack;
|
||
|
|
||
|
const number = parseInt(match.slice(3, -1), 10); // 提取数字
|
||
|
|
||
|
let newNumber = number;
|
||
|
|
||
|
if (spAttack !== 0) {
|
||
|
newNumber += spAttack; // 增加法强
|
||
|
}
|
||
|
|
||
|
|
||
|
if (newNumber != number) {
|
||
|
const highlightedNumber = `<color=green>${newNumber}</color>`;
|
||
|
desc = desc.replace(match, highlightedNumber); // 替换原数字
|
||
|
}
|
||
|
else {
|
||
|
const highlightedNumber = `${newNumber}`;
|
||
|
desc = desc.replace(match, highlightedNumber); // 替换原数字
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 处理 [b=数字] 格式
|
||
|
const regexB = /\[b=(\d+)\]/g;
|
||
|
const matchesB = desc.match(regexB);
|
||
|
|
||
|
if (matchesB) {
|
||
|
matchesB.forEach(match => {
|
||
|
const buffAdd = areanMgr.BattleView.getLocalPlayerData().attr.buffAdd;
|
||
|
|
||
|
|
||
|
const number = parseInt(match.slice(3, -1), 10); // 提取数字
|
||
|
let newNumber = number;
|
||
|
if (buffAdd !== 0) {
|
||
|
newNumber = Math.floor(newNumber * (1 + buffAdd * 0.1)); // 增加增效
|
||
|
}
|
||
|
|
||
|
if (newNumber != number) {
|
||
|
const highlightedNumber = `<color=green>${newNumber}</color>`;
|
||
|
desc = desc.replace(match, highlightedNumber); // 替换原数字
|
||
|
}
|
||
|
else {
|
||
|
const highlightedNumber = `${newNumber}`;
|
||
|
desc = desc.replace(match, highlightedNumber); // 替换原数字
|
||
|
}
|
||
|
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 处理 [b=数字] 格式
|
||
|
const regexH = /\[h=(\d+)\]/g;
|
||
|
const matchesH = desc.match(regexH);
|
||
|
|
||
|
if (matchesH) {
|
||
|
matchesH.forEach(match => {
|
||
|
const healAdd = areanMgr.BattleView.getLocalPlayerData().attr.healAdd;
|
||
|
|
||
|
|
||
|
const number = parseInt(match.slice(3, -1), 10); // 提取数字
|
||
|
let newNumber = number;
|
||
|
if (healAdd !== 0) {
|
||
|
newNumber += healAdd; // 增加治愈值
|
||
|
}
|
||
|
|
||
|
if (newNumber != number) {
|
||
|
const highlightedNumber = `<color=green>${newNumber}</color>`;
|
||
|
desc = desc.replace(match, highlightedNumber); // 替换原数字
|
||
|
}
|
||
|
else {
|
||
|
const highlightedNumber = `${newNumber}`;
|
||
|
desc = desc.replace(match, highlightedNumber); // 替换原数字
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
// 替换其他文本为黄色
|
||
|
const keywords = ["暴击", "闪避", "反伤"];
|
||
|
keywords.forEach(keyword => {
|
||
|
const keywordRegex = new RegExp(`\\b${keyword}\\b`, "g");
|
||
|
desc = desc.replace(keywordRegex, `<color=F02020DC>${keyword}</color>`);
|
||
|
});
|
||
|
return desc;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 更新道具表
|
||
|
*/
|
||
|
public updateItemList(item: ItemAttributes): void {
|
||
|
let curItemList: ItemAttributes[] = UserMgr.inst.userInfo.itemList;
|
||
|
let data: ItemAttributes = curItemList.find((itemAttr: ItemAttributes) => { return itemAttr.itemId == item.itemId });
|
||
|
if (data) {
|
||
|
data.itemCount = item.itemCount;
|
||
|
} else {
|
||
|
curItemList.push(item);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** 更新货币 */
|
||
|
public updateCurrey(reward : {
|
||
|
itemId : number; // 物品id
|
||
|
count : number; // 物品数量
|
||
|
}[]) : void{
|
||
|
if(reward && Array.isArray(reward)){
|
||
|
let len : number = reward.length;
|
||
|
for (let i : number = 0; i < len; i++) {
|
||
|
let element : {
|
||
|
itemId : number; // 物品id
|
||
|
count : number; // 物品数量
|
||
|
} = reward[i];
|
||
|
if(element.itemId == EAreanBuyType.Coin){
|
||
|
UserMgr.inst.userInfo.coin += element.count;
|
||
|
} else if (element.itemId == EAreanBuyType.Diamond) {
|
||
|
UserMgr.inst.userInfo.diamond += element.count;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** 道具是否存在 */
|
||
|
public itemIsExist(itemId : number) : ItemAttributes{
|
||
|
let propData : IPropCfg = areanMgr.cfgMgr.PropData.getData(itemId);
|
||
|
if(!propData) {
|
||
|
console.warn("没有找到相应的数据 === ",itemId);
|
||
|
return null;
|
||
|
}
|
||
|
return UserMgr.inst.userInfo.itemList.find((item : ItemAttributes) => item.itemId == itemId);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 展示道具信息
|
||
|
* @param target 响应点击事件的节点
|
||
|
* @param itemId 道具ID(string)
|
||
|
*/
|
||
|
public showItemInfo(target : Node,itemId : number) : void{
|
||
|
let itemData = areanMgr.cfgMgr.PropData.getData(itemId);
|
||
|
if(itemData){
|
||
|
target.off(Input.EventType.TOUCH_START);
|
||
|
target.off(Input.EventType.TOUCH_END);
|
||
|
target.off(Input.EventType.TOUCH_CANCEL);
|
||
|
let start = (event) => {
|
||
|
let _p = event.target.parent.getComponent(UITransform).convertToWorldSpaceAR(v3(event.target.position.x, event.target.position.y))
|
||
|
tgxUIMgr.inst.showUI(UICommonItemInfo,null,null,{itemId : itemId,position: _p});
|
||
|
}
|
||
|
let end = () => {
|
||
|
}
|
||
|
let canceled = () => {
|
||
|
}
|
||
|
target.on(Input.EventType.TOUCH_START, end, this);
|
||
|
target.on(Input.EventType.TOUCH_END, start, this);
|
||
|
target.on(Input.EventType.TOUCH_CANCEL, end, this);
|
||
|
}
|
||
|
}
|
||
|
}
|