Example #1
0
        /// <summary> Working with a given connection, a List of all views, SliceSize and Position,
        /// the connection has its scope updated for the given position/slice of the provided list
        /// of views. If views.Count == 512, Position == 64, and slice size == 32, connection will
        /// have scope updated for views occupying index 64 through 96 of the views List only. </summary>
        internal void UpdateScopeRange(NetConnection connection, List <NetView> views)
        {
            if (Position >= views.Count)
            {
                Position = 0;
            }

            int      count = SliceSize;
            NetScope scope = connection.Scope;

            for (int i = Position; i < views.Count; i++)
            {
                var view = views[i];
                if (view != connection.View && view.Server == Socket.Self && connection.InGroup(view.Group) && view.CanInstantiateFor(connection) && UpdateScope(scope, view))
                {
                    if (scope.In(view.Id))
                    {
                        view.SendInstantiateData(connection);
                    }
                    else
                    {
                        ViewManager.SendOutOfScope(view.Id, connection);
                    }
                }
                count--;
                if (count == 0)
                {
                    break;
                }
            }
        }
Example #2
0
        private NetView CreateView(NetConnection controller, NetConnection server, int viewId, int group, string prefabRoot, NetView.Relation relation)
        {
            NetView view = null;

            if (ViewLookup.ContainsKey(viewId))
            {
                NetView oldView = ViewLookup[viewId];
                if (oldView.CurrentRelation == relation)
                {
                    view = oldView;
                }
                else if (server == Socket.Self)
                {
                    NetScope oldScope = oldView.Scope;
                    Vector3  oldPos   = oldView.transform.position;
                    if (oldView.Server != server)
                    {
                        SendChangeViewServer(viewId);
                    }
                    DestroyView(oldView);
                    view = InstantiateView(prefabRoot, relation);
                    view.InternalScope      = oldScope;
                    view.transform.position = oldPos;
                    view.transform.position = new Vector3(100, 1, 100);
                }
            }

            if (view == null)
            {
                view = InstantiateView(prefabRoot, relation);
            }

            view.Server          = server;
            view.Id              = viewId;
            view.PrefabRoot      = prefabRoot;
            view.Group           = group;
            view.CurrentRelation = relation;

            if (controller != null)
            {
                view.AddController(controller);
                controller.AddAuthorization(view.Id);
                controller.AddToGroup(group);
                if (controller != Socket.Self)
                {
                    controller.View = view;
                    if (controller.InternalScope != null && view.Controllers.Count == 1)
                    {
                        view.InternalScope = controller.InternalScope;
                    }
                    controller.InternalScope = null;
                }
            }
            view.Scope.FireInEvent();
            RegisterNetView(view);

            return(view);
        }
Example #3
0
        /// <summary> The provided scope is updated if the provided view has gone in or out of scope.
        /// False is returned if there is no change. </summary>
        internal bool UpdateScope(NetScope source, NetView view)
        {
            bool scopeChanged = false;

            float distance = Vector3.Distance(source.Position, view.Scope.Position);
            // If the source scope is configured to override, use its scope distances instead:
            var rulesScope = source.TakePrecedence ? source : view.Scope;

            if (view.Scope.CalcDisabled || distance > rulesScope.OutScopeDist)
            {
                if (!source.In(view.Id))
                {
                    return(false);
                }
                scopeChanged = true;
                source.SetOut(view.Id);
            }
            else if (distance < rulesScope.InScopeDist)
            {
                if (!source.In(view.Id))
                {
                    scopeChanged = true;
                }

                if (distance < rulesScope.LevelOne)
                {
                    source.SetIn(view.Id, 1);
                }
                else if (distance < rulesScope.LevelTwo)
                {
                    source.SetIn(view.Id, 2);
                }
                else
                {
                    source.SetIn(view.Id, 3);
                }
            }
            return(scopeChanged);
        }
Example #4
0
        /// <summary> The provided scope is updated if the provided view has gone in or out of scope.
        /// False is returned if there is no change. </summary>
        internal bool UpdateScope(NetScope source, NetView view) {
            bool scopeChanged = false;

            float distance = Vector3.Distance(source.Position, view.Scope.Position);
            // If the source scope is configured to override, use its scope distances instead:
            var rulesScope = source.TakePrecedence ? source : view.Scope;
            if (view.Scope.CalcDisabled || distance > rulesScope.OutScopeDist) {
                if (!source.In(view.Id)) return false;
                scopeChanged = true;
                source.SetOut(view.Id);
            } else if (distance < rulesScope.InScopeDist) {
                if (!source.In(view.Id)) scopeChanged = true;

                if (distance < rulesScope.LevelOne) source.SetIn(view.Id, 1);
                else if (distance < rulesScope.LevelTwo) source.SetIn(view.Id, 2);
                else source.SetIn(view.Id, 3);
            }
            return scopeChanged;
        }