Exemple #1
0
   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();
   }
Exemple #2
0
   bool EntityManager::CloneEntity(EntityId target, EntityId origin)
   {
      bool success = true;
      std::vector<Component*> comps;

      for(EntitySystemStore::iterator i = mEntitySystemStore.begin();
          i != mEntitySystemStore.end(); ++i)
      {
         EntitySystem* sys = i->second;

         if(sys->AllowComponentCreationBySpawner())
         {
            Component* c;
            if(sys->GetComponent(origin, c))
            {
               comps.push_back(c);
            }
         }
      }

      // first create all components in target
      for(std::vector<Component*>::iterator i = comps.begin(); i != comps.end(); ++i)
      {
         Component* origincomp = *i;
         Component* newcomp;
         if(!CreateComponent(target, origincomp->GetType(), newcomp))
         {
            LOG_ERROR("Could not clone component " << GetStringFromSID(origincomp->GetType()));
            success = false;
         }
      }

      // then configure them
      for(std::vector<Component*>::iterator i = comps.begin(); i != comps.end(); ++i)
      {
         Component* origincomp = *i;
         Component* clonecomp;
         if(!GetComponent(target, origincomp->GetType(), clonecomp))
         {
            LOG_ERROR("Error in clone entity, could not find cloned component?!?");
            success = false;
            continue;
         }

         const PropertyGroup& props = origincomp->Get();

         for(PropertyGroup::const_iterator i = props.begin(); i != props.end(); ++i)
         {
            Property* prp = clonecomp->Get(i->first);
            if(prp)
            {
               prp->SetFrom(*i->second);
#if CALL_ONPROPERTYCHANGED_METHOD
               clonecomp->OnPropertyChanged(i->first, *prp);
#endif
            }
            else
            {
               LOG_ERROR("Error cloning: Property does not exist in target");
               success = false;
            }
         }
      }

      // then configure them
      for(std::vector<Component*>::iterator i = comps.begin(); i != comps.end(); ++i)
      {
         Component* origincomp = *i;
         Component* clonecomp;
         if(!GetComponent(target, origincomp->GetType(), clonecomp))
         {
            LOG_ERROR("Error in clone entity, could not find cloned component?!?");
            success = false;
            continue;
         }

         clonecomp->Finished();
      }

      return success;
   }