开发者

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

开发者 https://www.devze.com 2023-03-29 19:54 出处:网络
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 ob

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?

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

Thanks in advance.


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;
         }
     }    
}
0

精彩评论

暂无评论...
验证码 换一张
取 消