1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-31 06:32:39 +00:00

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

137 lines
3.9 KiB
C++
Raw Normal View History

#include "objects.hpp"
2018-08-14 23:05:43 +04:00
#include <components/debug/debuglog.hpp>
#include <components/esm3/loadcont.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/world.hpp"
2019-02-19 01:10:55 +03:00
#include "character.hpp"
2016-06-17 23:07:16 +09:00
namespace MWMechanics
{
void Objects::addObject(const MWWorld::Ptr& ptr)
{
removeObject(ptr);
MWRender::Animation* anim = MWBase::Environment::get().getWorld()->getAnimation(ptr);
if (anim == nullptr)
return;
const auto it = mObjects.emplace(mObjects.end(), ptr, anim);
mIndex.emplace(ptr.mRef, it);
}
void Objects::removeObject(const MWWorld::Ptr& ptr)
{
const auto iter = mIndex.find(ptr.mRef);
if (iter != mIndex.end())
{
mObjects.erase(iter->second);
mIndex.erase(iter);
}
}
void Objects::updateObject(const MWWorld::Ptr& old, const MWWorld::Ptr& ptr)
{
const auto iter = mIndex.find(old.mRef);
if (iter != mIndex.end())
iter->second->updatePtr(ptr);
}
2022-09-22 21:26:05 +03:00
void Objects::dropObjects(const MWWorld::CellStore* cellStore)
{
for (auto iter = mObjects.begin(); iter != mObjects.end();)
2022-09-22 21:26:05 +03:00
{
if (iter->getPtr().getCell() == cellStore)
2022-09-22 21:26:05 +03:00
{
mIndex.erase(iter->getPtr().mRef);
iter = mObjects.erase(iter);
2022-09-22 21:26:05 +03:00
}
else
++iter;
2022-09-22 21:26:05 +03:00
}
}
void Objects::update(float duration, bool paused)
2022-09-22 21:26:05 +03:00
{
if (!paused)
2022-09-22 21:26:05 +03:00
{
for (CharacterController& object : mObjects)
object.update(duration);
2022-09-22 21:26:05 +03:00
}
else
{
// We still should play container opening animation in the Container GUI mode.
MWGui::GuiMode mode = MWBase::Environment::get().getWindowManager()->getMode();
if (mode != MWGui::GM_Container)
return;
for (CharacterController& object : mObjects)
{
if (object.getPtr().getType() != ESM::Container::sRecordId)
2022-09-22 21:26:05 +03:00
continue;
if (object.isAnimPlaying("containeropen"))
2022-09-22 21:26:05 +03:00
{
object.update(duration);
MWBase::Environment::get().getWorld()->updateAnimatedCollisionShape(object.getPtr());
2022-09-22 21:26:05 +03:00
}
}
}
}
bool Objects::onOpen(const MWWorld::Ptr& ptr)
{
const auto iter = mIndex.find(ptr.mRef);
if (iter != mIndex.end())
return iter->second->onOpen();
return true;
}
void Objects::onClose(const MWWorld::Ptr& ptr)
{
const auto iter = mIndex.find(ptr.mRef);
if (iter != mIndex.end())
iter->second->onClose();
}
2022-08-23 18:25:25 +02:00
bool Objects::playAnimationGroup(
const MWWorld::Ptr& ptr, std::string_view groupName, int mode, int number, bool persist)
{
const auto iter = mIndex.find(ptr.mRef);
if (iter != mIndex.end())
2022-09-22 21:26:05 +03:00
{
return iter->second->playGroup(groupName, mode, number, persist);
2022-09-22 21:26:05 +03:00
}
else
{
2018-08-14 23:05:43 +04:00
Log(Debug::Warning) << "Warning: Objects::playAnimationGroup: Unable to find "
<< ptr.getCellRef().getRefId();
return false;
2022-09-22 21:26:05 +03:00
}
}
void Objects::skipAnimation(const MWWorld::Ptr& ptr)
{
2018-08-14 23:05:43 +04:00
const auto iter = mIndex.find(ptr.mRef);
if (iter != mIndex.end())
iter->second->skipAnim();
}
void Objects::persistAnimationStates()
{
for (CharacterController& object : mObjects)
object.persistAnimationState();
}
void Objects::getObjectsInRange(const osg::Vec3f& position, float radius, std::vector<MWWorld::Ptr>& out) const
2014-01-20 13:00:43 +01:00
{
for (const CharacterController& object : mObjects)
if ((position - object.getPtr().getRefData().getPosition().asVec3()).length2() <= radius * radius)
out.push_back(object.getPtr());
2014-01-20 13:00:43 +01:00
}
}