2022-12-08 21:14:02 +08:00
|
|
|
|
import { animation, AnimationClip, Sprite, SpriteFrame } from "cc";
|
|
|
|
|
import DataManager from "../Global/DataManager";
|
|
|
|
|
import { ResourceManager } from "../Global/ResourceManager";
|
|
|
|
|
import { sortSpriteFrame } from "../Utils";
|
|
|
|
|
import StateMachine from "./StateMachine";
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
* unit:milisecond
|
|
|
|
|
*/
|
2022-12-08 21:14:02 +08:00
|
|
|
|
export const ANIMATION_SPEED = 1 / 10;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
* 状态(每组动画的承载容器,持有SpriteAnimation组件执行播放)
|
|
|
|
|
*/
|
|
|
|
|
export default class State {
|
2022-12-08 21:14:02 +08:00
|
|
|
|
private animationClip: AnimationClip;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
constructor(
|
|
|
|
|
private fsm: StateMachine,
|
|
|
|
|
private path: string,
|
|
|
|
|
private wrapMode: AnimationClip.WrapMode = AnimationClip.WrapMode.Normal,
|
2022-12-08 21:14:02 +08:00
|
|
|
|
private force: boolean = false
|
2022-11-27 23:23:47 +08:00
|
|
|
|
) {
|
|
|
|
|
//生成动画轨道属性
|
2022-12-08 21:14:02 +08:00
|
|
|
|
const track = new animation.ObjectTrack();
|
|
|
|
|
track.path = new animation.TrackPath().toComponent(Sprite).toProperty("spriteFrame");
|
|
|
|
|
const spriteFrames = DataManager.Instance.textureMap.get(this.path);
|
|
|
|
|
const frames: Array<[number, SpriteFrame]> = sortSpriteFrame(spriteFrames).map((item, index) => [index * ANIMATION_SPEED, item]);
|
|
|
|
|
track.channel.curve.assignSorted(frames);
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
|
|
//动画添加轨道
|
2022-12-08 21:14:02 +08:00
|
|
|
|
this.animationClip = new AnimationClip();
|
|
|
|
|
this.animationClip.name = this.path;
|
|
|
|
|
this.animationClip.duration = frames.length * ANIMATION_SPEED;
|
|
|
|
|
this.animationClip.addTrack(track);
|
|
|
|
|
this.animationClip.wrapMode = this.wrapMode;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run() {
|
|
|
|
|
if (this.fsm.animationComponent.defaultClip?.name === this.animationClip.name && !this.force) {
|
2022-12-08 21:14:02 +08:00
|
|
|
|
return;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
}
|
2022-12-08 21:14:02 +08:00
|
|
|
|
this.fsm.animationComponent.defaultClip = this.animationClip;
|
|
|
|
|
this.fsm.animationComponent.play();
|
2022-11-27 23:23:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|