Ejemplo n.º 1
0
        /// <summary>
        ///     Allocates an entity and stores it but does not load components or do initialization.
        /// </summary>
        private protected Entity AllocEntity(EntityUid?uid = null)
        {
            if (uid == null)
            {
                uid = GenerateEntityUid();
            }

            if (EntityExists(uid.Value))
            {
                throw new InvalidOperationException($"UID already taken: {uid}");
            }

            var entity = new Entity();

            entity.SetManagers(this);
            entity.SetUid(uid.Value);

            // allocate the required MetaDataComponent
            _componentManager.AddComponent <MetaDataComponent>(entity);

            // allocate the required TransformComponent
            _componentManager.AddComponent <TransformComponent>(entity);

            Entities[entity.Uid] = entity;
            AllEntities.Add(entity);

            return(entity);
        }
        public void Setup()
        {
            // Initialize component manager.
            IoCManager.InitThread();

            IoCManager.Register <IComponentManager, ComponentManager>();
            IoCManager.Register <IRuntimeLog, RuntimeLog>();
            IoCManager.Register <ILogManager, LogManager>();
            IoCManager.Register <IDynamicTypeFactory, DynamicTypeFactory>();
            IoCManager.Register <IEntitySystemManager, EntitySystemManager>();
            var entityManager = new Mock <IEntityManager>().Object;

            IoCManager.RegisterInstance <IEntityManager>(entityManager);
            IoCManager.RegisterInstance <IReflectionManager>(new Mock <IReflectionManager>().Object);

            var dummyReg = new Mock <IComponentRegistration>();

            dummyReg.SetupGet(p => p.Name).Returns("Dummy");
            dummyReg.SetupGet(p => p.Type).Returns(typeof(DummyComponent));
            dummyReg.SetupGet(p => p.NetID).Returns((uint?)null);
            dummyReg.SetupGet(p => p.NetworkSynchronizeExistence).Returns(false);
            dummyReg.SetupGet(p => p.References).Returns(new [] { typeof(DummyComponent) });

            var componentFactory = new Mock <IComponentFactory>();

            componentFactory.Setup(p => p.GetComponent <DummyComponent>()).Returns(new DummyComponent());
            componentFactory.Setup(p => p.GetRegistration(It.IsAny <DummyComponent>())).Returns(dummyReg.Object);
            componentFactory.Setup(p => p.GetAllRefTypes()).Returns(new[] { typeof(DummyComponent) });

            IoCManager.RegisterInstance <IComponentFactory>(componentFactory.Object);

            IoCManager.BuildGraph();
            _componentManager = IoCManager.Resolve <IComponentManager>();
            _componentManager.Initialize();

            // Initialize N entities with one component.
            for (var i = 0; i < N; i++)
            {
                var entity = new Entity();
                entity.SetManagers(entityManager);
                entity.SetUid(new EntityUid(i + 1));
                _entities.Add(entity);

                _componentManager.AddComponent <DummyComponent>(entity);
            }
        }
