Search code examples
c#windows-phone-7xnaxna-4.0

What I need to create this control for this game? (Like pipeline)


This is my first post at this section (XNA and game development). I'm trying to get something the below image. As you can see, there's a highway and inside of it, there'll be some objects moving (millisecond). I guess the streeth behavier is like a pipeline. When the highway loads an object, it appears at the beggining and it'll be moving through the highware until it arrives in the another extreme of the highway.

My main problem is, how can I do to move several objects only inside of the highway?

enter image description here

Thanks in advance.


Solution

  • You need a list of points and a list of sprites

    class Path 
    {
       List<Vector2> Points;
       float[] Lengths;
       Vector2[] Directions;
    
       void Build()
       {
           Lengths = new float[Points.Count-1];
           Directions = new float[Points.Count-1];
           for (int i=0; i<Points.Count-1;i++)
           {
                Directions[i] = Points[i+1] - Points[i];
                Lengths[i] = Directions[i].Length();
                Directions[i].Normalize();
           }  
       }
    }
    
    class Sprite 
    {
         Vector2 Position;
         float StagePos;
         int StageIndex;
         Path Path;
         float Speed;
    
         void Update(float Seconds)
         {
             if (StageIndex!=Path.Points.Count-1)
             {
                 StagePos += Speed * Seconds;
                 while (StagePos>Path.Lengths[StageIndex])
                 {
                     StagePos -= Path.Lengths[StageIndex]; 
                     StageIndex++;              
                     if (StageIndex == Path.Points.Count-1) 
                     {
                         Position = Path.Points[StageIndex];
                         return;
                     }
                 }
                 Position = Path.Points[StageIndex] + Directions[StageIndex] * StagePos;
             }
         }    
    }