예제 #1
0
        public T AddUnique <T>(bool useExisted = true) where T : IComponent, IUnique, new()
        {
            int componentIndex = ComponentIndex <T> .FindIn(this.contextInfo);

            Entity entity = GetSingleEntity(componentIndex);

            if (entity != null)
            {
                if (!useExisted)
                {
                    throw new EntityAlreadyHasComponentException(
                              componentIndex, "Cannot add component '" +
                              _contextInfo.componentNames[componentIndex] + "' to " + entity + "!",
                              "You should check if an entity already has the component."
                              );
                }
                return((T)ModifyUniqueComponent(componentIndex));
            }

            entity = CreateEntity(typeof(T).Name);
            T component = entity.CreateComponent <T>(componentIndex);

            entity.AddComponent(componentIndex, component);

            return(component);
        }
        /// Adds copies of all specified components to the target entity.
        /// If replaceExisting is true it will replace exisintg components.
        public static void CopyTo(this Entity entity,
                                  Entity target,
                                  bool replaceExisting = false,
                                  params int[] indices)
        {
            var componentIndices = indices.Length == 0
                                        ? entity.GetComponentIndices()
                                        : indices;

            for (int i = 0; i < componentIndices.Length; i++)
            {
                var index           = componentIndices[i];
                var component       = entity.GetComponent(index);
                var clonedComponent = target.CreateComponent(
                    index, component.GetType()
                    );
                component.CopyPublicMemberValues(clonedComponent);

                if (replaceExisting)
                {
                    target.ReplaceComponent(index, clonedComponent);
                }
                else
                {
                    target.AddComponent(index, clonedComponent);
                }
            }
        }
예제 #3
0
        /// Adds copies of all specified components to the target entity.
        /// If replaceExisting is true it will replace exisintg components.
        public static void CopyTo(this Entity entity,
                                  Entity target,
                                  bool replaceExisting = false,
                                  params int[] indices)
        {
            var componentIndices = indices.Length == 0
                                        ? entity.GetComponentIndices()
                                        : indices;
            for(int i = 0; i < componentIndices.Length; i++) {
                var index = componentIndices[i];
                var component = entity.GetComponent(index);
                var clonedComponent = target.CreateComponent(
                    index, component.GetType()
                );
                component.CopyPublicMemberValues(clonedComponent);

                if(replaceExisting) {
                    target.ReplaceComponent(index, clonedComponent);
                } else {
                    target.AddComponent(index, clonedComponent);
                }
            }
        }