예제 #1
0
    public int Branch_Picking_SharedNode(SPData SPData, SharedNode sharedNode, data data, List <int> connectedBranches)
    {
        int branchKey = 0;
        var rand      = UnityEngine.Random.Range(0, connectedBranches.Count);

        if (sharedNode._Type == SharedNodeType.Defined)                                                                           // defined shared node type
        {
            branchKey = SPData.SplinePlus.FollowerClass.DefinedSharedNodeType(SPData, connectedBranches, sharedNode, data.wagon); // keyboard sharedPath Point type
        }
        else if (sharedNode._Type == SharedNodeType.Random)
        {
            if (connectedBranches.Count != 0)
            {
                branchKey = connectedBranches[rand];                              // random shared node type
            }
            else
            {
                branchKey = data.wagon._BranchKey; // if no eligible branch is found then pick previous branch back
            }
        }

        //events only for the trainhead
        SPData.SplinePlus.EventClass.EventsTriggering(data.train, data.wagon._BranchKey, branchKey);

        return(branchKey);
    }
예제 #2
0
    void BranchPicking(SPData SPData, SharedNode sharedNode, Follower follower)
    {
        List <int> connectedBranches = new List <int>();

        for (int i = 0; i < sharedNode.ConnectedBranches.Count; i++)// filter eligible branches that follower can switch to
        {
            var _newBranch = sharedNode.ConnectedBranches[i];
            if (_newBranch == follower._BranchKey)
            {
                continue;
            }

            if (sharedNode.Node._Type == NodeType.Smooth) // smooth
            {
                if (IsBranchValid(SPData, follower._BranchKey, _newBranch, sharedNode.Node) && SPData.DictBranches[_newBranch].Nodes.Count > 1)
                {
                    connectedBranches.Add(_newBranch);
                }
            }
            else // free
            {
                connectedBranches.Add(_newBranch);
            }
        }

        //branch picking decision
        Branch_Picking_SharedNode(SPData, sharedNode, follower, connectedBranches);
    }
예제 #3
0
    public static void SetTrainBranchKey(SPData SPData, SharedNode sharedNode, data data, int branchKey)
    {
        //concerve constant spacing values between wagons
        // this should be caculated before new branch option is selected
        if (data.wagon.Progress > SPData.DictBranches[data.wagon._BranchKey].BranchDistance)
        {
            data.wagon.Delta = data.wagon.Progress - SPData.DictBranches[data.wagon._BranchKey].BranchDistance;
        }
        else if (data.wagon.Progress < 0)
        {
            data.wagon.Delta = -data.wagon.Progress;
        }
        SPData.SplinePlus.EventClass.EventsTriggering(data.wagon, data.wagon._BranchKey, branchKey);

        //set new branch
        data.wagon._BranchKey = branchKey;


        //follower new progress value calculation
        var pathLength = SPData.DictBranches[data.wagon._BranchKey].BranchDistance;

        if (!SPData.DictBranches[data.wagon._BranchKey].Nodes[0].Equals(sharedNode.Node))
        {
            data.wagon.Reverse  = true;
            data.wagon.Progress = pathLength - data.wagon.Delta;
        }
        else
        {
            data.wagon.Reverse  = false;
            data.wagon.Progress = data.wagon.Delta;
        }

        TransformFollower(SPData, data);
    }
