简单角色控制

This commit is contained in:
janing
2025-12-14 22:39:15 +08:00
parent 1cfd79a231
commit cf77482fc2
2 changed files with 115 additions and 0 deletions

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "a6adabd0-b40d-4a14-b348-c186f5b7e958",
"files": [],
"subMetas": {},
"userData": {}
}