public void Destroy()
 {
     Negative?.Destroy();
     Negative = null;
     Positive?.Destroy();
     Positive = null;
 }
 private void OnSwitchChanged(SwitchableNodeSide self, Node junction, Node destination)
 {
     BeginAnimating();
     _eventBus?.Invoke(EventName);
 }
        private void BindController(Node intersection)
        {
            SwitchableNodeSide newController = null;

            if (intersection != null)
            {
                var data = SwitchableNodeData.GetOrCreate(intersection);

                bool useNegative;
                var  negativeOptions = 0;
                var  positiveOptions = 0;
                foreach (var k in intersection.Neighbors)
                {
                    if (SwitchableNodeSide.IsValidForSwitch(intersection, k, true))
                    {
                        negativeOptions++;
                    }
                    else
                    {
                        positiveOptions++;
                    }
                }

                var positiveWanted = (Entity.GetPosition() - intersection.Position).Dot(intersection.Tangent) > 0;

                if (positiveOptions >= 2 && positiveWanted)
                {
                    useNegative = false;
                }
                else if (negativeOptions >= 2 && !positiveWanted)
                {
                    useNegative = true;
                }
                else if (negativeOptions >= 2 && data.Negative == null)
                {
                    useNegative = true;
                }
                else if (positiveOptions >= 2 && data.Positive == null)
                {
                    useNegative = false;
                }
                else if (negativeOptions >= 2)
                {
                    useNegative = true;
                }
                else
                {
                    useNegative = false;
                }

                newController = useNegative ? data.NegativeOrCreate : data.PositiveOrCreate;
            }

            var newJunction = newController?.Junction;


            if (newController != _controller)
            {
                if (_controller != null)
                {
                    _controller.SwitchChanged -= OnSwitchChanged;
                }
                _controller = newController;
                if (_controller != null)
                {
                    _controller.SwitchChanged += OnSwitchChanged;
                    FlagAnimationWarp();
                    if (_controller.Target != null)
                    {
                        OnSwitchChanged(_controller, _controller.Junction, _controller.Target);
                    }
                }
            }

            // ReSharper disable once InvertIf
            if (newJunction != _controllerJunction)
            {
                if (_controllerJunction != null)
                {
                    _controllerJunction.NeighborAdded   -= NeighborsChanged;
                    _controllerJunction.NeighborRemoved -= NeighborsChanged;
                }

                _controllerJunction = newJunction;
                // ReSharper disable once InvertIf
                if (_controllerJunction != null)
                {
                    _controllerJunction.NeighborAdded   += NeighborsChanged;
                    _controllerJunction.NeighborRemoved += NeighborsChanged;
                }
            }

            LinkNeighbors();
        }
 private void OnSwitchChanged(SwitchableNodeSide self, Node junction, Node destination)
 {
     UpdateAnimator();
 }
 public SwitchableNodeSide SideOrCreateFor(Node n)
 {
     return(SwitchableNodeSide.IsValidForSwitch(Owner, n, true) ? NegativeOrCreate : PositiveOrCreate);
 }