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


Generated on Sat Jul 20 2019 05:00:58 for ResidualVM by doxygen 1.7.1
curved edge   curved edge