예제 #4
0
    public static void SmoothSharedPathPoint(SPData SPData, SharedNode sharedNode, float radius)
    {
        SPData.SmoothData.InitNodePos     = sharedNode.Node.Point.position;
        SPData.SmoothData.Nodes           = new Node[sharedNode.ConnectedBranches.Count];
        SPData.SmoothData.BranchesIndices = sharedNode.ConnectedBranches.ToArray();

        //shared path point breaking
        for (int i = 0; i < SPData.SmoothData.Nodes.Length; i++)
        {
            var branchKey = sharedNode.ConnectedBranches[i];

            var localIndex = sharedNode.Node.LocalIndex(SPData, branchKey);

            if (localIndex != 0)
            {
                BranchesClass.ReverseBranch(SPData, branchKey);
            }

            var duplicate = DuplicatePathPoint(SPData, SPData.DictBranches[branchKey].Nodes[0]);
            SPData.SmoothData.Nodes[i] = duplicate;
            SPData.DictBranches[branchKey].Nodes[0] = duplicate;
        }

        for (int i = 0; i < SPData.SmoothData.Nodes.Length; i++)
        {
            var branchKey = SPData.SmoothData.BranchesIndices[i];
            BranchesClass.FlipHandles(SPData, branchKey, 0);

            var mid = Vector3.Lerp(SPData.DictBranches[branchKey].Nodes[0].Point.position,
                                   SPData.DictBranches[branchKey].Nodes[1].Point.position, 0.5f);

            SPData.SmoothData.Nodes[i].Point.position = Vector3.Lerp(SPData.SmoothData.InitNodePos,
                                                                     mid, radius);

            SPData.SmoothData.Nodes[i].Point2.position      = SPData.SmoothData.InitNodePos;
            SPData.SmoothData.Nodes[i].Point1.localPosition = -SPData.SmoothData.Nodes[i].Point2.localPosition;
        }

        // path points welding
        for (int i = 0; i < SPData.SmoothData.Nodes.Length; i++)// path points created after chamfer,
        {
            for (int n = i; n < SPData.SmoothData.Nodes.Length; n++)
            {
                if (n == i)
                {
                    continue;
                }
                ConnectTwoNodes(SPData, SPData.SmoothData.Nodes[n], SPData.SmoothData.Nodes[i]);
            }
        }

        // remove initiale shared path point
        SPData.SharedNodes.Remove(sharedNode);
        MonoBehaviour.DestroyImmediate(sharedNode.Node.Point.gameObject);
    }
예제 #5
0
    public static void AddRefreshSharedNode(SPData SPData, Node node)
    {
        var index = SPData.SharedNodes.FindIndex(x => x.Node.Equals(node));

        if (index == -1)//create new shared path point
        {
            SharedNode sharedNodes = new SharedNode();
            sharedNodes.Node = node;
            SPData.SharedNodes.Add(sharedNodes);
        }
        ConnectedBranches(SPData);
    }
        public override void Initialize()
        {
            PRoot   root          = Canvas.Root;
            PCamera camera        = Canvas.Camera;
            PLayer  mainLayer     = Canvas.Layer;               // viewed by the PCanvas camera, the lens is added to this layer.
            PLayer  sharedLayer   = new PLayer();               // viewed by both the lens camera and the PCanvas camera
            PLayer  lensOnlyLayer = new PLayer();               // viewed by only the lens camera

            root.AddChild(lensOnlyLayer);
            root.AddChild(sharedLayer);
            camera.AddLayer(0, sharedLayer);

            PLens lens = new PLens();

            lens.SetBounds(10, 10, 80, 110);
            lens.AddLayer(0, lensOnlyLayer);
            lens.AddLayer(1, sharedLayer);
            mainLayer.AddChild(lens);
            PBoundsHandle.AddBoundsHandlesTo(lens);

            // Create an event handler that draws squiggles on the first layer of the bottom
            // most camera.
            PDragSequenceEventHandler squiggleEventHandler = new SquiggleEventHandler();

            // add the squiggle event handler to both the lens and the
            // canvas camera.
            lens.Camera.AddInputEventListener(squiggleEventHandler);
            camera.AddInputEventListener(squiggleEventHandler);

            // remove default event handlers, not really nessessary since the squiggleEventHandler
            // consumes everything anyway, but still good to do.
            //Canvas.RemoveInputEventListener(Canvas.PanEventHandler);
            Canvas.RemoveInputEventListener(Canvas.ZoomEventHandler);

            PNode sharedNode = new SharedNode(lens);

            sharedNode.Brush = new SolidBrush(Color.Green);             // Brushes.Green;
            sharedNode.SetBounds(0, 0, 100, 200);
            sharedNode.TranslateBy(100, 220);
            sharedLayer.AddChild(sharedNode);

            PText label = new PText("Move the lens \n (by dragging title bar) over the green rectangle, and it will appear red. press and drag the mouse on the canvas and it will draw squiggles. press and drag the mouse over the lens and drag squiggles that are only visible through the lens.");

            label.Font = new Font("Arial", 10, FontStyle.Regular);
            label.ConstrainWidthToTextWidth = false;
            label.SetBounds(100, 70, 130, 200);

            sharedLayer.AddChild(label);

            base.Initialize();
        }
