137 lines
4.1 KiB
TypeScript
137 lines
4.1 KiB
TypeScript
import { deepCopy } from "../../../common/net/CovertUtil";
|
|
|
|
//游戏中的物体对象,属性对象,聚合对象,值对象,升级对象
|
|
export enum objType{
|
|
None = 0,
|
|
//AggregationType = 1 << 1,//聚合类型
|
|
value = 1, //值类型
|
|
compose = 2, //组合类型
|
|
}
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
export class propBase{
|
|
protected eType:number = 0;//空对象
|
|
constructor(eType?:number){
|
|
this.eType = eType || 0;
|
|
}
|
|
getType(){return this.eType}
|
|
setType(tp:number){this.eType = tp}
|
|
}
|
|
|
|
export class baseObj{
|
|
protected eType:objType = objType.None;//空对象
|
|
protected id:number = 0;//物品唯一标识
|
|
protected name:string = "";//名字
|
|
|
|
protected propList:Map<number,propBase> = new Map<number,propBase>();//可扩展的功能
|
|
constructor(id?:number,name?:string){
|
|
this.id = id || 0;
|
|
this.name = name || "";
|
|
}
|
|
getName(){return this.name}
|
|
getId(){return this.id}
|
|
getObjType(){return this.eType}
|
|
//bDeep表示获取所有
|
|
getPropByType(eType:number){
|
|
// 使用对象解析
|
|
for (let [idkey, valueObj] of this.propList) {
|
|
if(idkey == eType)
|
|
return valueObj;
|
|
}
|
|
}
|
|
addProp(prop:propBase){
|
|
this.propList.set(prop.getType(),deepCopy(prop));
|
|
}
|
|
delProp(tp:number){
|
|
return this.propList.delete(tp);
|
|
}
|
|
//获取所有子对象属性
|
|
getPropDeep(propType:number){
|
|
let propList = Array<propBase>()
|
|
let selfProp = this.getPropByType(propType)
|
|
if (selfProp)
|
|
propList.push(selfProp)
|
|
|
|
return propList
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//组合类型
|
|
export class composeObj extends baseObj{
|
|
protected objMapList:Map<number,baseObj> = new Map<number,baseObj>();//id,--聚合
|
|
constructor(id?:number,name?:string){
|
|
super(id,name);
|
|
this.eType = objType.compose;
|
|
}
|
|
//
|
|
getMemberMap(){
|
|
return this.objMapList
|
|
}
|
|
//判断是否有某个对象
|
|
getMember(id:number){
|
|
if(this.getId()==id)
|
|
return this;
|
|
else
|
|
{
|
|
// 使用对象解析
|
|
return this.objMapList.get(id)
|
|
}
|
|
}
|
|
//添加成员
|
|
addMember(id:number,obj:baseObj){
|
|
this.objMapList.set(id,obj);
|
|
}
|
|
//删除成员
|
|
delMember(id:number):boolean{
|
|
return this.objMapList.delete(id);
|
|
}
|
|
getMemberLookDown(id:number):[number,baseObj]|undefined{//获取序号
|
|
// 使用对象解析
|
|
let index = 0
|
|
for (let [idkey, valueObj] of this.objMapList) {
|
|
index++;
|
|
if(idkey == id)
|
|
{
|
|
return [index,valueObj];
|
|
}
|
|
else if(valueObj.getObjType()==objType.compose)
|
|
{
|
|
let result = (valueObj as composeObj).getMemberLookDown(id)
|
|
if(result)
|
|
return result
|
|
}
|
|
}
|
|
}
|
|
//获取所有子对象属性
|
|
getPropDeep(propType:number){
|
|
let propList = super.getPropDeep(propType)
|
|
for (let [idkey, valueObj] of this.objMapList)
|
|
{
|
|
let childPropList = (valueObj as baseObj).getPropDeep(propType)
|
|
propList = propList.concat(childPropList)
|
|
}
|
|
return propList
|
|
}
|
|
}
|
|
|
|
//数量类对象
|
|
export class valueObj extends baseObj{
|
|
protected count:number = 0
|
|
constructor(id?:number,name?:string){
|
|
super(id,name);
|
|
this.eType = objType.value;
|
|
}
|
|
|
|
setCount(num:number){
|
|
this.count = Number(num)
|
|
}
|
|
getCount(){return this.count}
|
|
changeCount(num:number){
|
|
if(this.count==NaN)
|
|
this.count = 0
|
|
this.count += Number(num)
|
|
}
|
|
} |