示例#1
0
 public TrackPosition(TrackNode node, Direction direction = Direction.Forward, float distance = 0)
 {
     Node      = node;
     Distance  = distance;
     Direction = direction;
     Simplify();
 }
示例#2
0
 internal void HandleNodePass(TrackNode node)
 {
     if (OnNodePass != null)
     {
         OnNodePass.Invoke(node);
     }
 }
示例#3
0
        /// <summary>
        /// Convers a track string to a track position
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="trackString"></param>
        /// <returns></returns>
        public static TrackPosition FromString(SharedRailManager manager, string trackString)
        {
            string[]  parts     = trackString.Split("|");
            TrackNode node      = manager.Nodes[int.Parse(parts[0])];
            Direction direction = (Direction)(int.Parse(parts[1]));
            float     distance  = float.Parse(parts[2]);

            return(new TrackPosition(node, direction, distance));
        }
示例#4
0
        /// <summary>
        /// Get the actual world position of this track position
        /// </summary>
        /// <returns>A Vector3 of the world representation of this position</returns>
        public Vector3 ToVector3()
        {
            TrackNode toNode = Node.Next(Direction);

            if (toNode == null)
            {
                return(Node.Position);
            }

            // Calculate the vector by multiplying the distance with the normal of the relative vector
            return(Node.Position + (Vector3.Normalize(toNode.Position - Node.Position) * Distance));
        }
示例#5
0
        internal void HandleNodePass(TrackNode node)
        {
            TrackNode nextNode = FrontPosition.Node.Next(Direction);

            if (nextNode.IsEndNode)
            {
                float targetDistance = Vector3.Distance((Vector3)FrontPosition, nextNode.Position);
                Acceleration = -(targetDistance / Speed);
                Debug.WriteLine(Acceleration);
            }

            if (OnNodePass != null)
            {
                OnNodePass.Invoke(node);
            }
        }
示例#6
0
        private void Simplify()
        {
            if (Distance < 0)
            {
                TrackNode previousNode = Node.Previous(Direction);

                if (previousNode == null)
                {
                    return;
                }

                float totalDistanceBetweenPreviousNodes = Vector3.Distance(Node.Position, previousNode.Position);

                // Substract the remaining difference from the total distance between the nodes
                Node     = previousNode;
                Distance = totalDistanceBetweenPreviousNodes + Distance;
                Simplify();
            }
            else
            {
                TrackNode toNode = Node.Next(Direction);

                if (toNode == null)
                {
                    return;
                }

                float totalDistanceBetweenNodes = Vector3.Distance(Node.Position, toNode.Position);

                // Update the node if the next node is already reached
                if (Distance > totalDistanceBetweenNodes)
                {
                    Node      = toNode;
                    Distance -= totalDistanceBetweenNodes;
                    Simplify();
                }
            }
        }
示例#7
0
        public void LoadTracks(string path)
        {
            string[] lines = File.ReadAllLines(path);

            string[] meta         = lines[0].Split(" ");
            int      nodeCount    = int.Parse(meta[0]);
            int      segmentCount = int.Parse(meta[1]);

            Nodes = new TrackNode[nodeCount];

            for (int i = 1; i <= nodeCount; i++)
            {
                string[] nodeData   = lines[i].Split(" ");
                int      switchType = int.Parse(nodeData[3]);
                if (switchType > 0)
                {
                    Nodes[i - 1] = new TrackSwitch(i - 1, new Vector3(float.Parse(nodeData[0]), float.Parse(nodeData[1]), float.Parse(nodeData[2])), (Direction)(switchType) - 1);
                }
                else
                {
                    Nodes[i - 1] = new TrackNode(i - 1, new Vector3(float.Parse(nodeData[0]), float.Parse(nodeData[1]), float.Parse(nodeData[2])));
                }
            }

            for (int i = nodeCount + 1; i <= nodeCount + segmentCount; i++)
            {
                string[]     segment      = lines[i].Split(" ");
                TrackSegment trackSegment = new TrackSegment(Segments.Count);
                for (int j = 0; j < segment.Length - 1; j++)
                {
                    TrackNode n1 = Nodes[int.Parse(segment[j]) - 1];
                    TrackNode n2 = Nodes[int.Parse(segment[j + 1]) - 1];

                    trackSegment.Nodes.Add(n1);

                    if (n1 is TrackSwitch)
                    {
                        TrackSwitch s1 = (TrackSwitch)n1;
                        if (s1.SwitchDirection == Direction.Forward)
                        {
                            if (!Switches.Contains(s1))
                            {
                                Switches.Add(s1);
                            }
                            s1.SwitchNodes.Add(n2);
                            if (s1.ToNode == null)
                            {
                                s1.ToNode = n2;
                            }
                        }
                        else
                        {
                            s1.ToNode = n2;
                        }
                    }
                    else
                    {
                        n1.ToNode = n2;
                    }

                    if (n2 is TrackSwitch)
                    {
                        TrackSwitch s2 = (TrackSwitch)n2;
                        if (s2.SwitchDirection == Direction.Backward)
                        {
                            Switches.Add(s2);
                            s2.SwitchNodes.Add(n1);
                            if (s2.FromNode == null)
                            {
                                s2.FromNode = n1;
                            }
                        }
                        else
                        {
                            s2.FromNode = n1;
                        }
                    }
                    else
                    {
                        n2.FromNode = n1;
                    }

                    n1.Segment = trackSegment;
                }
                Segments.Add(trackSegment);
            }
        }