149 lines
3.9 KiB
Plaintext
149 lines
3.9 KiB
Plaintext
|
|
/*
|
||
|
|
文件名:AnimationClass.nut
|
||
|
|
路径:GameClass/AnimationClass.nut
|
||
|
|
创建日期:2024-05-07 23:25
|
||
|
|
文件用途:动画类
|
||
|
|
*/
|
||
|
|
class Animation extends Actor {
|
||
|
|
|
||
|
|
//Ani是否可用
|
||
|
|
IsUsability = true;
|
||
|
|
|
||
|
|
//当前帧数
|
||
|
|
CurrentFrameIndex = 0;
|
||
|
|
//当前帧时间
|
||
|
|
CurrentIndexT = 0;
|
||
|
|
//当前帧
|
||
|
|
CurrentFrame = null;
|
||
|
|
//状态机对象(只有存在时才有KeyFlag)
|
||
|
|
StateMachine = null;
|
||
|
|
|
||
|
|
//Ani的标签
|
||
|
|
AnimationFlag = null;
|
||
|
|
//帧对象数组
|
||
|
|
FrameArr = null;
|
||
|
|
//图片精灵帧对象
|
||
|
|
SpriteArr = null;
|
||
|
|
|
||
|
|
//Ani类型
|
||
|
|
Type = "normal";
|
||
|
|
|
||
|
|
constructor(Data) {
|
||
|
|
//精灵帧数组
|
||
|
|
SpriteArr = [];
|
||
|
|
//帧数组
|
||
|
|
FrameArr = [];
|
||
|
|
base.constructor();
|
||
|
|
|
||
|
|
//初始化数据
|
||
|
|
InitData(Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
function InitData(Data) {
|
||
|
|
local Buf;
|
||
|
|
if (type(Data) == "table") {
|
||
|
|
Buf = Data;
|
||
|
|
}
|
||
|
|
//从PVF数据加载
|
||
|
|
else if (type(Data) == "string") {
|
||
|
|
Buf = Util.DeepCopy(ScriptData.GetAni(Data));
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Buf) {
|
||
|
|
AnimationFlag = Buf.Flag;
|
||
|
|
FrameArr = Buf.Frame;
|
||
|
|
foreach(FrameObj in FrameArr) {
|
||
|
|
// print("path: " + "sprite/" + FrameObj.Img_Path + " index: " + FrameObj.Img_Index);
|
||
|
|
local SpriteFramebuf = CL_SpriteFrameObject("sprite/" + FrameObj.Img_Path, FrameObj.Img_Index);
|
||
|
|
local Spritebuf = CL_SpriteObject();
|
||
|
|
Spritebuf.SetFrame(SpriteFramebuf);
|
||
|
|
|
||
|
|
//线性减淡
|
||
|
|
if ("GRAPHIC_EFFECT_LINEARDODGE" in FrameObj.Flag) {
|
||
|
|
Spritebuf.SetMode(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
//坐标
|
||
|
|
Spritebuf.SetPosition(FrameObj.Pos);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
SpriteArr.append(Spritebuf);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
error("创建Ani失败,找不到Ani数据");
|
||
|
|
}
|
||
|
|
FlushFrame(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//重置Ani
|
||
|
|
function Reset() {
|
||
|
|
IsUsability = true;
|
||
|
|
FlushFrame(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
//赋予状态机
|
||
|
|
function BindenvStateMachine(gM) {
|
||
|
|
StateMachine = gM;
|
||
|
|
}
|
||
|
|
|
||
|
|
//获取当前帧信息
|
||
|
|
function GetCurrentFrameInfo() {
|
||
|
|
return FrameArr[CurrentFrameIndex];
|
||
|
|
}
|
||
|
|
|
||
|
|
function FlushFrame(Index) {
|
||
|
|
//同步当前帧
|
||
|
|
CurrentFrameIndex = Index;
|
||
|
|
//移除上一帧
|
||
|
|
if (CurrentFrame) Removechild(CurrentFrame);
|
||
|
|
//当前帧更换为本帧
|
||
|
|
CurrentFrame = SpriteArr[CurrentFrameIndex];
|
||
|
|
Addchild(SpriteArr[CurrentFrameIndex]);
|
||
|
|
|
||
|
|
//关键帧
|
||
|
|
if ("SET_FLAG" in FrameArr[CurrentFrameIndex].Flag) {
|
||
|
|
if (StateMachine && StateMachine.State != -1) StateMachine.ChangeAniKeyFlag(FrameArr[CurrentFrameIndex].Flag.SET_FLAG);
|
||
|
|
}
|
||
|
|
|
||
|
|
//Ani对象的大小同步为精灵帧对象的大小
|
||
|
|
if (CurrentFrame) SetSize(CurrentFrame.GetSize());
|
||
|
|
}
|
||
|
|
|
||
|
|
//override
|
||
|
|
function OnUpdate(dt) {
|
||
|
|
|
||
|
|
//可用性检查
|
||
|
|
if (IsUsability) {
|
||
|
|
//累加当前帧时间
|
||
|
|
CurrentIndexT += dt;
|
||
|
|
|
||
|
|
//当前帧时间 超过 当前帧延迟就需要切换帧了
|
||
|
|
if (CurrentIndexT >= FrameArr[CurrentFrameIndex].Delay) {
|
||
|
|
CurrentIndexT = 0;
|
||
|
|
//如果当前帧小于总帧数就切换
|
||
|
|
if (CurrentFrameIndex<(FrameArr.len() - 1)) {
|
||
|
|
FlushFrame(CurrentFrameIndex + 1);
|
||
|
|
}
|
||
|
|
//说明播放完毕了
|
||
|
|
else {
|
||
|
|
//如果有循环
|
||
|
|
if ("LOOP" in AnimationFlag) {
|
||
|
|
FlushFrame(0);
|
||
|
|
}
|
||
|
|
//没有循环触发状态机回调
|
||
|
|
else {
|
||
|
|
//将不再可用
|
||
|
|
IsUsability = false;
|
||
|
|
if (StateMachine && StateMachine.State != -1) StateMachine.ChangeAniEndFlag();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
base.OnUpdate(dt);
|
||
|
|
}
|
||
|
|
}
|