squidGame/tgx-games-client/assets/module_basic/red/RedPointTree.ts

310 lines
8.0 KiB
TypeScript
Raw Normal View History

2025-02-17 21:36:37 +08:00
import { RedPointInfo } from "./RedPointInfo";
import { IRedPointData } from "./IRedPointData";
import { RedCondEvent } from "./RedPointCoust";
export class RedPointTree {
/**
*
*/
public info: RedPointInfo = null;
/**
*
*/
public children: Map<string, RedPointTree> = new Map<string, RedPointTree>();
// /**
// * 是否绑定节点
// */
// public isBind: boolean = false;
/**
*
*/
private showType: number = 0;
// public showCount: number = 0;
/**
* id唯一值
*/
public id: string;
// public isBind: boolean = false;
public data: IRedPointData = null;
//自身是否有逻辑控制红点
public isSelfControl: boolean = false;
//是否要显示自身红点(不依赖子红点)
public isSelfShowRed: boolean = false;
public mCondition: Array<IRedTreeCond> = [];
public callFunc: Function = null;
/**
*
*/
public stepTag: number = 0;
/**
*
* @param info
* @param showType
* @param key
* @param isBind
*/
initData(key: string, data: IRedPointData) {
this.id = key;
this.data = data;
}
public getChildren(key: string): RedPointTree {
let children = this.children.get(key);
if (children != null) {
return children;
}
return null;
}
/**
*
* @param info
*/
public bindInfo(info: RedPointInfo, showType, isSelfControl: boolean = false) {
this.info = info;
this.showType = showType;
this.isSelfControl = isSelfControl;
console.log("updateRedPoint 绑定脚本" + this.data.mId);
this.updateRedPoint(this.data.mShowType >= 0 ? true : false, this.data.mShowCount);
}
/**
* Key值对应的对象
* @param _key
* @returns
*/
public findChild(_key: string) {
for (let [key, item] of this.children.entries()) {
if (item.id == _key) {
return item;
}
if (item) {
let childItem = item.findChild(_key);
if (childItem) {
return childItem;
}
}
}
return null;
}
/**
*
* @param key
* @returns
*/
public getOrAddChild(key: string,callFunc:Function): RedPointTree {
let children = this.children.get(key);
if (children != null) {
return children;
}
children = this.addChild(key,callFunc);
return children;
}
/**
* Id创建节点
* @param key Id
* @returns
*/
public addChild(key: string,callFunc:Function) {
let children = this.children.get(key);
if (children != null) {
return children;
}
children = new RedPointTree();
let data: IRedPointData = {
mId: key,
mName: key,
mFunctionId: 0,
mParent: null,
mRedNode: null,
mParentNode: null,
mRefreshType: 0,
mShowCount: 0,
mShowType: -1
};
children.callFunc = callFunc;
children.initData(key, data);
this.children.set(key, children);
return children;
}
setRedSelf(value: boolean) {
if (this.isSelfControl) {
this.isSelfShowRed = value;
}
}
/**
*
* @param isShow
* @param showCount
* @returns
*/
updateRedPoint(isShow: boolean, showCount: number) {
// this.stepTag+= isShow?1:-1;
let _showType: number = isShow ? this.showType : -1;
if (this.isSelfControl) {
if (this.isSelfShowRed) {
_showType = this.showType
} else {
// 更新红点
if (this.children.size > 0) {
for (let [key, value] of this.children) {
if (value.data.mShowType != -1) {
_showType = this.showType
break
}
}
}
}
} else {
// 更新红点
if (this.children.size > 0) {
for (let [key, value] of this.children) {
if (value.data.mShowType != -1) {
_showType = this.showType
break
}
}
}
}
this.data.mShowType = _showType;
this.data.mShowCount = showCount;
this.updateInfo();
if (this.data.mParent) {
if (isShow) {
if (this.data.mParent.data.mShowType >= 0) return;
} else {
if (this.data.mParent.data.mShowType < 0) return;
}
this.data.mParent.updateRedPoint(isShow, showCount);
}
}
private updateInfo() {
if (this.data.mRedNode && this.data.mRedNode.active && this.data.mRedNode.isValid) {
// console.log("updateRedPoint" + this.data.mId, this.data.mRedNode);
// this.info.updateRedPoint(this.data.mShowType, showCount);
// this.data.mRedNode.getChildByName('RedPoint').getComponent(RedPointInfo).updateRedPoint(this.data.mShowType, this.data.mShowCount);
this.data.mRedNode.getComponentInChildren(RedPointInfo).updateRedPoint(this.data.mShowType, this.data.mShowCount);
}
}
/**
*
* @param cond
*/
public addCond(cond: IRedTreeCond) {
this.mCondition.push(cond);
this.updateCondCount(cond)
}
/**
*
* @param event
* @param count
*/
public updateCondCount(event: IRedTreeCond) {
let condIndex = this.mCondition.find(p => p.condEvent == event.condEvent && p.condId == event.condId && event.condType == p.condType);
if (condIndex != null) {
condIndex.currValue = event.currValue;
if (event.condValue != 0) {
condIndex.condValue = event.condValue;
}
this.updateCond();
}
}
/**
*
* @param cond
*/
public removeCond(redEvent: string) {
let condIndex = this.mCondition.find(p => p.condEvent == redEvent);
if (condIndex != null) {
let index = this.mCondition.indexOf(condIndex);
this.mCondition.splice(index, 1);
}
this.updateCond();
}
/**
*
*/
refresh() {
let isShow = true;
for (let i = 0; i < this.mCondition.length; i++) {
let item = this.mCondition[i];
if (item.condValue > item.currValue) {
isShow = false;
break;
}
}
this.data.mShowType = isShow ? this.showType : -1;
this.updateInfo();
}
/**
*
*/
private updateCond() {
let cond = this.callFunc==null?false: this.callFunc();
if (this.data.mShowType < 0) {
if (cond) {
this.updateRedPoint(true, this.data.mShowCount);
}
} else {
if (!cond) {
this.updateRedPoint(false, this.data.mShowCount);
}
}
}
}
export interface IRedTreeCond {
/**
* Id
*/
condId: number
/**
*
*/
condType: number
/**
*
*/
condValue: number
/**
*
*/
condEvent: RedCondEvent;
/**
*
*/
currValue: number;
}