238 lines
9.0 KiB
TypeScript
238 lines
9.0 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, EquipAttrTarget, EquipSpecialAttr} from '../common/config/Equips';
|
|
import { BuffAttr, DebuffAttr, geDebuffNum, getBuffNum } from '../common/config/SkillSet';
|
|
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_buff:getBuffNum(),
|
|
friend_buff:getBuffNum(),
|
|
hero_debuff:geDebuffNum(),
|
|
friend_debuff:geDebuffNum(),
|
|
enemy_debuff:geDebuffNum(),
|
|
}
|
|
/** 视图层逻辑代码分离演示 */
|
|
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:0,
|
|
name:"weapon",
|
|
type:"weapon",
|
|
level:0,
|
|
}
|
|
this.armor={
|
|
uuid:0,
|
|
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)
|
|
}
|
|
this.count_attrs()
|
|
}
|
|
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();
|
|
console.log("重置后的属性", this.attrs);
|
|
|
|
// 获取所有装备的属性
|
|
let weapon_buffs = this.weapon.uuid ? EquipInfo[this.weapon.uuid]?.buff || [] : [];
|
|
let armor_buffs = this.armor.uuid ? EquipInfo[this.armor.uuid]?.buff || [] : [];
|
|
let accessory_buffs = this.accessory.uuid ? EquipInfo[this.accessory.uuid]?.buff || [] : [];
|
|
let weapon_debuffs = this.weapon.uuid ? EquipInfo[this.weapon.uuid]?.debuff || [] : [];
|
|
let armor_debuffs = this.armor.uuid ? EquipInfo[this.armor.uuid]?.debuff || [] : [];
|
|
let accessory_debuffs = this.accessory.uuid ? EquipInfo[this.accessory.uuid]?.debuff || [] : [];
|
|
let weapon_special_attr = this.weapon.uuid ? EquipInfo[this.weapon.uuid]?.special_attr : 0;
|
|
let armor_special_attr = this.armor.uuid ? EquipInfo[this.armor.uuid]?.special_attr : 0;
|
|
let accessory_special_attr = this.accessory.uuid ? EquipInfo[this.accessory.uuid]?.special_attr : 0;
|
|
|
|
console.log("武器特殊属性", weapon_special_attr);
|
|
console.log("防具特殊属性", armor_special_attr);
|
|
console.log("饰品特殊属性", accessory_special_attr);
|
|
console.log("武器属性", weapon_buffs);
|
|
console.log("防具属性", armor_buffs);
|
|
console.log("饰品属性", accessory_buffs);
|
|
console.log("武器减益", weapon_debuffs);
|
|
console.log("防具减益", armor_debuffs);
|
|
console.log("饰品减益", accessory_debuffs);
|
|
//特殊属性附加
|
|
switch(weapon_special_attr){
|
|
case EquipSpecialAttr.ICE:
|
|
oops.message.dispatchEvent(GameEvent.ChangeATK,EquipSpecialAttr.ICE)
|
|
break
|
|
case EquipSpecialAttr.FIRE:
|
|
oops.message.dispatchEvent(GameEvent.ChangeATK,EquipSpecialAttr.FIRE)
|
|
break
|
|
case EquipSpecialAttr.WIND:
|
|
oops.message.dispatchEvent(GameEvent.ChangeATK,EquipSpecialAttr.WIND)
|
|
break
|
|
default:
|
|
oops.message.dispatchEvent(GameEvent.ChangeATK,0)
|
|
break
|
|
}
|
|
// 合并所有装备属性
|
|
const allBuff = [...weapon_buffs, ...armor_buffs, ...accessory_buffs];
|
|
const allDebuff = [...weapon_debuffs, ...armor_debuffs, ...accessory_debuffs];
|
|
console.log("合并后的所有属性", allBuff);
|
|
console.log("合并后的所有减益", allDebuff);
|
|
// 计算每个目标的属性加成
|
|
allBuff.forEach(attr => {
|
|
const target = attr.target || EquipAttrTarget.HERO;
|
|
let targetKey = null;
|
|
// 根据目标类型获取对应的key
|
|
switch (target) {
|
|
case EquipAttrTarget.HERO:
|
|
targetKey = 'hero_buff';
|
|
break;
|
|
case EquipAttrTarget.FRIEND:
|
|
targetKey = 'friend_buff';
|
|
break;
|
|
}
|
|
this.add_attr(targetKey,attr)
|
|
});
|
|
allDebuff.forEach(attr => {
|
|
const target = attr.target || EquipAttrTarget.HERO;
|
|
let targetKey = null;
|
|
// 根据目标类型获取对应的key
|
|
switch (target) {
|
|
case EquipAttrTarget.HERO:
|
|
targetKey = 'hero_debuff';
|
|
break;
|
|
case EquipAttrTarget.FRIEND:
|
|
targetKey = 'friend_debuff';
|
|
break;
|
|
case EquipAttrTarget.ENEMY:
|
|
targetKey = 'enemy_debuff';
|
|
break;
|
|
}
|
|
this.add_debuff(targetKey,attr)
|
|
});
|
|
|
|
console.log("最终属性加成", this.attrs);
|
|
oops.message.dispatchEvent(GameEvent.EquipChange, this.attrs);
|
|
}
|
|
|
|
add_attr(targetKey:string,attr:any){
|
|
if(targetKey){
|
|
this.attrs[targetKey][BuffAttr[attr.type]] += attr.value;
|
|
}
|
|
}
|
|
|
|
add_debuff(targetKey:string,attr:any){
|
|
if(targetKey){
|
|
this.attrs[targetKey][DebuffAttr[attr.type]] += attr.value;
|
|
}
|
|
}
|
|
|
|
// 重置所有属性为0
|
|
private reset_attrs() {
|
|
// 创建新的属性对象
|
|
const newAttrs = {
|
|
hero_buff: getBuffNum(),
|
|
friend_buff: getBuffNum(),
|
|
hero_debuff: geDebuffNum(),
|
|
friend_debuff: geDebuffNum(),
|
|
enemy_debuff: geDebuffNum(),
|
|
};
|
|
|
|
// 替换整个 attrs 对象
|
|
this.attrs = newAttrs;
|
|
|
|
console.log("重置属性", {
|
|
hero_buff: this.attrs.hero_buff,
|
|
hero_debuff: this.attrs.hero_debuff,
|
|
friend_buff: this.attrs.friend_buff,
|
|
friend_debuff: this.attrs.friend_debuff,
|
|
enemy_debuff: this.attrs.enemy_debuff,
|
|
});
|
|
}
|
|
|
|
equip_remove(e:GameEvent,data:any){
|
|
console.log("equip_remove",data)
|
|
}
|
|
update(dt: number): void {
|
|
if(!smc.mission.play||smc.mission.pause) return
|
|
}
|
|
}
|
|
|
|
|