diff --git a/client/assets/scripts/CC/RoleController.ts b/client/assets/scripts/CC/RoleController.ts new file mode 100644 index 0000000..0497290 --- /dev/null +++ b/client/assets/scripts/CC/RoleController.ts @@ -0,0 +1,106 @@ +import { _decorator, Component, Node, SkeletalAnimation, AnimationClip, CCBoolean } from 'cc'; +const { ccclass, property } = _decorator; + +@ccclass('RoleController') +export class RoleController extends Component { + @property(SkeletalAnimation) + skeletalAnimation: SkeletalAnimation = null; + + @property(AnimationClip) + idleClip: AnimationClip = null; + + @property(AnimationClip) + moveClip: AnimationClip = null; + + @property(AnimationClip) + attackClip: AnimationClip = null; + + @property(AnimationClip) + deathClip: AnimationClip = null; + + @property({type: CCBoolean}) + islog: boolean = false; + + private currentAnimIndex: number = 0; + private animationNames: string[] = []; + private animationClips: AnimationClip[] = []; + + start() { + // 将外部传入的动画剪辑添加到骨骼动画组件 + if (this.skeletalAnimation) { + // 添加待机动画 + if (this.idleClip) { + this.skeletalAnimation.addClip(this.idleClip,"idle"); + this.animationClips.push(this.idleClip); + this.animationNames.push("idle"); + } + + // 添加移动动画 + if (this.moveClip) { + this.skeletalAnimation.addClip(this.moveClip, "move"); + this.animationClips.push(this.moveClip); + this.animationNames.push("move"); + } + + // 添加攻击动画 + if (this.attackClip) { + this.skeletalAnimation.addClip(this.attackClip, "attack"); + this.animationClips.push(this.attackClip); + this.animationNames.push("attack"); + } + + // 添加死亡动画 + if (this.deathClip) { + this.skeletalAnimation.addClip(this.deathClip, "death"); + this.animationClips.push(this.deathClip); + this.animationNames.push("death"); + } + + if (this.animationClips.length > 0) { + // 播放第一个动画 + this.skeletalAnimation.play(this.animationNames[0]); + this.log(`当前动画: ${this.animationNames[0]}`); + } + } + } + + /** + * 播放指定动作的动画 + * @param action 动作名称: "idle" | "move" | "attack" | "death" + * @param loop 是否循环播放 + */ + public PlayAnimation(action: string, loop: boolean = true): void { + if (!this.skeletalAnimation) { + console.warn('SkeletalAnimation 组件未设置'); + return; + } + + // 检查动画是否存在 + if (!this.animationNames.includes(action)) { + console.warn(`动画 "${action}" 不存在,可用动画: ${this.animationNames.join(', ')}`); + return; + } + + // 获取动画状态并设置循环 + const animState = this.skeletalAnimation.getState(action); + if (animState) { + animState.wrapMode = loop ? AnimationClip.WrapMode.Loop : AnimationClip.WrapMode.Normal; + } + + // 播放动画 + this.skeletalAnimation.play(action); + this.log(`播放动画: ${action}, 循环: ${loop}`); + } + + update(deltaTime: number) { + + } + + private log(...args: any[]): void { + if (this.islog){ + console.log('[RoleController]', ...args); + } + } +} + + diff --git a/client/assets/scripts/CC/RoleController.ts.meta b/client/assets/scripts/CC/RoleController.ts.meta new file mode 100644 index 0000000..f73d898 --- /dev/null +++ b/client/assets/scripts/CC/RoleController.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "a6adabd0-b40d-4a14-b348-c186f5b7e958", + "files": [], + "subMetas": {}, + "userData": {} +}