Пример #1
0
    public Vector3 moveDistance(float deltaDistance, Vector3 curPoint, ref pathPoint pa, ref int pathSection, ref float curDist, ref float direction)
    {
        //no path, no moving.
        if (pa == null)
        {
            return(curPoint);
        }

        Vector3[] path      = pa.getPath();
        float[]   pathDists = pa.getPathDists();

        int i = (pathSection * 2) + 0;
        int j = (pathSection * 2) + 1;
        int k = (pathSection * 2) + 2;

        //set the current distance
        curDist += deltaDistance;

        //check if that distance is out of range
        if (curDist > pathDists [pathSection])
        {
            curDist -= pathDists [pathSection];
            pathSection++;
            if (pathSection > pathDists.Length - 1)
            {
                pa          = pa.getNextPath();
                pathSection = 0;
            }
            return(moveDistance(0, curPoint, ref pa, ref pathSection, ref curDist, ref direction));
        }
        else if (curDist < 0)
        {
            pathSection--;
            if (pathSection < 0)
            {
                pa            = pa.getPrevPath();
                pathSection   = 0;
                deltaDistance = Mathf.Abs(curDist);
                curDist       = 0;
                direction     = 1f;
            }
            else
            {
                deltaDistance = curDist;
                curDist       = pathDists [pathSection];
            }
            return(moveDistance(deltaDistance, curPoint, ref pa, ref pathSection, ref curDist, ref direction));
        }

        //at some point, we will reach here and return some a new point
        return(lerp2(path [i], path [j], path [k], curDist / pathDists [pathSection]));
    }
Пример #2
0
    public void drawPath(pathPoint curPath, bool drawNext = false)
    {
        Vector3[] path;
        if (drawNext && curPath.getNextPath() != null)
        {
            Vector3[] path1 = curPath.getPath();
            Vector3[] path2 = curPath.getNextPath().getPath();
            path = new Vector3[path1.Length + path2.Length - 1];
            Array.Copy(path1, path, path1.Length - 1);
            Array.Copy(path2, 0, path, path1.Length - 1, path2.Length);
        }
        else
        {
            path = curPath.getPath();
        }

        line.SetVertexCount(path.Length);
        line.SetPositions(path);
    }
Пример #3
0
    public pathPoint findCloseTrainDrop(Vector3 cursor, float maxDistance = 2.5f)
    {
        pathPoint closestPoint     = null;
        float     smallestDistance = maxDistance;

        foreach (GameObject go in GameObject.FindGameObjectsWithTag("pathPoint"))
        {
            pathPoint pa   = go.GetComponent <pathPoint> ();
            float     dist = Vector3.Distance(pa.getTrainDropPoint(), cursor);

            if (dist <= smallestDistance)
            {
                closestPoint     = pa;
                smallestDistance = dist;
            }
        }

        return(closestPoint);
    }
Пример #4
0
    public void checkEnds(pathPoint pa)
    {
        if (pa.getID() == id)
        {
            return;
        }

        //check if that point meets this endpoint
        for (int i = 0; i < pathEnds.Length; i++)
        {
            if (Vector3.Distance(pa.getStartPoint(), pathEnds [i]) < 0.01f)
            {
                connectsTo [i] = pa;
            }
        }

        //check if that start point meets this startpoint
        //(this is checked more than necessary, but makes for a more understandable program)
        if (Vector3.Distance(pa.getStartPoint(), pathStart) < 0.01f)
        {
            connectsFrom = pa;
        }
    }
Пример #5
0
    public float[] secDistance(pathPoint pa)
    {
        int accuracy = 100;

        Vector3[]    path     = pa.getPath();
        List <float> distance = new List <float> ();

        Vector3 lastPoint;

        for (int i = 0; i < path.Length - 1; i += 2)
        {
            lastPoint = path [i];
            distance.Add(0f);
            for (int j = 1; j <= accuracy; j++)
            {
                Vector3 nextPoint = lerp2(path [i + 0], path [i + 1], path [i + 2], (j / accuracy));
                distance[distance.Count - 1] += Vector3.Distance(lastPoint, nextPoint);
                lastPoint = nextPoint;
            }
        }

        return(distance.ToArray());
    }
Пример #6
0
    public float pathDistance(pathPoint pa)
    {
        int accuracy = 100;

        Vector3[] path = pa.getPath();

        float   totalDistance = 0;
        Vector3 lastPoint;

        for (int i = 0; i < path.Length - 1; i += 2)
        {
            lastPoint = path [i];
            for (int j = 1; j <= 1; j++)
            {
                Vector3 nextPoint = lerp2(path [i + 0], path [i + 1], path [i + 2], (j / accuracy));
                totalDistance += Vector3.Distance(lastPoint, nextPoint);

                lastPoint = nextPoint;
            }
        }

        return(totalDistance);
    }
