Пример #1
0
        /// <summary>Creates a deep copy of the object, reusing the given object.</summary>
        /// <param name="into">The object to copy into.</param>
        /// <returns>The copy.</returns>
        public void CopyInto(IManager into)
        {
            // Validate input.
            if (into == this)
            {
                throw new ArgumentException("Cannot copy a manager into itself.");
            }

            // Get the properly typed version.
            var copy = (Manager)into;

            // Copy id managers.
            _entityIds.CopyInto(copy._entityIds);
            _componentIds.CopyInto(copy._componentIds);

            // Copy components and entities.
            copy._components.Clear();
            foreach (var component in _componentIds.Select(id => _components[id]))
            {
                // The create the component and set it up.
                var componentCopy = AllocateComponent(component.GetType()).Initialize(component);
                componentCopy.Id      = component.Id;
                componentCopy.Entity  = component.Entity;
                componentCopy.Manager = copy;
                copy._components[componentCopy.Id] = componentCopy;
            }

            copy._entities.Clear();
            foreach (var entity in _entityIds)
            {
                // Create copy.
                var entityCopy = AllocateEntity();
                copy._entities[entity] = entityCopy;

                // Assign copied components.
                foreach (var component in _entities[entity].Components)
                {
                    entityCopy.Add(copy.GetComponentById(component.Id));
                }
            }

            // Copy systems after copying components so they can fetch their
            // components again.
            foreach (var system in _systems
                     .Where(system => !system.GetType().IsDefined(typeof(PresentationOnlyAttribute), true)))
            {
                copy.CopySystem(system);
            }

            // All done, send message to allow post-processing.
            Initialize message;

            SendMessage(message);
        }