private void SwitchNet(int junction, int target) { if (_bendy?.Nodes == null) { MyEventContext.ValidationFailed(); return; } if (junction >= _bendy.Nodes.Length) { MyEventContext.ValidationFailed(); return; } if (target >= _bendy.Nodes.Length) { MyEventContext.ValidationFailed(); return; } var junctionNode = _bendy.Nodes[junction]; var targetNode = _bendy.Nodes[target]; var edge = junctionNode.ConnectionTo(targetNode); if (edge == null) { MyEventContext.ValidationFailed(); return; } SwitchableNodeData.GetOrCreate(junctionNode).SideOrCreateFor(targetNode).SwitchToInternal(edge); }
public static void SwitchTo(this Node junction, Node destination) { if (junction.ConnectionTo(destination) == null) { return; } SwitchableNodeData.GetOrCreate(junction).SwitchTo(destination); }
private void FillSwitchableControllers(BendyComponent bc) { EmptySwitchableControllers(); for (var i = 0; i < Definition.Switchables.Length; i++) { var config = Definition.Switchables[i]; var junction = _bendy.Nodes[config.Intersection]; var target = _bendy.Nodes[config.Destinations[0]]; if (junction == null || target == null) { continue; } _controllers[i] = SwitchableNodeData.GetOrCreate(junction).SideOrCreateFor(target); _controllers[i].SwitchChanged += OnSwitchChanged; } UpdateAnimator(); }
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(); }