register actors in active cells with MechanicsManager

This commit is contained in:
Marc Zinnschlag 2010-07-27 20:05:05 +08:00 committed by apreiml
parent 858f174355
commit 362605860b
5 changed files with 72 additions and 13 deletions

View file

@ -187,7 +187,7 @@ void OMW::Engine::go()
loadBSA(); loadBSA();
// Create the world // Create the world
mEnvironment.mWorld = new MWWorld::World (mOgre, mDataDir, mMaster, mNewGame); mEnvironment.mWorld = new MWWorld::World (mOgre, mDataDir, mMaster, mNewGame, mEnvironment);
// Set up the GUI system // Set up the GUI system
mGuiManager = new OEngine::GUI::MyGUIManager(mOgre.getWindow(), mGuiManager = new OEngine::GUI::MyGUIManager(mOgre.getWindow(),

View file

@ -39,5 +39,23 @@ namespace MWMechanics
mWindowManager.setLabel (names[i][0], label); mWindowManager.setLabel (names[i][0], label);
} }
} }
void MechanicsManager::addActor (const MWWorld::Ptr& ptr)
{
mActors.insert (ptr);
}
void MechanicsManager::dropActors (const MWWorld::Ptr::CellStore *cellStore)
{
std::set<MWWorld::Ptr>::iterator iter = mActors.begin();
while (iter!=mActors.end())
if (iter->getCell()==cellStore)
{
mActors.erase (iter++);
}
else
++iter;
}
} }

View file

@ -1,6 +1,10 @@
#ifndef GAME_MWMECHANICS_MECHANICSMANAGER_H #ifndef GAME_MWMECHANICS_MECHANICSMANAGER_H
#define GAME_MWMECHANICS_MECHANICSMANAGER_H #define GAME_MWMECHANICS_MECHANICSMANAGER_H
#include <set>
#include "../mwworld/ptr.hpp"
namespace ESMS namespace ESMS
{ {
class ESMStore; class ESMStore;
@ -15,14 +19,21 @@ namespace MWMechanics
{ {
class MechanicsManager class MechanicsManager
{ {
const ESMS::ESMStore& mStore; const ESMS::ESMStore& mStore;
MWGui::WindowManager& mWindowManager; MWGui::WindowManager& mWindowManager;
std::set<MWWorld::Ptr> mActors;
public: public:
MechanicsManager (const ESMS::ESMStore& store, MWGui::WindowManager& windowManager); MechanicsManager (const ESMS::ESMStore& store, MWGui::WindowManager& windowManager);
void configureGUI(); void configureGUI();
void addActor (const MWWorld::Ptr& ptr);
///< Register an actor for stats management
void dropActors (const MWWorld::Ptr::CellStore *cellStore);
///< Deregister all actors in the given cell.
}; };
} }

View file

@ -4,12 +4,15 @@
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
#include "components/bsa/bsa_archive.hpp" #include <components/bsa/bsa_archive.hpp>
#include "apps/openmw/mwrender/sky.hpp" #include "../mwrender/sky.hpp"
#include "apps/openmw/mwrender/interior.hpp" #include "../mwrender/interior.hpp"
#include "../mwmechanics/mechanicsmanager.hpp"
#include "ptr.hpp" #include "ptr.hpp"
#include "environment.hpp"
namespace namespace
{ {
@ -164,9 +167,9 @@ namespace MWWorld
} }
World::World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& dataDir, World::World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& dataDir,
const std::string& master, bool newGame) const std::string& master, bool newGame, Environment& environment)
: mSkyManager (0), mScene (renderer), mPlayerPos (0), mCurrentCell (0), mGlobalVariables (0), : mSkyManager (0), mScene (renderer), mPlayerPos (0), mCurrentCell (0), mGlobalVariables (0),
mSky (false), mCellChanged (false) mSky (false), mCellChanged (false), mEnvironment (environment)
{ {
boost::filesystem::path masterPath (dataDir); boost::filesystem::path masterPath (dataDir);
masterPath /= master; masterPath /= master;
@ -413,35 +416,59 @@ namespace MWWorld
{ {
// Load cell. // Load cell.
mInteriors[cellName].loadInt (cellName, mStore, mEsm); mInteriors[cellName].loadInt (cellName, mStore, mEsm);
Ptr::CellStore *cell = &mInteriors[cellName];
// remove active // remove active
CellRenderCollection::iterator active = mActiveCells.begin(); CellRenderCollection::iterator active = mActiveCells.begin();
if (active!=mActiveCells.end()) if (active!=mActiveCells.end())
{ {
mEnvironment.mMechanicsManager->dropActors (active->first);
active->second->destroy(); active->second->destroy();
delete active->second; delete active->second;
mActiveCells.erase (active); mActiveCells.erase (active);
} }
// register local scripts
mLocalScripts.clear(); // FIXME won't work with exteriors mLocalScripts.clear(); // FIXME won't work with exteriors
insertInteriorScripts (mInteriors[cellName]); insertInteriorScripts (*cell);
// adjust player
mPlayerPos->setPos (position.pos[0], position.pos[1], position.pos[2]); mPlayerPos->setPos (position.pos[0], position.pos[1], position.pos[2]);
mPlayerPos->setCell (&mInteriors[cellName]); mPlayerPos->setCell (cell);
// TODO orientation // TODO orientation
// This connects the cell data with the rendering scene. // This connects the cell data with the rendering scene.
std::pair<CellRenderCollection::iterator, bool> result = std::pair<CellRenderCollection::iterator, bool> result =
mActiveCells.insert (std::make_pair (&mInteriors[cellName], mActiveCells.insert (std::make_pair (cell,
new MWRender::InteriorCellRender (mInteriors[cellName], mScene))); new MWRender::InteriorCellRender (*cell, mScene)));
if (result.second) if (result.second)
{ {
// Load the cell and insert it into the renderer // Load the cell and insert it into the renderer
result.first->second->show(); result.first->second->show();
} }
// Actors
mEnvironment.mMechanicsManager->addActor (mPlayerPos->getPlayer());
for (ESMS::CellRefList<ESM::Creature, RefData>::List::iterator iter (
cell->creatures.list.begin());
iter!=cell->creatures.list.end(); ++iter)
{
Ptr ptr (&*iter, cell);
mEnvironment.mMechanicsManager->addActor (ptr);
}
for (ESMS::CellRefList<ESM::NPC, RefData>::List::iterator iter (
cell->npcs.list.begin());
iter!=cell->npcs.list.end(); ++iter)
{
Ptr ptr (&*iter, cell);
mEnvironment.mMechanicsManager->addActor (ptr);
}
// Sky system
if (mSky) if (mSky)
{ {
toggleSky(); toggleSky();

View file

@ -33,6 +33,8 @@ namespace MWRender
namespace MWWorld namespace MWWorld
{ {
class Environment;
/// \brief The game world and its visual representation /// \brief The game world and its visual representation
class World class World
@ -58,6 +60,7 @@ namespace MWWorld
MWWorld::Globals *mGlobalVariables; MWWorld::Globals *mGlobalVariables;
bool mSky; bool mSky;
bool mCellChanged; bool mCellChanged;
Environment& mEnvironment;
// not implemented // not implemented
World (const World&); World (const World&);
@ -74,7 +77,7 @@ namespace MWWorld
public: public:
World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& master, World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& master,
const std::string& dataDir, bool newGame); const std::string& dataDir, bool newGame, Environment& environment);
~World(); ~World();