| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- import { _decorator, Component, Node, Button, Sprite, Label, resources, SpriteFrame } from 'cc';
- import { SkillData, SkillManager } from './SkillManager';
- import { SkillButtonAnimator } from './SkillButtonAnimator';
- import { Audio } from '../../AudioManager/AudioManager';
- import { BundleLoader } from '../../Core/BundleLoader';
- const { ccclass, property } = _decorator;
- /**
- * SkillButtonController
- * 单个技能按钮的控制器,负责显示技能信息和处理点击事件
- * 配合预制体使用,提高代码复用性和维护性
- */
- @ccclass('SkillButtonController')
- export class SkillButtonController extends Component {
- @property({ type: Node, tooltip: '技能图标节点' })
- public skillSpriteNode: Node = null!;
- @property({ type: Node, tooltip: '技能名称节点' })
- public skillNameNode: Node = null!;
- @property({ type: Node, tooltip: '技能介绍节点' })
- public skillIntroduceNode: Node = null!;
- // 当前技能数据
- private _skillData: SkillData | null = null;
- // 按钮组件
- private _button: Button | null = null;
- // 动画组件
- private _animator: SkillButtonAnimator | null = null;
- // 点击回调
- private _onClickCallback: ((skillButton: SkillButtonController) => void) | null = null;
- protected onLoad() {
- this._button = this.getComponent(Button);
- this._animator = this.getComponent(SkillButtonAnimator);
-
- // 如果没有手动设置节点引用,尝试自动查找
- if (!this.skillSpriteNode) {
- this.skillSpriteNode = this.node.getChildByName('SkillSprite');
- }
- if (!this.skillNameNode) {
- this.skillNameNode = this.node.getChildByName('SkillName');
- }
- if (!this.skillIntroduceNode && this.skillNameNode) {
- this.skillIntroduceNode = this.skillNameNode.getChildByName('SkillIntroduce');
- }
- }
- protected start() {
- if (this._button) {
- this._button.node.on(Button.EventType.CLICK, this.onButtonClick, this);
- }
- }
- protected onDestroy() {
- if (this._button) {
- this._button.node.off(Button.EventType.CLICK, this.onButtonClick, this);
- }
- }
- /**
- * 设置技能数据并更新UI显示
- */
- public setSkillData(skillData: SkillData) {
- this._skillData = skillData;
- this.updateUI();
- }
- /**
- * 获取当前技能数据
- */
- public getSkillData(): SkillData | null {
- return this._skillData;
- }
- /**
- * 设置点击回调
- */
- public setClickCallback(callback: (skillButton: SkillButtonController) => void) {
- this._onClickCallback = callback;
- }
- /**
- * 更新UI显示
- */
- private updateUI() {
- if (!this._skillData) {
- this.node.active = false;
- return;
- }
- this.node.active = true;
- // 更新技能图标
- this.updateSkillIcon();
-
- // 更新技能名称
- this.updateSkillName();
-
- // 更新技能介绍
- this.updateSkillDescription();
-
- // 更新星级显示
- this.updateSkillLevel();
- }
- /**
- * 更新技能图标
- */
- private async updateSkillIcon() {
- if (!this.skillSpriteNode || !this._skillData) return;
- const sprite = this.skillSpriteNode.getComponent(Sprite);
- if (sprite) {
- // 转换路径格式,去除"images/"前缀
- const bundlePath = this._skillData.iconPath.replace(/^images\//, '');
-
- try {
- // 使用BundleLoader从images Bundle加载SpriteFrame,需要添加/spriteFrame后缀
- const bundleLoader = BundleLoader.getInstance();
- const spriteFrame = await bundleLoader.loadSpriteFrame(bundlePath + '/spriteFrame');
-
- if (spriteFrame && sprite && sprite.isValid) {
- sprite.spriteFrame = spriteFrame;
- console.log(`技能图标加载成功: ${this._skillData.iconPath}`);
- }
- } catch (err) {
- console.warn(`加载技能图标失败: ${this._skillData.iconPath}`, err);
- }
- }
- }
- /**
- * 更新技能名称
- */
- private updateSkillName() {
- if (!this.skillNameNode || !this._skillData) return;
- const label = this.skillNameNode.getComponent(Label);
- if (label) {
- label.string = this._skillData.name;
- }
- }
- /**
- * 更新技能介绍
- */
- private updateSkillDescription() {
- if (!this.skillIntroduceNode || !this._skillData) return;
- const label = this.skillIntroduceNode.getComponent(Label);
- if (label) {
- const skillManager = SkillManager.getInstance();
- if (skillManager) {
- // 检查技能是否满级
- if (skillManager.isSkillMaxLevel(this._skillData.id)) {
- label.string = "技能等级已满";
- } else {
- // 获取当前技能等级的描述
- const currentLevel = skillManager.getSkillLevel(this._skillData.id);
- const description = skillManager.getSkillDescription(this._skillData.id, currentLevel);
- label.string = description;
- }
- } else {
- label.string = this._skillData.description;
- }
- }
- }
- /**
- * 更新技能等级显示
- */
- private updateSkillLevel() {
- if (!this._animator || !this._skillData) return;
- const skillManager = SkillManager.getInstance();
- const actualLevel = skillManager ? skillManager.getSkillLevel(this._skillData.id) : 0;
- this._animator.setSkillLevel(actualLevel);
- console.log(`设置技能 ${this._skillData.name} 星级: ${actualLevel}`);
- }
- /**
- * 刷新技能等级显示(用于技能升级后的UI更新)
- */
- public refreshSkillLevel() {
- this.updateSkillLevel();
- this.updateSkillDescription(); // 同时更新描述
- }
- /**
- * 按钮点击事件
- */
- private onButtonClick() {
- Audio.playUISound('data/弹球音效/level up 2');
- if (this._onClickCallback) {
- this._onClickCallback(this);
- }
- }
- /**
- * 播放收缩动画
- */
- public playShrinkAnimation(duration: number, targetPos?: any, onComplete?: () => void) {
- if (this._animator) {
- this._animator.playShrink(duration, targetPos, onComplete);
- } else if (onComplete) {
- onComplete();
- }
- }
- /**
- * 重置动画状态
- */
- public resetAnimationState() {
- if (this._animator) {
- this._animator.resetState();
- }
- }
- /**
- * 设置按钮交互状态
- */
- public setInteractable(interactable: boolean) {
- if (this._button) {
- this._button.interactable = interactable;
- }
- }
- }
|