英雄召唤基本完成 下一步 满3个英雄后 不再出现其他英雄
This commit is contained in:
@@ -10,17 +10,25 @@ import { HeroInfo } from "../common/config/heroSet";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/** 英雄数据接口 */
|
||||
interface HeroData {
|
||||
uuid: number;
|
||||
name: string;
|
||||
type: number; // 1 被动 0 主动
|
||||
level: number;
|
||||
quality: number;
|
||||
cd: number;
|
||||
cd_time: number;
|
||||
active: boolean;
|
||||
}
|
||||
|
||||
/** 视图层对象 */
|
||||
@ccclass('HeroUiComp')
|
||||
@ecs.register('HeroUi', false)
|
||||
export class HeroUiComp extends CCComp {
|
||||
@property(CCString)
|
||||
hero_slot:string="skill1"
|
||||
@property(CCInteger)
|
||||
hero_slot_index:number=1
|
||||
heroUi:any=null
|
||||
skill_cd_bar_progress:any=null
|
||||
max_show:boolean=false
|
||||
heroes: HeroData[] = []
|
||||
private readonly MAX_HEROES = 3
|
||||
private readonly HERO_NODE_NAMES = ['hero1', 'hero2', 'hero3']
|
||||
/** 视图层逻辑代码分离演示 */
|
||||
onLoad() {
|
||||
this.on(GameEvent.FightReady,this.fight_ready,this)
|
||||
@@ -29,22 +37,30 @@ export class HeroUiComp extends CCComp {
|
||||
}
|
||||
start(){
|
||||
this.fight_ready()
|
||||
|
||||
}
|
||||
|
||||
fight_ready(){
|
||||
console.log("[HeroUiComp]: fight_ready",this.node)
|
||||
this.heroUi={
|
||||
uuid:0,
|
||||
name:"hero",
|
||||
type:0, //1 被动 0 主动
|
||||
level:0,
|
||||
quality:0,
|
||||
cd:0,
|
||||
cd_time:0,
|
||||
active:false,
|
||||
this.heroes = []
|
||||
for(let i = 0; i < this.MAX_HEROES; i++) {
|
||||
this.heroes.push(this.createDefaultHero())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private createDefaultHero(): HeroData {
|
||||
return {
|
||||
uuid: 0,
|
||||
name: "hero",
|
||||
type: 0, //1 被动 0 主动
|
||||
level: 1,
|
||||
quality: 0,
|
||||
cd: 0,
|
||||
cd_time: 0,
|
||||
active: false,
|
||||
}
|
||||
}
|
||||
|
||||
update(dt: number): void {
|
||||
if(!smc.mission.play||smc.mission.pause) return
|
||||
|
||||
@@ -52,17 +68,137 @@ export class HeroUiComp extends CCComp {
|
||||
}
|
||||
|
||||
|
||||
get_hero(e:GameEvent,data:any){
|
||||
console.log("[HeroUiComp]: get_hero",data)
|
||||
let hero=HeroInfo[data.uuid]
|
||||
let icon=this.node.getChildByName("icon")
|
||||
icon.active=true
|
||||
var icon_path = "game/heros/herois"
|
||||
get_hero(e: GameEvent, data: { uuid: number }) {
|
||||
console.log("[HeroUiComp]: get_hero", data)
|
||||
|
||||
// 尝试升级现有英雄
|
||||
if (this.tryUpgradeExistingHero(data.uuid)) {
|
||||
return
|
||||
}
|
||||
|
||||
// 添加新英雄
|
||||
this.addNewHero(data)
|
||||
}
|
||||
|
||||
resources.load(icon_path, SpriteAtlas, (err: any, atlas) => {
|
||||
const sprite = icon.getComponent(Sprite);
|
||||
sprite.spriteFrame = atlas.getSpriteFrame(hero.path);
|
||||
});
|
||||
/**
|
||||
* 尝试升级现有英雄
|
||||
* @param uuid 英雄UUID
|
||||
* @returns 是否成功升级
|
||||
*/
|
||||
private tryUpgradeExistingHero(uuid: number): boolean {
|
||||
for (let i = 0; i < this.heroes.length; i++) {
|
||||
if (this.heroes[i].uuid === uuid) {
|
||||
this.heroes[i].level++
|
||||
// 播放升级动画
|
||||
this.playCardScaleAnimation(i)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加新英雄
|
||||
* @param data 英雄数据
|
||||
*/
|
||||
private addNewHero(data: { uuid: number }) {
|
||||
const heroInfo = HeroInfo[data.uuid]
|
||||
if (!heroInfo) {
|
||||
console.error("[HeroUiComp]: Hero info not found for uuid:", data.uuid)
|
||||
return
|
||||
}
|
||||
|
||||
// 查找空闲位置
|
||||
const emptySlotIndex = this.findEmptyHeroSlot()
|
||||
if (emptySlotIndex === -1) {
|
||||
console.warn("[HeroUiComp]: No empty slot available for new hero")
|
||||
return
|
||||
}
|
||||
|
||||
// 设置英雄数据
|
||||
this.heroes[emptySlotIndex] = {
|
||||
uuid: data.uuid,
|
||||
name: heroInfo.name,
|
||||
type: heroInfo.type,
|
||||
level: 1,
|
||||
quality: heroInfo.quality,
|
||||
cd: 0,
|
||||
cd_time: 0,
|
||||
active: false,
|
||||
}
|
||||
|
||||
// 更新UI
|
||||
this.updateHeroUI(emptySlotIndex, heroInfo)
|
||||
// 播放新卡牌动画
|
||||
this.playCardScaleAnimation(emptySlotIndex)
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找空闲英雄槽位
|
||||
* @returns 空闲槽位索引,-1表示没有空闲槽位
|
||||
*/
|
||||
private findEmptyHeroSlot(): number {
|
||||
for (let i = 0; i < this.heroes.length; i++) {
|
||||
if (this.heroes[i].uuid === 0) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新英雄UI(图标和信息)
|
||||
* @param heroIndex 英雄索引
|
||||
* @param heroInfo 英雄信息
|
||||
*/
|
||||
private updateHeroUI(heroIndex: number, heroInfo: { path: string }) {
|
||||
const heroNodeName = this.HERO_NODE_NAMES[heroIndex]
|
||||
const heroNode = this.node.getChildByName(heroNodeName)
|
||||
if (!heroNode) return
|
||||
|
||||
const iconNode = heroNode.getChildByName("icon")
|
||||
if (iconNode) {
|
||||
iconNode.active = true
|
||||
this.loadHeroIcon(iconNode, heroInfo.path)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载英雄图标
|
||||
* @param iconNode 图标节点
|
||||
* @param heroPath 英雄路径
|
||||
*/
|
||||
private loadHeroIcon(iconNode: Node, heroPath: string) {
|
||||
const iconPath = "game/heros/herois"
|
||||
resources.load(iconPath, SpriteAtlas, (err: any, atlas) => {
|
||||
if (err) {
|
||||
console.error("[HeroUiComp]: Failed to load hero icon atlas:", err)
|
||||
return
|
||||
}
|
||||
const sprite = iconNode.getComponent(Sprite)
|
||||
if (sprite && atlas) {
|
||||
sprite.spriteFrame = atlas.getSpriteFrame(heroPath)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放卡牌缩放动画
|
||||
* @param heroIndex 英雄索引
|
||||
*/
|
||||
private playCardScaleAnimation(heroIndex: number) {
|
||||
const heroNodeName = this.HERO_NODE_NAMES[heroIndex]
|
||||
const heroNode = this.node.getChildByName(heroNodeName)
|
||||
if (!heroNode) return
|
||||
|
||||
// 停止可能存在的动画
|
||||
tween(heroNode).stop()
|
||||
|
||||
// 播放缩放动画:放大 -> 恢复原始大小
|
||||
tween(heroNode)
|
||||
.to(0.1, { scale: v3(1.2, 1.2, 1) }, { easing: 'backOut' })
|
||||
.to(0.15, { scale: v3(1, 1, 1) }, { easing: 'backOut' })
|
||||
.start()
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user