213 lines
7.1 KiB
TypeScript
213 lines
7.1 KiB
TypeScript
import { _decorator, Component, Node, resources, Sprite, SpriteAtlas } from 'cc';
|
|
import { oops } from 'db://oops-framework/core/Oops';
|
|
import { GameEvent } from '../common/config/GameEvent';
|
|
import { smc } from '../common/SingletonModuleComp';
|
|
import { EquipInfo, EquipType, EquipAttr, EquipAttrTarget } from '../common/config/Equips';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('EquipsComp')
|
|
export class EquipsComp extends Component {
|
|
weapon:any=null
|
|
armor:any=null
|
|
accessory:any=null
|
|
skill1:any=null
|
|
skill2:any=null
|
|
skill3:any=null
|
|
boxs:Node=null
|
|
attrs:any={
|
|
hero:{
|
|
ap:0,
|
|
hp:0,
|
|
atk_count:0,
|
|
atk_speed:0,
|
|
skill_dmg:0,
|
|
skill_cd:0,
|
|
},
|
|
ally:{
|
|
ap:0,
|
|
hp:0,
|
|
atk_count:0,
|
|
atk_speed:0,
|
|
skill_dmg:0,
|
|
skill_cd:0,
|
|
},
|
|
enemy:{
|
|
ap:0,
|
|
hp:0,
|
|
atk_count:0,
|
|
atk_speed:0,
|
|
skill_dmg:0,
|
|
skill_cd:0,
|
|
},
|
|
friend:{
|
|
ap:0,
|
|
hp:0,
|
|
atk_count:0,
|
|
atk_speed:0,
|
|
skill_dmg:0,
|
|
skill_cd:0,
|
|
}
|
|
}
|
|
/** 视图层逻辑代码分离演示 */
|
|
onLoad() {
|
|
oops.message.on(GameEvent.FightReady,this.fight_ready,this)
|
|
oops.message.on(GameEvent.EquipAdd,this.equip_add,this)
|
|
oops.message.on(GameEvent.EquipRemove,this.equip_remove,this)
|
|
this.boxs=this.node.getChildByName("boxs")
|
|
}
|
|
start(){
|
|
this.fight_ready()
|
|
}
|
|
fight_ready(){
|
|
this.boxs.getChildByName("weapon").getChildByName("icon").active=false
|
|
this.boxs.getChildByName("armor").getChildByName("icon").active=false
|
|
this.boxs.getChildByName("accessory").getChildByName("icon").active=false
|
|
this.weapon={
|
|
uuid:2001,
|
|
name:"weapon",
|
|
type:"weapon",
|
|
level:0,
|
|
}
|
|
this.armor={
|
|
uuid:2002,
|
|
name:"armor",
|
|
type:"armor",
|
|
level:0,
|
|
}
|
|
this.accessory={
|
|
uuid:0,
|
|
name:"accessory",
|
|
type:"accessory",
|
|
level:0,
|
|
}
|
|
this.count_attrs()
|
|
}
|
|
equip_add(e:GameEvent,data:any){
|
|
console.log("equip_add",data)
|
|
if(data.type==EquipType.WEAPON){
|
|
this.weapon.uuid=data.uuid
|
|
this.weapon.name=data.name
|
|
this.weapon.type=data.type
|
|
this.weapon.level=data.level
|
|
this.show_weapon(data.uuid)
|
|
}
|
|
if(data.type==EquipType.ARMOR){
|
|
this.armor.uuid=data.uuid
|
|
this.armor.name=data.name
|
|
this.armor.type=data.type
|
|
this.armor.level=data.level
|
|
this.show_armor(data.uuid)
|
|
}
|
|
if(data.type==EquipType.ACCESSORY){
|
|
this.accessory.uuid=data.uuid
|
|
this.accessory.name=data.name
|
|
this.accessory.type=data.type
|
|
this.accessory.level=data.level
|
|
this.show_accessory(data.uuid)
|
|
}
|
|
}
|
|
show_weapon(uuid:number){
|
|
let icon = this.node.getChildByName("boxs").getChildByName("weapon").getChildByName("icon")
|
|
icon.active=true
|
|
var icon_path = "game/heros/equips"
|
|
resources.load(icon_path, SpriteAtlas, (err: any, atlas) => {
|
|
const sprite = icon.getChildByName("icon").getComponent(Sprite);
|
|
sprite.spriteFrame = atlas.getSpriteFrame(uuid.toString());
|
|
});
|
|
|
|
}
|
|
show_armor(uuid:number){
|
|
let icon = this.node.getChildByName("boxs").getChildByName("armor").getChildByName("icon")
|
|
icon.active=true
|
|
var icon_path = "game/heros/equips"
|
|
resources.load(icon_path, SpriteAtlas, (err: any, atlas) => {
|
|
const sprite = icon.getChildByName("icon").getComponent(Sprite);
|
|
sprite.spriteFrame = atlas.getSpriteFrame(uuid.toString());
|
|
});
|
|
}
|
|
show_accessory(uuid:number){
|
|
let icon = this.node.getChildByName("boxs").getChildByName("accessory").getChildByName("icon")
|
|
icon.active=true
|
|
var icon_path = "game/heros/equips"
|
|
resources.load(icon_path, SpriteAtlas, (err: any, atlas) => {
|
|
const sprite = icon.getChildByName("icon").getComponent(Sprite);
|
|
sprite.spriteFrame = atlas.getSpriteFrame(uuid.toString());
|
|
});
|
|
}
|
|
count_attrs(){
|
|
// 重置所有属性
|
|
this.reset_attrs();
|
|
|
|
// 获取所有装备的属性
|
|
let weapon_attrs = this.weapon.uuid ? EquipInfo[this.weapon.uuid]?.attributes || [] : [];
|
|
let armor_attrs = this.armor.uuid ? EquipInfo[this.armor.uuid]?.attributes || [] : [];
|
|
let accessory_attrs = this.accessory.uuid ? EquipInfo[this.accessory.uuid]?.attributes || [] : [];
|
|
|
|
// 合并所有装备属性
|
|
const allAttrs = [...weapon_attrs, ...armor_attrs, ...accessory_attrs];
|
|
|
|
// 计算每个目标的属性加成
|
|
allAttrs.forEach(attr => {
|
|
const target = attr.target || EquipAttrTarget.ALL;
|
|
const targetKey = this.getTargetKey(target);
|
|
|
|
if (targetKey) {
|
|
switch (attr.type) {
|
|
case EquipAttr.ATK:
|
|
this.attrs[targetKey].ap += attr.value;
|
|
break;
|
|
case EquipAttr.HP:
|
|
this.attrs[targetKey].hp += attr.value;
|
|
break;
|
|
case EquipAttr.ATK_COUNT:
|
|
this.attrs[targetKey].atk_count += attr.value;
|
|
break;
|
|
case EquipAttr.ATK_SPEED:
|
|
this.attrs[targetKey].atk_speed += attr.value;
|
|
break;
|
|
case EquipAttr.SKILL_DMG:
|
|
this.attrs[targetKey].skill_dmg += attr.value;
|
|
break;
|
|
case EquipAttr.SKILL_SPEED:
|
|
this.attrs[targetKey].skill_cd += attr.value;
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
console.log("count_attrs",this.attrs)
|
|
}
|
|
|
|
// 重置所有属性为0
|
|
private reset_attrs() {
|
|
Object.keys(this.attrs).forEach(key => {
|
|
Object.keys(this.attrs[key]).forEach(attrKey => {
|
|
this.attrs[key][attrKey] = 0;
|
|
});
|
|
});
|
|
}
|
|
|
|
// 根据目标类型获取对应的key
|
|
private getTargetKey(target: EquipAttrTarget): string | null {
|
|
switch (target) {
|
|
case EquipAttrTarget.ALL:
|
|
return 'hero'; // 默认作用于英雄
|
|
case EquipAttrTarget.HERO:
|
|
return 'hero';
|
|
case EquipAttrTarget.FRIEND:
|
|
return 'friend';
|
|
case EquipAttrTarget.ENEMY:
|
|
return 'enemy';
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
equip_remove(e:GameEvent,data:any){
|
|
console.log("equip_remove",data)
|
|
}
|
|
update(dt: number): void {
|
|
if(!smc.mission.play||smc.mission.pause) return
|
|
}
|
|
}
|
|
|
|
|