예제 #7
0
    void BranchPicking(SPData SPData, SharedNode sharedNode, data data)
    {
        List <int> connectedBranches = new List <int>();

        for (int n = 0; n < sharedNode.ConnectedBranches.Count; n++)// filter eligible branches that subFollower can switch to
        {
            var _newBranch = sharedNode.ConnectedBranches[n];
            if (_newBranch == data.wagon._BranchKey)
            {
                continue;
            }

            if (sharedNode.Node._Type == NodeType.Smooth)//smooth
            {
                if (FollowerClass.IsBranchValid(SPData, data.wagon._BranchKey, _newBranch, sharedNode.Node) &&
                    SPData.DictBranches[_newBranch].Nodes.Count > 1)   //
                {
                    connectedBranches.Add(_newBranch);
                }
            }
            else//free
            {
                connectedBranches.Add(_newBranch);
            }
        }

        int branchKey = 0;
        var r         = (data.train.IsForward) ? data.train.Wagons.Count - 1 : 0;

        //branch picking decision
        if (data.i == r)
        {
            branchKey = Branch_Picking_SharedNode(SPData, sharedNode, data, connectedBranches);
        }
        else
        {
            if (data.train.IsForward)
            {
                branchKey = data.train.Wagons[data.i + 1]._BranchKey;
            }
            else
            {
                branchKey = data.train.Wagons[data.i - 1]._BranchKey;
            }
        }

        SetTrainBranchKey(SPData, sharedNode, data, branchKey);
    }
예제 #8
0
    public void Branch_Picking_SharedNode(SPData SPData, SharedNode sharedNode, Follower follower, List <int> connectedBranches)
    {
        var rand      = UnityEngine.Random.Range(0, connectedBranches.Count);
        int branchKey = 0;

        if (sharedNode._Type == SharedNodeType.Defined)                                         // defined shared node type
        {
            branchKey = DefinedSharedNodeType(SPData, connectedBranches, sharedNode, follower); // keyboard sharedPath Point type
        }
        else if (sharedNode._Type == SharedNodeType.Random)
        {
            if (connectedBranches.Count != 0)
            {
                branchKey = connectedBranches[rand];                              // random shared node type
            }
            else
            {
                branchKey = follower._BranchKey; // if no eligible branch is found then pick previous branch back
            }
        }

        SetFollowerBranchKey(SPData, sharedNode, follower, branchKey);
    }
예제 #9
0
		public override void Initialize() {
			PRoot root = Canvas.Root;
			PCamera camera = Canvas.Camera;		
			PLayer mainLayer = Canvas.Layer;	// viewed by the PCanvas camera, the lens is added to this layer.
			PLayer sharedLayer = new PLayer();			// viewed by both the lens camera and the PCanvas camera
			PLayer lensOnlyLayer = new PLayer();	// viewed by only the lens camera
		
			root.AddChild(lensOnlyLayer);
			root.AddChild(sharedLayer);
			camera.AddLayer(0, sharedLayer);
		
			PLens lens = new PLens();
			lens.SetBounds(10, 10, 100, 130);
			lens.AddLayer(0, lensOnlyLayer);		
			lens.AddLayer(1, sharedLayer);
			mainLayer.AddChild(lens);
			PBoundsHandle.AddBoundsHandlesTo(lens);

			// Create an event handler that draws squiggles on the first layer of the bottom
			// most camera.
			PDragSequenceEventHandler squiggleEventHandler = new SquiggleEventHandler();

			// add the squiggle event handler to both the lens and the
			// canvas camera.
			lens.Camera.AddInputEventListener(squiggleEventHandler);
			camera.AddInputEventListener(squiggleEventHandler);

			// remove default event handlers, not really nessessary since the squiggleEventHandler
			// consumes everything anyway, but still good to do.
			Canvas.RemoveInputEventListener(Canvas.PanEventHandler);
			Canvas.RemoveInputEventListener(Canvas.ZoomEventHandler);

			PNode sharedNode = new SharedNode(lens);
			sharedNode.Brush = Brushes.Green;
			sharedNode.SetBounds(0, 0, 100, 200);
			sharedNode.TranslateBy(200, 200);
			sharedLayer.AddChild(sharedNode);
		
			PText label = new PText("Move the lens \n (by dragging title bar) over the green rectangle, and it will appear red. press and drag the mouse on the canvas and it will draw squiggles. press and drag the mouse over the lens and drag squiggles that are only visible through the lens.");
			label.ConstrainWidthToTextWidth = false;
			label.SetBounds(200, 100, 200, 200);
		
			sharedLayer.AddChild(label);				
		}
