Beispiel #1
0
        private static bool ApplyInterestChanges <T>(IDictionary <uint, WorkerComponentInterest> dict, WorkerComponentInterest interest) where T : IComponentMetaclass
        {
            var shouldNotify = true;

            var componentId = Dynamic.GetComponentId <T>();

            switch (interest)
            {
            case WorkerComponentInterest.Default:
                // Only notify if interest was previously overridden.
                shouldNotify = dict.Remove(componentId);
                break;

            case WorkerComponentInterest.Always:
            case WorkerComponentInterest.Never:
                WorkerComponentInterest oldInterest;
                if (dict.TryGetValue(componentId, out oldInterest) && oldInterest == interest)
                {
                    // The value of the interest isn't changing.
                    shouldNotify = false;
                }
                else
                {
                    dict[componentId] = interest;
                }

                break;

            default:
                throw new ArgumentOutOfRangeException("interest", interest, null);
            }

            return(shouldNotify);
        }
Beispiel #2
0
        public void SetComponentInterest <T>(WorkerComponentInterest interest) where T : IComponentMetaclass
        {
            var shouldNotify = ApplyInterestChanges <T>(globalInterests, interest);

            if (InterestInvalidationHandler == null || EntityObjects == null || !shouldNotify)
            {
                return;
            }

            foreach (var obj in EntityObjects.GetAll())
            {
                // Only notify that a global interest has changed if it's not overridden on the entity.
                if (!perEntityInterests.ContainsKey(obj.EntityId))
                {
                    InterestInvalidationHandler(obj);
                }
            }
        }
Beispiel #3
0
        public void SetComponentInterest <T>(EntityId entityId, WorkerComponentInterest interest) where T : IComponentMetaclass
        {
            var shouldNotify = true;
            var componentId  = Dynamic.GetComponentId <T>();

            if (perEntityInterests.ContainsKey(entityId))
            {
                // We've already setup interests, so modify what's there.
                var workerComponentInterests = perEntityInterests[entityId];
                shouldNotify = ApplyInterestChanges <T>(workerComponentInterests, interest);

                // Clean up if no overrides remain for the entity.
                if (workerComponentInterests.Count == 0)
                {
                    perEntityInterests.Remove(entityId);
                }
            }
            else if (interest == WorkerComponentInterest.Default)
            {
                // No previous interest to remove/update, nothing has changed.
                shouldNotify = false;
            }
            else
            {
                // Create the first interest override
                perEntityInterests[entityId] = new Dictionary <uint, WorkerComponentInterest> {
                    { componentId, interest }
                };
            }

            if (!shouldNotify || InterestInvalidationHandler == null)
            {
                return;
            }

            var obj = EntityObjects.Get(entityId);

            if (obj != null)
            {
                InterestInvalidationHandler(obj);
            }
        }
Beispiel #4
0
        private static void ApplyComponentInterest(uint componentId, WorkerComponentInterest interest, HashSet <uint> interestedComponents)
        {
            switch (interest)
            {
            case WorkerComponentInterest.Default:
                Debug.LogErrorFormat("Internal logic error: {0} is not valid for component interest.", Enum.GetName(typeof(WorkerComponentInterest), WorkerComponentInterest.Default));
                break;

            case WorkerComponentInterest.Always:
                interestedComponents.Add(componentId);
                break;

            case WorkerComponentInterest.Never:
                interestedComponents.Remove(componentId);
                break;

            default:
                throw new ArgumentOutOfRangeException("interest", interest, null);
            }
        }