/// <summary>
        /// Verifies the given path against the given definition's constraints
        /// </summary>
        /// <param name="def">Constraint source</param>
        /// <param name="layer">Layer to use for node snapping, or null</param>
        /// <param name="nodes">Nodes in path</param>
        /// <param name="errors">Destination for all errors that occurred, or null</param>
        /// <returns>true if the path is valid</returns>
        public static bool ValidatePath(BendyComponentDefinition def, BendyLayer layer, IList <AnnotatedNode> nodes,
                                        IList <string> errors)
        {
            if (nodes.Count <= 1)
            {
                errors?.Add("Not enough points to form a path");
                return(false);
            }

            if (nodes.Count >= RailConstants.MaxNodesPlaced)
            {
                errors?.Add($"Can't place more than {RailConstants.MaxNodesPlaced} nodes at once");
                return(false);
            }

            for (var i = 1; i < nodes.Count; i++)
            {
                if (!VerifyEdge(def, nodes[i - 1], nodes[i], errors) && errors == null)
                {
                    return(false);
                }
            }

            return(errors == null || errors.Count == 0);
        }
 private void BindLayer(BendyLayer l)
 {
     _layers.Add(l);
     l.NodeCreated += OnNodeMoved;
     l.NodeMoved   += OnNodeMoved;
     l.NodeRemoved += OnNodeRemoved;
 }
 private void OnLayerAdded(string arg1, BendyLayer arg2)
 {
     if (Definition.AcceptLayer(arg1))
     {
         BindLayer(arg2);
     }
 }
        public static AnnotatedNode[] AnnotateNodes(BendyLayer layer, Vector3D[] nodes)
        {
            var res = new AnnotatedNode[nodes.Length];

            for (var i = 0; i < nodes.Length; i++)
            {
                res[i] = new AnnotatedNode {
                    Position = nodes[i]
                }
            }
            ;
            AnnotateNodes(layer, res);
            return(res);
        }
        public override void OnAddedToScene()
        {
            Graph     = MySession.Static.Components.Get <BendyController>()?.GetOrCreateLayer(Definition.Layer);
            _attacher = Container.Get <MyModelAttachmentComponent>();
            if (_attacher != null)
            {
                _attacher.OnEntityAttached += FixupSkinEntity;
                foreach (var e in _attacher.GetAttachedEntities(SkinHash))
                {
                    FixupSkinEntity(_attacher, e);
                }
            }

            base.OnAddedToScene();
            _powerObserver.RequiredPower = Definition.NeedsPower;
        }
        public static void AnnotateNodes(BendyLayer layer, IList <AnnotatedNode> nodes)
        {
            for (var i = 0; i < nodes.Count; i++)
            {
                var here = layer?.GetNode(nodes[i].Position);
                var tmp  = nodes[i];
                tmp.Position = here?.Position ?? nodes[i].Position;
                tmp.Existing = here;
                tmp.Up       = here?.Up ?? nodes[i].Up;
                nodes[i]     = tmp;
            }

            for (var i = 0; i < nodes.Count; i++)
            {
                var a       = nodes[i];
                var tanHere = Vector3.Zero;
                if (a.Existing != null)
                {
                    tanHere = a.Existing.Tangent * a.Existing.Neighbors.Count();
                }

                if (i > 0 && (nodes[i - 1].Existing == null || a.Existing?.ConnectionTo(nodes[i - 1].Existing) == null))
                {
                    var tP = Vector3.Normalize(nodes[i - 1].Position - a.Position);
                    if (tP.Dot(tanHere) < 0)
                    {
                        tP = -tP;
                    }
                    tanHere += tP;
                }

                // ReSharper disable once InvertIf
                if (i + 1 < nodes.Count && (nodes[i + 1].Existing == null || a.Existing?.ConnectionTo(nodes[i + 1].Existing) == null))
                {
                    var tP = Vector3.Normalize(nodes[i + 1].Position - a.Position);
                    if (tP.Dot(tanHere) < 0)
                    {
                        tP = -tP;
                    }
                    tanHere += tP;
                }

                a.Tangent = Vector3.Normalize(tanHere.LengthSquared() > 0 ? tanHere : Vector3.CalculatePerpendicularVector(nodes[i].Up));
                nodes[i]  = a;
            }
        }
Exemple #7
0
        public override void OnAddedToScene()
        {
            base.OnAddedToScene();
            _graph = MySession.Static.Components.Get <BendyController>()?.GetOrCreateLayer(Definition.Layer);
            if (_attacher != null)
            {
                _attacher.OnEntityAttached += FixupSkinEntity;
                foreach (var e in _attacher.GetAttachedEntities(SkinHash))
                {
                    FixupSkinEntity(_attacher, e);
                }
            }

            _blockComponent.Move += BlockMoved;
            BlockMoved(_blockComponent, _blockComponent.GridData);
            _powerObserver.RequiredPower = Definition.NeedsPower;
        }
        public Edge GetEdge(BendyLayer layer)
        {
            if (_from == null || _to == null)
            {
                return(null);
            }
            if (_edgeCache != null && _edgeCache.Graph == layer && _edgeCache.InScene)
            {
                return(_edgeCache);
            }
            var fromNode = layer.GetNode(_from.Value);

            if (fromNode == null)
            {
                return(null);
            }
            var toNode = layer.GetNode(_to.Value);

            if (toNode == null)
            {
                return(null);
            }
            return(_edgeCache = layer.GetEdge(fromNode, toNode));
        }