예제 #10
0
    public int DefinedSharedNodeType(SPData SPData, List <int> connectedBranches, SharedNode sharedNode, Follower follower)
    {
        //if follower current branch is not _Backward or _Forward or _Left or _Right then pick the more legitimate choice if available
        Func <int> FollowerOnUndefinedBranch = delegate
        {
            var definedBranches = new List <int>();
            if (sharedNode._Left != -1)
            {
                definedBranches.Add(sharedNode._Left);
            }
            if (sharedNode._Right != -1)
            {
                definedBranches.Add(sharedNode._Right);
            }
            if (sharedNode._Backward != -1)
            {
                definedBranches.Add(sharedNode._Backward);
            }
            if (sharedNode._Forward != -1)
            {
                definedBranches.Add(sharedNode._Forward);
            }


            for (int i = 0; i < definedBranches.Count; i++)
            {
                if (IsBranchValid(SPData, follower._BranchKey, definedBranches[i], sharedNode.Node))
                {
                    return(definedBranches[i]);
                }
            }
            return(-1);
        };


        int branch = -1;

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            if (follower.IsForward)
            {
                if (follower._BranchKey == sharedNode._Forward)
                {
                    branch = sharedNode._Left;
                }
                else if (follower._BranchKey == sharedNode._Backward)
                {
                    branch = sharedNode._Left;
                }
                else if (follower._BranchKey == sharedNode._Left)
                {
                    branch = sharedNode._Forward;
                }
                else if (follower._BranchKey == sharedNode._Right)
                {
                    branch = sharedNode._Backward;
                }
                else
                {
                    branch = FollowerOnUndefinedBranch();
                }
            }
            else
            {
                if (follower._BranchKey == sharedNode._Forward)
                {
                    branch = sharedNode._Right;
                }
                else if (follower._BranchKey == sharedNode._Backward)
                {
                    branch = sharedNode._Right;
                }
                else if (follower._BranchKey == sharedNode._Left)
                {
                    branch = sharedNode._Forward;
                }
                else if (follower._BranchKey == sharedNode._Right)
                {
                    branch = sharedNode._Backward;
                }
                else
                {
                    branch = FollowerOnUndefinedBranch();
                }
            }
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            if (follower.IsForward)
            {
                if (follower._BranchKey == sharedNode._Forward)
                {
                    branch = sharedNode._Right;
                }
                else if (follower._BranchKey == sharedNode._Backward)
                {
                    branch = sharedNode._Right;
                }
                else if (follower._BranchKey == sharedNode._Left)
                {
                    branch = sharedNode._Backward;
                }
                else if (follower._BranchKey == sharedNode._Right)
                {
                    branch = sharedNode._Forward;
                }
                else
                {
                    branch = FollowerOnUndefinedBranch();
                }
            }
            else
            {
                if (follower._BranchKey == sharedNode._Forward)
                {
                    branch = sharedNode._Left;
                }
                else if (follower._BranchKey == sharedNode._Backward)
                {
                    branch = sharedNode._Left;
                }
                else if (follower._BranchKey == sharedNode._Left)
                {
                    branch = sharedNode._Backward;
                }
                else if (follower._BranchKey == sharedNode._Right)
                {
                    branch = sharedNode._Forward;
                }
                else
                {
                    branch = FollowerOnUndefinedBranch();
                }
            }
        }

        else
        {
            if (follower._BranchKey == sharedNode._Forward)
            {
                branch = sharedNode._Backward;
            }
            else if (follower._BranchKey == sharedNode._Backward)
            {
                branch = sharedNode._Forward;
            }
            else if (follower._BranchKey == sharedNode._Left)
            {
                branch = sharedNode._Right;
            }
            else if (follower._BranchKey == sharedNode._Right)
            {
                branch = sharedNode._Left;
            }
            else
            {
                branch = FollowerOnUndefinedBranch();
            }
        }
        //if no branch choice is defined then pick a random choice if not then return branch value
        if (branch == -1)
        {
            var rand = UnityEngine.Random.Range(0, connectedBranches.Count);
            return(connectedBranches[rand]);
        }

        if (IsBranchValid(SPData, follower._BranchKey, branch, sharedNode.Node))
        {
            return(branch);
        }
        else
        {
            branch = FollowerOnUndefinedBranch();
            return(branch);
        }
    }