Exemple #1
0
    private void AddSplinePoint()
    {
        splinePointRenderers[splinePointRenderers.Count - 1].EnableRightControlPoint();

        Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        spline.AddPoint(mousePosition);

        SplinePointRenderer splinePoint =
            Instantiate(splinePointRendererPrefab,
                        mousePosition,
                        Quaternion.identity);

        splinePoint.Parent = this;
        splinePoint.Index  = splinePointRenderers.Count;
        splinePointRenderers.Add(splinePoint);

        splinePoint.RenderPoint(
            spline.AnchorPoints[spline.AnchorPoints.Count - 1],
            spline.ControlPoints[spline.ControlPoints.Count - 1]);

        splinePoint.ControlPointRight.IsEndPoint = true;

        RenderSpline();
    }
 public Path(List <Vector> points, double travelTime)
 {
     foreach (Vector v in points)
     {
         _spline.AddPoint(v);
     }
     _tween = new Tween(0, 1, travelTime);
 }
        public override void OnInspectorGUI()
        {
            _spline = target as Spline;

            serializedObject.Update();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("showGizmo"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("showNumbers"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("showVelocities"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("showAccelerations"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("color"));
            serializedObject.ApplyModifiedProperties();

            EditorGUI.BeginChangeCheck();
            bool loop = EditorGUILayout.Toggle("Loop", _spline.loop);

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObjects(serializedObject.targetObjects, "Toggle Loop");

                foreach (Object o in serializedObject.targetObjects)
                {
                    Spline s = o as Spline;

                    s.loop = loop;
                    EditorUtility.SetDirty(s);
                }
            }

            if (_selectedIndex >= 0 && _selectedIndex <= _spline.curveCount * 3)
            {
                _DrawSelectedPointInspector();
            }

            if (GUILayout.Button("Add Point"))
            {
                Undo.RecordObject(_spline, "Add Point");
                _spline.AddPoint(_selectedIndex);
                EditorUtility.SetDirty(_spline);
            }

            if (_spline.curveCount >= 2 && (_selectedIndex == 0 || _selectedIndex % 3 == 0))
            {
                if (GUILayout.Button("Delete Point"))
                {
                    Undo.RecordObject(_spline, "Delete Point");
                    _spline.DeletePoint(_selectedIndex);
                    _selectedIndex = -1;
                    EditorUtility.SetDirty(_spline);
                }
            }

            if (SplinesGUI.Foldout(ref foldouts[0], "Spline Info"))
            {
                EditorGUILayout.LabelField("Total Length: " + _spline.length);
            }
        }
Exemple #4
0
    private Spline CreateSpline(Point nextP)
    {
        GameObject newSpline = (GameObject)Instantiate(SplinePrefab, Vector3.zero, Quaternion.identity);

        Spline s = newSpline.GetComponent <Spline> ();


        s.name     = lastPoint.name + "—" + nextP.name;
        s.Selected = curPoint;
        progress   = 0;

        if (lastPoint != curPoint)
        {
            s.AddPoint(lastPoint);
        }
        s.AddPoint(curPoint);
        s.AddPoint(nextP);

        s.transform.position = Vector3.Lerp(curPoint.Pos, nextP.Pos, 0.5f);

        s.DrawMesh();

        return(s);
    }
 public SplineIdle(JSONTransform[] jsonSpline)
 {
     timeElapsed = 0;
     spline      = new Spline();
     for (int i = 0; i < jsonSpline.Length; ++i)
     {
         JSONTransform element = jsonSpline[i];
         spline.AddPoint(
             element.position,
             Quaternion.Euler(element.eulerAngles),
             step * i,
             new Vector2(0, 1)
             );
     }
     spline.StartInterpolation(null, true, eWrapMode.LOOP);
 }
Exemple #6
0
    public override void OnInspectorGUI()
    {
        EditorGUILayout.PropertyField(_numSubDivisions);

        _spline.ApplyModifiedProperties();

        if (SerializedProperty.EqualContents(_numSubDivisions, __numSubDivisions))
        {
            __numSubDivisions = _numSubDivisions;
            spline            = (Spline)target;
            spline.UpdateNumberSubDivisions();
        }

        //DrawDefaultInspector();

        if (GUILayout.Button("Add Point"))
        {
            Spline spline = (Spline)target;
            spline.AddPoint(false);
        }
    }
Exemple #7
0
    public void PointSwitch()
    {
        Spline nextSpline    = null;
        Point  nextPoint     = null;
        bool   newSpline     = false;
        bool   newPoint      = false;
        float  angleToSpline = Mathf.Infinity;

        if (curPoint.HasSplines())
        {
            bool forward = true;

            Spline closestSpline = null;
            Point  pointDest     = null;

            foreach (Spline s in curPoint.GetSplines())
            {
                foreach (Point p in curPoint.GetNeighbours())
                {
                    if (!p._connectedSplines.Contains(s))
                    {
                        //do nothing if the point is in another spline
                    }
                    else
                    {
                        float curAngle;

                        int indexDifference = s.SplinePoints.IndexOf(p) - s.SplinePoints.IndexOf(curPoint);

                        if (indexDifference == -1 || indexDifference > 1)
                        {
                            curAngle = s.CompareAngleAtPoint(cursorDir, p, true);
                        }
                        else
                        {
                            curAngle = s.CompareAngleAtPoint(cursorDir, curPoint);
                        }

                        if (curAngle < angleToSpline)
                        {
                            angleToSpline = curAngle;
                            closestSpline = s;
                            pointDest     = p;
                        }
                    }
                }
            }

            int indexdiff = closestSpline.SplinePoints.IndexOf(pointDest) - closestSpline.SplinePoints.IndexOf(curPoint);

            if (indexdiff == -1 || indexdiff > 1)
            {
                closestSpline.Selected = pointDest;
                forward = false;
                Services.Player.GetComponent <PlayerBehaviour> ().SetProgress(1f);
            }
            else
            {
                Services.Player.GetComponent <PlayerBehaviour> ().SetProgress(0f);
                forward = true;
                closestSpline.Selected = curPoint;
            }

            if (angleToSpline <= StopAngleDiff)
            {
                nextSpline = closestSpline;
            }
        }

        if (Input.GetButton("x") && nextSpline == null && angleToSpline > LineAngleDiff)
        {
            Ray        ray = Camera.main.ScreenPointToRay(Camera.main.WorldToScreenPoint(cursor.transform.position));
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.tag == "Point")
                {
                    Point hitPoint = hit.collider.GetComponent <Point> ();

                    if (hitPoint == curPoint)
                    {
                        //you clicked on the node you are currently on
                    }
                    else
                    {
                        newPoint  = true;
                        nextPoint = hitPoint;
                    }
                }
            }
            else if (inventory.Count > 0)
            {
                //DOUBLE POINT ERROR IS ONLY HAPPENING IN THE MIDDLE OF LINES?

                newPoint  = true;
                nextPoint = CreatePoint(cursor.transform.position);
            }

            if (newPoint)
            {
                if (curSpline != null && (curPoint == curSpline.StartPoint() || curPoint == curSpline.EndPoint()))
                {
                    //EDGE CASE
                    //Creating endpoint when you're on startpoint
                    //make it so that the start/midpoint get shifted down one index, insert at startpoint

                    newSpline = false;

                    if (nextPoint == curSpline.StartPoint() || nextPoint == curSpline.EndPoint() && !curSpline.closed)
                    {
                        curSpline.closed    = true;
                        curSpline.LoopIndex = curSpline.SplinePoints.IndexOf(nextPoint);

                        curPoint.AddPoint(nextPoint);
                        nextPoint.AddPoint(curPoint);

                        if (curSpline.GetPointIndex(nextPoint) - curSpline.GetPointIndex(curPoint) > 1)
                        {
                            curSpline.Selected = nextPoint;
                        }
                    }
                    else
                    {
                        curSpline.AddPoint(nextPoint);
                    }

                    nextSpline     = curSpline;
                    curSpline.name = nextSpline.SplinePoints [0].name + "—" + curSpline.EndPoint().name;
                }
                else
                {
                    newSpline = true;

                    //YOU CANT JUST ADD THE NEW POINT INTO THE OLD SPLINE IF YOU'RE DOING SO IN THE MIDDLE OF THE SPLINE
                    //IT WILL CREATE PROBLEMS. IT WONT KNOW THE PROPER NEIGHBOURS
                    //MAYBE INSERT IT AT A POSITION
                    //If at the end or start of the current line, insert the new point at the ends
                    //if not, don't insert it

//					if (curSpline != null && !curSpline.SplinePoints.Contains (nextPoint)) {
//						curSpline.AddPoint (nextPoint);
//					}


                    nextSpline = CreateSpline(nextPoint);
                }
            }
        }

        //BREAK OUT OF FUNCTION BEFORE THE NEXT SPLINE IS SET TO THE CURRENT SPLINE

        if (!newPoint && nextSpline == null)
        {
            flow = Mathf.Lerp(flow, 0, decay * Time.deltaTime);

//			transform.position = curPoint.transform.position;

            return;
        }
        else
        {
            //SET NEXT SPLINE TO CURRENT SPLINE
            curSpline = nextSpline;
            curSpline.OnSplineExit();

            if (newPoint)
            {
                if (curPoint == curSpline.Selected)
                {
                    if (!goingForward)
                    {
//					flow = -flow;
                    }
                    goingForward = true;
                    progress     = 0;
                }
                else
                {
                    if (goingForward)
                    {
//					flow = -flow;
                    }
                    goingForward = false;
                    progress     = 1;
                }
            }

            traversing = true;
        }
    }