Example #1
0
        public Vector3 GetTrainPosition(out Vector3 forward, out Vector3 up)
        {
            double timeToLeave;
            int    targetIndex = GetNextPointIndex(out timeToLeave);

            Railway.Point targetPoint = Railway.Manager.Instance.GetPoint(pointList[targetIndex].id);
            if (pointList[targetIndex].pointType != Railway.Point.EType.Joint && timeToLeave <= pointList[targetIndex].stayTime + PETools.PEMath.Epsilon)
            {
                forward = targetPoint.GetJointRotation() * Vector3.forward;
                if (targetIndex == pointCount - 1)
                {
                    forward = -1 * forward;
                }
                up          = Quaternion.Euler(targetPoint.rotation) * Vector3.up;
                stayStation = Railway.Manager.Instance.GetPoint(pointList[targetIndex].id);
                return(stayStation.GetJointPosition());
            }
            stayStation = null;
            float timeToArrive = (float)(timeToLeave);

            if (pointList[targetIndex].pointType != Railway.Point.EType.Joint)
            {
                timeToArrive -= pointList[targetIndex].stayTime;
            }
            Vector3 targetPos = targetPoint.GetJointPosition();

            Railway.Point prePoint = Railway.Manager.Instance.GetPoint(pointList[targetIndex - moveDir].id);
            Vector3       prePos   = prePoint.GetJointPosition();

            forward = moveDir * (targetPos - prePos).normalized;
            up      = Vector3.Lerp(Quaternion.Euler(targetPoint.rotation) * Vector3.up
                                   , Quaternion.Euler(prePoint.rotation) * Vector3.up
                                   , timeToArrive * Railway.Manager.TrainSteerSpeed / (targetPos - prePos).magnitude);
            return(targetPos + (prePos - targetPos).normalized * Railway.Manager.TrainSteerSpeed * timeToArrive);
        }
Example #2
0
        public List <Railway.Point> GetNearPoint(Railway.Point centerPoint)
        {
            List <Railway.Point> retList = GetNearPoint(centerPoint.position, JointMaxDistance);

            retList.Remove(centerPoint);
            return(retList);
        }
Example #3
0
        public Stats GetStats()
        {
            Stats stats = new Stats();

            Railway.Point lastPoint = null;
            foreach (Railway.Point point in pointList)
            {
                if (null != lastPoint)
                {
                    stats.totalDis += Vector3.Distance(point.position, lastPoint.position);
                }

                lastPoint = point;

                if (point.pointType != Railway.Point.EType.Joint)
                {
                    stats.stationNum++;
                }
                else
                {
                    stats.jointNum++;
                }
            }

            return(stats);
        }
Example #4
0
        void UpdateRotation()
        {
            Vector3 forwrd = Vector3.zero;

            Railway.Point prePoint  = GetPrePoint();
            Railway.Point nextpoint = GetNextPoint();

            Vector3 upDir = Quaternion.Euler(rotation) * Vector3.up;

            if (pointType != Railway.Point.EType.Joint)
            {
                upDir = Vector3.up;
            }

            if (null != prePoint)
            {
                forwrd = (position - prePoint.position).normalized;
            }

            if (null != nextpoint)
            {
                forwrd = (forwrd.normalized + (nextpoint.position - position).normalized).normalized;
            }
            else
            {
                forwrd = -forwrd;
            }

            if (forwrd != Vector3.zero)
            {
                rotation = Quaternion.LookRotation(forwrd, upDir).eulerAngles;
            }
        }
Example #5
0
        public Railway.Route GetRouteByPointId(int pointId)
        {
            Railway.Point point = GetPoint(pointId);
            if (null == point)
            {
                return(null);
            }

            return(GetRoute(point.routeId));
        }
Example #6
0
 public Railway.Point GetAnotherEndPoint(Railway.Point endPoint)
 {
     Railway.Route route = GetRoute(endPoint.routeId);
     if (null != route)
     {
         if (route.GetPointByIndex(0) == endPoint)
         {
             return(route.GetPointByIndex(route.pointCount - 1));
         }
         else
         {
             return(route.GetPointByIndex(0));
         }
     }
     return(null);
 }
