Exemplo n.º 1
0
        private void OnDrawGizmosSelected()
        {
            if (_spline == null)
            {
                _spline = GetComponent <SplineComputer>();
            }
            var points = _spline.GetPoints();

            for (var i = 0; i < points.Length; i++)
            {
                var point = _spline.Evaluate(i);
                Gizmos.color = Color.black;
                Gizmos.DrawSphere(point.position, 0.1f);
                if (showUp)
                {
                    Gizmos.color = Color.green;
                    Gizmos.DrawLine(point.position, point.position + point.normal * _length);
                }
                if (showForward)
                {
                    Gizmos.color = Color.blue;
                    Gizmos.DrawLine(point.position, point.position + point.direction * _length);
                }
                if (showRight)
                {
                    Gizmos.color = Color.red;
                    Gizmos.DrawLine(point.position, point.position + point.right * _length);
                    if (showXz)
                    {
                        var r = point.right;
                        r.y = 0f;
                        Gizmos.DrawLine(point.position, point.position + r * _length);
                    }
                }
                if (i > 0)
                {
                    var point2 = _spline.Evaluate(i - 1);
                    if (showUp)
                    {
                        Gizmos.color = Color.green;
                        Gizmos.DrawLine(point2.position + point2.normal * _length, point.position + point.normal * _length);
                    }
                    if (showRight)
                    {
                        Gizmos.color = Color.red;
                        Gizmos.DrawLine(point2.position + point2.right * _length, point.position + point.right * _length);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static double ScreenPointToSplinePercent(SplineComputer computer, Vector2 screenPoint)
        {
            SplinePoint[] points          = computer.GetPoints();
            float         closestDistance = (screenPoint - HandleUtility.WorldToGUIPoint(points[0].position)).sqrMagnitude;
            double        closestPercent  = 0.0;
            double        add             = computer.moveStep;

            if (computer.type == Spline.Type.Linear)
            {
                add /= 2f;
            }
            int count = 0;

            for (double i = add; i < 1.0; i += add)
            {
                SplineSample result = computer.Evaluate(i);
                Vector2      point  = HandleUtility.WorldToGUIPoint(result.position);
                float        dist   = (point - screenPoint).sqrMagnitude;
                if (dist < closestDistance)
                {
                    closestDistance = dist;
                    closestPercent  = i;
                }
                count++;
            }
            return(closestPercent);
        }
Exemplo n.º 3
0
        void AddConnection(SplineComputer computer, int pointIndex)
        {
            Node node = (Node)target;

            Node.Connection[] connections = node.GetConnections();
            if (EditorUtility.DisplayDialog("Link point?", "Add point " + (pointIndex + 1) + " to connections?", "Yes", "No"))
            {
                Undo.RecordObject(addComp, "Add connection");
                Undo.RecordObject(node, "Add Connection");
                if (connections.Length == 0)
                {
                    switch (EditorUtility.DisplayDialogComplex("Align node to point?", "This is the first connection for the node, would you like to snap or align the node's Transform the spline point.", "No", "Snap", "Snap and Align"))
                    {
                    case 1: SplinePoint point = addComp.GetPoint(pointIndex);
                        node.transform.position = point.position;
                        break;

                    case 2:
                        SplineSample result = addComp.Evaluate(pointIndex);
                        node.transform.position = result.position;
                        node.transform.rotation = result.rotation;
                        break;
                    }
                }
                computer.ConnectNode(node, pointIndex);
                addComp  = null;
                addPoint = 0;
                SceneView.RepaintAll();
                Repaint();
            }
        }
Exemplo n.º 4
0
        public static bool CheckLineSwap(SplineComputer dest, SplineResult pos, float lineWidth)
        {
            var p1 = dest.Evaluate(dest.Project(pos.position));             // closest point on side line
            var d  = pos.position - p1.position;
            var v  = Vector3.Project(d, pos.right * lineWidth);             // project vector RIGHT from current on vector between points on lines
            var v2 = Vector3.Dot(d.normalized, pos.normal);

            return((v.magnitude <= lineWidth * 1.1f) && (Mathf.Abs(v2) <= 0.01f));
        }
Exemplo n.º 5
0
    void GenerateStair()
    {
        float stairSize = stairPrefab.transform.localScale.z * stairPrefab.GetComponent <BoxCollider>().size.z;

        float pathLenght = splineComputer.CalculateLength();

        float P = 0;

        SplineResult stairResult = splineComputer.Evaluate(0);

        GameObject obj = Instantiate(stairPrefab);

        obj.transform.position         = stairResult.position;
        obj.transform.rotation         = stairResult.rotation;
        obj.transform.localEulerAngles = new Vector3(0, obj.transform.localEulerAngles.y, obj.transform.localEulerAngles.z);


        Vector3 currentPos = obj.transform.position;

        while (P < 1)
        {
            P += 0.001f;

            stairResult = splineComputer.Evaluate(P);

            float distance = Vector3.Distance(stairResult.position, currentPos);

            if (distance >= stairSize)
            {
                obj = Instantiate(stairPrefab);

                obj.transform.position = stairResult.position;
                obj.transform.rotation = stairResult.rotation;


                obj.transform.localEulerAngles = new Vector3(0, obj.transform.localEulerAngles.y, obj.transform.localEulerAngles.z);

                currentPos = obj.transform.position;
            }
        }
    }
Exemplo n.º 6
0
    private void Generate()
    {
        data = new List <RoadEntityData>();
        var    isTutorial        = PlayerDataHolder.GetTutorial() == 0;
        var    length            = roadSpline.CalculateLength();
        double percentLength     = length / 100;
        var    percent           = percentLength / length;
        var    randomDist        = DistanceToPercent(Random.Range(minDistance, maxDistance));
        var    randomDistPercent = (int)(randomDist * 100);
        var    i = isTutorial ? tutorialEndPercent : startBarriersPercent;

        for (; i < 98; i += randomDistPercent)
        {
            if (randomDist > DistanceToPercent(maxDistance - 25))
            {
                // 70% chanse to spawn coins on long road sector
                var randomInt = Random.Range(0, 100);
                if (randomInt > 30)
                {
                    var moneyPercent = 0.0;
                    for (var j = 0; j < countOfRoads; j++)
                    {
                        moneyPercent = percent * (i + randomDistPercent / 2);
                        GenerateRingsOnRoad((float)moneyPercent, 0, 10, j);
                    }
                    var entityData = new RoadEntityData {
                        entityType = EntityType.Coins, percentAtRoad = (float)moneyPercent, height = 0, roadCount = countOfRoads
                    };
                    AddData(entityData);
                }
            }
            var splineOnPercent = roadSpline.Evaluate(percent * i);
            //if (levelHolder.InDeathLoops((float)(percent * i))) continue;
            var position = splineOnPercent.position;
            var rotation = splineOnPercent.rotation;
            GenerateEnemyOrBarrier(percent * i);
            randomDist        = DistanceToPercent(Random.Range(minDistance, maxDistance));
            randomDistPercent = (int)(randomDist * 100);
        }
    }
Exemplo n.º 7
0
        private void RecalculateGrindTriggers(GameObject go)
        {
            foreach (Transform transform in go.GetComponentsInChildren <Transform>())
            {
                if (transform.name.Contains("GrindSpline") && !transform.name.Contains("Colliders"))
                {
                    Transform grindColliders = go.transform.Find(transform.name + "Colliders").transform;

                    Vector3[]     grindPoints  = new Vector3[transform.childCount];
                    SplinePoint[] splinePoints = new SplinePoint[grindPoints.Length];
                    for (int i = 0; i < grindPoints.Length; i++)
                    {
                        grindPoints[i]  = transform.GetChild(i).position;
                        splinePoints[i] = new SplinePoint(grindPoints[i]);
                    }

                    SplineComputer splineComputer = grindColliders.gameObject.GetComponent <SplineComputer>();
                    Vector3[]      grindNormals   = new Vector3[grindPoints.Length];
                    for (int i = 0; i < grindPoints.Length - 1; i++)
                    {
                        GameObject grindCollider = grindColliders.Find("RailCol" + i).gameObject;
                        grindCollider.transform.position = grindPoints[i];
                        grindCollider.transform.LookAt(grindPoints[i + 1]);
                        BoxCollider boxCollider   = grindCollider.GetComponent <BoxCollider>();
                        float       segmentLength = Vector3.Distance(grindPoints[i], grindPoints[i + 1]);
                        boxCollider.size   = new Vector3(0.08f / go.transform.lossyScale.x, 0.08f / go.transform.lossyScale.y, segmentLength);
                        boxCollider.center = Vector3.forward * segmentLength / 2f;
                        grindNormals[i]    = grindCollider.transform.up;
                    }

                    grindNormals[grindNormals.Length - 1] = grindNormals[grindNormals.Length - 2];
                    splineComputer.SetPoints(splinePoints);
                    splineComputer.Evaluate(0.9);
                    for (int i = 0; i < grindPoints.Length; i++)
                    {
                        splineComputer.SetPointNormal(i, splineComputer.GetPointNormal(i, SplineComputer.Space.World) + grindNormals[i], SplineComputer.Space.World);
                    }
                }
                if (transform.name.Contains("GrindCollider"))
                {
                    transform.localScale = new Vector3(1f / go.transform.lossyScale.x, 1f / go.transform.lossyScale.y, 1f);
                }
            }
        }
Exemplo n.º 8
0
    public static bool BuildSplineComputer(GrindSegment segment)
    {
        // Create Spline Computer component and settings for GrindSpline Object
        SplineComputer sc = segment.grindRoot.gameObject.AddComponent <SplineComputer>();

        sc.type = Spline.Type.Linear;
        sc.SetPoints(segment.points, SplineComputer.Space.World);
        sc.Evaluate(0.9);

        // Shift normal vectors up a position for spline points points
        segment.normals[segment.normals.Length - 1] = segment.normals[segment.normals.Length - 2];

        // Set point normals
        for (int i = 0; i < segment.points.Length; i++)
        {
            sc.SetPointNormal(i, sc.GetPoint(i, SplineComputer.Space.World).normal + segment.normals[i], SplineComputer.Space.World);
        }

        return(true);
    }
        private void AddGrindTriggers(GameObject go)
        {
            foreach (Transform transform in go.GetComponentsInChildren <Transform>())
            {
                if (transform.name.Contains("GrindSpline"))
                {
                    Transform grindColliders = new GameObject(transform.name + "Colliders").transform;
                    grindColliders.parent           = go.transform;
                    grindColliders.gameObject.layer = 12;

                    if (transform.name.Contains("Metal"))
                    {
                        transform.tag = "Metal";
                    }
                    if (transform.name.Contains("Wood"))
                    {
                        transform.tag = "Wood";
                    }
                    if (transform.name.Contains("Concrete"))
                    {
                        transform.tag = "Concrete";
                    }

                    Vector3[]     grindPoints  = new Vector3[transform.childCount];
                    SplinePoint[] splinePoints = new SplinePoint[grindPoints.Length];
                    for (int i = 0; i < grindPoints.Length; i++)
                    {
                        grindPoints[i]  = transform.GetChild(i).position;
                        splinePoints[i] = new SplinePoint(grindPoints[i]);
                    }

                    SplineComputer splineComputer = grindColliders.gameObject.AddComponent <SplineComputer>();
                    splineComputer.type = Spline.Type.Linear;
                    Vector3[] grindNormals = new Vector3[grindPoints.Length];
                    for (int i = 0; i < grindPoints.Length - 1; i++)
                    {
                        GameObject grindCollider = new GameObject("RailCol" + i);
                        grindCollider.layer = 12;
                        grindCollider.transform.position = grindPoints[i];
                        grindCollider.transform.LookAt(grindPoints[i + 1]);
                        BoxCollider boxCollider   = grindCollider.AddComponent <BoxCollider>();
                        float       segmentLength = Vector3.Distance(grindPoints[i], grindPoints[i + 1]);
                        boxCollider.size               = new Vector3(0.08f / go.transform.lossyScale.x, 0.08f / go.transform.lossyScale.y, segmentLength);
                        boxCollider.center             = Vector3.forward * segmentLength / 2f;
                        boxCollider.isTrigger          = true;
                        grindCollider.transform.parent = grindColliders;
                        grindNormals[i] = grindCollider.transform.up;

                        if (transform.name.Contains("Metal"))
                        {
                            grindCollider.tag = "Metal";
                        }
                        if (transform.name.Contains("Wood"))
                        {
                            grindCollider.tag = "Wood";
                        }
                        if (transform.name.Contains("Concrete"))
                        {
                            grindCollider.tag = "Concrete";
                        }
                    }

                    grindNormals[grindNormals.Length - 1] = grindNormals[grindNormals.Length - 2];
                    splineComputer.SetPoints(splinePoints);
                    splineComputer.Evaluate(0.9);
                    for (int i = 0; i < grindPoints.Length; i++)
                    {
                        splineComputer.SetPointNormal(i, splineComputer.GetPointNormal(i, SplineComputer.Space.World) + grindNormals[i], SplineComputer.Space.World);
                    }
                }
            }
        }
Exemplo n.º 10
0
        public static void DrawSplineComputer(SplineComputer comp, double fromPercent = 0.0, double toPercent = 1.0, float alpha = 1f)
        {
            if (comp == null)
            {
                return;
            }
            Color prevColor   = Handles.color;
            Color orange      = new Color(1f, 0.564f, 0f);
            Color handleColor = comp.editorPathColor;

            handleColor.a = alpha;
            Handles.color = handleColor;
            if (comp.pointCount < 2)
            {
                return;
            }

            if (comp.type == Spline.Type.BSpline && comp.pointCount > 1)
            {
                SplinePoint[] compPoints = comp.GetPoints();
                Handles.color = new Color(handleColor.r, handleColor.g, handleColor.b, 0.5f * alpha);
                for (int i = 0; i < compPoints.Length - 1; i++)
                {
                    Handles.DrawLine(compPoints[i].position, compPoints[i + 1].position);
                }
                Handles.color = handleColor;
            }

            if (!comp.drawThinckness)
            {
                if (positions.Length != comp.sampleCount * 2)
                {
                    positions = new Vector3[comp.sampleCount * 2];
                }
                Vector3 prevPoint  = comp.EvaluatePosition(fromPercent);
                int     pointIndex = 0;
                for (int i = 1; i < comp.sampleCount; i++)
                {
                    positions[pointIndex] = prevPoint;
                    pointIndex++;
                    positions[pointIndex] = comp.samples[i].position;
                    pointIndex++;
                    prevPoint = positions[pointIndex - 1];
                }
                Handles.DrawLines(positions);
            }
            else
            {
                Transform editorCamera = SceneView.currentDrawingSceneView.camera.transform;
                if (positions.Length != comp.sampleCount * 6)
                {
                    positions = new Vector3[comp.sampleCount * 6];
                }
                SplineSample prevResult = comp.Evaluate(fromPercent);
                Vector3      prevNormal = prevResult.normal;
                if (comp.billboardThickness)
                {
                    prevNormal = (editorCamera.position - prevResult.position).normalized;
                }
                Vector3 prevRight  = Vector3.Cross(prevResult.direction, prevNormal).normalized *prevResult.size * 0.5f;
                int     pointIndex = 0;
                for (int i = 1; i < comp.sampleCount; i++)
                {
                    SplineSample newResult = comp.samples[i];
                    Vector3      newNormal = newResult.normal;
                    if (comp.billboardThickness)
                    {
                        newNormal = (editorCamera.position - newResult.position).normalized;
                    }
                    Vector3 newRight = Vector3.Cross(newResult.direction, newNormal).normalized *newResult.size * 0.5f;

                    positions[pointIndex] = prevResult.position + prevRight;
                    positions[pointIndex + comp.sampleCount * 2] = prevResult.position - prevRight;
                    positions[pointIndex + comp.sampleCount * 4] = newResult.position - newRight;
                    pointIndex++;
                    positions[pointIndex] = newResult.position + newRight;
                    positions[pointIndex + comp.sampleCount * 2] = newResult.position - newRight;
                    positions[pointIndex + comp.sampleCount * 4] = newResult.position + newRight;
                    pointIndex++;
                    prevResult = newResult;
                    prevRight  = newRight;
                    prevNormal = newNormal;
                }
                Handles.DrawLines(positions);
            }
            Handles.color = prevColor;
        }
Exemplo n.º 11
0
 public SplineResult GetTrackDataAt(double normalizedTrackProgress)
 {
     return(spline.Evaluate(normalizedTrackProgress));
     // PathController.Instance.GetWorldPosition(normalizedTrackProgress)
 }
Exemplo n.º 12
0
        public static bool Slider(SplineComputer spline, ref double percent, Color color, string text = "", SplineSliderGizmo gizmo = SplineSliderGizmo.Rectangle, float buttonSize = 1f)
        {
            Camera cam = SceneView.currentDrawingSceneView.camera;

            spline.Evaluate(percent, evalResult);
            float size = HandleUtility.GetHandleSize(evalResult.position);

            Handles.color = new Color(color.r, color.g, color.b, 0.4f);
            Handles.DrawSolidDisc(evalResult.position, cam.transform.position - evalResult.position, size * 0.2f * buttonSize);
            Handles.color = Color.white;
            if ((color.r + color.g + color.b + color.a) / 4f >= 0.9f)
            {
                Handles.color = Color.black;
            }

            Vector3 center         = evalResult.position;
            Vector2 screenPosition = HandleUtility.WorldToGUIPoint(center);

            screenPosition.y += 20f;
            Vector3 localPos = cam.transform.InverseTransformPoint(center);

            if (text != "" && localPos.z > 0f)
            {
                Handles.BeginGUI();
                DreamteckEditorGUI.Label(new Rect(screenPosition.x - 120 + text.Length * 4, screenPosition.y, 120, 25), text);
                Handles.EndGUI();
            }
            bool    buttonClick  = SliderButton(center, false, Color.white, 0.3f);
            Vector3 lookAtCamera = (cam.transform.position - evalResult.position).normalized;
            Vector3 right        = Vector3.Cross(lookAtCamera, evalResult.direction).normalized *size * 0.1f * buttonSize;
            Vector3 front        = Vector3.forward;

            switch (gizmo)
            {
            case SplineSliderGizmo.BackwardTriangle:
                center += evalResult.direction * size * 0.06f * buttonSize;
                front   = center - evalResult.direction * size * 0.2f * buttonSize;
                Handles.DrawLine(center + right, front);
                Handles.DrawLine(front, center - right);
                Handles.DrawLine(center - right, center + right);
                break;

            case SplineSliderGizmo.ForwardTriangle:
                center -= evalResult.direction * size * 0.06f * buttonSize;
                front   = center + evalResult.direction * size * 0.2f * buttonSize;
                Handles.DrawLine(center + right, front);
                Handles.DrawLine(front, center - right);
                Handles.DrawLine(center - right, center + right);
                break;

            case SplineSliderGizmo.DualArrow:
                center += evalResult.direction * size * 0.025f * buttonSize;
                front   = center + evalResult.direction * size * 0.17f * buttonSize;
                Handles.DrawLine(center + right, front);
                Handles.DrawLine(front, center - right);
                Handles.DrawLine(center - right, center + right);
                center -= evalResult.direction * size * 0.05f * buttonSize;
                front   = center - evalResult.direction * size * 0.17f * buttonSize;
                Handles.DrawLine(center + right, front);
                Handles.DrawLine(front, center - right);
                Handles.DrawLine(center - right, center + right);
                break;

            case SplineSliderGizmo.Rectangle:

                break;

            case SplineSliderGizmo.Circle:
                Handles.DrawWireDisc(center, lookAtCamera, 0.13f * size * buttonSize);
                break;
            }
            Vector3 lastPos = evalResult.position;

            Handles.color       = Color.clear;
            evalResult.position = Handles.FreeMoveHandle(evalResult.position, Quaternion.LookRotation(cam.transform.position - evalResult.position), size * 0.2f * buttonSize, Vector3.zero, Handles.CircleHandleCap);
            if (evalResult.position != lastPos)
            {
                percent = spline.Project(evalResult.position).percent;
            }
            Handles.color = Color.white;
            return(buttonClick);
        }
Exemplo n.º 13
0
    public void InitScene()
    {
        currentPatternIndex         = 0;
        currentPatternSpawnedAmount = 0;

        stairControllers.Clear();


        float stairSize = StairPrefab.transform.localScale.z * StairPrefab.GetComponent <BoxCollider>().size.z - 0.1f;

        stairWidth = stairSize;
        float pathLenght = splineComputer.CalculateLength();

        float P = 0;

        SplineResult stairResult = splineComputer.Evaluate(0);

        int i = 0;

        GameObject obj = Instantiate(StairPrefab);

        obj.transform.position         = stairResult.position;
        obj.transform.rotation         = stairResult.rotation;
        obj.transform.localEulerAngles = new Vector3(0, obj.transform.localEulerAngles.y, obj.transform.localEulerAngles.z);

        StairController stairController = obj.GetComponent <StairController>();

        stairController.InitStair(i, GameManager.self.colorManager.GetStairColor(i));

        stairControllers.Add(stairController);

        Vector3 currentPos = obj.transform.position;


        while (P < 1)
        {
            P += 0.001f;

            stairResult = splineComputer.Evaluate(P);

            float distance = Vector3.Distance(stairResult.position, currentPos);

            if (distance >= stairSize)
            {
                obj = Instantiate(StairPrefab);

                obj.transform.position = stairResult.position;
                obj.transform.rotation = stairResult.rotation;


                obj.transform.localEulerAngles = new Vector3(0, obj.transform.localEulerAngles.y, obj.transform.localEulerAngles.z);

                currentPos = obj.transform.position;


                stairController = obj.GetComponent <StairController>();
                i += 1;
                stairController.InitStair(i, GameManager.self.colorManager.GetStairColor(i));

                stairControllers.Add(stairController);
            }
        }

        int lastIndex = stairControllers.Count - 1;

        FinishContainer.position = stairControllers[lastIndex].transform.position;
        stairControllers[lastIndex].isLastStair = true;

        StairsAmount = stairControllers.Count;

        for (int j = 0; j < stairControllers.Count; j++)
        {
            stairControllers[j].SetParameters(StairPatterns(j));
            stairControllers[j].DoMovement();
        }
    }