1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "engines/stark/resources/animhierarchy.h"
#include "common/debug.h"
#include "common/random.h"
#include "engines/stark/formats/xrc.h"
#include "engines/stark/resources/anim.h"
#include "engines/stark/resources/bonesmesh.h"
#include "engines/stark/resources/item.h"
#include "engines/stark/resources/textureset.h"
#include "engines/stark/services/services.h"
namespace Stark {
namespace Resources {
AnimHierarchy::~AnimHierarchy() {
}
AnimHierarchy::AnimHierarchy(Object *parent, byte subType, uint16 index, const Common::String &name) :
Object(parent, subType, index, name),
_currentActivity(0),
_currentAnim(nullptr),
_field_5C(0),
_idleActionsFrequencySum(0) {
_type = TYPE;
}
void AnimHierarchy::readData(Formats::XRCReadStream *stream) {
_animationReferences.clear();
uint32 refCount = stream->readUint32LE();
for (uint32 i = 0; i < refCount; i++) {
_animationReferences.push_back(stream->readResourceReference());
}
_parentAnimHierarchyReference = stream->readResourceReference();
_field_5C = stream->readFloatLE();
}
void AnimHierarchy::onAllLoaded() {
Object::onAllLoaded();
loadActivityAnimations();
loadIdleAnimations();
}
void AnimHierarchy::loadActivityAnimations() {
AnimHierarchy *parentHierarchy = _parentAnimHierarchyReference.resolve<AnimHierarchy>();
// Activity animations are inherited from the parent ...
if (parentHierarchy) {
_activityAnimations = parentHierarchy->_activityAnimations;
}
// ... but can be overridden
for (uint i = 0; i < _animationReferences.size(); i++) {
Anim *anim = _animationReferences[i].resolve<Anim>();
bool inserted = false;
for (uint j = 0; j < _activityAnimations.size(); j++) {
if (_activityAnimations[j]->getActivity() == anim->getActivity()) {
_activityAnimations[j] = anim;
inserted = true;
}
}
if (!inserted) {
_activityAnimations.push_back(anim);
}
}
}
void AnimHierarchy::loadIdleAnimations() {
AnimHierarchy *parentHierarchy = _parentAnimHierarchyReference.resolve<AnimHierarchy>();
if (parentHierarchy) {
_idleAnimations = parentHierarchy->_idleAnimations;
}
for (uint i = 0; i < _animationReferences.size(); i++) {
Anim *anim = _animationReferences[i].resolve<Anim>();
if (anim->getActivity() == Anim::kActorActivityIdleAction) {
_idleAnimations.push_back(anim);
}
}
_idleActionsFrequencySum = 0;
for (uint i = 0; i < _idleAnimations.size(); i++) {
_idleActionsFrequencySum += _idleAnimations[i]->getIdleActionFrequency();
}
}
void AnimHierarchy::setItemAnim(ItemVisual *item, int32 activity) {
unselectItemAnim(item);
_currentActivity = activity;
selectItemAnim(item);
}
void AnimHierarchy::unselectItemAnim(ItemVisual *item) {
if (_currentAnim && _currentAnim->isInUse()) {
_currentAnim->removeFromItem(item);
}
_currentAnim = nullptr;
}
void AnimHierarchy::selectItemAnim(ItemVisual *item) {
// Search for an animation with the appropriate index
for (uint i = 0; i < _activityAnimations.size(); i++) {
if (_activityAnimations[i]->getActivity() == _currentActivity) {
_currentAnim = _activityAnimations[i];
break;
}
}
// Default to the first animation
if (!_currentAnim && !_activityAnimations.empty()) {
_currentAnim = _activityAnimations[0];
}
if (!_currentAnim) {
error("Failed to set an animation for item %s", item->getName().c_str());
}
if (!_currentAnim->isInUse()) {
_currentAnim->applyToItem(item);
}
}
Anim *AnimHierarchy::getCurrentAnim() {
return _currentAnim;
}
BonesMesh *AnimHierarchy::findBonesMesh() {
return findChild<BonesMesh>();
}
TextureSet *AnimHierarchy::findTextureSet(uint32 textureType) {
return findChildWithSubtype<TextureSet>(textureType);
}
Anim *AnimHierarchy::getAnimForActivity(uint32 activity) {
// Search for an animation with the appropriate use
for (uint i = 0; i < _activityAnimations.size(); i++) {
if (_activityAnimations[i]->getActivity() == activity) {
return _activityAnimations[i];
}
}
return nullptr;
}
Visual *AnimHierarchy::getVisualForUsage(uint32 usage) {
Anim *anim = getAnimForActivity(usage);
if (anim) {
return anim->getVisual();
}
return nullptr;
}
Anim *AnimHierarchy::getIdleActionAnim() const {
if (_idleActionsFrequencySum == 0) {
return nullptr; // There are no idle animations
}
int pick = StarkRandomSource->getRandomNumber(_idleActionsFrequencySum - 1);
for (uint i = 0; i < _idleAnimations.size(); i++) {
pick -= _idleAnimations[i]->getIdleActionFrequency();
if (pick < 0) {
return _idleAnimations[i];
}
}
return nullptr;
}
void AnimHierarchy::printData() {
for (uint i = 0; i < _animationReferences.size(); i++) {
debug("anim %d: %s", i, _animationReferences[i].describe().c_str());
}
debug("animHierarchy: %s", _parentAnimHierarchyReference.describe().c_str());
debug("field_5C: %f", _field_5C);
}
} // End of namespace Resources
} // End of namespace Stark
|