Exemplo n.º 1
0
    void PlaceShiftedLane(bool right)
    {
        if (BaseLane == null)
        {
            return;
        }

        Curve c1 = BaseLane.xz_curve.Clone();

        c1.ShiftRight(right ? shiftStep : -shiftStep);
        BaseLane = new Lane(c1, BaseLane.y_func);

        var placeCmd = new PlaceLaneCommand();

        inputHandler.commandSequence.Push(placeCmd);
        placeCmd.Execute(BaseLane);
        BaseLane.SetGameobjVisible(false);

        // Set new BaseLane
        foreach (Lane added in placeCmd.added)
        {
            if ((added.GetThreedPos(1f) - BaseLane.GetThreedPos(1f)).sqrMagnitude < 0.001f)
            {
                BaseLane = added;
                break;
            }
        }

        BuildRoad.UseDefaultStickyMouseForRoad(inputHandler.stickyMouse);
    }
Exemplo n.º 2
0
    void OnEnable()
    {
        inputHandler.OnClick += delegate(object sender, (Vector3, Curve3DSampler)pos_parent) {
            if (pos_parent.Item2 == null)
            {
                BaseLane = null;
            }
            else
            {
                BaseLane = (Lane)pos_parent.Item2;
            }
        };

        inputHandler.OnForwardKeyPressed += delegate {
            if (BaseLane == null)
            {
                return;
            }

            Function currentFunc  = new LinearFunction(0f, 0f); // TODO: Create more
            Curve    currentCurve = null;

            if (spawnType == typeof(Line))
            {
                var curveParams = appendAtFront ?
                                  new List <Vector3> {
                    BaseLane.xz_curve.GetTwodPos(1f), BaseLane.xz_curve.GetTwodPos(1f) + BaseLane.xz_curve.GetFrontDir(1f) * forwardStep
                } :
                new List <Vector3> {
                    BaseLane.xz_curve.GetTwodPos(0f) - BaseLane.xz_curve.GetFrontDir(0f) * forwardStep, BaseLane.xz_curve.GetTwodPos(0f)
                };
                currentCurve = new Line(curveParams[0], curveParams[1]);
            }
            else
            {
                if (spawnType == typeof(Arc))
                {
                    float radius = forwardStep / turnStep;
                    if (BaseLane.xz_curve is Arc)
                    {
                        radius = ((Arc)(BaseLane.xz_curve)).Radius;
                    }

                    if (appendAtFront)
                    {
                        if (turnRight)
                        {
                            currentCurve = new Arc(
                                BaseLane.xz_curve.GetTwodPos(1f) + BaseLane.xz_curve.GetRightDir(1f) * radius,
                                BaseLane.xz_curve.GetTwodPos(1f),
                                -turnStep);
                        }
                        else
                        {
                            currentCurve = new Arc(
                                BaseLane.xz_curve.GetTwodPos(1f) - BaseLane.xz_curve.GetRightDir(1f) * radius,
                                BaseLane.xz_curve.GetTwodPos(1f),
                                turnStep);
                        }
                    }
                    else
                    {
                        if (turnRight)
                        {
                            currentCurve = new Arc(
                                BaseLane.xz_curve.GetTwodPos(0f) - BaseLane.xz_curve.GetRightDir(0f) * radius,
                                BaseLane.xz_curve.GetTwodPos(0f),
                                -turnStep);
                            currentCurve.Crop(1f, 0f);
                        }
                        else
                        {
                            currentCurve = new Arc(
                                BaseLane.xz_curve.GetTwodPos(0f) + BaseLane.xz_curve.GetRightDir(0f) * radius,
                                BaseLane.xz_curve.GetTwodPos(0f),
                                turnStep);
                            currentCurve.Crop(1f, 0f);
                        }
                    }
                }
                else
                {
                    // Bezeir mode not supported
                    return;
                }
            }


            BaseLane = new Lane(currentCurve, currentFunc);
            var placeCmd = new PlaceLaneCommand();
            inputHandler.commandSequence.Push(placeCmd);
            placeCmd.Execute(BaseLane);

            BaseLane.SetGameobjVisible(false);

            // Set new BaseLane
            foreach (Lane added in placeCmd.added)
            {
                if ((added.GetThreedPos(1f) - BaseLane.GetThreedPos(1f)).sqrMagnitude < 0.001f)
                {
                    BaseLane = added;
                    break;
                }
            }

            BuildRoad.UseDefaultStickyMouseForRoad(inputHandler.stickyMouse);
        };

        inputHandler.OnLeftKeyPressed += delegate {
            PlaceShiftedLane(false);
        };

        inputHandler.OnRightKeyPressed += delegate {
            PlaceShiftedLane(true);
        };
    }
