From 92ab459736adf138eab23e29d5d5024bfd5ff804 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Wed, 16 Nov 2022 22:46:56 +0100 Subject: [PATCH] Improve some skel anim edge cases; warning fixes. Also new Lua function. + entity_setAnimationTime() --- Aquaria/ScriptInterface.cpp | 31 +++++++++++++++++------- BBGE/SkeletalSprite.cpp | 48 ++++++++++++++++++------------------- BBGE/SkeletalSprite.h | 2 +- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/Aquaria/ScriptInterface.cpp b/Aquaria/ScriptInterface.cpp index 5ea2071..d2f3873 100644 --- a/Aquaria/ScriptInterface.cpp +++ b/Aquaria/ScriptInterface.cpp @@ -4323,16 +4323,15 @@ luaFunc(entity_isAnimating) luaFunc(entity_getAnimationName) { Entity *e = entity(L); - const char *ret = ""; int layer = lua_tointeger(L, 2); if (e) { if (Animation *anim = e->skeletalSprite.getCurrentAnimation(layer)) { - ret = anim->name.c_str(); + luaReturnStr(anim->name.c_str()); } } - luaReturnStr(ret); + luaReturnNil(); } luaFunc(entity_getAnimationLength) @@ -4342,7 +4341,7 @@ luaFunc(entity_getAnimationLength) if (e) { Animation *anim = 0; - if (lua_isstring(L, 2)) + if (lua_type(L, 2) == LUA_TSTRING) // lua_isstring() is incorrect here anim = e->skeletalSprite.getAnimation(lua_tostring(L, 2)); else { @@ -4696,6 +4695,21 @@ luaFunc(entity_getAnimationTime) luaReturnNil(); } +luaFunc(entity_setAnimationTime) +{ + SkeletalSprite *skel = getSkeletalSprite(entity(L)); + int layer = lua_tointeger(L, 3); + if (skel) + { + AnimationLayer *a = skel->getAnimationLayer(layer); + if(a) + { + a->timer = lua_tonumber(L, 2); + } + } + luaReturnNil(); +} + // entity, x, y, time, ease, relative luaFunc(entity_move) { @@ -6449,8 +6463,8 @@ luaFunc(bone_getIndex) Bone *b = bone(L); int idx = -1; if (b) - idx = b->boneIdx; - luaReturnNum(idx); + idx = (int)b->boneIdx; + luaReturnInt(idx); } luaFunc(bone_getName) @@ -9114,7 +9128,7 @@ luaFunc(isShuttingDownGameState) static void _fillPathfindTables(lua_State *L, VectorPath& path, int xs_idx, int ys_idx) { - const unsigned num = path.getNumPathNodes(); + const unsigned num = (unsigned)path.getNumPathNodes(); if(lua_istable(L, xs_idx)) lua_pushvalue(L, xs_idx); @@ -9159,7 +9173,7 @@ luaFunc(findPath) if(!PathFinding::generatePathSimple(path, start, end, lua_tointeger(L, 5), obs)) luaReturnBool(false); - const unsigned num = path.getNumPathNodes(); + const unsigned num = (unsigned)path.getNumPathNodes(); lua_pushinteger(L, num); _fillPathfindTables(L, path, 6, 7); @@ -10304,6 +10318,7 @@ static const struct { luaRegister(entity_stopAnimation), luaRegister(entity_getAnimationLoop), luaRegister(entity_getAnimationTime), + luaRegister(entity_setAnimationTime), luaRegister(entity_setCurrentTarget), luaRegister(entity_stopInterpolating), diff --git a/BBGE/SkeletalSprite.cpp b/BBGE/SkeletalSprite.cpp index f8c8260..8d43fbe 100644 --- a/BBGE/SkeletalSprite.cpp +++ b/BBGE/SkeletalSprite.cpp @@ -552,24 +552,28 @@ float AnimationLayer::transitionAnimate(std::string anim, float time, int loop) { stringToLower(anim); float totalTime =0; - if (createTransitionAnimation(anim, time)) + if(Animation *a = this->s->getAnimation(anim)) { - timeMultiplier = 1; - - currentAnimation = -1; - this->loop = 0; - timer = 0; - animating = 1; - animationLength = getCurrentAnimation()->getAnimationLength(); - enqueueAnimation(anim, loop); - Animation *a = this->s->getAnimation(anim); - if (a) + if (time <= 0) // no transition? { - if (loop > -1) - totalTime = a->getAnimationLength()*(loop+1) + time; - else - totalTime = a->getAnimationLength() + time; + animate(anim, loop); } + else + { + createTransitionAnimation(*a, time); + timeMultiplier = 1; + + currentAnimation = -1; + this->loop = 0; + timer = 0; + animating = 1; + animationLength = getCurrentAnimation()->getAnimationLength(); + enqueueAnimation(anim, loop); + } + if (loop > -1) + totalTime = a->getAnimationLength()*(loop+1) + time; + else + totalTime = a->getAnimationLength() + time; } return totalTime; } @@ -593,11 +597,8 @@ Animation* AnimationLayer::getCurrentAnimation() return &s->animations[currentAnimation]; } -bool AnimationLayer::createTransitionAnimation(const std::string& anim, float time) +void AnimationLayer::createTransitionAnimation(Animation& to, float time) { - - Animation *to = s->getAnimation(anim); - if (!to) return false; blendAnimation.keyframes.clear(); SkeletalKeyframe k; k.t = 0; @@ -615,14 +616,11 @@ bool AnimationLayer::createTransitionAnimation(const std::string& anim, float ti blendAnimation.keyframes.push_back(k); SkeletalKeyframe k2; - SkeletalKeyframe *rk = to->getKeyframe(0); - if (!rk) return false; - k2 = *rk; + k2 = *to.getKeyframe(0); k2.t = time; blendAnimation.keyframes.push_back(k2); - blendAnimation.name = anim; - return true; + blendAnimation.name = to.name; } @@ -908,7 +906,7 @@ void AnimationLayer::update(float dt) { timer += dt*timeMultiplier.x; - if (timer > animationLength) + if (timer >= animationLength) { float leftover; if (animationLength > 0) diff --git a/BBGE/SkeletalSprite.h b/BBGE/SkeletalSprite.h index 2653842..41acaae 100644 --- a/BBGE/SkeletalSprite.h +++ b/BBGE/SkeletalSprite.h @@ -203,7 +203,7 @@ public: void updateBones(); void stopAnimation(); float getAnimationLength(); - bool createTransitionAnimation(const std::string& anim, float time); + void createTransitionAnimation(Animation& to, float time); void playAnimation(int idx, int loop); void playCurrentAnimation(int loop); void enqueueAnimation(const std::string& anim, int loop);