示例#1
0
        /// <summary>
        /// Creates a GridPortal instance.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="type">The type of the portal.</param>
        /// <param name="direction">The direction of the portal.</param>
        /// <param name="portalOne">The bounds of the first portal.</param>
        /// <param name="portalTwo">The bounds of the second portal.</param>
        /// <param name="exclusiveTo">The attribute mask to indicate if this portal is exclusive to (only available to) certain units.</param>
        /// <param name="action">The action that handles moving between portals.</param>
        /// <returns>The portal instance</returns>
        public static GridPortal Create(string name, PortalType type, PortalDirection direction, Bounds portalOne, Bounds portalTwo, AttributeMask exclusiveTo, IPortalAction action)
        {
            var p = new GridPortal();

            p.Initialize(name, type, direction, portalOne, portalTwo, exclusiveTo, action);

            return(p);
        }
示例#2
0
        void IHandleMessage <GridStatusMessage> .Handle(GridStatusMessage message)
        {
            var gridBounds    = message.gridBounds;
            var currentActive = _activeGrids;
            var delta         = ActiveGrids.None;

            if (gridBounds.Contains(this.actualPortalOne.center))
            {
                delta |= ActiveGrids.One;
            }

            if (gridBounds.Contains(this.actualPortalTwo.center))
            {
                delta |= ActiveGrids.Two;
            }

            if (delta != ActiveGrids.None)
            {
                switch (message.status)
                {
                case GridStatusMessage.StatusCode.DisableComplete:
                {
                    _activeGrids &= ~delta;
                    break;
                }

                case GridStatusMessage.StatusCode.InitializationComplete:
                {
                    _activeGrids |= delta;
                    break;
                }
                }

                //If this resulted in no change, return. This will happen when doing run time creation as the messages are delayed, e.g. a portal already initialized will receive a message that its grid is initialized.
                if (_activeGrids == currentActive)
                {
                    return;
                }

                if (_activeGrids == ActiveGrids.Both)
                {
                    Initialize();

                    if (_portal != null)
                    {
                        _portal.enabled = this.enabled;
                    }
                }
                else if (_portal != null)
                {
                    GridManager.instance.UnregisterPortal(_portal.name);
                    _portal.enabled = false;
                    _portal         = null;
                }
            }
        }
示例#3
0
        void IHandleMessage <GridStatusMessage> .Handle(GridStatusMessage message)
        {
            var  gridBounds  = message.gridBounds;
            bool containsOne = gridBounds.Contains(this.actualPortalOne.center);
            bool containsTwo = gridBounds.Contains(this.actualPortalTwo.center);
            int  increment   = 0;

            if (containsOne || containsTwo)
            {
                increment = (containsOne && containsTwo) ? 2 : 1;
            }

            if (increment > 0)
            {
                switch (message.status)
                {
                case GridStatusMessage.StatusCode.DisableComplete:
                {
                    _activeGrids -= (byte)increment;
                    break;
                }

                case GridStatusMessage.StatusCode.InitializationComplete:
                {
                    _activeGrids += (byte)increment;
                    break;
                }
                }

                if (_activeGrids == 2)
                {
                    Initialize();

                    if (_portal != null)
                    {
                        _portal.enabled = this.enabled;
                    }
                }
                else if (_portal != null)
                {
                    GridManager.instance.UnregisterPortal(_portal.name);
                    _portal.enabled = false;
                    _portal         = null;
                }
            }
        }
示例#4
0
        private void Initialize()
        {
            IPortalAction action;

            if (this.type != PortalType.Connector)
            {
                action = this.As <IPortalAction>();
                if (action == null)
                {
                    var fact = this.As <IPortalActionFactory>();
                    if (fact != null)
                    {
                        action = fact.Create();
                    }
                }

                if (action == null)
                {
                    Debug.LogError("A portal must have an accompanying portal action component, please add one.");
                    this.enabled = false;
                    return;
                }
            }
            else
            {
                action = PortalConnectorAction.instance;
            }

            if (this.portalOne.size.sqrMagnitude == 0f || this.portalTwo.size.sqrMagnitude == 0f)
            {
                Debug.LogError("A portal's end points must have a size greater than 0.");
                this.enabled = false;
                return;
            }

            try
            {
                _portal = GridPortal.Create(this.portalName, this.type, this.direction, this.actualPortalOne, this.actualPortalTwo, this.exclusiveTo, action);
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
                this.enabled = false;
            }
        }
示例#5
0
        /// <summary>
        /// Registers a portal.
        /// </summary>
        /// <param name="name">The unique name of the portal.</param>
        /// <param name="portal">The portal.</param>
        /// <returns>The actual name the portal received</returns>
        public string RegisterPortal(string name, GridPortal portal)
        {
            if (string.IsNullOrEmpty(name))
            {
                name = string.Concat("Grid Portal ", (_portalsLookup.Count + 1));
            }

            int idx = 1;

            while (_portalsLookup.ContainsKey(name))
            {
                name = string.Concat("Grid Portal ", idx++);
            }

            _portalsLookup.Add(name, portal);
            _portals.Add(portal);

            return(name);
        }
示例#6
0
 internal PortalCell(GridPortal parentPortal, bool oneWay, IPortalAction action)
 {
     _parentPortal = parentPortal;
     _isOneWay     = oneWay;
     _action       = action;
 }
示例#7
0
 internal PortalCell(GridPortal parentPortal, IPortalAction action)
 {
     _parentPortal = parentPortal;
     _action = action;
 }
示例#8
0
        /// <summary>
        /// Creates a GridPortal instance.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="type">The type of the portal.</param>
        /// <param name="direction">The direction of the portal.</param>
        /// <param name="portalOne">The bounds of the first portal.</param>
        /// <param name="portalTwo">The bounds of the second portal.</param>
        /// <param name="exclusiveTo">The attribute mask to indicate if this portal is exclusive to (only available to) certain units.</param>
        /// <param name="action">The action that handles moving between portals.</param>
        /// <returns>The portal instance</returns>
        public static GridPortal Create(string name, PortalType type, PortalDirection direction, Bounds portalOne, Bounds portalTwo, AttributeMask exclusiveTo, IPortalAction action)
        {
            var p = new GridPortal();
            p.Initialize(name, type, direction, portalOne, portalTwo, exclusiveTo, action);

            return p;
        }
 internal PortalCell(GridPortal parentPortal, bool oneWay, IPortalAction action)
 {
     _parentPortal = parentPortal;
     _isOneWay = oneWay;
     _action = action;
 }
        /// <summary>
        /// Registers a portal.
        /// </summary>
        /// <param name="name">The unique name of the portal.</param>
        /// <param name="portal">The portal.</param>
        /// <returns>The actual name the portal received</returns>
        public string RegisterPortal(string name, GridPortal portal)
        {
            if (string.IsNullOrEmpty(name))
            {
                name = string.Concat("Grid Portal ", (_portalsLookup.Count + 1));
            }

            int idx = 1;
            while (_portalsLookup.ContainsKey(name))
            {
                name = string.Concat("Grid Portal ", idx++);
            }

            _portalsLookup.Add(name, portal);
            _portals.Add(portal);

            return name;
        }