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

location.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/location.h"
00024 
00025 #include "engines/stark/formats/xrc.h"
00026 #include "engines/stark/gfx/driver.h"
00027 
00028 #include "engines/stark/movement/movement.h"
00029 
00030 #include "engines/stark/resources/anim.h"
00031 #include "engines/stark/resources/container.h"
00032 #include "engines/stark/resources/item.h"
00033 #include "engines/stark/resources/layer.h"
00034 #include "engines/stark/resources/level.h"
00035 #include "engines/stark/resources/scroll.h"
00036 #include "engines/stark/resources/sound.h"
00037 
00038 #include "engines/stark/scene.h"
00039 #include "engines/stark/services/global.h"
00040 #include "engines/stark/services/services.h"
00041 #include "engines/stark/services/stateprovider.h"
00042 
00043 #include "common/random.h"
00044 
00045 namespace Stark {
00046 namespace Resources {
00047 
00048 Location::~Location() {
00049 }
00050 
00051 Location::Location(Object *parent, byte subType, uint16 index, const Common::String &name) :
00052         Object(parent, subType, index, name),
00053         _canScroll(false),
00054         _currentLayer(nullptr),
00055         _hasActiveScroll(false),
00056         _scrollFollowCharacter(false),
00057         _rumbleDurationRemaining(0),
00058         _fadeOut(false),
00059         _fadePosition(0),
00060         _fadeDuration(0),
00061         _swayPeriodMs(0),
00062         _swayAmplitude(0),
00063         _swayOffset(0),
00064         _swayPosition(0),
00065         _idleActionWaitMs(5500),
00066         _floatPeriodMs(0),
00067         _floatAmplitude(0),
00068         _floatPosition(0) {
00069     _type = TYPE;
00070 }
00071 
00072 void Location::onAllLoaded() {
00073     Object::onAllLoaded();
00074 
00075     _layers = listChildren<Layer>();
00076 }
00077 
00078 void Location::onGameLoop() {
00079     Object::onGameLoop();
00080 
00081     ModelItem *april = StarkGlobal->getCurrent()->getInteractive();
00082     if (april) {
00083         _idleActionWaitMs -= StarkGlobal->getMillisecondsPerGameloop();
00084         if (_idleActionWaitMs <= 0) {
00085             if (!april->getActionAnim() && april->getAnimActivity() == Anim::kActorActivityIdle) {
00086                 Anim *idleAction = april->getIdleActionAnim();
00087                 if (idleAction) {
00088                     april->playActionAnim(idleAction);
00089                 }
00090             }
00091 
00092             _idleActionWaitMs = 11000; // 330 frames at 30 fps
00093         }
00094     }
00095 
00096     if (_floatPeriodMs > 0) {
00097         _floatPosition += StarkGlobal->getMillisecondsPerGameloop() / (float) _floatPeriodMs;
00098         if (_floatPosition > 1.0) {
00099             _floatPosition -= 1.0;
00100         }
00101 
00102         float floatOffset = sinf(_floatPosition * 2.0f * M_PI) * _floatAmplitude;
00103         StarkScene->setFloatOffset(floatOffset);
00104     }
00105 
00106     if (_swayPeriodMs > 0) {
00107         _swayPosition += StarkGlobal->getMillisecondsPerGameloop() / (float) _swayPeriodMs;
00108         if (_swayPosition > 1.0) {
00109             _swayPosition -= 1.0;
00110         }
00111 
00112         float sway = sinf((_swayOffset + _swayPosition) * 2.0f * M_PI) * _swayAmplitude;
00113         StarkScene->setSwayAngle(_swayAngle * sway);
00114     }
00115 
00116     if (_fadeDuration > 0) {
00117         float fadeSpeed = StarkGlobal->getMillisecondsPerGameloop() / (float) _fadeDuration;
00118 
00119         _fadePosition += fadeSpeed * (_fadeOut ? -1.0 : 1.0);
00120 
00121         if (_fadeOut && _fadePosition < 0.0) {
00122             _fadePosition = 0.0;
00123             _fadeDuration = 0;
00124         } else if (!_fadeOut && _fadePosition > 1.0) {
00125             _fadePosition = 1.0;
00126             _fadeDuration = 0;
00127         }
00128 
00129         StarkScene->setFadeLevel(_fadePosition);
00130     }
00131 
00132     if (_hasActiveScroll) {
00133         // Script triggered scrolling has precedence over following the character
00134         _scrollFollowCharacter = false;
00135     }
00136 
00137     if (_scrollFollowCharacter) {
00138         assert(april);
00139 
00140         Movement *movement = april->getMovement();
00141 
00142         bool scrollComplete = scrollToCharacter(april);
00143         if (scrollComplete && (!movement || movement->hasEnded())) {
00144             _scrollFollowCharacter = false;
00145         }
00146     }
00147 
00148     if (_rumbleDurationRemaining > 0) {
00149         _rumbleDurationRemaining -= StarkGlobal->getMillisecondsPerGameloop();
00150     }
00151 }
00152 
00153 bool Location::has3DLayer() {
00154     return findChildWithSubtype<Layer>(Layer::kLayer3D) != nullptr;
00155 }
00156 
00157 Gfx::RenderEntryArray Location::listRenderEntries() {
00158     Gfx::RenderEntryArray renderEntries;
00159 
00160     for (uint i = 0; i < _layers.size(); i++) {
00161         Layer *layer = _layers[i];
00162         if (layer->isEnabled()) {
00163             Common::Point baseScroll;
00164 
00165             if (_rumbleDurationRemaining > 0) {
00166                 baseScroll = layer->getScroll();
00167                 Common::Point offsetScroll = baseScroll;
00168                 offsetScroll.x = StarkRandomSource->getRandomBit() - 1;
00169                 offsetScroll.y = StarkRandomSource->getRandomBit() - 1;
00170 
00171                 layer->setScroll(offsetScroll);
00172             }
00173 
00174             renderEntries.push_back(layer->listRenderEntries());
00175 
00176             if (_rumbleDurationRemaining > 0) {
00177                 layer->setScroll(baseScroll);
00178             }
00179         }
00180     }
00181 
00182     return renderEntries;
00183 }
00184 
00185 Gfx::LightEntryArray Location::listLightEntries() {
00186     Gfx::LightEntry *ambient = nullptr;
00187     Gfx::LightEntryArray others;
00188 
00189     // Build a list of lights from all the layers ...
00190     for (uint i = 0; i < _layers.size(); i++) {
00191         Layer *layer = _layers[i];
00192         if (layer->isEnabled()) {
00193             Gfx::LightEntryArray layerLights = layer->listLightEntries();
00194 
00195             for (uint j = 0; j < layerLights.size(); j++) {
00196                 Gfx::LightEntry *light = layerLights[j];
00197 
00198                 // ... but store the ambient light in a separate variable ...
00199                 if (light->type == Gfx::LightEntry::kAmbient) {
00200                     ambient = light;
00201                 } else {
00202                     others.push_back(light);
00203                 }
00204             }
00205         }
00206     }
00207 
00208     // ... so that it is first in the final light list
00209     Gfx::LightEntryArray lightEntries;
00210     lightEntries.push_back(ambient);
00211     lightEntries.push_back(others);
00212     return lightEntries;
00213 }
00214 
00215 void Location::initScroll(const Common::Point &maxScroll) {
00216     _maxScroll = maxScroll;
00217     _canScroll = _maxScroll.x != 0 || _maxScroll.y != 0;
00218 }
00219 
00220 Common::Point Location::getScrollPosition() const {
00221     return _scroll;
00222 }
00223 
00224 void Location::setScrollPosition(const Common::Point &position) {
00225     _scroll.x = CLIP<int16>(position.x, 0, _maxScroll.x);
00226     _scroll.y = CLIP<int16>(position.y, 0, _maxScroll.y);
00227 
00228 
00229     // Setup the layers scroll position
00230     for (uint i = 0; i < _layers.size(); i++) {
00231         _layers[i]->setScrollPosition(_scroll);
00232     }
00233 
00234     // Reconfigure the camera
00235     Common::Rect viewport(Gfx::Driver::kGameViewportWidth, Gfx::Driver::kGameViewportHeight);
00236     viewport.translate(_scroll.x, _scroll.y);
00237     StarkScene->scrollCamera(viewport);
00238 }
00239 
00240 Common::Point Location::getCharacterScrollPosition(ModelItem *item) {
00241     Common::Point position2D = StarkScene->convertPosition3DToGameScreenOriginal(item->getPosition3D());
00242 
00243     Common::Point newScroll;
00244     if (_maxScroll.x > 0) {
00245         newScroll.x = _scroll.x + position2D.x - Gfx::Driver::kGameViewportWidth / 2;
00246         newScroll.y = _scroll.y;
00247     } else {
00248         Gfx::RenderEntry *renderEntry = item->getRenderEntry(_scroll);
00249         Common::Rect boundingRect = renderEntry->getBoundingRect();
00250         if (!boundingRect.isEmpty()) {
00251             position2D.y = (boundingRect.top + boundingRect.bottom) / 2;
00252         }
00253 
00254         newScroll.x = _scroll.x;
00255         newScroll.y = _scroll.y + position2D.y - Gfx::Driver::kGameViewportHeight / 2;
00256     }
00257 
00258     return newScroll;
00259 }
00260 
00261 bool Location::scrollToCharacter(ModelItem *item) {
00262     if (!_canScroll) {
00263         return true;
00264     }
00265 
00266     Common::Point newScroll = getCharacterScrollPosition(item);
00267     if (_maxScroll.x > 0) {
00268         if (newScroll.x < _scroll.x - 15 || newScroll.x > _scroll.x + 15) {
00269             newScroll.x = CLIP<int16>(newScroll.x, 0, _maxScroll.x);
00270             return scrollToSmooth(newScroll, true);
00271         }
00272     } else {
00273         if (newScroll.y < _scroll.y - 15 || newScroll.y > _scroll.y + 15) {
00274             newScroll.y = CLIP<int16>(newScroll.y, 0, _maxScroll.y);
00275             return scrollToSmooth(newScroll, true);
00276         }
00277     }
00278 
00279     return false;
00280 }
00281 
00282 void Location::scrollToCharacterImmediate() {
00283     if (!_canScroll) {
00284         return;
00285     }
00286 
00287     ModelItem *april = StarkGlobal->getCurrent()->getInteractive();
00288     setScrollPosition(getCharacterScrollPosition(april));
00289 }
00290 
00291 uint Location::getScrollStepFollow() {
00292     ModelItem *april = StarkGlobal->getCurrent()->getInteractive();
00293     Common::Point position2D = StarkScene->convertPosition3DToGameScreenOriginal(april->getPosition3D());
00294 
00295     // TODO: Complete
00296 
00297     uint scrollStep;
00298     if (_maxScroll.x > 0) {
00299         scrollStep = abs((Gfx::Driver::kGameViewportWidth / 2 - position2D.x) / 16);
00300     } else {
00301         scrollStep = abs((Gfx::Driver::kGameViewportHeight / 2 - position2D.y) / 16);
00302     }
00303 
00304     return CLIP<uint>(scrollStep, 1, 4);
00305 }
00306 
00307 uint Location::getScrollStep() {
00308     uint scrollStep;
00309     if (_maxScroll.x > 0) {
00310         if (_scroll.x <= _maxScroll.x / 2) {
00311             scrollStep = _scroll.x / 16;
00312         } else {
00313             scrollStep = (_maxScroll.x - _scroll.x) / 16;
00314         }
00315     } else {
00316         if (_scroll.y <= _maxScroll.y / 2) {
00317             scrollStep = _scroll.y / 16;
00318         } else {
00319             scrollStep = (_maxScroll.y - _scroll.y) / 16;
00320         }
00321     }
00322 
00323     return CLIP<uint>(scrollStep, 1, 4);
00324 }
00325 
00326 bool Location::scrollToSmooth(const Common::Point &position, bool followCharacter) {
00327     uint scrollStep;
00328     if (followCharacter) {
00329         scrollStep = getScrollStepFollow();
00330     } else {
00331         scrollStep = getScrollStep();
00332     }
00333 
00334     Common::Point delta;
00335     if (position.x < _scroll.x) {
00336         delta.x = -scrollStep;
00337         delta.x = CLIP<int16>(delta.x, position.x - _scroll.x, 0);
00338     } else if (position.x > _scroll.x) {
00339         delta.x = scrollStep;
00340         delta.x = CLIP<int16>(delta.x, 0, position.x - _scroll.x);
00341     }
00342 
00343     if (position.y < _scroll.y) {
00344         delta.y = -scrollStep;
00345         delta.y = CLIP<int16>(delta.y, position.y - _scroll.y, 0);
00346     } else if (position.y > _scroll.y) {
00347         delta.y = scrollStep;
00348         delta.y = CLIP<int16>(delta.y, 0, position.y - _scroll.y);
00349     }
00350 
00351     if (delta.x == 0 && delta.y == 0) {
00352         // We already are at the target position, scrolling has completed
00353         return true;
00354     }
00355 
00356     setScrollPosition(_scroll + delta);
00357     return false;
00358 }
00359 
00360 bool Location::scrollToCoordinateSmooth(uint32 coordinate) {
00361     Common::Point newScroll = getScrollPointFromCoordinate(coordinate);
00362     return scrollToSmooth(newScroll, false);
00363 }
00364 
00365 void Location::scrollToCoordinateImmediate(uint32 coordinate) {
00366     Common::Point newScroll = getScrollPointFromCoordinate(coordinate);
00367     return setScrollPosition(newScroll);
00368 }
00369 
00370 Common::Point Location::getScrollPointFromCoordinate(uint32 coordinate) const {
00371     Common::Point newScroll = _scroll;
00372 
00373     if (_maxScroll.x > 0) {
00374         newScroll.x = coordinate;
00375     } else {
00376         newScroll.y = coordinate;
00377     }
00378 
00379     return newScroll;
00380 }
00381 
00382 void Location::stopFollowingCharacter() {
00383     _scrollFollowCharacter = false;
00384 }
00385 
00386 void Location::startFollowingCharacter() {
00387     _scrollFollowCharacter = true;
00388 }
00389 
00390 void Location::setHasActiveScroll() {
00391     _hasActiveScroll = true;
00392 }
00393 
00394 void Location::stopAllScrolls() {
00395     Common::Array<Scroll *> scrolls = listChildrenRecursive<Scroll>();
00396     for (uint i = 0; i < scrolls.size(); i++) {
00397         scrolls[i]->stop();
00398     }
00399 
00400     _hasActiveScroll = false;
00401 }
00402 
00403 void Location::goToLayer(Layer *layer) {
00404     if (_currentLayer) {
00405         _currentLayer->enable(false);
00406     }
00407 
00408     layer->enable(true);
00409     _currentLayer = layer;
00410 }
00411 
00412 ItemVisual *Location::getCharacterItem(int32 character) const {
00413     return _characterItemMap.getVal(character, nullptr);
00414 }
00415 
00416 void Location::registerCharacterItem(int32 character, ItemVisual *item) {
00417     if (character >= 0) {
00418         _characterItemMap[character] = item;
00419     }
00420 }
00421 
00422 Common::Array<ItemVisual *> Location::listCharacters() const {
00423     Common::Array<ItemVisual *> characters;
00424 
00425     CharacterMap::const_iterator it;
00426     for (it = _characterItemMap.begin(); it != _characterItemMap.end(); it++) {
00427         characters.push_back(it->_value);
00428     }
00429 
00430     return characters;
00431 }
00432 
00433 void Location::printData() {
00434 }
00435 
00436 void Location::resetAnimationBlending() {
00437     Common::Array<ModelItem *> items = listChildren<ModelItem>(Item::kItemModel);
00438     for (uint i = 0; i < items.size(); i++) {
00439         items[i]->resetAnimationBlending();
00440     }
00441 }
00442 
00443 Sound *Location::findStockSound(uint32 stockSoundType) const {
00444     Sound *sound = findStockSound(this, stockSoundType);
00445 
00446     if (!sound) {
00447         Level *currentLevel = StarkGlobal->getCurrent()->getLevel();
00448         sound = findStockSound(currentLevel, stockSoundType);
00449     }
00450 
00451     if (!sound) {
00452         Level *globalLevel = StarkGlobal->getLevel();
00453         sound = findStockSound(globalLevel, stockSoundType);
00454     }
00455 
00456     return sound;
00457 }
00458 
00459 Sound *Location::findStockSound(const Object *parent, uint32 stockSoundType) const {
00460     Container *stockSoundContainer = parent->findChildWithSubtype<Container>(Container::kStockSounds);
00461     if (stockSoundContainer) {
00462         Common::Array<Sound *> stockSounds = stockSoundContainer->listChildren<Sound>(Sound::kSoundStock);
00463 
00464         for (uint i = 0; i < stockSounds.size(); i++) {
00465             Sound *sound = stockSounds[i];
00466             if (sound->getStockSoundType() == stockSoundType) {
00467                 return sound;
00468             }
00469         }
00470     }
00471 
00472     return nullptr;
00473 }
00474 
00475 void Location::startRumble(int32 rumbleDurationRemaining) {
00476     _rumbleDurationRemaining = rumbleDurationRemaining;
00477 }
00478 
00479 void Location::fadeInInit(int32 fadeDuration) {
00480     _fadeOut = false;
00481     _fadePosition = 0.0;
00482     _fadeDuration = fadeDuration;
00483 }
00484 
00485 void Location::fadeOutInit(int32 fadeDuration) {
00486     _fadeOut = true;
00487     _fadePosition = 1.0;
00488     _fadeDuration = fadeDuration;
00489 }
00490 
00491 void Location::swayScene(int32 periodMs, const Math::Angle &angle, float amplitude, float offset) {
00492     if (periodMs < 33) {
00493         periodMs = 1000;
00494     }
00495 
00496     _swayPeriodMs = periodMs;
00497     _swayAngle = angle;
00498     _swayAmplitude = amplitude;
00499     _swayOffset = offset;
00500     _swayPosition = offset;
00501 }
00502 
00503 void Location::floatScene(int32 periodMs, float amplitude, float offset) {
00504     if (periodMs < 33) {
00505         periodMs = 1000;
00506     }
00507 
00508     _floatPeriodMs = periodMs;
00509     _floatAmplitude = amplitude;
00510     _floatPosition = offset;
00511 }
00512 
00513 void Location::saveLoadCurrent(ResourceSerializer *serializer) {
00514     serializer->syncAsSint32LE(_scroll.x);
00515     serializer->syncAsSint32LE(_scroll.y);
00516 
00517     if (serializer->isLoading()) {
00518         setScrollPosition(_scroll);
00519     }
00520 
00521     serializer->syncAsResourceReference(&_currentLayer);
00522 
00523     serializer->syncAsSint32LE(_floatPeriodMs);
00524     serializer->syncAsFloat(_floatAmplitude);
00525     serializer->syncAsFloat(_floatPosition);
00526 
00527     serializer->syncAsSint32LE(_swayPeriodMs);
00528     serializer->syncAsFloat(_swayAmplitude);
00529     serializer->syncAsFloat(_swayOffset);
00530     serializer->syncAsFloat(_swayPosition);
00531 
00532     float swayAngle = _swayAngle.getDegrees();
00533     serializer->syncAsFloat(swayAngle);
00534     if (serializer->isLoading()) {
00535         _swayAngle = swayAngle;
00536     }
00537 }
00538 
00539 Layer *Location::getLayerByName(const Common::String &name) {
00540     for (uint i = 0; i < _layers.size(); ++i) {
00541         if (_layers[i]->getName().equalsIgnoreCase(name)) {
00542             return _layers[i];
00543         }
00544     }
00545     return nullptr;
00546 }
00547 
00548 Gfx::RenderEntry *Location::getRenderEntryByName(const Common::String &name) {
00549     Gfx::RenderEntryArray renderEntries = listRenderEntries();
00550     for (uint i = 0; i < renderEntries.size(); ++i) {
00551         if (renderEntries[i]->getName().equalsIgnoreCase(name)) {
00552             return renderEntries[i];
00553         }
00554     }
00555     return nullptr;
00556 }
00557 
00558 Common::Array<Common::Point> Location::listExitPositions() {
00559     Common::Array<Item *> items = listChildrenRecursive<Item>();
00560     Common::Array<Common::Point> positions;
00561 
00562     Common::Array<Item *>::iterator element = items.begin();
00563     while (element != items.end()) {
00564         positions.push_back((*element)->listExitPositions());
00565         ++element;
00566     }
00567 
00568     return positions;
00569 }
00570 
00571 } // End of namespace Resources
00572 } // End of namespace Stark


Generated on Sat May 18 2019 05:01:07 for ResidualVM by doxygen 1.7.1
curved edge   curved edge