Example #7
0
        //network need it to resore
        public void Import(byte[] data)
        {
            PETools.Serialize.Import(data, (r) =>
            {
                saveVersion = r.ReadInt32();

                int count = r.ReadInt32();
                for (int i = 0; i < count; i++)
                {
                    Railway.Point point = Point.Deserialize(PETools.Serialize.ReadBytes(r));

                    mPointDic[point.id] = point;
                }

                count = r.ReadInt32();
                for (int i = 0; i < count; i++)
                {
                    Railway.Route route = Railway.Route.Deserialize(PETools.Serialize.ReadBytes(r));

                    mRouteList.Add(route);
                }
                //-----------
                if (saveVersion >= Version4)
                {
                    StroyManager.m_Passengers.Clear();
                    int n = r.ReadInt32();
                    byte[] buff;
                    for (int i = 0; i < n; i++)
                    {
                        buff             = PETools.Serialize.ReadBytes(r);
                        PassengerInfo pi = new PassengerInfo();
                        pi.Import(buff);

                        StroyManager.m_Passengers.Add(pi.npcID, pi);
                    }
                }
            });

            foreach (Point point in mPointDic.Values)
            {
                point.UpdateLinkTarget();
            }
        }
Example #8
0
        public Railway.Point AddPoint(Vector3 pos
                                      , int prePointId
                                      , Point.EType type = Point.EType.Joint
                                      , int pointId      = -1)
        {
            if (GetPoint(pointId) != null)
            {
                return(null);
            }

            if (pointId == InvalId)
            {
                pointId = GetValidPointId();
            }

            Railway.Point point = new Railway.Point(pointId, type);
            point.position = pos;

            if (type == Railway.Point.EType.Station)
            {
                point.stayTime = Railway.Manager.DefaultStayTime;
            }
            else if (type == Railway.Point.EType.End)
            {
                point.stayTime = Railway.Manager.DefaultStayTime;// / 2f;
            }
            else
            {
                point.stayTime = 0f;
            }

            mPointDic[point.id] = point;

            point.ChangePrePoint(prePointId);

            pointChangedEventor.Dispatch(new PointChanged()
            {
                bAdd  = true,
                point = point
            });

            return(point);
        }
Example #9
0
        public static Railway.Point GetTail(Railway.Point point)
        {
            if (point == null)
            {
                return(null);
            }

            while (true)
            {
                Railway.Point nextPoint = point.GetNextPoint();
                if (nextPoint == null)
                {
                    return(point);
                }
                else
                {
                    point = nextPoint;
                }
            }
        }
Example #10
0
        public static Railway.Point GetHeader(Railway.Point point)
        {
            if (point == null)
            {
                return(null);
            }

            while (true)
            {
                Railway.Point prePoint = point.GetPrePoint();
                if (prePoint == null)
                {
                    return(point);
                }
                else
                {
                    point = prePoint;
                }
            }
        }
Example #11
0
        public void Destroy()
        {
            Railway.Point getPoint = GetPrePoint();
            if (null != getPoint)
            {
                getPoint.ChangeNextPoint(Railway.Manager.InvalId);
            }

            getPoint = GetNextPoint();

            if (null != getPoint)
            {
                getPoint.ChangePrePoint(Railway.Manager.InvalId);
            }

            if (null != station)
            {
                GameObject.Destroy(station.gameObject);
            }
        }
Example #12
0
        public static void Travel(Railway.Point point, System.Action <Railway.Point> action)
        {
            if (point == null)
            {
                return;
            }

            while (true)
            {
                action(point);

                Railway.Point nextPoint = point.GetNextPoint();

                if (nextPoint == null)
                {
                    break;
                }
                else
                {
                    point = nextPoint;
                }
            }
        }
Example #13
0
        public bool SetStayTime(int pointId, float time)
        {
            if (trainRunning)
            {
                return(false);
            }

            Railway.Point point = Railway.Manager.Instance.GetPoint(pointId);
            if (point == null)
            {
                return(false);
            }

            if (point.routeId != id)
            {
                return(false);
            }

            point.stayTime = time;

            UpdateTimeSchedule();

            return(true);
        }
Example #14
0
        public void ChangeNextPoint(int nextID)
        {
            if (nextID == nextPointId)
            {
                return;
            }

            Railway.Point oldNextPoint = GetNextPoint();

            nextPointId = nextID;

            if (null != oldNextPoint)
            {
                oldNextPoint.ChangePrePoint(Railway.Manager.InvalId);
            }

            Railway.Point newNextPoint = Railway.Manager.Instance.GetPoint(nextID);
            if (null != newNextPoint)
            {
                newNextPoint.ChangePrePoint(id);
            }

            UpdateRotation();
        }
Example #15
0
        public void ChangePrePoint(int preId)
        {
            if (preId == prePointId)
            {
                return;
            }

            Railway.Point oldPrePoint = GetPrePoint();

            prePointId = preId;

            if (null != oldPrePoint)
            {
                oldPrePoint.ChangeNextPoint(Railway.Manager.InvalId);
            }

            Railway.Point newPrePoint = Railway.Manager.Instance.GetPoint(preId);
            if (null != newPrePoint)
            {
                newPrePoint.ChangeNextPoint(id);
            }

            UpdateRotation();
        }