EMI: Chore fading fixes.

Fading out chores remain in playing state until they have completely faded out. Also using StopChore on a fading out chore will actually stop the chore, unlike in Grim where the chore will keep fading out.
This commit is contained in:
Joni Vähämäki 2014-05-28 14:39:42 +03:00
parent 4acfb933e9
commit a180b3b562
12 changed files with 180 additions and 95 deletions

View File

@ -2294,18 +2294,20 @@ Actor::ActionChore::ActionChore(Costume *cost, int chore, Chore::ChoreType chore
void Actor::ActionChore::play(bool fade, unsigned int time) {
if (isValid()) {
_costume->playChore(_chore);
if (fade) {
_costume->fadeChoreIn(_chore, time);
_costume->playChore(_chore, time);
} else {
_costume->playChore(_chore);
}
}
}
void Actor::ActionChore::playLooping(bool fade, unsigned int time) {
if (isValid()) {
_costume->playChoreLooping(_chore);
if (fade) {
_costume->fadeChoreIn(_chore, time);
_costume->playChoreLooping(_chore, time);
} else {
_costume->playChoreLooping(_chore);
}
}
}
@ -2313,9 +2315,10 @@ void Actor::ActionChore::playLooping(bool fade, unsigned int time) {
void Actor::ActionChore::stop(bool fade, unsigned int time) {
if (isValid()) {
if (fade) {
_costume->fadeChoreOut(_chore, time);
_costume->stopChore(_chore, time);
} else {
_costume->stopChore(_chore);
}
_costume->stopChore(_chore);
}
}

View File

@ -292,10 +292,10 @@ void Costume::setChoreType(int num, Chore::ChoreType choreType) {
_chores[num]->setChoreType(choreType);
}
void Costume::playChoreLooping(const char *name) {
void Costume::playChoreLooping(const char *name, uint msecs) {
for (int i = 0; i < _numChores; ++i) {
if (strcmp(_chores[i]->getName(), name) == 0) {
playChoreLooping(i);
playChoreLooping(i, msecs);
return;
}
}
@ -303,12 +303,12 @@ void Costume::playChoreLooping(const char *name) {
return;
}
void Costume::playChoreLooping(int num) {
void Costume::playChoreLooping(int num, uint msecs) {
if (num < 0 || num >= _numChores) {
Debug::warning(Debug::Chores, "Requested chore number %d is outside the range of chores (0-%d)", num, _numChores);
return;
}
_chores[num]->playLooping();
_chores[num]->playLooping(msecs);
if (Common::find(_playingChores.begin(), _playingChores.end(), _chores[num]) == _playingChores.end()) {
_playingChores.push_back(_chores[num]);
sortPlayingChores();
@ -336,10 +336,10 @@ int Costume::getChoreId(const char *name) {
return -1;
}
void Costume::playChore(const char *name) {
void Costume::playChore(const char *name, uint msecs) {
for (int i = 0; i < _numChores; ++i) {
if (strcmp(_chores[i]->getName(), name) == 0) {
playChore(i);
playChore(i, msecs);
return;
}
}
@ -347,25 +347,24 @@ void Costume::playChore(const char *name) {
return;
}
void Costume::playChore(int num) {
void Costume::playChore(int num, uint msecs) {
if (num < 0 || num >= _numChores) {
Debug::warning(Debug::Chores, "Requested chore number %d is outside the range of chores (0-%d)", num, _numChores);
return;
}
_chores[num]->play();
_chores[num]->play(msecs);
if (Common::find(_playingChores.begin(), _playingChores.end(), _chores[num]) == _playingChores.end()) {
_playingChores.push_back(_chores[num]);
sortPlayingChores();
}
}
void Costume::stopChore(int num) {
void Costume::stopChore(int num, uint msecs) {
if (num < 0 || num >= _numChores) {
Debug::warning(Debug::Chores, "Requested chore number %d is outside the range of chores (0-%d)", num, _numChores);
return;
}
_chores[num]->stop();
_playingChores.remove(_chores[num]);
_chores[num]->stop(msecs);
}
void Costume::setColormap(const Common::String &map) {
@ -379,17 +378,16 @@ void Costume::setColormap(const Common::String &map) {
_components[i]->setColormap(NULL);
}
void Costume::stopChores(bool ignoreLoopingChores) {
void Costume::stopChores(bool ignoreLoopingChores, int msecs) {
for (int i = 0; i < _numChores; i++) {
if (ignoreLoopingChores && _chores[i]->isLooping()) {
continue;
}
_chores[i]->stop();
_playingChores.remove(_chores[i]);
_chores[i]->stop(msecs);
}
}
void Costume::fadeChoreIn(int chore, int msecs) {
void Costume::fadeChoreIn(int chore, uint msecs) {
if (chore < 0 || chore >= _numChores) {
Debug::warning(Debug::Chores, "Requested chore number %d is outside the range of chores (0-%d)", chore, _numChores);
return;
@ -401,7 +399,7 @@ void Costume::fadeChoreIn(int chore, int msecs) {
}
}
void Costume::fadeChoreOut(int chore, int msecs) {
void Costume::fadeChoreOut(int chore, uint msecs) {
if (chore < 0 || chore >= _numChores) {
Debug::warning(Debug::Chores, "Requested chore number %d is outside the range of chores (0-%d)", chore, _numChores);
return;

View File

@ -51,20 +51,20 @@ public:
virtual void load(Common::SeekableReadStream *data);
const Common::String &getFilename() const { return _fname; }
void playChore(const char *name);
virtual void playChore(int num);
void playChoreLooping(const char *name);
virtual void playChoreLooping(int num);
void playChore(const char *name, uint msecs = 0);
virtual void playChore(int num, uint msecs = 0);
void playChoreLooping(const char *name, uint msecs = 0);
virtual void playChoreLooping(int num, uint msecs = 0);
void setChoreType(int num, Chore::ChoreType choreType);
void setChoreLastFrame(int num);
void setChoreLooping(int num, bool val);
void stopChore(int num);
void fadeChoreIn(int chore, int msecs);
void fadeChoreOut(int chore, int msecs);
void stopChore(int num, uint msecs = 0);
void fadeChoreIn(int chore, uint msecs);
void fadeChoreOut(int chore, uint msecs);
ModelNode *getModelNodes();
Model *getModel();
void setColormap(const Common::String &map);
void stopChores(bool ignoreLoopingChores = false);
void stopChores(bool ignoreLoopingChores = false, int msecs = 0);
int isChoring(const char *name, bool excludeLooping);
int isChoring(int num, bool excludeLooping);
int isChoring(bool excludeLooping);

View File

@ -64,22 +64,28 @@ void Chore::load(TextSplitter &ts) {
}
}
void Chore::play() {
void Chore::play(uint msecs) {
_playing = true;
_hasPlayed = true;
_looping = false;
_currTime = -1;
fade(Animation::None, 0);
if (msecs > 0)
fade(Animation::FadeIn, msecs);
else
fade(Animation::None, 0);
}
void Chore::playLooping() {
void Chore::playLooping(uint msecs) {
_playing = true;
_hasPlayed = true;
_looping = true;
_currTime = -1;
fade(Animation::None, 0);
if (msecs > 0)
fade(Animation::FadeIn, msecs);
else
fade(Animation::None, 0);
}
Component *Chore::getComponentForTrack(int i) const {
@ -89,7 +95,10 @@ Component *Chore::getComponentForTrack(int i) const {
return _owner->_components[_tracks[i].compID];
}
void Chore::stop() {
void Chore::stop(uint msecs) {
if (msecs > 0)
fade(Animation::FadeOut, msecs);
_playing = false;
_hasPlayed = false;
@ -165,6 +174,17 @@ void Chore::update(uint time) {
}
void Chore::fade(Animation::FadeMode mode, uint msecs) {
if (mode == Animation::FadeIn) {
if (!_playing) {
_playing = true;
_hasPlayed = true;
_currTime = -1;
}
} else if (mode == Animation::FadeOut) {
// Stop the chore, but do not alter the components state.
_playing = false;
}
for (int i = 0; i < _numTracks; i++) {
Component *comp = getComponentForTrack(i);
if (comp) {
@ -174,12 +194,6 @@ void Chore::fade(Animation::FadeMode mode, uint msecs) {
}
void Chore::fadeIn(uint msecs) {
if (!_playing) {
_playing = true;
_hasPlayed = true;
_currTime = -1;
}
fade(Animation::FadeIn, msecs);
}
@ -187,9 +201,6 @@ void Chore::fadeOut(uint msecs) {
// Note: It doesn't matter whether the chore is playing or not. The keyframe
// components should fade out in either case.
fade(Animation::FadeOut, msecs);
// Stop the chore, but do not alter the components state.
_playing = false;
}
void Chore::saveState(SaveGame *state) const {

View File

@ -57,11 +57,11 @@ public:
virtual ~Chore();
void load(TextSplitter &ts);
void play();
void playLooping();
virtual void play(uint msecs);
virtual void playLooping(uint msecs);
void setLooping(bool val) { _looping = val; }
void stop();
void update(uint time);
virtual void stop(uint msecs);
virtual void update(uint time);
void setLastFrame();
void fadeIn(uint msecs);
void fadeOut(uint msecs);
@ -81,9 +81,9 @@ public:
void restoreState(SaveGame *state);
ChoreType getChoreType() const { return _choreType; };
void setChoreType(ChoreType choreType) { _choreType = choreType; };
private:
protected:
void setKeys(int startTime, int stopTime);
void fade(Animation::FadeMode, uint msecs);
virtual void fade(Animation::FadeMode, uint msecs);
Component *getComponentForTrack(int i) const;
Costume *_owner;

View File

@ -210,8 +210,8 @@ Bone::~Bone() {
}
AnimationStateEmi::AnimationStateEmi(const Common::String &anim) :
_skel(NULL), _looping(false), _active(false),
_fadeMode(Animation::None), _fade(1.0f), _fadeLength(0), _time(0.0f) {
_skel(NULL), _looping(false), _active(false),
_fadeMode(Animation::None), _fade(1.0f), _fadeLength(0), _time(0.0f), _startFade(1.0f) {
_anim = g_resourceloader->getAnimationEmi(anim);
}
@ -258,13 +258,13 @@ void AnimationStateEmi::update(uint time) {
if (_fadeMode != Animation::None) {
if (_fadeMode == Animation::FadeIn) {
_fade += (float)time / _fadeLength;
_fade += (float)time * (1.0f - _startFade) / _fadeLength;
if (_fade >= 1.f) {
_fade = 1.f;
_fadeMode = Animation::None;
}
} else {
_fade -= (float)time / _fadeLength;
_fade -= (float)time * _startFade / _fadeLength;
if (_fade <= 0.f) {
_fade = 0.f;
// Don't reset the _fadeMode here. This way if fadeOut() was called
@ -275,9 +275,6 @@ void AnimationStateEmi::update(uint time) {
}
}
}
else {
_fade = 1.f;
}
}
void AnimationStateEmi::play() {
@ -285,18 +282,16 @@ void AnimationStateEmi::play() {
_time = 0.f;
if (_fadeMode == Animation::FadeOut)
_fadeMode = Animation::None;
activate();
if (_fadeMode == Animation::FadeIn || _fade > 0.f)
activate();
}
_paused = false;
}
void AnimationStateEmi::stop() {
if (_fadeMode != Animation::FadeOut) {
_fadeMode = Animation::None;
_time = 0.f;
_fade = 1.f;
deactivate();
}
_fadeMode = Animation::None;
_time = 0.f;
deactivate();
}
void AnimationStateEmi::pause() {
@ -318,12 +313,12 @@ void AnimationStateEmi::setSkeleton(Skeleton *skel) {
}
void AnimationStateEmi::fade(Animation::FadeMode mode, int fadeLength) {
if (!_active) {
if (mode == Animation::FadeIn) {
_fade = 0.f;
}
if (mode == Animation::None) {
_fade = 1.f;
} else if (_fadeMode != Animation::FadeOut && mode == Animation::FadeIn) {
_fade = 0.f;
}
_startFade = _fade;
_fadeMode = mode;
_fadeLength = fadeLength;
}

View File

@ -97,6 +97,7 @@ private:
bool _paused;
float _time;
float _fade;
float _startFade;
Animation::FadeMode _fadeMode;
int _fadeLength;
};

View File

@ -26,7 +26,8 @@
namespace Grim {
EMIChore::EMIChore(char name[32], int id, Costume *owner, int length, int numTracks) :
Chore(name, id, owner, length, numTracks), _mesh(NULL), _skeleton(NULL) {
Chore(name, id, owner, length, numTracks), _mesh(NULL), _skeleton(NULL),
_fadeMode(Animation::None), _fade(1.f), _fadeLength(0), _startFade(1.0f) {
}
void EMIChore::addComponent(Component *component) {
@ -40,4 +41,78 @@ void EMIChore::addComponent(Component *component) {
}
}
void EMIChore::update(uint time) {
if (!_playing)
return;
if (_fadeMode != Animation::None) {
if (_fadeMode == Animation::FadeIn) {
_fade += (float)time * (1.0f - _startFade) / _fadeLength;
if (_fade >= 1.f) {
_fade = 1.f;
_fadeMode = Animation::None;
}
} else {
_fade -= (float)time * _startFade / _fadeLength;
if (_fade <= 0.f) {
_fade = 0.f;
stop(0);
return;
}
}
}
int newTime;
if (_currTime < 0)
newTime = 0; // For first time through
else
newTime = _currTime + time;
setKeys(_currTime, newTime);
if (newTime > _length) {
if (!_looping && _fadeMode != Animation::FadeOut) {
stop(0);
}
else {
do {
newTime -= _length;
setKeys(-1, newTime);
} while (newTime > _length);
}
}
_currTime = newTime;
}
void EMIChore::stop(uint msecs) {
if (msecs > 0) {
fade(Animation::FadeOut, msecs);
} else {
_playing = false;
_hasPlayed = false;
for (int i = 0; i < _numTracks; i++) {
Component *comp = getComponentForTrack(i);
if (comp)
comp->reset();
}
}
}
void EMIChore::fade(Animation::FadeMode mode, uint msecs) {
if (mode == Animation::None) {
_fade = 1.0f;
}
_startFade = _fade;
_fadeMode = mode;
_fadeLength = msecs;
for (int i = 0; i < _numTracks; i++) {
Component *comp = getComponentForTrack(i);
if (comp) {
comp->fade(mode, msecs);
}
}
}
} // end of namespace Grim

View File

@ -35,12 +35,20 @@ public:
EMIChore(char name[32], int id, Costume *owner, int length, int numTracks);
static int32 getStaticTag() { return MKTAG('C', 'H', 'O', 'R'); }
void update(uint msecs) override;
void stop(uint msecs) override;
void fade(Animation::FadeMode mode, uint msecs) override;
void addComponent(Component *component);
bool isWearChore() { return _mesh && _skeleton; }
EMIMeshComponent *getMesh() { return _mesh; }
EMISkelComponent *getSkeleton() { return _skeleton; }
private:
Animation::FadeMode _fadeMode;
float _fade;
float _startFade;
int _fadeLength;
EMIMeshComponent *_mesh;
EMISkelComponent *_skeleton;
};

View File

@ -130,20 +130,22 @@ void EMICostume::load(Common::SeekableReadStream *data) {
_isWearChoreActive = true;
}
void EMICostume::playChore(int num) {
void EMICostume::playChore(int num, uint msecs) {
// FIXME: Original EMI can play multiple instances of a chore at the same time.
EMIChore *chore = static_cast<EMIChore *>(_chores[num]);
if (chore->isWearChore()) {
setWearChore(chore);
}
Costume::playChore(num);
Costume::playChore(num, msecs);
}
void EMICostume::playChoreLooping(int num) {
void EMICostume::playChoreLooping(int num, uint msecs) {
// FIXME: Original EMI can play multiple instances of a chore at the same time.
EMIChore *chore = static_cast<EMIChore *>(_chores[num]);
if (chore->isWearChore()) {
setWearChore(chore);
}
Costume::playChoreLooping(num);
Costume::playChoreLooping(num, msecs);
}
Component *EMICostume::loadEMIComponent(Component *parent, int parentID, const char *name, Component *prevComponent) {

View File

@ -43,10 +43,11 @@ public:
void load(Common::SeekableReadStream *data);
void playChore(int num) override;
void playChoreLooping(int num) override;
void draw();
int update(uint time);
void playChore(int num, uint msecs = 0) override;
void playChoreLooping(int num, uint msecs = 0) override;
void saveState(SaveGame *state) const override;
bool restoreState(SaveGame *state) override;

View File

@ -321,25 +321,22 @@ void Lua_V2::PauseChore() {
void Lua_V2::StopChore() {
lua_Object choreObj = lua_getparam(1);
lua_Object timeObj = lua_getparam(2);
lua_Object fadeTimeObj = lua_getparam(2);
if (!lua_isuserdata(choreObj) || lua_tag(choreObj) != MKTAG('C','H','O','R'))
return;
int chore = lua_getuserdata(choreObj);
float time = 0.0f;
float fadeTime = 0.0f;
if (!lua_isnil(timeObj)) {
if (lua_isnumber(timeObj))
time = lua_getnumber(timeObj);
if (!lua_isnil(fadeTimeObj)) {
if (lua_isnumber(fadeTimeObj))
fadeTime = lua_getnumber(fadeTimeObj);
}
Chore *c = EMIChore::getPool().getObject(chore);
if (c) {
if (time != 0.0f) {
c->fadeOut((int)(time * 1000));
}
c->stop();
c->stop((int)(fadeTime * 1000));
}
}
@ -651,7 +648,6 @@ bool Lua_V2::findCostume(lua_Object costumeObj, Actor *actor, Costume **costume)
return (*costume != NULL);
}
// TODO: Implement, verify, and rename unknown 5th parameter
void Lua_V2::PlayActorChore() {
lua_Object actorObj = lua_getparam(1);
lua_Object choreObj = lua_getparam(2);
@ -659,7 +655,7 @@ void Lua_V2::PlayActorChore() {
lua_Object modeObj = lua_getparam(4);
lua_Object fadeTimeObj = lua_getparam(5);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A', 'C', 'T', 'R'))
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
@ -717,14 +713,9 @@ void Lua_V2::PlayActorChore() {
}
if (mode) {
costume->playChoreLooping(choreName);
costume->playChoreLooping(choreName, (int)(fadeTime * 1000));
} else {
costume->playChore(choreName);
}
if (fadeTime != 0.0f) {
Chore *c = EMIChore::getPool().getObject(chore->getId());
if (c)
c->fadeIn((int)(fadeTime * 1000));
costume->playChore(choreName, (int)(fadeTime * 1000));
}
if (chore) {
lua_pushusertag(chore->getId(), MKTAG('C','H','O','R'));