Exemplo n.º 3
0
    void OnEnable()
    {
        if (inputHandler.stickyMouse != null)
        {
            UseDefaultStickyMouseForRoad(inputHandler.stickyMouse);
        }

        // Init behavior
        inputHandler.OnClick += delegate(object sender, (Vector3, Curve3DSampler)pos_parent) {
            if (currentLane == null)
            {
                Debug.Log("add new");
                Curve currentCurve = null;
                if (spawnType == typeof(Line))
                {
                    currentCurve = Line.GetDefault();
                }
                if (spawnType == typeof(Arc))
                {
                    currentCurve = Arc.GetDefault();
                }
                if (spawnType == typeof(Bezier))
                {
                    currentCurve = Bezier.GetDefault();
                }

                Function currentFunc = new LinearFunction(); // TODO: Create more
                currentLane = new Lane(currentCurve, currentFunc);
            }

            Vector3        position;
            Curve3DSampler parentCurve; // which virtual curve does the road under construction belong to?
            (position, parentCurve) = pos_parent;


            new PlaceEndingCommand(position).Execute(currentLane);

            if (currentLane.IsValid)
            {
                // Place
                GetComponent <FollowMouseBehavior>().enabled = false;
                var placeCmd = new PlaceLaneCommand();
                inputHandler.commandSequence.Push(placeCmd);
                placeCmd.Execute(currentLane);
                currentLane.SetGameobjVisible(false);
                currentLane = null;
                GetComponent <HighLightCtrlPointBehavior>().radius = highlightRadius;

                UseDefaultStickyMouseForRoad(inputHandler.stickyMouse);
            }
            else
            {
                // Next ctrl point Pending
                if (parentCurve != null)
                {
                    UseSingleVirtualCurveForRoad(parentCurve);
                }
                else
                {
                    UseDefaultStickyMouseForRoad(inputHandler.stickyMouse);
                }

                GetComponent <FollowMouseBehavior>().enabled = true;
                GetComponent <FollowMouseBehavior>().SetTarget(currentLane);
                GetComponent <HighLightCtrlPointBehavior>().radius = 0f;
            }
        };

        // Adjust behavior
        inputHandler.OnDragStart += delegate(object sender, Vector3 position)
        {
            Lane targetLane = RoadPositionRecords.QueryClosestCPs3DCurve(position);
            if (targetLane != null)
            {
                GetComponent <FollowMouseBehavior>().enabled = true;
                currentLane = new Lane(targetLane);
                GetComponent <FollowMouseBehavior>().SetTarget(currentLane);

                //replace targetLane with a temporary object (currentLane)
                var removeCmd = new RemoveLaneCommand();
                inputHandler.commandSequence.Push(removeCmd);
                removeCmd.Execute(targetLane);

                GetComponent <HighLightCtrlPointBehavior>().radius = 0f;
            }
        };

        inputHandler.OnDragEnd += delegate(object sender, Vector3 position)
        {
            if (currentLane != null)
            {
                GetComponent <FollowMouseBehavior>().enabled       = false;
                GetComponent <HighLightCtrlPointBehavior>().radius = highlightRadius;

                // add actual lane to network
                var placeCmd = new PlaceLaneCommand();
                inputHandler.commandSequence.Push(placeCmd);
                placeCmd.Execute(currentLane);

                currentLane.SetGameobjVisible(false);
                currentLane = null;
            }
        };
    }
Exemplo n.º 4
0
    private void Start()
    {
        // Init behavior
        inputHandler.OnClick += delegate(object sender, Vector3 position) {
            Debug.Log("Onclick");

            if (currentLane == null)
            {
                Curve currentCurve = null;
                if (spawnType == typeof(Line))
                {
                    currentCurve = Line.GetDefault();
                }
                if (spawnType == typeof(Arc))
                {
                    currentCurve = Arc.GetDefault();
                }
                if (spawnType == typeof(Bezier))
                {
                    currentCurve = Bezier.GetDefault();
                }

                Function currentFunc = new LinearFunction(); // TODO: Create more
                currentLane = new Lane(currentCurve, currentFunc);
            }

            new PlaceEndingCommand(position).Execute(currentLane);

            if (currentLane.IsValid)
            {
                // Quit Init
                GetComponent <FollowMouseBehavior>().enabled = false;
                var placeCmd = new PlaceLaneCommand();
                commandSequence.Push(placeCmd);
                placeCmd.Execute(currentLane);
                currentLane.SetGameobjVisible(false);
                currentLane = null;
                GetComponent <HighLightCtrlPointBehavior>().radius = highlightRadius;
            }
            else
            {
                // Pending
                GetComponent <FollowMouseBehavior>().enabled = true;
                GetComponent <FollowMouseBehavior>().SetTarget(currentLane);
                GetComponent <HighLightCtrlPointBehavior>().radius = 0f;
            }
        };

        // Adjust behavior
        inputHandler.OnDragStart += delegate(object sender, Vector3 position)
        {
            Lane targetLane = RoadPositionRecords.QueryClosestCPs3DCurve(position);
            if (targetLane != null)
            {
                GetComponent <FollowMouseBehavior>().enabled = true;
                currentLane = new Lane(targetLane);
                GetComponent <FollowMouseBehavior>().SetTarget(currentLane);

                //replace targetLane with a temporary object (currentLane)
                var removeCmd = new RemoveLaneCommand();
                commandSequence.Push(removeCmd);
                removeCmd.Execute(targetLane);

                GetComponent <HighLightCtrlPointBehavior>().radius = 0f;
            }
        };

        inputHandler.OnDragEnd += delegate(object sender, Vector3 position)
        {
            if (currentLane != null)
            {
                GetComponent <FollowMouseBehavior>().enabled       = false;
                GetComponent <HighLightCtrlPointBehavior>().radius = highlightRadius;

                // add actual lane to network
                var placeCmd = new PlaceLaneCommand();
                commandSequence.Push(placeCmd);
                placeCmd.Execute(currentLane);

                currentLane.SetGameobjVisible(false);
                currentLane = null;
            }
        };

        inputHandler.OnUndoPressed += delegate {
            var latestCmd = commandSequence.Pop();
            latestCmd.Undo();
        };
    }