ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

animhierarchy.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/stark/resources/animhierarchy.h"
00024 
00025 #include "common/debug.h"
00026 #include "common/random.h"
00027 
00028 #include "engines/stark/formats/xrc.h"
00029 #include "engines/stark/resources/anim.h"
00030 #include "engines/stark/resources/bonesmesh.h"
00031 #include "engines/stark/resources/item.h"
00032 #include "engines/stark/resources/textureset.h"
00033 #include "engines/stark/services/services.h"
00034 
00035 namespace Stark {
00036 namespace Resources {
00037 
00038 AnimHierarchy::~AnimHierarchy() {
00039 }
00040 
00041 AnimHierarchy::AnimHierarchy(Object *parent, byte subType, uint16 index, const Common::String &name) :
00042         Object(parent, subType, index, name),
00043         _currentActivity(0),
00044         _currentAnim(nullptr),
00045         _field_5C(0),
00046         _idleActionsFrequencySum(0) {
00047     _type = TYPE;
00048 }
00049 
00050 void AnimHierarchy::readData(Formats::XRCReadStream *stream) {
00051     _animationReferences.clear();
00052 
00053     uint32 refCount = stream->readUint32LE();
00054     for (uint32 i = 0; i < refCount; i++) {
00055         _animationReferences.push_back(stream->readResourceReference());
00056     }
00057 
00058     _parentAnimHierarchyReference = stream->readResourceReference();
00059     _field_5C = stream->readFloatLE();
00060 }
00061 
00062 void AnimHierarchy::onAllLoaded() {
00063     Object::onAllLoaded();
00064 
00065     loadActivityAnimations();
00066     loadIdleAnimations();
00067 }
00068 
00069 void AnimHierarchy::loadActivityAnimations() {
00070     AnimHierarchy *parentHierarchy = _parentAnimHierarchyReference.resolve<AnimHierarchy>();
00071 
00072     // Activity animations are inherited from the parent ...
00073     if (parentHierarchy) {
00074         _activityAnimations = parentHierarchy->_activityAnimations;
00075     }
00076 
00077     // ... but can be overridden
00078     for (uint i = 0; i < _animationReferences.size(); i++) {
00079         Anim *anim = _animationReferences[i].resolve<Anim>();
00080 
00081         bool inserted = false;
00082         for (uint j = 0; j < _activityAnimations.size(); j++) {
00083             if (_activityAnimations[j]->getActivity() == anim->getActivity()) {
00084                 _activityAnimations[j] = anim;
00085                 inserted = true;
00086             }
00087         }
00088 
00089         if (!inserted) {
00090             _activityAnimations.push_back(anim);
00091         }
00092     }
00093 }
00094 
00095 void AnimHierarchy::loadIdleAnimations() {
00096     AnimHierarchy *parentHierarchy = _parentAnimHierarchyReference.resolve<AnimHierarchy>();
00097     if (parentHierarchy) {
00098         _idleAnimations = parentHierarchy->_idleAnimations;
00099     }
00100 
00101     for (uint i = 0; i < _animationReferences.size(); i++) {
00102         Anim *anim = _animationReferences[i].resolve<Anim>();
00103         if (anim->getActivity() == Anim::kActorActivityIdleAction) {
00104             _idleAnimations.push_back(anim);
00105         }
00106     }
00107 
00108     _idleActionsFrequencySum = 0;
00109     for (uint i = 0; i < _idleAnimations.size(); i++) {
00110         _idleActionsFrequencySum += _idleAnimations[i]->getIdleActionFrequency();
00111     }
00112 }
00113 
00114 void AnimHierarchy::setItemAnim(ItemVisual *item, int32 activity) {
00115     unselectItemAnim(item);
00116     _currentActivity = activity;
00117     selectItemAnim(item);
00118 }
00119 
00120 void AnimHierarchy::unselectItemAnim(ItemVisual *item) {
00121     if (_currentAnim && _currentAnim->isInUse()) {
00122         _currentAnim->removeFromItem(item);
00123     }
00124 
00125     _currentAnim = nullptr;
00126 }
00127 
00128 void AnimHierarchy::selectItemAnim(ItemVisual *item) {
00129     // Search for an animation with the appropriate index
00130     for (uint i = 0; i < _activityAnimations.size(); i++) {
00131         if (_activityAnimations[i]->getActivity() == _currentActivity) {
00132             _currentAnim = _activityAnimations[i];
00133             break;
00134         }
00135     }
00136 
00137     // Default to the first animation
00138     if (!_currentAnim && !_activityAnimations.empty()) {
00139         _currentAnim = _activityAnimations[0];
00140     }
00141 
00142     if (!_currentAnim) {
00143         error("Failed to set an animation for item %s", item->getName().c_str());
00144     }
00145 
00146     if (!_currentAnim->isInUse()) {
00147         _currentAnim->applyToItem(item);
00148     }
00149 }
00150 
00151 Anim *AnimHierarchy::getCurrentAnim() {
00152     return _currentAnim;
00153 }
00154 
00155 BonesMesh *AnimHierarchy::findBonesMesh() {
00156     return findChild<BonesMesh>();
00157 }
00158 
00159 TextureSet *AnimHierarchy::findTextureSet(uint32 textureType) {
00160     return findChildWithSubtype<TextureSet>(textureType);
00161 }
00162 
00163 Anim *AnimHierarchy::getAnimForActivity(uint32 activity) {
00164     // Search for an animation with the appropriate use
00165     for (uint i = 0; i < _activityAnimations.size(); i++) {
00166         if (_activityAnimations[i]->getActivity() == activity) {
00167             return _activityAnimations[i];
00168         }
00169     }
00170 
00171     return nullptr;
00172 }
00173 
00174 Visual *AnimHierarchy::getVisualForUsage(uint32 usage) {
00175     Anim *anim = getAnimForActivity(usage);
00176     if (anim) {
00177         return anim->getVisual();
00178     }
00179 
00180     return nullptr;
00181 }
00182 
00183 Anim *AnimHierarchy::getIdleActionAnim() const {
00184     if (_idleActionsFrequencySum == 0) {
00185         return nullptr; // There are no idle animations
00186     }
00187 
00188     int pick = StarkRandomSource->getRandomNumber(_idleActionsFrequencySum - 1);
00189     for (uint i = 0; i < _idleAnimations.size(); i++) {
00190         pick -= _idleAnimations[i]->getIdleActionFrequency();
00191 
00192         if (pick < 0) {
00193             return _idleAnimations[i];
00194         }
00195     }
00196 
00197     return nullptr;
00198 }
00199 
00200 void AnimHierarchy::printData() {
00201     for (uint i = 0; i < _animationReferences.size(); i++) {
00202         debug("anim %d: %s", i, _animationReferences[i].describe().c_str());
00203     }
00204 
00205     debug("animHierarchy: %s", _parentAnimHierarchyReference.describe().c_str());
00206     debug("field_5C: %f", _field_5C);
00207 }
00208 
00209 } // End of namespace Resources
00210 } // End of namespace Stark


Generated on Sat May 18 2019 05:00:55 for ResidualVM by doxygen 1.7.1
curved edge   curved edge