Ejemplo n.º 3
0
 public Entity AddComponent <T>(T component) where T : ID3DComponent
 {
     manager.AddComponent(Tag, component);
     return(this);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// The method attaches a new component to the entity
        /// </summary>
        /// <param name="entityId">Entity's identifier</param>
        /// <param name="componentInitializer">A type's value that is used to initialize fields of a new component</param>
        /// <typeparam name="T">A type of a component that should be attached</typeparam>

        public void AddComponent <T>(EntityId entityId, T componentInitializer = default(T)) where T : struct, IComponent
        {
            mComponentManager.AddComponent <T>(entityId, componentInitializer);
        }
Ejemplo n.º 5
0
        private void HandleEntityState(IComponentManager compMan, IEntity entity, EntityState curState,
                                       EntityState nextState)
        {
            var compStateWork = new Dictionary <uint, (ComponentState curState, ComponentState nextState)>();
            var entityUid     = entity.Uid;

            if (curState?.ComponentChanges != null)
            {
                foreach (var compChange in curState.ComponentChanges)
                {
                    if (compChange.Deleted)
                    {
                        if (compMan.TryGetComponent(entityUid, compChange.NetID, out var comp))
                        {
                            compMan.RemoveComponent(entityUid, comp);
                        }
                    }
                    else
                    {
                        if (compMan.HasComponent(entityUid, compChange.NetID))
                        {
                            continue;
                        }

                        var newComp = (Component)_compFactory.GetComponent(compChange.ComponentName);
                        newComp.Owner = entity;
                        compMan.AddComponent(entity, newComp, true);
                    }
                }
            }

            if (curState?.ComponentStates != null)
            {
                foreach (var compState in curState.ComponentStates)
                {
                    compStateWork[compState.NetID] = (compState, null);
                }
            }

            if (nextState?.ComponentStates != null)
            {
                foreach (var compState in nextState.ComponentStates)
                {
                    if (compStateWork.TryGetValue(compState.NetID, out var state))
                    {
                        compStateWork[compState.NetID] = (state.curState, compState);
                    }
                    else
                    {
                        compStateWork[compState.NetID] = (null, compState);
                    }
                }
            }

            foreach (var kvStates in compStateWork)
            {
                if (!compMan.TryGetComponent(entityUid, kvStates.Key, out var component))
                {
                    var eUid                  = entityUid;
                    var eExpectedNetUid       = kvStates.Key;
                    var eRegisteredNetUidName = _compFactory.GetRegistration(eExpectedNetUid).Name;
                    DebugTools.Assert($"Component does not exist for state: entUid={eUid}, expectedNetId={eExpectedNetUid}, expectedName={eRegisteredNetUidName}");
                    continue;
                }

                try
                {
                    component.HandleComponentState(kvStates.Value.curState, kvStates.Value.nextState);
                }
                catch (Exception e)
                {
                    var wrapper = new ComponentStateApplyException(
                        $"Failed to apply comp state: entity={component.Owner}, comp={component.Name}", e);
#if EXCEPTION_TOLERANCE
                    _runtimeLog.LogException(wrapper, "Component state apply");
#else
                    throw wrapper;
#endif
                }
            }
        }
Ejemplo n.º 6
0
 public void TestAddComponent_AddComponent_ReturnsComponentsValue()
 {
     Assert.DoesNotThrow(() =>
     {
         mComponentManager.AddComponent <TTestComponent>((EntityId)0);
     });
 }
Ejemplo n.º 7
0
        private void HandleEntityState(IComponentManager compMan, IEntity entity, EntityState?curState,
                                       EntityState?nextState)
        {
            var compStateWork = new Dictionary <uint, (ComponentState?curState, ComponentState?nextState)>();
            var entityUid     = entity.Uid;

            if (curState?.ComponentChanges != null)
            {
                foreach (var compChange in curState.ComponentChanges)
                {
                    if (compChange.Deleted)
                    {
                        if (compMan.TryGetComponent(entityUid, compChange.NetID, out var comp))
                        {
                            compMan.RemoveComponent(entityUid, comp);
                        }
                    }
                    else
                    {
                        if (compMan.HasComponent(entityUid, compChange.NetID))
                        {
                            continue;
                        }

                        var newComp = (Component)_compFactory.GetComponent(compChange.ComponentName !);
                        newComp.Owner = entity;
                        compMan.AddComponent(entity, newComp, true);
                    }
                }
            }

            if (curState?.ComponentStates != null)
            {
                foreach (var compState in curState.ComponentStates)
                {
                    compStateWork[compState.NetID] = (compState, null);
                }
            }

            if (nextState?.ComponentStates != null)
            {
                foreach (var compState in nextState.ComponentStates)
                {
                    if (compStateWork.TryGetValue(compState.NetID, out var state))
                    {
                        compStateWork[compState.NetID] = (state.curState, compState);
                    }
                    else
                    {
                        compStateWork[compState.NetID] = (null, compState);
                    }
                }
            }

            foreach (var(netId, (cur, next)) in compStateWork)
            {
                if (compMan.TryGetComponent(entityUid, netId, out var component))
                {
                    try
                    {
                        component.HandleComponentState(cur, next);
                    }
                    catch (Exception e)
                    {
                        var wrapper = new ComponentStateApplyException(
                            $"Failed to apply comp state: entity={component.Owner}, comp={component.Name}", e);
#if EXCEPTION_TOLERANCE
                        _runtimeLog.LogException(wrapper, "Component state apply");
#else
                        throw wrapper;
#endif
                    }
                }
                else
                {
                    // The component can be null here due to interp.
                    // Because the NEXT state will have a new component, but this one doesn't yet.
                    // That's fine though.
                    if (cur == null)
                    {
                        continue;
                    }

                    var eUid = entityUid;
                    var eRegisteredNetUidName = _compFactory.GetRegistration(netId).Name;
                    DebugTools.Assert(
                        $"Component does not exist for state: entUid={eUid}, expectedNetId={netId}, expectedName={eRegisteredNetUidName}");
                }
            }
        }
Ejemplo n.º 8
0
        private static void HandleEntityState(IComponentManager compMan, IEntity entity, EntityState curState,
                                              EntityState nextState)
        {
            var compStateWork = new Dictionary <uint, (ComponentState curState, ComponentState nextState)>();
            var entityUid     = entity.Uid;

            if (curState?.ComponentChanges != null)
            {
                foreach (var compChange in curState.ComponentChanges)
                {
                    if (compChange.Deleted)
                    {
                        if (compMan.TryGetComponent(entityUid, compChange.NetID, out var comp))
                        {
                            compMan.RemoveComponent(entityUid, comp);
                        }
                    }
                    else
                    {
                        if (compMan.HasComponent(entityUid, compChange.NetID))
                        {
                            continue;
                        }

                        var newComp = (Component)IoCManager.Resolve <IComponentFactory>().GetComponent(compChange.ComponentName);
                        newComp.Owner = entity;
                        compMan.AddComponent(entity, newComp, true);
                    }
                }
            }

            if (curState?.ComponentStates != null)
            {
                foreach (var compState in curState.ComponentStates)
                {
                    compStateWork[compState.NetID] = (compState, null);
                }
            }

            if (nextState?.ComponentStates != null)
            {
                foreach (var compState in nextState.ComponentStates)
                {
                    if (compStateWork.TryGetValue(compState.NetID, out var state))
                    {
                        compStateWork[compState.NetID] = (state.curState, compState);
                    }
                    else
                    {
                        compStateWork[compState.NetID] = (null, compState);
                    }
                }
            }

            foreach (var kvStates in compStateWork)
            {
                if (!compMan.TryGetComponent(entityUid, kvStates.Key, out var component))
                {
                    DebugTools.Assert("Component does not exist for state.");
                    continue;
                }

                DebugTools.Assert(kvStates.Value.curState == null ||
                                  kvStates.Value.curState.GetType() == component.StateType,
                                  "Component state is of the wrong type.");

                component.HandleComponentState(kvStates.Value.curState, kvStates.Value.nextState);
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Add a component to this entity.
 /// Updates context's entity sets.
 /// </summary>
 public void Add <TComponent>(TComponent component) where TComponent : class
 {
     _componentManager.AddComponent(this, component);
 }
Ejemplo n.º 10
0
        private static void HandleEntityState(IComponentManager compMan, IEntity entity, EntityState curState,
                                              EntityState nextState)
        {
            var compStateWork = new Dictionary <uint, (ComponentState curState, ComponentState nextState)>();
            var entityUid     = entity.Uid;

            if (curState?.ComponentChanges != null)
            {
                foreach (var compChange in curState.ComponentChanges)
                {
                    if (compChange.Deleted)
                    {
                        if (compMan.TryGetComponent(entityUid, compChange.NetID, out var comp))
                        {
                            compMan.RemoveComponent(entityUid, comp);
                        }
                    }
                    else
                    {
                        if (compMan.HasComponent(entityUid, compChange.NetID))
                        {
                            continue;
                        }

                        var newComp = (Component)IoCManager.Resolve <IComponentFactory>().GetComponent(compChange.ComponentName);
                        newComp.Owner = entity;
                        compMan.AddComponent(entity, newComp, true);
                    }
                }
            }

            if (curState?.ComponentStates != null)
            {
                foreach (var compState in curState.ComponentStates)
                {
                    compStateWork[compState.NetID] = (compState, null);
                }
            }

            if (nextState?.ComponentStates != null)
            {
                foreach (var compState in nextState.ComponentStates)
                {
                    if (compStateWork.TryGetValue(compState.NetID, out var state))
                    {
                        compStateWork[compState.NetID] = (state.curState, compState);
                    }
                    else
                    {
                        compStateWork[compState.NetID] = (null, compState);
                    }
                }
            }

            foreach (var kvStates in compStateWork)
            {
                if (!compMan.TryGetComponent(entityUid, kvStates.Key, out var component))
                {
                    DebugTools.Assert("Component does not exist for state.");
                    continue;
                }

                try
                {
                    component.HandleComponentState(kvStates.Value.curState, kvStates.Value.nextState);
                }
                catch (Exception e)
                {
                    Logger.ErrorS("entity", $"Failed to apply comp state: entity={component.Owner}, comp={component.Name}\n  {e}");
                    DebugTools.Assert(e.Message);
                }
            }
        }