Пример #7
0
    void Update()
    {
        if (placing)
        {
            if (cursorMarker == null)
            {
                cursorMarker = GameObject.Find("cursorMarker").transform;
                track        = GameObject.Find("track").transform;
            }

            curPath = trackUtility.utility.findCloseTrainDrop(cursorMarker.position, 3f);
            Vector3 startPoint;
            if (curPath != null)
            {
                startPoint         = curPath.getTrainDropPoint();
                transform.position = new Vector3(startPoint.x, 0.35f, startPoint.z);
            }
            else if (cursorMarker.position.y > -0.001f)
            {
                transform.position = new Vector3(cursorMarker.position.x, 0.35f, cursorMarker.position.z);
            }
        }
        else
        {
            //determine speed
            if (!braking)
            {
                curSpeed += (engine.accel * Time.deltaTime);
                if (curSpeed > engine.topSpeed)
                {
                    curSpeed = engine.topSpeed;
                }
                if (curSpeed < -engine.topSpeed)
                {
                    curSpeed = -engine.topSpeed;
                }
            }
            else
            {
                curSpeed -= (engine.decel * Time.deltaTime);
                if (curSpeed < .001)
                {
                    curSpeed = 0;
                }
            }

            //we need to know if any point moves to a new path
            //as we start going forward on a new path
            float direction = -1f;

            //find the engines next point
            //this is the point that this object's variables follow
            Vector3 nextPoint = trackUtility.utility.moveDistance(curSpeed * Time.deltaTime, engine.go.transform.position, ref curPath, ref pathSection, ref curDist, ref direction);

            //we copy those variables and use them elsewhere
            pathPoint curPathCopy     = curPath;
            int       pathSectionCopy = pathSection;
            float     curDistCopy     = curDist;

            //ever car will follow the pattern of move the car, point towards the lookAt
            Vector3 lookAt = trackUtility.utility.moveDistance((direction * engine.lengths[1]), nextPoint, ref curPathCopy, ref pathSectionCopy, ref curDistCopy, ref direction);

            if (engine.go.transform.position == nextPoint || nextPoint == lookAt)
            {
                return;
            }
            engine.go.transform.position = nextPoint;
            engine.go.transform.LookAt(lookAt);
            engine.go.transform.RotateAround(nextPoint, Vector3.up, -90);

            //determine the point behind it
            float nextDistance = (direction * (engine.lengths[2] + bufferBetweenCars));

            //loop through cars
            for (int i = 0; i < cars.Count; i++)
            {
                //find its point
                nextPoint = trackUtility.utility.moveDistance(nextDistance + (direction * cars[i].lengths[0]), lookAt, ref curPathCopy, ref pathSectionCopy, ref curDistCopy, ref direction);

                //find its tail point
                lookAt = trackUtility.utility.moveDistance(direction * cars[i].lengths[1], nextPoint, ref curPathCopy, ref pathSectionCopy, ref curDistCopy, ref direction);

                if (cars[i].go.transform.position == nextPoint || nextPoint == lookAt)
                {
                    return;
                }

                cars[i].go.transform.position = nextPoint;
                cars[i].go.transform.LookAt(lookAt);
                cars[i].go.transform.RotateAround(nextPoint, Vector3.up, -90);

                nextDistance = (direction * (cars [i].lengths [2] + bufferBetweenCars));
            }
        }
    }
Пример #8
0
    public void runConnection()
    {
        connectsTo = new pathPoint[pathMeshs.Length];

        for (int i = 0; i < pathMeshs.Length; i++)
        {
            //generate our list of path vertices from the armature children
            List <Vector3> path = new List <Vector3> ();
            Transform      t    = pathMeshs [i].transform.GetChild(0);
            while (true)
            {
                //add the position
                path.Add(t.position);

                //find next point
                if (t.childCount == 1)
                {
                    t = t.GetChild(0);
                }
                else if (t.childCount > 1)
                {
                    Debug.LogError("too many children");
                }
                else
                {
                    break;
                }
            }
            //if the path is followed backward, simply reverse this array
            if (!followPathForward)
            {
                path.Reverse();
            }

            //finally set variables based on aboce
            pathVertices [i] = path.ToArray <Vector3> ();
            pathStart        = pathVertices [i] [0];
            pathEnds [i]     = pathVertices [i].Last();

            foreach (GameObject go in GameObject.FindGameObjectsWithTag("pathPoint"))
            {
                //get the pathPoint from other object
                pathPoint pa = go.GetComponent <pathPoint> ();

                //make sure it is not this point
                if (pa.getID() == gameObject.transform.parent.gameObject.GetInstanceID())
                {
                    continue;
                }

                //check if that point meets this endpoint
                if (Vector3.Distance(pa.getStartPoint(), pathEnds [i]) < 0.01f)
                {
                    connectsTo [i] = pa;
                }

                //check if that start point meets this startpoint
                //(this is checked more than necessary, but makes for a more understandable program)
                if (Vector3.Distance(pa.getStartPoint(), pathStart) < 0.01f)
                {
                    connectsFrom = pa;
                }

                //check if this point meets that point
                pa.checkEnds(this);
            }
        }

        int p = pathVertices[0].Length - 3;

        trainDropPoint = trackUtility.utility.lerp2(pathVertices [0] [p], pathVertices [0] [p + 1], pathVertices [0] [p + 2], 0.5f);
        totalLength    = trackUtility.utility.pathDistance(this);
        lengths        = trackUtility.utility.secDistance(this);
    }