bool MapSystem::AddEmptyMap(const std::string& dataPath, const std::string& mapname) { if(IsMapLoaded(mapname)) { return false; } if(GetSystemInterface()->FindDataFile(mapname) != "") { mLoadedMaps.push_back(MapData(mapname, dataPath, static_cast<unsigned int>(mLoadedMaps.size()))); return false; } std::string mappathrel = GetFilePath(mapname); std::ostringstream os; os << dataPath << "/" << mappathrel; std::string mappathabs = os.str(); if(!GetSystemInterface()->FileExists(mappathabs)) { LOG_ERROR("Cannot create map in directory " << mappathabs << "! Does it exist?"); return false; } MapBeginLoadMessage msg; msg.SetMapPath(mapname); GetEntityManager().EmitMessage(msg); mLoadedMaps.push_back(MapData(mapname, dataPath, static_cast<unsigned int>(mLoadedMaps.size()))); MapLoadedMessage msg2; msg2.SetMapPath(mapname); GetEntityManager().EmitMessage(msg2); return true; }
void MapSystem::OnSpawnEntity(const Message& msg) { const SpawnEntityMessage& m = static_cast<const SpawnEntityMessage&>(msg); Entity* entity; GetEntityManager().CreateEntity(entity); std::string spawnerName = m.GetSpawnerName(); bool success = Spawn(spawnerName, *entity); if(!success) { LOG_ERROR("Could not spawn entity: spawner not found: " + spawnerName); return; } MapComponent* comp; if(!entity->GetComponent(comp)) { entity->CreateComponent(comp); } comp->SetUniqueId(m.GetUniqueId()); comp->SetEntityName(m.GetEntityName()); EntitySpawnedMessage smsg; smsg.SetSpawnerName(spawnerName); smsg.SetAboutEntityId(entity->GetId()); GetEntityManager().EmitMessage(smsg); if(m.GetAddToScene()) { GetEntityManager().AddToScene(entity->GetId()); } }
bool MapSystem::UnloadMap(const std::string& path) { if(!IsMapLoaded(path)) { LOG_ERROR("Cannot unload map: not loaded! " + path); return false; } MapBeginUnloadMessage msg; msg.SetMapPath(path); GetEntityManager().EmitMessage(msg); // first collect entity ids of all entities in this map std::vector<EntityId> ids; GetEntitiesInMap(path, ids); std::vector<EntityId>::const_iterator j; for(j = ids.begin(); j != ids.end(); ++j) { GetEntityManager().RemoveFromScene(*j); } for(j = ids.begin(); j != ids.end(); ++j) { GetEntityManager().KillEntity(*j); } MapSystem::SpawnerStorage children = GetChildren(mSpawners, ""); EmitSpawnerDeleteMessages(children, path); SpawnerStorage::iterator k = mSpawners.begin(); while(k != mSpawners.end()) { Spawner* spawner = k->second; if(spawner->GetMapName() == path) { mSpawners.erase(k++); } else { ++k; } } MapUnloadedMessage msg1; msg1.SetMapPath(path); for(LoadedMaps::iterator i = mLoadedMaps.begin(); i != mLoadedMaps.end(); ++i) { if(i->mMapPath == path) { mLoadedMaps.erase(i); break; } } GetEntityManager().EmitMessage(msg1); return true; }
void MapSystem::OnDeleteEntity(const Message& msg) { const DeleteEntityMessage& m = static_cast<const DeleteEntityMessage&>(msg); Entity* entity; bool success = GetEntityByUniqueId(m.GetUniqueId(), entity); if(!success) { LOG_ERROR("Cannot delete: Entity with unique id not found: " + m.GetUniqueId()); return; } GetEntityManager().RemoveFromScene(entity->GetId()); GetEntityManager().KillEntity(entity->GetId()); }
MapSystem::~MapSystem() { for(MapEncoders::iterator i = mMapEncoders.begin(); i != mMapEncoders.end(); ++i) { delete *i; } GetEntityManager().UnregisterForMessages(SpawnEntityMessage::TYPE, mSpawnEntityFunctor); GetEntityManager().UnregisterForMessages(DeleteEntityMessage::TYPE, mDeleteEntityFunctor); GetEntityManager().UnregisterForMessages(StopSystemMessage::TYPE, mStopSystemFunctor); GetEntityManager().UnregisterForMessages(SetComponentPropertiesMessage::TYPE, mSetComponentPropertiesFunctor); GetEntityManager().UnregisterForMessages(SetSystemPropertiesMessage::TYPE, mSetSystemPropertiesFunctor); }
bool MapSystem::LoadMap(const std::string& path) { if(IsMapLoaded(path)) { LOG_ERROR("Map already loaded: " + path); return false; } if(!MapExists(path)) { LOG_ERROR("Map not found: " + path); return false; } MapEncoder* enc = GetEncoderForMap(GetFileExtension(path)); if(!enc) { LOG_ERROR("Could not load map: Loader not found for extension " << GetFileExtension(path)); return false; } // get data path containing this map std::string abspath = GetSystemInterface()->FindDataFile(path); std::string mapdatapath = dtEntity::GetSystemInterface()->GetDataFilePathFromFilePath(abspath); assert(mapdatapath != ""); LoadedMaps::size_type mapsaveorder = mLoadedMaps.size(); MapBeginLoadMessage msg; msg.SetMapPath(path); msg.SetDataPath(mapdatapath); msg.SetSaveOrder(mapsaveorder); GetEntityManager().EmitMessage(msg); bool success = enc->LoadMapFromFile(path); if(success) { mLoadedMaps.push_back(MapData(path, mapdatapath, static_cast<unsigned int>(mLoadedMaps.size()))); MapLoadedMessage msg1; msg1.SetMapPath(path); msg1.SetDataPath(mapdatapath); msg1.SetSaveOrder(mapsaveorder); GetEntityManager().EmitMessage(msg1); } return success; }
bool MapSystem::OnEntityChangedUniqueId(EntityId id, const std::string& oldUniqueId, const std::string& newUniqueId) { if(oldUniqueId == newUniqueId) { return true; } typedef std::map<std::string, EntityId> UIMap; MapComponent* comp = GetComponent(id); assert(comp != NULL); UIMap::iterator j = mEntitiesByUniqueId.find(newUniqueId); if(j != mEntitiesByUniqueId.end()) { return false; } UIMap::iterator i = mEntitiesByUniqueId.find(oldUniqueId); if(i != mEntitiesByUniqueId.end()) { mEntitiesByUniqueId.erase(i); } mEntitiesByUniqueId[newUniqueId] = id; EntityNameUpdatedMessage msg; msg.SetAboutEntityId(id); msg.SetEntityName(comp->GetEntityName()); msg.SetUniqueId(newUniqueId); GetEntityManager().EmitMessage(msg); return true; }
void MapSystem::OnSetSystemProperties(const Message& m) { const SetSystemPropertiesMessage& msg = static_cast<const SetSystemPropertiesMessage&>(m); EntitySystem* sys = GetEntityManager().GetEntitySystem(SIDHash(msg.GetComponentType())); if(sys == NULL) { LOG_WARNING("Cannot process SetSystemProperties message. Entity system not found: " << msg.GetComponentType()); return; } const PropertyGroup& props = msg.GetSystemProperties(); for(PropertyGroup::const_iterator i = props.begin(); i != props.end(); ++i) { Property* target = sys->Get(i->first); if(!target) { LOG_ERROR("Cannot process SetSystemProperties message. Entity system " << msg.GetComponentType() << " has no property named " << GetStringFromSID(i->first)); continue; } target->SetFrom(*i->second); #if CALL_ONPROPERTYCHANGED_METHOD sys->OnPropertyChanged(i->first, *target); #endif } sys->Finished(); }
ScriptSystem::ScriptSystem(dtEntity::EntityManager& em) : dtEntity::EntitySystem(em) , mDebugPortOpened(false) { V8::Initialize(); Register(ScriptsId, &mScripts); Register(DebugPortId, &mDebugPort); Register(DebugEnabledId, &mDebugEnabled); mDebugPort.Set(9222); //V8::AddGCPrologueCallback(GCStartCallback); //V8::AddGCEpilogueCallback(GCEndCallback); mSceneLoadedFunctor = dtEntity::MessageFunctor(this, &ScriptSystem::OnSceneLoaded); GetEntityManager().RegisterForMessages(dtEntity::SceneLoadedMessage::TYPE, mSceneLoadedFunctor, "ScriptSystem::OnSceneLoaded"); mTickFunctor = dtEntity::MessageFunctor(this, &ScriptSystem::Tick); em.RegisterForMessages(dtEntity::TickMessage::TYPE, mTickFunctor, "ScriptSystem::Tick"); mLoadScriptFunctor = dtEntity::MessageFunctor(this, &ScriptSystem::OnLoadScript); em.RegisterForMessages(ExecuteScriptMessage::TYPE, mLoadScriptFunctor, "ScriptSystem::OnLoadScript"); HandleScope scope; mEntityIdString = Persistent<String>::New(String::New("__entityid__")); mPropertyNamesString = Persistent<String>::New(String::New("__propertynames__")); }
bool MapSystem::LoadScene(const std::string& path) { // get data path containing this map std::string abspath = GetSystemInterface()->FindDataFile(path); if(abspath == "") { LOG_ERROR("Cannot find scene file: " << path); return false; } std::string scenedatapath = GetSystemInterface()->GetDataFilePathFromFilePath(abspath); if(scenedatapath == "") { LOG_ERROR("No data path for scene found!"); return false; } MapEncoder* enc = GetEncoderForScene(GetFileExtension(path)); if(!enc) { LOG_ERROR("Could not load scene: Loader not found for extension " << GetFileExtension(path)); return false; } bool success = enc->LoadSceneFromFile(path); SceneLoadedMessage msg; msg.SetSceneName(path); GetEntityManager().EmitMessage(msg); return success; }
bool MapSystem::DeleteSpawner(const std::string& name) { if(mSpawners.find(name) != mSpawners.end()) { SpawnerRemovedMessage msg; SpawnerStorage::iterator i = mSpawners.find(name); if(i == mSpawners.end()) return false; for(ComponentStore::iterator i = mComponents.begin(); i != mComponents.end(); ++i) { MapComponent* mc = i->second; if(mc->GetSpawnerName() == name) { mc->SetSpawnerName(""); } } msg.SetName(name); msg.SetMapName(i->second->GetMapName()); msg.SetCategory(i->second->GetGUICategory()); mSpawners.erase(i); GetEntityManager().EmitMessage(msg); return true; } else { return false; } }
void MapSystem::OnSetComponentProperties(const Message& m) { const SetComponentPropertiesMessage& msg = static_cast<const SetComponentPropertiesMessage&>(m); ComponentType ctype = dtEntity::SIDHash(msg.GetComponentType()); std::string uniqueid = msg.GetEntityUniqueId(); MapSystem* ms; GetEntityManager().GetEntitySystem(MapComponent::TYPE, ms); EntityId id = ms->GetEntityIdByUniqueId(uniqueid); if(id == 0) { LOG_ERROR("Entity not found for SetComponentPropertiesMessage!"); return; } Component* component; bool found = GetEntityManager().GetComponent(id, ctype, component); if(!found) { LOG_WARNING("Cannot process SetComponentProperties message. Component not found: " + msg.GetComponentType()); return; } const PropertyGroup& props = msg.GetComponentProperties(); for(PropertyGroup::const_iterator i = props.begin(); i != props.end(); ++i) { Property* target = component->Get(i->first); if(!target) { LOG_ERROR( "Cannot process SetComponentProperties message. Component " << msg.GetComponentType() << " has no property named " << GetStringFromSID(i->first) ); continue; } target->SetFrom(*i->second); #if CALL_ONPROPERTYCHANGED_METHOD component->OnPropertyChanged(i->first, *target); #endif } component->Finished(); }
void ArGame::SetHero(const DiString& configFile) { if (!mHero) mHero = GetEntityManager()->CreateHero(1, configFile); if (mCamera) mCamera->SetTarget(mHero->GetRenderObj()); }
bool MapSystem::GetEntityByUniqueId(const std::string& name, Entity*& entity) const { EntityId id = GetEntityIdByUniqueId(name); if(id > 0) { GetEntityManager().GetEntity(id, entity); return true; } return false; }
bool MapSystem::RemoveFromScene(EntityId eid) { if(!GetEntityManager().EntityExists(eid)) { LOG_ERROR("Cannot remove from scene: Entity with this ID not found!"); return false; } EntityRemovedFromSceneMessage msg; msg.SetUInt(EntityRemovedFromSceneMessage::AboutEntityId, eid); MapComponent* mc = GetComponent(eid); if(mc) { msg.SetMapName(mc->GetMapName()); msg.SetEntityName(mc->GetEntityName()); msg.SetUniqueId(mc->GetUniqueId()); msg.SetVisibleInEntityList(mc->GetVisibleInEntityList()); } GetEntityManager().EmitMessage(msg); return true; }
bool MapSystem::UnloadScene() { SceneUnloadedMessage msg; GetEntityManager().EmitMessage(msg); while(!mLoadedMaps.empty()) { UnloadMap(mLoadedMaps.front().mMapPath); } return true; }
void CBombProjectile::ProcessFrame(unsigned int dwCurrentTime,double dTimeFraction) { CEntityBase::ProcessFrame(dwCurrentTime,dTimeFraction); m_nCurrentTime=dwCurrentTime; if(GetState()==eBombState_Normal) { if(dwCurrentTime>(m_dwCreationTime+m_pType->m_nTimeToExplode)) { if(m_pTypeBase->GetStateAnimations(eBombState_Hit)) { m_PhysicInfo.vVelocity=Origin; m_PhysicInfo.dwMoveType=PHYSIC_MOVE_TYPE_NONE; SetState(eBombState_Hit); } else { Remove(); } } } else if(GetState()==eBombState_Hit) { if(dwCurrentTime>(m_dwCreationTime+m_pType->m_nTimeToExplode+m_pType->m_nDamageEndTime)) { Remove(); } else if(dwCurrentTime>(m_dwCreationTime+m_pType->m_nTimeToExplode+m_pType->m_nDamageStartTime)) { IGenericCamera *piCamera=g_PlayAreaManagerWrapper.m_piInterface->GetCamera(); CVector vCameraPos=piCamera?piCamera->GetPosition():Origin; REL(piCamera); double dElapsedTime=((double)(dwCurrentTime-(m_dwCreationTime+m_pType->m_nTimeToExplode+m_pType->m_nDamageStartTime))); double dTotalDuration=((double)(m_pType->m_nDamageEndTime-m_pType->m_nDamageStartTime)); double dElapsedFraction=dElapsedTime/dTotalDuration; SBombDamageData data; data.dTimeFraction=dTimeFraction; data.dDamage=m_pType->m_dDamagePerSecond*dTimeFraction; data.dRadius=(m_pType->m_dDamageEndRadius-m_pType->m_dDamageStartRadius)*dElapsedFraction+m_pType->m_dDamageStartRadius; data.vCameraPos=vCameraPos; data.playAreaPlane=CPlane(AxisPosY,m_PhysicInfo.vPosition); if(dwCurrentTime>m_nNextDamageEffect) { data.bDamageEffect=true; m_nNextDamageEffect=dwCurrentTime+m_pType->m_nDamageEffectInterval; } GetEntityManager()->PerformUnaryOperation(ApplyDamageOperation,this,&data); } } }
DETOUR_DECL_MEMBER1(PostConstructor, void, const char *, szClassname) { CBaseEntity *pEntity = (CBaseEntity*)this; const char *szName = (szCurrentReplacedClassname)?szCurrentReplacedClassname:szClassname; DETOUR_MEMBER_CALL(PostConstructor)(szName); CEntity *cent = GetEntityManager()->CBaseEntityPostConstructor(pEntity, szName); szCurrentReplacedClassname = NULL; if(cent) { cent->PostConstructor(); } }
ScriptSystem::~ScriptSystem() { //V8::RemoveGCPrologueCallback(GCStartCallback); //V8::RemoveGCEpilogueCallback(GCEndCallback); for(ComponentMap::iterator i = mComponentMap.begin(); i != mComponentMap.end(); ++i) { i->second.Dispose(); } mComponentMap.clear(); for(TemplateMap::iterator i = mTemplateMap.begin(); i != mTemplateMap.end(); ++i) { i->second.Dispose(); } mTemplateMap.clear(); mGlobalContext.Dispose(); GetEntityManager().UnregisterForMessages(dtEntity::SceneLoadedMessage::TYPE, mSceneLoadedFunctor); GetEntityManager().UnregisterForMessages(dtEntity::TickMessage::TYPE, mTickFunctor); GetEntityManager().UnregisterForMessages(ExecuteScriptMessage::TYPE, mLoadScriptFunctor); }
bool MapSystem::DeleteEntitiesByMap(const std::string& mapName) { if(!IsMapLoaded(mapName)) { LOG_ERROR("Cannot unload map: not loaded! " + mapName); return false; } // first collect entity ids of all entities in this map std::vector<EntityId> ids; GetEntitiesInMap(mapName, ids); std::vector<EntityId>::const_iterator j; for(j = ids.begin(); j != ids.end(); ++j) { GetEntityManager().RemoveFromScene(*j); } for(j = ids.begin(); j != ids.end(); ++j) { GetEntityManager().KillEntity(*j); } return true; }
IEntity *CStaticStructure::GetTarget() { if(m_piTarget==NULL) { IEntity *piTarget=NULL; IEntityManager *piManager=GetEntityManager(); if(piManager){piTarget=piManager->FindEntity("Player");} if(piTarget && piTarget->GetHealth()>0) { SetTarget(piTarget); } } return m_piTarget; }
void MapSystem::AddSpawner(Spawner& spawner) { SpawnerAddedMessage msg; mSpawners[spawner.GetName()] = &spawner; msg.SetName(spawner.GetName()); if(spawner.GetParent() != NULL) { msg.SetParentName(spawner.GetParent()->GetName()); } msg.SetMapName(spawner.GetMapName()); GetEntityManager().EmitMessage(msg); }
bool MapSystem::CreateEntitySystem(EntityManager* em, ComponentType t) { if(ComponentPluginManager::GetInstance().FactoryExists(t)) { ComponentPluginManager::GetInstance().StartEntitySystem(GetEntityManager(), t); if(!em->HasEntitySystem(t)) { LOG_ERROR("Factory error: Factory is registered for type but " "does not create entity system with that type"); return false; } return true; } else { return false; } }
void ScriptSystem::SetupContext() { HandleScope handle_scope; if(!mGlobalContext.IsEmpty()) { mGlobalContext.Dispose(); } // create a template for the global object Handle<ObjectTemplate> global = ObjectTemplate::New(); // create persistent global context mGlobalContext = Persistent<Context>::New(Context::New(NULL, global)); // store pointer to script system into isolate data to have it globally available in javascript Isolate::GetCurrent()->SetData(this); RegisterGlobalFunctions(this, mGlobalContext); RegisterPropertyFunctions(this, mGlobalContext); InitializeAllWrappers(GetEntityManager()); Handle<Context> context = GetGlobalContext(); Context::Scope context_scope(context); Handle<FunctionTemplate> tmplt = FunctionTemplate::New(); tmplt->InstanceTemplate()->SetInternalFieldCount(2); tmplt->SetClassName(String::New("ScriptSystem")); context->Global()->Set(String::New("Screen"), WrapScreen(this)); dtEntity::InputInterface* ipiface = dtEntity::GetInputInterface(); if(ipiface) { context->Global()->Set(String::New("Input"), WrapInputInterface(GetGlobalContext(), ipiface)); context->Global()->Set(String::New("Axis"), WrapAxes(ipiface)); context->Global()->Set(String::New("Key"), WrapKeys(ipiface)); } context->Global()->Set(String::New("TouchPhase"), WrapTouchPhases()); context->Global()->Set(String::New("Priority"), WrapPriorities()); context->Global()->Set(String::New("Order"), WrapPriorities()); }
void MapSystem::EmitSpawnerDeleteMessages(MapSystem::SpawnerStorage& spawners, const std::string& path) { MapSystem::SpawnerStorage::iterator i; for(i = spawners.begin(); i != spawners.end(); ++i) { Spawner* spawner = i->second; if(spawner->GetMapName() == path) { MapSystem::SpawnerStorage children = GetChildren(mSpawners, spawner->GetName()); EmitSpawnerDeleteMessages(children, path); SpawnerRemovedMessage msg; msg.SetName(i->first); msg.SetMapName(spawner->GetMapName()); msg.SetCategory(spawner->GetGUICategory()); GetEntityManager().EmitMessage(msg); } } }
ScreenShotSaveDialog::ScreenShotSaveDialog (const QPixmap& source, QWidget *parent) : QDialog (parent) , Source_ (source) , PixmapHolder_ (new QLabel ()) , RenderScheduled_ (false) { PixmapHolder_->setAlignment (Qt::AlignTop | Qt::AlignLeft); Ui_.setupUi (this); QList<QByteArray> formats = QImageWriter::supportedImageFormats (); formats.removeAll ("ico"); if (formats.contains ("jpg")) formats.removeAll ("jpeg"); std::sort (formats.begin (), formats.end ()); for (QList<QByteArray>::const_iterator i = formats.begin (), end = formats.end (); i != end; ++i) Ui_.FormatCombobox_->addItem (i->toUpper ()); if (formats.contains ("png")) Ui_.FormatCombobox_->setCurrentIndex (formats.indexOf ("png")); Ui_.Scroller_->setWidget (PixmapHolder_); auto proxy = Core::Instance ().GetProxy (); const auto& dfs = Util::GetDataFilters (QVariant::fromValue (Source_.toImage ()), proxy->GetEntityManager ()); for (auto df : dfs) { auto idf = qobject_cast<IDataFilter*> (df); for (const auto& var : idf->GetFilterVariants ()) { Ui_.ActionBox_->addItem (var.Icon_, var.Name_); Filters_.append ({ df, var.ID_ }); } } Ui_.ActionBox_->addItem (proxy->GetIcon ("document-save"), tr ("Save")); }
void MapSystem::OnStopSystem(const Message& msg) { ComponentPluginManager::GetInstance().UnloadAllPlugins(GetEntityManager()); }